Starting from:

$30

Operating Systems PROJECT 5 CRITICAL SECTION – SEMAPHORES


CECS-326: Operating Systems
PROJECT 5
CRITICAL SECTION – SEMAPHORES
Name: ____________________________________
Last, First
.
 ABSOLUTELY NO LATE PROJECTS WILL BE ACCEPTED
PROJECT DESCRIPTION:
Write a program using C/C++ to do the following.
Create N child processes. Each child process writes its child number, its PID, its parent PID, and its child
PID in a buffer. Create a critical section where each child process displays the content of the buffer.
Create a semaphore mechanism to protect the critical section. Use a delay adjustment parameter k in the
critical section to adjust the speed of the display process to show that without semaphore protection the
displayed contents of the buffer are randomly interleaved.
 semaphore N opt k
where:
N: number of child processes (integer)
opt: option: “n” Æ no semaphore protection, “s” Æ with semaphore protection
k: delay adjustment parameter (integer)
Examples of program output are shown below:
semaphore 3 n 200
i: 3 pi: 1 processi:2 process ID:4084 parent ID:4049 child ID:40 ID:4085 parent ID:4084 child ID:0
rocess ID:4086 parent ID:4084 child ID:086
semaphore 3 s 200
i: 1 process ID:4087 parent ID:4049 child ID:4088
i: 2 process ID:4088 parent ID:4087 child ID:4089
i: 3 process ID:4089 parent ID:4088 child ID:0
DOCUMENTATION:
The project must be carefully documented. The write-up must include: one page of problem analysis and
discussion (include a flow chart or structure chart when relevant), program listing with proper comments,
one page of output listing using typical values of N, opt (including at least one case of each), and k. The
discussion should include general considerations, UNIX function calls, any reasonable assumptions, and
any other relevant information. Attach this sheet as the front page of your project. Firmly staple the entire
project together. No folder is used. Note that this project is worth twice as project 1. 
29
HINTS FOR PROJECT 5:
1) Function to initialize the struct sembuf structure members sem_num, sem_op, and sem_flg in an
implementation-independent manner:
Prototype:
 void set_sembuf_struct(struct sembuf *s, int semnum, int semop, int semflg);
 void set_sembuf_struct(struct sembuf *s, int num, int op, int flg)
 {
 s- sem_num = (short) num;
 s- sem_op = op;
 s- sem_flg = flg;
 return;
 }
2) Implement wait() and signal()
 struct sembuf semwait[1];
 struct sembuf semsignal[1];
 /* Initialize semaphore element to 1 */
 set_sembuf_struct(semwait, 0, -1, 0);
 set_sembuf_struct(semsignal, 0, 1, 0);
 if (semop(semid, semsignal, 1) == -1) {
 printf ("%ld: semaphore increment failed - %s\n", (long)getpid(), strerror(errno));
 if (semctl(semid, 0, IPC_RMID) == -1)
 printf ("%ld: could not delete semaphore - %s\n", (long)getpid(), strerror(errno));
 exit(1);
 }
3) Entry section:
 while (( (semop_ret = semop(semid, semwait, 1) ) == -1) && (errno ==EINTR));
 if (semop_ret == -1)
 printf ("%ld: semaphore decrement failed - %s\n", (long)getpid(), strerror(errno));
 else
4) Exit section:
 while (((semop_ret = semop(semid, semsignal, 1)) == -1) && (errno == EINTR));
 if (semop_ret == -1)
 printf ("%ld: semaphore increment failed - %s\n", (long)getpid(), strerror(errno));
END OF PROJECT 5
 

More products