歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 進程間通訊之共享內存,進程間通訊共享

進程間通訊之共享內存,進程間通訊共享

日期:2017/3/3 18:10:27   编辑:學習Linux

進程間通訊之共享內存,進程間通訊共享

進程間通訊之共享內存,進程間通訊共享


共享內存是被多個時程共享的一部份物理內存,共享內存是進程間共享數據的一種最快的方法。

頭文件:#include<sys/types.h> #include<sys/shm.h>

A:創建共享內存。

int shmget(key_t key,int size,int shmflg)

1、key:鍵值,由ftok函數獲得。

2、size:創建共享內存的大小。

3、shmflg:標識位。

成功返回共享內存標識符,失敗返回-1。

B:映射共享內存。

int shmat(int shmid,char *shmaddr,int flag)

1、shmid:shmget函數返回的共享內存標識符。

2、flag:決定以什麼方式來確定映射的地址(通常為NULL)。

成功返回共享內存映射到進程中的地址,失敗返回-1。

C:刪除映射

int shmdt(char *shmddr)

D:清除共享內存

int shmctl(int shmid,int cmd,struct shmid_ds *buf)

例子:兩個進程利用共享內存相互通信

server.c

 1 /*************************************************************************
 2     > File Name: server1.c
 3     > Author: xu
 4     > Mail: [email protected] 
 5     > Created Time: 2016年10月09日 星期日 14時38分18秒
 6  ************************************************************************/
 7 
 8 #include<stdio.h>
 9 #include<stdlib.h>
10 #include<signal.h>
11 #include<sys/stat.h>
12 #include<sys/types.h>
13 #include<fcntl.h>
14 #include<unistd.h>
15 #include<sys/shm.h>
16 #include<sys/ipc.h>
17 #define BUFFER_SIZE 128
18 
19 void myfun()
20 {
21     return;
22 }
23 
24 int main()
25 {
26     struct mybuf
27     {
28         int pid;
29         char buf[BUFFER_SIZE];
30     };
31     
32     int key,shmid;
33     int pid;
34     struct mybuf *p;
35     //注冊SIGUSR1到函數中
36     signal(SIGUSR2,myfun);
37     key = ftok("./e.c",'a');
38     if(key < 0)
39     {
40         printf("creat two process error\n");
41         exit(1);
42     }
43     //創建共享內存
44     shmid = shmget(key,BUFFER_SIZE,IPC_CREAT|S_IRWXU);
45     if(shmid < 0)
46     {
47         perror("shmget");
48         exit(1);
49     }
50     printf("create share memory success\n");
51     //映射共享內存
52     p = (struct mybuf *)shmat(shmid,NULL,0);
53     if(p == NULL)
54     {
55         perror("shmat");
56         exit(1);
57     }
58     printf("map the share memory success\n");
59     
60     p->pid = getpid();    //把服務器PID寫入共享內存中
61 
62     pause();    //等待客戶端
63 
64     pid = p->pid;    //讀取客戶端PID
65 
66     while(1)
67     {
68         printf("parent process start write share memory\n");
69         fgets(p->buf,124,stdin);
70         kill(pid,SIGUSR1);
71         pause();
72     }
73     shmdt(p);
74     shmctl(shmid,IPC_RMID,NULL);
75     
76     return 0;
77 }

client.c

/*************************************************************************
    > File Name: client1.c
    > Author: xu
    > Mail: [email protected] 
    > Created Time: 2016年10月09日 星期日 14時38分30秒
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#define BUFFER_SIZE 128

void my_fun()
{
    return;
}

int main()
{
    struct my_buf
    {
        int pid;
        char buf[BUFFER_SIZE];
    };
    
    int key,shmid;
    int pid;
    struct my_buf *p;
    //注冊SIGUSR2到函數中
    signal(SIGUSR1,my_fun);
    key = ftok("./e.c",'a');
    if(key < 0)
    {
        perror("fotk");
        exit(1);
    }
    //創建共享內存
    shmid = shmget(key,BUFFER_SIZE,IPC_CREAT|S_IRWXU);
    if(shmid < 0)
    {
        perror("shmget");
        exit(1);
    }
    //映射共享內存
    p = (struct my_buf *)shmat(shmid,NULL,0);
    if(p == NULL)
    {
        perror("shmat");
        exit(1);
    }
    printf("map the share memory success\n");
    
     pid = p->pid;    //讀取服務器PID

    p->pid = getpid();    //將客戶端PID寫入共享內存中

    kill(pid,SIGUSR2);    //給服務器發信號

    while(1)
    {
        pause();
        printf("client process recave data form share memory:%s\n",p->buf);
        kill(pid,SIGUSR2);
    }
    
    shmdt(p);
    shmctl(shmid,IPC_RMID,NULL);
    
    return 0;
}

http://xxxxxx/Linuxjc/1164284.html TechArticle

Copyright © Linux教程網 All Rights Reserved