C program to implement interprocess communication using shared memory




















The server writes this data in a message using either a pipe, fifo or message queue. A total of four copies of data are required 2 read and 2 write. So, shared memory provides a way by letting two or more processes share a memory segment. With Shared Memory the data is only copied twice — from input file into shared memory and from shared memory to the output file. Skip to content. Change Language. Related Articles. Learn more. Asked 3 years, 10 months ago. Active 3 years, 10 months ago. Viewed 1k times.

This is the structure I want to share between the parent and its multiple child processes. Improve this question. Harrish A Harrish A 27 7 7 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Sign up or log in Sign up using Google.

One way of communication using shared memory can be imagined like this: Suppose process1 and process2 are executing simultaneously, and they share some resources or use some information from another process. Process1 generates information about certain computations or resources being used and keeps it as a record in shared memory. When process2 needs to use the shared information, it will check in the record stored in shared memory and take note of the information generated by process1 and act accordingly.

Processes can use shared memory for extracting information as a record from another process as well as for delivering any specific information to other processes. The producer produces some items and the Consumer consumes that item. The two processes share a common space or memory location known as a buffer where the item produced by the Producer is stored and from which the Consumer consumes the item if needed.

There are two versions of this problem: the first one is known as the unbounded buffer problem in which the Producer can keep on producing items and there is no limit on the size of the buffer, the second one is known as the bounded buffer problem in which the Producer can produce up to a certain number of items before it starts waiting for Consumer to consume it.

We will discuss the bounded buffer problem. First, the Producer and the Consumer will share some common memory, then the producer will start producing items. If the total produced item is equal to the size of the buffer, the producer will wait to get it consumed by the Consumer.

Similarly, the consumer will first check for the availability of the item. If no item is available, the Consumer will wait for the Producer to produce it. If there are items available, Consumer will consume them. NULL in shmat means that the OS will itself attach the shared segment at a suitable address of this process.

Then some data is read from the user using read system call and it is finally written to the shared segment using strcpy function. Finally, it reads the content of the shared memory. Remember to give the same key value. Next, shmat attaches the shared segment to the current process.

After that, the data is printed from the shared segment. In the output, you will see that it is the same data that you have written while executing the Program 1.

What does shmget function return on success?



0コメント

  • 1000 / 1000