I. Introduction to the IPC (inter-process communication, interprocess communication) object
The IPC objects for System V have shared memory, message queues, and semaphores.
Note: In the IPC communication mode, each IPC object has a unique name, called "Key", whether it is using Message Queuing or shared memory, or even a semaphore . With the key, the process is able to identify the object being used. The relationship between the "key" and the IPC object is like a file name in a file, through a filename, a process can read and write data within a file, and even multiple processes can share a single file. In the IPC communication mode, the use of "keys" also enables an IPC object to be shared by multiple processes. Introduction to Shared memory <1> shared memory is one of the most efficient ways to communicate between processes, and processes can read and write directly to memory without requiring any copy of the data. <2>In
order to exchange information between multiple processes, the kernel specifically leaves out a chunk of memory that can be mapped to its own private address space by the process that needs to be accessed. The process can read and write directly to this piece of memory without having to copy the data, thereby greatly improving efficiency. <3> because multiple processes share a piece of memory, you also need to rely on some kind of synchronization mechanism. Three, the characteristics of shared memory
Iv. operation flow of shared memory <1> Create/Open Shared memory <2> map shared memory, that is, map the specified shared memory to the address space of the process for access <3> revoke shared memory mappings <4> Delete shared memory Objects v. Related APIs A. Getting A piece of shared memory
Function: Allocate a piece of shared memory return value: The call successfully returns a shmid (like opening one or creating a file with a file descriptor); call failed return-1. Parameter description: <1>key identifies the key value of shared memory (as if the file's identity is a filename): 0/ipc_private. When the value of key is Ipc_private, the function shmget () Creates a new shared memory, and if the value of key is 0, and the ipc_create is set in the parameter SHMFLG, a new shared memory is created as well.
shared memory, which is allocated in this way, is typically used for inter-process communication between relatives. Note: We generally get the key value by Ftok this function
Function: Gets the key value parameter description of an IPC object: Pthname is the path to the file name you specify (it must be present and accessible), generally we write a directory proj_id: together with Pthname to complete the parameters that create the key value, although it is an int, But only 8 bits are used. Generally we write a character instead. Example: Case:
Results of the operation:
<2>
the size is the length of the shared memory to be established. All memory allocation operations are in page units. So if a process only requests a single byte of memory, the memory will also be allocated a full page (the default size of a page in the i386 machine Pace_size = 4096 bytes). <3>SHMFLG valid flags include Ipc_creat and IPC_EXCL, and their function is equivalent to open () O_creat and O_EXCL. Ipc_creat if the shared memory does not exist, create a shared memory, or open the existing IPC_EXCL directly if the shared memory does not exist, the new shared memory is established, or an error occurs
Example one: If the key value is key, create a shared memory size of 4k, the access permission is 066, if it already exists then return its identification number int shmid;if (Shmid = Shmget (key,4 * 1024,0666 | ipc_creat)) < 0) {perror ("Fail to Shmget"); Exit (Exit_failure)} example two, the assumption that the key value is key, create a shared memory size of 1k, access to 0666, if already exists the error int shmid;if ((Shmid = Shmget (key,1024,0666 | Ipc_creat | IPC_EXCL)) < 0) {perror ("Fail to Shmget"); Exit (exit_failure);} B. Mapping of Shared memory
The function Shmat maps the identification number to Shmid shared memory into the address space of the calling process. Parameter description: Shmid: Shared memory zone identifier to be mapped SHMADDR: Maps shared memory to the specified address (if NULL, indicates system auto-complete mapping) shmflg:shm_rdonly shared memory read-only default 0: Shared memory is readable and writable. Return value: The call successfully put back the mapped address, error put back (void *)-1; C. Cancel the mapping between shared memory and user processes
The parameter shmaddr is the address where the Shmat mapping was successfully put back. Note: When a process no longer needs a shared memory segment, it calls the SHMDT () system call to cancel the segment, but this does not really remove the segment from the kernel,
instead, the value of the Shm_nattch domain of the associated SHMID_DS structure is reduced by 1,
when this value is 0 o'clock, the kernel removes the shared segment physically.。 D. Controlling shared memory
Parameter description: Shmid shared memory ID cmd Ipc_stat get the state of shared memory Ipc_set change the state of shared memory
ipc_rmid Deleting shared memoryBUF is a struct-body pointer. When Ipc_stat, the acquired state is placed in the structure. If you want to change the state of shared memory, specify it with this struct;
Attention:1. The
ipc_rmid command does not actually delete a segment from the kernel, but simply marks the segment as deleted, and the actual deletion occurs when the last process leaves the shared segment .
2. When CMD is Ipc_rmid, the third argument should be null. Well, most of us are doing this, using this function to delete shared memory. Case study:
Click (here) to collapse or open
- #include <sys/shm.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <semaphore.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #define BUFF_SIZE 1024
- int father_do_work (int shmid)
- {
- Char *buf;
- void *shmaddr;
- Sem_t *prsem;
- Sem_t *pwsem;
- Well-known signal volume
- if (Prsem = Sem_open ("Rsem", o_creat,0666,0) = = = sem_failed)
- {
- Perror ("Fail to SEM open");
- return-1;
- }
- Well-known signal volume
- if (Pwsem = Sem_open ("Wsem", o_creat,0666,1) = = = sem_failed)
- {
- Perror ("Fail to SEM open");
- return-1;
- }
- Mapping shared memory
- if ((shmaddr = Shmat (shmid,null,0)) = = (void *)-1)
- {
- Perror ("Fail to Shmat");
- Exit (Exit_failure);
- }
- BUF = (char *) shmaddr;
- while (1)
- {
- if (sem_wait (Pwsem) < 0)
- {
- Perror ("Fail to Sem wait");
- Break
- }
- printf (">");
- Fgets (Buf,buff_size,stdin);
- Buf[strlen (BUF)-1] = ' + ';
- if (Sem_post (Prsem) < 0)
- {
- Perror ("Fail to Sem post");
- Break
- }
- if (strncmp (buf, "Quit", 4) = = 0)
- {
- if (SHMDT (SHMADDR) < 0)
- {
- Perror ("Fail to Shmaddr");
- Exit (Exit_failure);
- }
- Break
- }
- Usleep (500);
- }
- return 0;
- }
- int child_do_work (int shmid)
- {
- Char *buf;
- void *shmaddr;
- Sem_t *prsem;
- Sem_t *pwsem;
- //
- if (Prsem = Sem_open ("Rsem", o_creat,0666,0) = = = sem_failed)
- {
- Perror ("Fail to SEM open");
- return-1;
- }
- if (Pwsem = Sem_open ("Wsem", o_creat,0666,1) = = = sem_failed)
- {
- Perror ("Fail to SEM open");
- return-1;
- }
- Mapping shared memory
- if ((shmaddr = Shmat (shmid,null,0)) = = (void *)-1)
- {
- Perror ("Fail to Shmat");
- Exit (Exit_failure);
- }
- BUF = (char *) shmaddr;
- while (1)
- {
- if (sem_wait (Prsem) < 0)
- {
- Perror ("Fail to Prsem");
- Break
- }
- printf ("Read buf:%s.\n", buf);
- if (Sem_post (Pwsem) < 0)
- {
- Perror ("Fail to Pwsem");
- Break
- }
- if (strncmp (buf, "Quit", 4) = = 0)
- {
- if (SHMDT (SHMADDR) < 0)
- {
- Perror ("Fail to Shmaddr");
- Exit (Exit_failure);
- }
- Break
- }
- }
- return 0;
- }
- int main ()
- {
- int shmid;
- int pid;
- void *shmaddr;
- Create shared memory
- if (Shmid = Shmget (ipc_private,buff_size,0666 | ipc_creat)) < 0)
- {
- Perror ("Fail to Shmget");
- Exit (Exit_failure);
- }
- if ((PID = fork ()) < 0)
- {
- Perror ("Fail to Fork");
- Exit (Exit_failure);
- }else if (pid = = 0) {
- Child_do_work (Shmid);
- }else{
- Father_do_work (Shmid);
- Wait (NULL);
- if (Shmctl (Shmid,ipc_rmid,null) < 0)
- {
- Perror ("Fail to Shmctl");
- Exit (Exit_failure);
- }
- }
- Exit (exit_success);
- }
Operation Result:
Inter-process communication---shared memory