Linux process communication mode--shared memory

Source: Internet
Author: User


1. Concept

Different processes see common areas


650) this.width=650; "title=" QQ picture 20160707140543.png "alt=" wkiol1d98gby7gq0aaal35_vs6o688.png "src="/http S5.51cto.com/wyfs02/m01/83/d7/wkiol1d98gby7gq0aaal35_vs6o688.png "/>

2. Features

(1) is the fastest way to communicate between processes, mapping different memory (two copies less)

(2) does not provide any synchronous mutual exclusion mechanism, also does not maintain itself

(3) Simple interface

3. Means of communication

Provided by file system--pipeline

Supplied by System V-Message Queuing, semaphores, shared memory

shared memory combined with semaphores

4, realize Shmat, SHMDT

At: Hook up dt: go to hook up

0
1
2
1
0
Create
Hook up
Seen by another process
Exit
And then quit.

Code implementation:

Shm.h file:

#ifndef __shm__
#define __SHM__

#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <unistd.h>

#define __PATH__ "."
#define __PROJECT__ 8888
#define __SHM_SIZE__ 4096

int Get_shm ();
char* At_shm ();
int Delete_shm ();
int Rm_shm ();
#endif

SHM.C file:

#include "shm.h"
int Get_shm ()
{
key_t Key=ftok (__path__,__project__);
int flag=ipc_creat|0666;
int Shm_id=shmget (KEY,__SHM_SIZE__,FLAG);
if (shm_id==-1) {
printf ("Get share Memory error!\n");
}else{
printf ("Get share Memory success!\n");
}
return shm_id;
}

char *at_shm (int shm_id)
{
Return (char*) Shmat (shm_id,null,0);
}

int Delete_shm (char *addr)
{
return Shmdt (addr);
}

If Success,return (), Else return-1
int rm_shm (int shm_id)
{
Return Shmctl (Shm_id,ipc_rmid,null);
}

TEST_SHM.C file:

#include "shm.h"

int main ()
{
int Shm_id=get_shm ();
pid_t id=fork ();
if (id<0) {
printf ("fork error\n");
return 1;
}else if (id==0) {//child
Char *buf=at_shm (shm_id);
int i=0;
while (i<4096) {
buf[i]= ' X ';
i++;
}
buf[4096]= ' + ';
DELETE_SHM (BUF);
}else{//father
Char *buf=at_shm (shm_id);
Sleep (5);
printf ("%s\n", buf);
DELETE_SHM (BUF);
Waitpid (id,null,0);
RM_SHM (shm_id);
}
return 0;
}

Makefile File:

TEST_SHM:SHM.C TEST_SHM.C
Gcc-o [email protected] $^
. Phony:clean
Clean
Rm-f TEST_SHM

Operation Result:

[[email protected] shm]$ make

Gcc-o Test_shm shm.c TEST_SHM.C

[Email protected] shm]$./TEST_SHM

Get Share Memory success!

[Email protected] shm]$


Linux process communication mode--shared memory

Related Article

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.