Question: In C / C + + The project will require learning three Unix techniques: forking a child, using POSIX semaphores, using POSIX shared memory. The
In CC The project will require learning three Unix techniques: forking a child, using POSIX semaphores, using POSIX shared memory. The program should create named semaphores which both parent and child can access. It should then create a block of shared memory that will serve as a shared buffer. The producer should begin placing numbers to into the shared buffer while the consumer begins getting the numbers out of the buffer and printing them. The actions should be coordinated using semaphores, as shown in slides and of the chapter slides. The buffer should hold numbers. Examples of the three needed techniques will be in eLearning.
SLIDES &
do
produce an item in nextproduced
waitempty;
waitmutex;
add next produced to the buffer
signalmutex;
signalfull;
while true;
Do
waitfull;
waitmutex;
remove an item from buffer to nextconsumed
signalmutex;
signalempty;
consume the item in next consumed
while true;
ADDITIONAL CODE ALSO NEEDED:
int main
pidt pid;
const char semname delay;
semt semaphore semopensemname OCREAT OEXCL, ;
if semaphore SEMFAILED
printfCreate semaphore
;
exit;
switch pid fork
case :
cout "The fork failed!";
exit;
case :
if semwait semaphore
printfWait on semaphore
;
exit;
cout "Child: hello!
;
exit;
default:
cout "Parent running...
;
if sempost semaphore
printfPost semaphore
;
exit;
waitpid NULL, ;
if semclosesemaphore
printfClose semaphore
;
exit;
if semunlinksemname
printfUnlink semaphore
;
exit;
int main
pidt pid;
const char shmname myshare;
int sharefd shmopenshmname OCREAT ORDWR;
if sharefd
printfCreate shared memory failed
;
exit;
int trunc ftruncatesharefd sizeofint;
if trunc
printfTruncate failed
;
exit;
void ptr mmap sizeofint PROTREAD PROTWRITE, MAPSHARED, sharefd, ;
if ptr MAPFAILED
printfmmap failed
;
exit;
int iptr intptr;
switch pid fork
case :
cout "The fork failed!";
exit;
case :
sleep; use semaphore instead
int x iptr;
cout "Child read x endl;
exit;
default:
int y ;
iptr y;
waitpid NULL, ;
if shmunlinkshmname
printfUnlink shm
;
exit;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
