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 <br>
Program #0 is always executed (called) first, it also creates semid's <br>
Program #1 can ONLY be executed if Program #0 has ran <br>
Program #2 can ONLY be executed if Program #1 has ran <br>
Program #3 can ONLY be executed if Program #2 has ran <br>

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

Program Call <br>
./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;
    }