ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

共享内存

2022-04-17 08:31:06  阅读:140  来源: 互联网

标签:共享内存 addr mutex pthread shmid include shm


进程A:
 1 //processA.c文件
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #include <sys/shm.h>
 5 #include <unistd.h>
 6 #include <string.h>
 7 #include <sys/types.h>
 8 #include <sys/ipc.h>
 9 #include <errno.h>
10 #include <pthread.h>
11  
12 #define BUF_SIZE 4096
13  
14 int main()
15 {
16     void *shm_addr = NULL;
17     char buffer[BUF_SIZE];
18     pthread_mutex_t * sharedLock;
19     pthread_mutexattr_t ma;
20  
21     int shmid;
22     // 使用约定的键值创建共享内存
23     shmid = shmget((key_t) 1234,  BUF_SIZE, 0666 | IPC_CREAT);
24     printf("shmid : %u\n", shmid);
25     if (shmid < 0)
26     {
27         perror("shmget error!");
28         exit(1);
29     }
30  
31     // 将共享内存附加到本进程
32     shm_addr = shmat(shmid, NULL, 0);
33     if (shm_addr == (void *) -1)
34     {
35         perror("shmat error!");
36         exit(1);
37     }
38  
39     sharedLock = (pthread_mutex_t *)shm_addr;
40  
41     pthread_mutexattr_init(&ma);
42     pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED);
43     pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
44     pthread_mutex_init(sharedLock,&ma);
45  
46     // 写入数据
47     while(1){
48         pthread_mutex_lock(sharedLock);
49         bzero(buffer, BUF_SIZE);
50         sprintf(buffer, "Hello, My PID is %u\n", (unsigned int) getpid());
51         printf("send data: %s\n", buffer);
52         memcpy(((pthread_mutex_t *)shm_addr)+1, buffer, strlen(buffer));
53         pthread_mutex_unlock(sharedLock);
54     }
55  
56     sleep(5);
57  
58     // 分离
59     if (shmdt(shm_addr) == -1)
60     {
61         printf("shmdt error!\n");
62         exit(1);
63     }
64 }
View Code   进程B:
 1 //processB.c文件
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <sys/shm.h>
 5 #include <sys/ipc.h>
 6 #include <sys/types.h>
 7 #include <unistd.h>
 8 #include <string.h>
 9 #include <errno.h>
10 #include <pthread.h>
11  
12 #define BUF_SIZE 4096
13  
14 int main()
15 {
16     void *shm_addr = NULL;
17     pthread_mutex_t * sharedLock;
18     char tmp[BUF_SIZE];
19  
20     int shmid;
21     // 使用约定的键值打开共享内存
22     shmid = shmget((key_t) 1234, BUF_SIZE, IPC_CREAT);
23     printf("shmid : %u\n", shmid);
24     if (shmid == -1)
25     {
26         perror("shmget error!");
27         exit(1);
28     }
29  
30     // 将共享内存附加到本进程
31     shm_addr = shmat(shmid, NULL, 0);
32     if (shm_addr == (void *) -1)
33     {
34         perror("shmat error!");
35         exit(1);
36     }
37  
38     sharedLock = (pthread_mutex_t *)shm_addr;
39  
40     // 读取数据
41     while(1){
42         pthread_mutex_lock(sharedLock);
43         bzero(tmp, BUF_SIZE);
44         memcpy(tmp, ((pthread_mutex_t *)shm_addr)+1, 50);
45         printf("read from shared memory: %s\n", tmp);
46         pthread_mutex_unlock(sharedLock);
47     }
48  
49     sleep(5);
50  
51     // 分离
52     if (shmdt(shm_addr) == -1)
53     {
54         printf("shmdt error!\n");
55         exit(1);
56     }
57  
58     // 删除共享内存
59     if (shmctl(shmid, IPC_RMID, 0) == -1)
60     {
61         printf("shmctl error!\n");
62         exit(1);
63     }
64 }
View Code

 

标签:共享内存,addr,mutex,pthread,shmid,include,shm
来源: https://www.cnblogs.com/smartptr/p/16154952.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有