0% found this document useful (0 votes)
9 views

OSL Assignment 8

Uploaded by

patilpranita4221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

OSL Assignment 8

Uploaded by

patilpranita4221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment 8:Inter-process Communication

using Shared Memory: Application to


demonstrate: Client and Server Programs in
which server process creates a shared
memory segment and writes the message to
the shared memory segment. Client process
reads the message from the shared memory
segment and displays it to the screen.
IPC through shared memory
• Inter-Process Communication (IPC) is a fundamental concept in operating systems
that allows multiple processes to communicate and synchronize their actions.
• Among the various methods of IPC shared memory is one of the most efficient
mechanisms especially when it comes to performance-critical applications.
• This article delves into the concept of IPC through shared memory explaining its
working, advantages, and disadvantages.
• Inter-process Communication through shared memory is a concept where two or more
processes can access the common memory and communication is done via this shared
memory where changes made by one process can be viewed by another process.
IPC through shared memory
The problem with pipes, fifo a,d message queue – is that for two processes to exchange
information. The information has to go through the kernel.
• The server reads from the input file.
• The server writes this data in a message using either a pipe, FIFO, or message queue.
• The client reads the data from the IPC channel, again requiring the data to be copied
from the kernel’s IPC buffer to the client’s buffer.
• Finally, the data is copied from the client’s buffer.
IPC through shared memory
Four copies of data are required (2 read and 2 write). So, shared memory provides a way
to let two or more processes share a memory segment. With Shared Memory the data is
only copied twice from the input file into shared memory and from shared memory to
the output file.
What is Shared Memory?
The Shared memory is a memory segment that multiple processes can access
concurrently. It is one of the fastest IPC methods because the processes communicate by
the reading and writing to a shared block of the memory. Unlike other IPC mechanisms
that involve the more complex synchronization and data exchange procedures shared
memory provides the straightforward way for the processes to share data.
How Shared Memory IPC Works?
The Shared memory IPC works by creating a memory segment that is accessible by the
multiple processes. Here’s a basic outline of how it operates:
Creation of Shared Memory Segment: A process usually the parent, creates a shared
memory segment using the system calls like shmget() in Unix-like systems. This segment
is assigned the unique identifier (shmid).

Attaching to the Shared Memory Segment: The Processes that need to access the
shared memory attach themselves to this segment using shmat() system call. Once
attached the processes can directly read from and write to the shared memory.
How Shared Memory IPC Works?

Synchronization: Since multiple processes can access the shared memory


simultaneously synchronization mechanisms like semaphores are often used to the
prevent race conditions and ensure data consistency.

Detaching and Deleting the Segment: When a process no longer needs access to the
shared memory it can detach from the segment using shmdt() system call. The shared
memory segment can be removed entirely from system using shmctl() once all processes
have the detached.
Used System Calls
The system calls that are used in the program are:
Function Signature Description

ftok() key_t ftok() It is used to generate a unique key.

int shmget(key_t key,size_t size, int Upon successful completion, shmget() returns an identifier for the
shmflg); shared memory segment.
shmget()

Before you can use a shared memory segment, you have to attach
void *shmat(int shmid ,void *shmaddr ,int yourself to it using shmat(). Here, shmid is a shared memory ID
shmflg); and shmaddr specifies the specific address to use but we should set
shmat() it to zero and OS will automatically choose the address.

When you’re done with the shared memory segment, your program
int shmdt(void *shmaddr); should detach itself from it using shmdt().
shmdt()

When you detach from shared memory, it is not destroyed. So, to


shmctl(int shmid,IPC_RMID,NULL); destroy shmctl() is used.
shmctl()
Conclusion
The Shared memory IPC is a powerful tool for the process communication offering
the high speed and low overhead for the data exchange between the processes.
However, it comes with the challenges related to synchronization, security and
resource management. Understanding these aspects is crucial for the effectively
implementing shared memory IPC in a multitasking environment.

You might also like