Semaphores error to synchronize access

BenTom07

New Member
Joined
Dec 7, 2022
Messages
7
Reaction score
0
Credits
128
As a Linux developer working on a multi-process application, I'm experiencing semaphore deadlock. In my code, I've used semaphores to synchronize access to common resources across several processes. Nevertheless, under some circumstances, my software appears to become stuck and does not respond to user input.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

#define SEM_KEY 1234

int semid;

int main()
{
    // Create semaphore set with one semaphore
    semid = semget(SEM_KEY, 1, IPC_CREAT | 0666);
    if (semid == -1) {
        perror("semget");
        exit(1);
    }

    // Initialize semaphore value to 1
    union semun {
        int val;
        struct semid_ds *buf;
        unsigned short *array;
    } arg;
    arg.val = 1;
    if (semctl(semid, 0, SETVAL, arg) == -1) {
        perror("semctl");
        exit(1);
    }

    // Fork child process
    pid_t pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(1);
    }

    if (pid == 0) {
        // Child process
        while (1) {
            printf("Child: Waiting for semaphore...\n");
            struct sembuf sb;
            sb.sem_num = 0;
            sb.sem_op = -1;
            sb.sem_flg = SEM_UNDO;
            if (semop(semid, &sb, 1) == -1) {
                perror("semop");
                exit(1);
            }
            printf("Child: Acquired semaphore!\n");
            sleep(1);
            printf("Child: Releasing semaphore...\n");
            sb.sem_op = 1;
            if (semop(semid, &sb, 1) == -1) {
                perror("semop");
                exit(1);
            }
            printf("Child: Released semaphore!\n");
        }
    } else {
        // Parent process
        while (1) {
            printf("Parent: Waiting for semaphore...\n");
            struct sembuf sb;
            sb.sem_num = 0;
            sb.sem_op = -1;
            sb.sem_flg = SEM_UNDO;
            if (semop(semid, &sb, 1) == -1) {
                perror("semop");
                exit(1);
            }
            printf("Parent: Acquired semaphore!\n");
            sleep(1);
            printf("Parent: Releasing semaphore...\n");
            sb.sem_op = 1;
            if (semop(semid, &sb, 1) == -1) {
                perror("semop");
                exit(1);
            }
            printf("Parent: Released semaphore!\n");
        }
    }

    return 0;
}
I'm using a single semaphore in this code to synchronize access to the shared resource across the parent and child processes. The parent and child processes alternate gaining and releasing the semaphore, and each action is separated by a second.

When I execute this program, however, it occasionally gets trapped in a stalemate in which both processes are waiting for the semaphore to be released by the other process. Could you please help me?
 

Members online


Latest posts

Top