I'll use a few blogs to summarize some of the ways in which you can communicate between processes in Linux, and I'm going to make this summary part of the beginning in every blog in this series.
Ways to communicate between processes
- Pipeline
- Message Queuing
- Signal
- Signal Volume
- Shared storage Area
- Socket (socket)
Interprocess Communication (iii)-Semaphore Portal: http://www.cnblogs.com/lenomirei/p/5649792.html
interprocess communication (ii)-Message Queuing Portal: http://www.cnblogs.com/lenomirei/p/5642575.html
interprocess communication (i)-Pipeline Portal: http://www.cnblogs.com/lenomirei/p/5636339.html
This is the main record of the share of the operation of the storage area, that is, shared storage, is actually shared memory, the process has the ability to communicate with each other storage area also has memory
Why use shared storage for communication? Because hurry! the pipeline is a file, the operation is slow, the message queue creation operation has consumed so slow, shared memory is to create two processes can directly to the memory of the operation, each other is visible.
Why write programs with shared storage? Because the interface is simple! The operation is definitely much simpler than Message Queuing.
- Create a shared storage area
Although the feeling is very simple thing, but still want to create to open up, otherwise each process uses its own address space to map to different physical address, even if the virtual address is the same, is independent of each other, is not visible to each other, after declaring the shared storage area, Before you can map to this common area (that's useful).
- function prototypes: int shmget (key_t key, size_t size, int shmflg);
- Header files: #include <sys/ipc.h> #include <sys/shm.h>
- Parameter resolution
- The key parameter is obtained by the return value of the Ftok function, or the incoming ipc_private is automatically assigned by the operating system
- Size means that you want to open up how much shared storage space,PS: Allocation of space will eventually become allocated physical space, is through the allocation of full page, the next page size of Linux system is 4kb=4096b, less than 4096B is allocated a page, Size incoming 4097 assigns two pages
- SHMFLG has Ipc_creat and ipc_excl.
Call this function to open a shared storage area, you can view the current shared storage state by ipcs-m
The first key value is the incoming Key,shmid identity unique shared memory segment, the owner privilege byte does not say much, this nattch refers to how many processes are currently connected to the shared store
Nattch: Creating a shared store is not enough, you need to link it to a process in order to have an address in the process's address space mapped to a shared memory segment.
- Connections to shared storage areas
This tells you what function to use to link shared storage, it is important to Note that two processes need to be linked, the process of creating a shared store is not automatically connected, and you need to call the link function
- Function prototypes: void *shmat (int shmid, const void *shmaddr, int shmflg);
- Header files: #include <sys/types.h> #include <sys.shm.h>
- Parameter resolution
- Shmid represents the identity of a shared store
- The second parameter represents the start address of the shared store, and if it is not well-known to the memory , it is recommended to the operating system, set to 0
- SHMFLG can set the current process read and write permissions, Sem_rdonly and the like, the default 0 for reading and writing can be, test program I gave 0
Each time a process calls this function, it causes Nattch to increment by 1, and the return value is because a pointer that is an empty type often requires a cast
- Broken link for shared store
It's a good habit to disconnect after using the link, and to destroy the shared storage space
- Function prototype: int SHMDT (const void *SHMADDR);
- Header files: #include <sys/types.h> #include <sys.shm.h>
- Parameter resolution Delete shared storage
- Is the starting address of the shared store to be deleted, because it is an empty type of pointer, no need to convert, can be deleted directly
- To delete a shared storage area
Finally, the SHMCTL function is used to delete the shared storage area.
- function prototypes: int shmctl (int shmid, int cmd, struct shmid_ds *buf);
- Header files: #include <sys/ipc.h> #include <sys/shm.h>
- Parameter resolution
- Shmid represents the identity of a shared store
- CMD gives ipc_rmid means delete
- Because the second parameter is set to Ipc_rmid for deletion, the third parameter is useless, and the third parameter directly gives null (0)
Things have been done, the basic operation is finished, nonsense less,Show me the Code
My program is divided into comm.h (Common header File) COMM.C (package basic function) SERVER.C (simple server side) altogether 3 files
function primarily implements simple string sharing? The parent process writes, the child process prints, it's that simple
The results of the diagram and see what the Ghost
Comm.h
1#include <stdio.h>2#include <sys/types.h>3#include <string.h>4#include <sys/ipc.h>5#include <unistd.h>6#include <sys/shm.h>7#include <errno.h>8#include <stdlib.h>9 Ten One #define_path_name_ "/tmp" A #define_proj_id_ 0x6666 - - the - Static intCOMM_CREATE_SSM (intflags,size_t size); - intCreate_shm (size_t size); - intGet_shm (); + Char*shm_at (intshm_id); - voidDESTORY_SHM (intshm_id); + intShm_dt (Char*ADDR);
Comm.c
1#include"comm.h"2 3 4 Static intCOMM_CREATE_SHM (intflags,size_t size)5 {6key_t _key=Ftok (_path_name_,_proj_id_);7 if(_key<0)8 {9printf"%d:%s", Errno,strerror (errno));Ten } One intshm_id; A if((Shm_id=shmget (_key,size,flags)) <0) - { -printf"Shmget error,%d:%s", Errno,strerror (errno)); the } - returnshm_id; - } - + - intCreate_shm (size_t size) + { A intFlags=ipc_creat |ipc_excl; at returnComm_create_shm (flags,size); - } - - intGet_shm () - { - intflags=ipc_creat; in returnCOMM_CREATE_SHM (Flags,0); - } to + Char*shm_at (intshm_id) - { the return(Char*) Shmat (Shm_id,null,0); * } $ intShm_dt (Char*addr)Panax Notoginseng { - returnshmdt (addr); the } + A voidDESTORY_SHM (intshm_id) the { +Shmctl (Shm_id,ipc_rmid,0); -}
Server.c
1#include"comm.h"2 3 4 5 intMain ()6 {7 intPid=fork ();8 if(pid>0)9 {Ten //Father One intSHM_ID=CREATE_SHM (4096); A Char*buf=Shm_at (shm_id); - intI=0; - while(i<4096) the { -Sleep1); -buf[i]='A'; -i++; +buf[i]=' /'; - } + } A Else at { - // Child - intShm_id=Get_shm (); - Char*buf=Shm_at (shm_id); - while(1) - { inSleep1); -printf"%s\n", buf); to } + } - return 0; the}
Interprocess communication (quad)-Shared memory