Question: Write a code that in C that Uses the system service calls shmget and shmat to obtain some new memory that will be accessible to

Write a code that in C that Uses the system service calls shmget and shmat to obtain some new memory that will be accessible to multiple processes (and use shmdt and shmctl to clean up when you're done).

1) First use shmget to tell the OS to assign you a segment of memory (to be shared among multiple processes) big enough to hold an integer; have your program print out the shared memory identifier it receives from shmget.

Next, use shmat to get the address for your process to use to refer to the shared memory (this step is known as "attaching" the shared memory to your process).

2) The parent process should set the new (shareable) integer to 0 and then spawn a child process

3) In the parent (after the spawn):

Ask the user for a (non-zero) value to store in the new integer.

Once that has been accomplished, the parent should spin on that integer until it becomes zero again.

After exiting from the spin, the parent should print a message saying that the shared integer is now 0 again (thus confirming successful two way communications via shared memory).

Detach the shared memory from the process's address space using shmdt

Return the shared memory segment to the memory manager using shmctl (roughly analogous to the "free" function used to return malloc'd memory).

Print out an "all done" message.

4) In the child (after the spawn):

Print some sort of "I'm here" message

Spin on the new integer until it becomes non-zero.

After exiting from the spin (because the parent eventually puts a user-supplied non-zero value in the shared integer), print out the value just received from its parent and then re-set the shared integer back to 0 (so the parent can eventually stop spinning and finish up).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!