共享内存

2022/4/18 7:14:33

本文主要是介绍共享内存,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

进程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

 



这篇关于共享内存的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程