Linux-interprocess communication-shared memory

Source: Internet
Author: User

One, shared memory

Shared memory is the most efficient way to communicate, because there is no need for a process to be copied to the kernel and another process to read in the kernel.

Second, ipcs-m View shared memory

Ipcrm-m Deleting shared memory

Three, the main function

Shmget Create

Shmctl Delete

Shmat Hook up

SHMDT Cancel Hook up

Man function Name View * * * *

Iv. implementation of the Code

Comm.h

1 #pragma once 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5 #include <sys/ipc.h  > 6 #include <sys/shm.h> 7 #define _PATH_ "." 8 #define _PROJID_ 0x666 9 #define _SHM_SIZE_ 4096 int getshmget (); one int destoryshm (int shm_id); char* at_shm (int shm_id); int Delete_shm (char *addr);

Comm.c

  1  #include "comm.h"   2 int getshmget ()   3 {  4      key_t key=ftok (_path_,_projid_);  5   6      int shmflg=ipc_creat |0666;  7     int  shm_id=shmget (KEY,_SHM_SIZE_,SHMFLG);   8     if (shm_id<0)    9     { 10         perror (" Shmget "); 11         return -1; 12      }    13     return shm_id; 14 } &NBSP;&NBSP;&NBSP;&NBSP;15&NBSP;&NBSP;16&NBSP;INT&NBSP;DESTORYSHM (int shm_id)  17 { 18      return shmctl (shm_id,ipc_rmid,null); 19 }     20  21 char* at_SHM (int shm_id)  22 { 23     return  (char*) Shmat (Shm_id,null, 0);  24 } 25 int delete_shm (CHAR&NBSP;*ADDR)  26 { 27    &NBSP;&NBSP;RETURN&NBSP;SHMDT (addr);  28 }

Client.c

1 #include "comm.h" 2 int main () 3 {4 int shm_id=getshmget ();  5 char *addr=at_shm (shm_id);  6 int i=0;     7 for (; i<_shm_size_;i++) 8 {9 addr[i]= ' A '; addr[i+1]= ' + '; sleep (1); 12} Delete_shm (addr); return 0; 15}

Server.c

1 #include "comm.h" 2 int main () 3 {4 int shm_id=getshmget ();  5 char * ADDR=AT_SHM (SHM_ID);  6 int i=0; 7 for (; i<_shm_size_;i++) 8 {9 printf ("%s\n", addr), sleep (1), one} and Delete_shm (a DDR); DESTORYSHM (shm_id); return 0; 15}

Run results

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7F/07/wKioL1cQj3LTm-1QAAKNoA_IxzY768.jpg "title=" SHM. JPG "alt=" Wkiol1cqj3ltm-1qaaknoa_ixzy768.jpg "/>

Linux-interprocess communication-shared memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.