Question
Semaphores can be used for synchronization. For instance, consider two processes, P1 and P2. P1 has to wait until P2 finishes statement S2 before P1
Semaphores can be used for synchronization. For instance, consider two processes, P1 and P2. P1 has to wait until P2 finishes statement S2 before P1 can execute statement S1. Implement the example using the system semaphore functions and the functions provided by the Linux book.
semget The semget function creates a new semaphore or obtains the semaphore key of an existing semaphore: int semget(key_t key, int num_sems, int sem_flags);
semop The function semop is used for changing the value of the semaphore: int semop(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops); The first parameter, sem_id, is the semaphore identifier, as returned from semget. The second parameter, sem_ops, is a pointer to an array of structures, each of which will have at least the following members: struct sembuf { short sem_num; short sem_op; short sem_flg; }
semctl The semctl function allows direct control of semaphore information: int semctl(int sem_id, int sem_num, int command, ...); The first parameter, sem_id, is a semaphore identifier, obtained from semget. The sem_num parameter is the semaphore number. You use this when youre working with arrays of semaphores. Usually, this is 0, the first and only semaphore. The command parameter is the action to take, and a fourth parameter, if present, is a union semun, which according to the X/OPEN specification must have at least the following members: union semun { int val; struct semid_ds *buf; unsigned short *array; }
HERE IS THE EXAMPLE code given by the book
#include
int main(int argc, char *argv[]) { int i; int pause_time; char op_char = 'O'; srand((unsigned int)getpid()); sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); if (argc > 1) { if (!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore "); exit(EXIT_FAILURE); } op_char = 'X'; sleep(2); } for(i = 0; i < 10; i++) { if (!semaphore_p()) exit(EXIT_FAILURE); printf("%c", op_char);fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char);fflush(stdout); if (!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time); } printf(" %d - finished ", getpid()); if (argc > 1) { sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); }
static int set_semvalue(void) { union semun sem_union; sem_union.val = 1; if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0); return(1); }
static void del_semvalue(void) { union semun sem_union; if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1) fprintf(stderr, "Failed to delete semaphore "); }
static int semaphore_p(void) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; /* P() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_p failed "); return(0); } return(1); }
static int semaphore_v(void) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; /* V() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_v failed "); return(0); } return(1); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started