Question

In: Computer Science

Write a program to demonstrate the implementation of Inter Process Communication (IPC) using shared memory. Single...

Write a program to demonstrate the implementation of Inter Process Communication (IPC) using shared memory. Single line text.

Solutions

Expert Solution

Inter Process Communication through shared memory is a concept where two or more process 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.

Shared memory for writer process:-

#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
using namespace std;

int main()
{
   // ftok to generate unique key
   key_t key = ftok("shmfile",65);

   // shmget returns an identifier in shmid
   int shmid = shmget(key,1024,0666|IPC_CREAT);

   // shmat to attach to shared memory
   char *str = (char*) shmat(shmid,(void*)0,0);

   cout<<"Write Data : ";
   gets(str);

   printf("Data written in memory: %s\n",str);
  
   //detach from shared memory
   shmdt(str);

   return 0;
}

SHARED MEMORY FOR READER PROCESS:-

#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
using namespace std;

int main()
{
   // ftok to generate unique key
   key_t key = ftok("shmfile",65);

   // shmget returns an identifier in shmid
   int shmid = shmget(key,1024,0666|IPC_CREAT);

   // shmat to attach to shared memory
   char *str = (char*) shmat(shmid,(void*)0,0);

   printf("Data read from memory: %s\n",str);
  
   //detach from shared memory
   shmdt(str);
  
   // destroy the shared memory
   shmctl(shmid,IPC_RMID,NULL);
  
   return 0;
}


Related Solutions

please use c write a program that utilizes pipes for Interprocess Communication (IPC). The program will...
please use c write a program that utilizes pipes for Interprocess Communication (IPC). The program will create a pipe(s) to allow the parent and child to communicate. The parent process will generate a random number based on the pid of the child process created. The parent will send this random number to the child and wait for a response (Received) from the child. After receiving a response from the child, the parent will send another message to the child telling...
Write a 500-word paper on what is inter processor communication in parallel machines. Further, explain shared...
Write a 500-word paper on what is inter processor communication in parallel machines. Further, explain shared memory conflicts in inter processor communication. And finally discuss how it can be solved—specifically with Shared Memory and MPI (message-passing-interface).
Using pipes for inter-process communications, write a C program to produce a parent/child relationship in which...
Using pipes for inter-process communications, write a C program to produce a parent/child relationship in which the parent process repeatedly counts and displays on the screen the number of characters for any user password the child process inputs to the pipe. The communications should cease once the string “Done” is inputted.
List two ways that inter -process communication is Achieved ?
List two ways that inter -process communication is Achieved ?
C++ On linux Write a C++ program using the IPC. Declare two variables a=5 and b=6...
C++ On linux Write a C++ program using the IPC. Declare two variables a=5 and b=6 in writer process. Now their product (a*b) will be communicated to the reader process along with your name. The reader process will now calculate the square root of the number and display it along with your name.
how to accept string from shared memory using c language ?
how to accept string from shared memory using c language ?
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Write an inline assembly program that initializes a 100 byte area of memory to 0xFF using...
Write an inline assembly program that initializes a 100 byte area of memory to 0xFF using the STOS instruction with REP using DWORD transfers.
Write an inline assembly program that initialized a 100 byte area of memory using the STOS...
Write an inline assembly program that initialized a 100 byte area of memory using the STOS instruction.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT