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. S1 and S2 can be just printf() statements.
P1.c
#include
static int set_semvalue(void); static void del_semvalue(void); static int semaphore_p(void); static int semaphore_v(void); static int sem_id;
static int set_semvalue(void){ union semmm 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 semmm 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); }
int main(int argc, char *argv[]){ int i; int pause_time; char op = '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 ='X'; sleep(2); }
for (i =0; i<10; i++){ if (!semaphore_v()) exit(EXIT_FAILURE); printf("%c", op);fflush(stdout); pause_time = rand() %2; sleep(pause_time); }
printf(" %d - finished ", getpid()); if (argc>1){ sleep(10); del_semvalue(); } exit(EXIT_FAILURE); }
P2.c
#include
static int set_semvalue(void); static void del_semvalue(void); static int semaphore_p(void); static int semaphore_v(void); static int sem_id;
static int set_semvalue(void){ union semmm 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 semmm 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); }
int main(int argc, char *argv[]){ int i; int pause_time; char op = '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 ='X'; sleep(2); }
for (i =0; i<10; i++){ if (!semaphore_p()) exit(EXIT_FAILURE); printf("%c", op); fflush(stdout); pause_time = rand() %3; sleep(pause_time); }
printf(" %d - finished ", getpid()); if (argc>1){ sleep(10); del_semvalue(); } exit(EXIT_FAILURE); }
semmm.c
#ifndef SEMMMM_H #define SEMMMM_H #include
union semmm { int val; struct semid_ds* buf; unsigned short* array; #if defined(_linux_) struct seminf* __buf; #endif };
#endif
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