Linux进程间通信之内存共享

来源:互联网 发布:纯5g网络概念股龙头 编辑:程序博客网 时间:2024/05/16 23:53

共享内存:使得多个进程可以访问同⼀一块内存空间,是最快的可⽤的IPC形式。是针对其他通信机制运⾏行效率较低⽽而设计的。往往与其它通信机制,如信号量结合使⽤用,来达到进程间的同步及互斥。
内存共享的实现:
1、创建共享内存
使用函数shmget,原型为: int shmget(key_t key, size_t size, int shmflg);
参数key:key_t key = ftok(PATHNAME,PROJ_ID);成功返回非负整数,失败返回-1;
参数size:指定共享内存的大小,一、以字节为单位;
参数shmflag:权限标志,IPC_CREAT|IPC_EXCL:若共享内存存在变出错返回,不存在便创建;IPC_CREAT:若共享内存存在便打开返回,若不存在便创建;
2、释放共享内存
使用函数shmctl:原型为:int shmctl(int shm_id, int cmd, struct shmid_ds *buf);
参数shm_id:shmget函数返回的共享内存标识符;
参数cmd:要做的操作,一般为IPC_RMID,表示删除共享内存段;
参数buf:结构指针,
struct shmid_ds
{
uid_t shm_perm.uid;
uid_t shm_perm.gid;
mode_t shm_perm.mode;
};
3、将共享内存挂接到进程的地址空间
使用shmat函数,原型为:void *shmat(int shm_id, const void *shm_addr, int shmflg);
第二个参数一般为NULL,第三个参数一般为0;
4.将共享内存从当前进程中分离,但并不是删除,只是不能再访问此内存
使用函数shmdt,原型为:int shmctl(int shm_id, int command, struct shmid_ds *buf);
下面是连个进程之间通过共享内存通信:

comm.h#ifndef _COMM_H#define _COMM_H#define PATHNAME "."#define PROJ_ID 0x6666#include<stdio.h>#include<sys/ipc.h>#include<sys/shm.h>int create_shm(int size);int get_shm();int distroy_shm(int shmid);#endifcomm.c#include"comm.h"static int comm_shm(int size,int flags){    key_t key = ftok(PATHNAME,PROJ_ID);    if(key<0)    {        perror("ftok");        return -1;    }    int shmid = shmget(key,size,flags);  if(shmid<0)  {    perror("shmget");    return -2;  }  return shmid; }int create_shm(int size){        comm_shm( size,IPC_CREAT|IPC_EXCL|0666);}int get_shm(){     comm_shm( 0,IPC_CREAT);    }int distroy_shm(int shmid){    if(shmctl(shmid,IPC_RMID,NULL)<0)    {        perror("shmctl");        return -1;    }}server.c#include"comm.h"int main(){    int shmid = create_shm(4096);    char *buf = shmat(shmid,NULL,0);    sleep(5);    int count = 0;    while(count<40)    {        buf[count] = 'A'+count%26;        count++;        buf[count] = 0;        sleep(2);    }    shmdt(buf);    distroy_shm(shmid);    return 0;}client.c#include"comm.h"int main(){    int shmid = get_shm();    sleep(5);    char *buf = shmat(shmid,NULL,0);    while(1)    {     printf("%s",buf);    sleep(5);    }    sleep(5);    shmdt(buf);    return 0;}
0 0
原创粉丝点击