Skip to main content
edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Exclusive Order Enforce exclusive order of four processes using Semaphoressemaphores

I am on the learning curve of C and it'sits capabilities. Below I have tried to create 4 individual programs where the execution is always in order (program 0,1,2,3).

Exclusive Order using Semaphores

I am on the learning curve of C and it's capabilities. Below I have tried to create 4 individual programs where the execution is always in order (program 0,1,2,3).

Enforce exclusive order of four processes using semaphores

I am on the learning curve of C and its capabilities. Below I have tried to create 4 individual programs where the execution is always in order (program 0,1,2,3).

Source Link

Exclusive Order using Semaphores

I am on the learning curve of C and it's capabilities. Below I have tried to create 4 individual programs where the execution is always in order (program 0,1,2,3).

About the program
Program #0 is always executed (called) first, it also creates semid's
Program #1 can ONLY be executed if Program #0 has ran
Program #2 can ONLY be executed if Program #1 has ran
Program #3 can ONLY be executed if Program #2 has ran

What Do I Need?
Since, I am a beginner I thought an expert's knowledge on creating a better design or code structuring would help me.

Program Call
./prog0 && ./prog1 && ./prog2 && ./prog3

Program #0

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/ipc.h> 
#include <sys/sem.h>

int
main ( )
{
    int semid;
    key_t key;
    union semun arg;

    key = ftok("program0.c", 'a');
    semid = semget(key, 3, 0666 | IPC_CREAT);
    if ( semid < 0 )
    {
        perror("Could Not Created The Requested Semaphore");
        exit(1);
    }
    arg.val = 1;
    semctl(semid, 0, SETVAL, arg);
    arg.val = 0;
    semctl(semid, 1, SETVAL, arg);
    semctl(semid, 2, SETVAL, arg);
    printf("Executing process #0.\n");
    return 0;
}

Program #1

int
main ( )
{
    int semid, key;
    union semun arg;
    key = ftok("program0.c", 'a');
    semid = semget(key, 3, 0666 | IPC_CREAT);
    if ( semid < 0 )
    {
        perror("Could Not Created The Requested Semaphore");
        exit(1);
    }
    if ( semctl(semid, 0, GETVAL, 0) != 1 )
    {
        semctl(semid, 0, IPC_RMID);
        exit(1);
    }
    printf("Executing process #1.\n");
    arg.val = 0;
    semctl(semid, 0, SETVAL, arg);
    arg.val++;
    semctl(semid, 1, SETVAL, arg);
    return 0;
}

Program #2

int
main ( )
{
    int semid, key;
    union semun arg;
    key = ftok("program0.c", 'a');
    semid = semget(key, 3, 0666 | IPC_CREAT);
    if ( semid < 0 )
    {
        perror("Could Not Created The Requested Semaphore");
        exit(1);
    }
    if ( semctl(semid, 1, GETVAL, 0) != 1 )
    {
        semctl(semid, 0, IPC_RMID);
        exit(1);
    }
    printf("Executing process #2.\n");
    arg.val = 0;
    semctl(semid, 1, SETVAL, arg);
    arg.val++;
    semctl(semid, 2, SETVAL, arg);
    return 0;
}

Program #3

int
main ( )
{
    int semid, key;
    key = ftok("program0.c", 'a');
    semid = semget(key, 3, 0666 | IPC_CREAT);
    if ( semid < 0 )
    {
        perror("Could Not Created The Requested Semaphore");
        exit(1);
    }
    if ( semctl(semid, 2, GETVAL, 0) != 1 )
    {
        semctl(semid, 0, IPC_RMID);
        exit(1);
    }
    printf("Executing process #3.\n");
    semctl(semid, 0, IPC_RMID);
    return 0;
}