步骤1:同上

步骤2:建立目录

  cd workdir/linux/application/10-comm

  mkdir shmem

步骤3:复制

  cp /mnt/hgfs/share/2.Linux系统部分/16.Linux系统共享内存通信实验/实验代码/reader.c  ./  -a

  cp /mnt/hgfs/share/2.Linux系统部分/16.Linux系统共享内存通信实验/实验代码/writer.c  ./  -a

步骤4:执行

  gcc reader.c -o reader

  gcc writer.c -o writer

  ./reader

打开一个新终端

  cd workdir/linux/application/10-comm/shmem

  ./writer

附:程序源码

  reader.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/types.h>
 4 #include <sys/shm.h>
 5 #include <sys/ipc.h>
 6 #include <unistd.h>
 7 #include <signal.h>
 8 #include <errno.h>
 9 
10 #define N 64
11 
12 typedef struct
13 {
14      int pid;
15      char buf[N];
16 } shmbuf;
17 
18 void handler(int signo)
19 {
20      return;
21 }
22 
23 int main(int argc, char *argv[])
24 {
25    int shmid;
26    key_t key;
27    pid_t pid;
28    shmbuf *shmaddr;
29 
30    signal(SIGUSR1, handler);
31    if ((key = ftok(".", 'a')) < 0)
32    {
33       perror("fail to ftok");
34       exit(-1);
35    }
36    if((shmid = shmget(key, sizeof(shmbuf), IPC_CREAT|IPC_EXCL|0666)) < 0)
37    {
38       if (errno == EEXIST) 
39       {
40          shmid = shmget(key, sizeof(shmbuf), 0666);
41                  shmaddr = (shmbuf *)shmat(shmid, NULL, 0);
42                  pid = shmaddr->pid;
43                  shmaddr->pid = getpid();
44                  kill(pid, SIGUSR1);
45       }
46       else
47       {
48           perror("fail to shmget");
49           exit(-1);
50       }
51    }
52      else
53      {
54             shmaddr = (shmbuf *)shmat(shmid, NULL, 0);
55             shmaddr->pid = getpid();
56             pause();
57             pid = shmaddr->pid;
58      }
59 
60    while ( 1 )
61    {
62             pause();
63       if ( strncmp(shmaddr->buf, "quit", 4) == 0)
64       {
65          break;
66       };
67             printf("message from shm : %s", shmaddr->buf);
68             usleep(100000);
69       kill(pid, SIGUSR1);
70    }
71      shmdt(shmaddr);
72 
73    return 0;
74 }

  writer.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/types.h>
 4 #include <sys/shm.h>
 5 #include <sys/ipc.h>
 6 #include <unistd.h>
 7 #include <signal.h>
 8 #include <errno.h>
 9 
10 #define N 64
11 
12 typedef struct
13 {
14      int pid;
15      char buf[N];
16 } shmbuf;
17 
18 void handler(int signo)
19 {
20      return;
21 }
22 
23 int main(int argc, char *argv[])
24 {
25    int shmid;
26    key_t key;
27    pid_t pid;
28    shmbuf *shmaddr;
29 
30    signal(SIGUSR1, handler);
31    if ((key = ftok(".", 'a')) < 0)
32    {
33       perror("fail to ftok");
34       exit(-1);
35    }
36    if((shmid = shmget(key, sizeof(shmbuf), IPC_CREAT|IPC_EXCL|0666)) < 0)
37    {
38       if (errno == EEXIST) 
39       {
40          shmid = shmget(key, sizeof(shmbuf), 0666);
41                  shmaddr = (shmbuf *)shmat(shmid, NULL, 0);
42                  pid = shmaddr->pid;
43                  shmaddr->pid = getpid();
44                  kill(pid, SIGUSR1);
45       }
46       else
47       {
48           perror("fail to shmget");
49           exit(-1);
50       }
51    }
52      else
53      {
54             shmaddr = (shmbuf *)shmat(shmid, NULL, 0);
55             shmaddr->pid = getpid();
56             pause();
57             pid = shmaddr->pid;
58      }
59 
60    while ( 1 )
61    {
62       printf("please input : ");
63       fgets(shmaddr->buf, N, stdin);
64       kill(pid, SIGUSR1);
65       if (strncmp(shmaddr->buf, "quit", 4) == 0)
66       {
67          break;
68       }
69       pause();
70    }
71    sleep(1);
72    shmdt(shmaddr);
73    shmctl(shmid, IPC_RMID, NULL);
74 
75    return 0;
76 }

 

posted on 2018-08-03 09:06  kingofloong  阅读(861)  评论(0)    收藏  举报