Você está na página 1de 4

Interprocess Communication With Shared Memory

There are various ways to communicate from one process to another. A few ways to do so are through Unix domain sockets, sockets on the local loopback, signals between processes, open files, message queues, pipes, and even memory mapped files. One interesting way is to use shared memory segments using a key to identify where in memory the shared segment will be. The main idea behind shared memory is based on the server / client paradigm. The server maps a section of memory and the client may have access to that shared memory for reading or writing, in doing so there is a window between the two processes in which data can be exchanged.

Functions For Accessing Shared Memory


To open this so called window we have to locate the memory segment, map it, then perform an action upon it
// Required header files #include <sys/ipc.h> #include <sys/shm.h> // Function definition int shmget(key_t key, size_t size, int shmflg);

This function returns the identifier associated with the value of the first argument key. The shmget function takes three parameters. The first parameter key is an integer value used as the identifier in which both processes use to map the memory segment. The second parameter, size is the amount of memory to map, where size is equal to the value of size rounded up to a multiple of PAGE_SIZE. You can view the system PAGE_SIZE from the command line via getconf PAGESIZE.Finally, the third parameter is used for access permissions to the shared memory segment. The values are analogous to the open(2) permission settings. In our case we use IPC_CREATE | 0666.

// Required header files #include <sys/types.h> #include <sys/shm.h> // Function definition void *shmat(int shmid, const void *shmaddr, int shmflg);

The shmat function returns the attached shared memory segment. The first argument is the return value from the shmget function call. The second argument is shmaddr, if it is NULL, the system chooses a suitable (unused) address at which to attach the segment. The third argument is akin to the shmflg mentioned above and is used for permissions. The return value is a pointer to the shared memory and can be acted upon.

Finally, once all work is complete on our address. We can close our handle using shmdt.
// Required header files #include <sys/types.h> #include <sys/shm.h> // Function definition int shmdt(const void *shmaddr);

Examples Program Using Shared Memory


#include<stdio.h> #include<fcntl.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <unistd.h> #include<sys/wait.h> int main() { pid_t pid; int shmId,status; char *start,*startchild;

pid = fork(); if(pid==0)//child { shmId=shmget(1,25, 0666); //getting the shared region startchild = shmat(shmId,NULL,SHM_RDONLY); //read only mode

printf("%s\n",startchild); shmdt((void*)shmId); //work finished and detache the shared region } else //parent { shmId = shmget(1,25, (IPC_CREAT | 0666)); //create shared region with all permissions start = shmat(shmId,NULL,0); // attach to process sprintf(start,"Hello World\n\0"); //write into shared region wait(&status); shmdt((void *)shmId); } return 0; } // work finished and detache the shared region

Exercise: You have to create a server and client example. The client grabs user input from standard input and writes it to memory then the server reads that input from the first byte of memory and prints it to the screen.

Você também pode gostar