有名信号量在无相关进程间的同步

[复制链接]
619|0
 楼主| keer_zu 发表于 2020-3-27 09:59 | 显示全部楼层 |阅读模式
有名信号量在无相关进程间的同步

       有名信号量的特点是把信号量的值保存在文件中。这决定了它的用途非常广:既可以用于线程,也可以用于相关进程间,甚至是不相关进程。由于有名信号量的值是保存在文件中的,所以对于相关进程来说,子进程是继承了父进程的文件描述符,那么子进程所继承的文件描述符所指向的文件是和父进程一样的,当然文件里面保存的有名信号量值就共享了。

       有名信号量是位于共享内存区的,那么它要保护的资源也必须是位于共享内存区,只有这样才能被无相关的进程所共享。在下面这个例子中,服务进程和客户进程都使用shmget和shmat来获取得一块共享内存资源。然后利用有名信号量来对这块共享内存资源进行互斥保护。

server.c

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <semaphore.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>

  9. #define SHMSZ 27
  10. char SEM_NAME[]= "vik";

  11. int main()
  12. {
  13.     char ch;
  14.     int shmid;
  15.     key_t key;
  16.     char *shm,*s;
  17.     sem_t *mutex;

  18.     //name the shared memory segment
  19.     key = 1000;

  20.     //create & initialize semaphore
  21.     mutex = sem_open(SEM_NAME,O_CREAT,0644,1);
  22.     if(mutex == SEM_FAILED)
  23.     {
  24.       perror("unable to create semaphore");
  25.       sem_unlink(SEM_NAME);
  26.       exit(-1);
  27.     }

  28.     //create the shared memory segment with this key
  29.     shmid = shmget(key,SHMSZ,IPC_CREAT|0666);
  30.     if(shmid<0)
  31. {
  32.         perror("failure in shmget");
  33.         exit(-1);
  34. }

  35.     //attach this segment to virtual memory
  36.     shm = shmat(shmid,NULL,0);

  37.     //start writing into memory
  38.     s = shm;
  39.     for(ch='A';ch<='Z';ch++)
  40.     {
  41.         sem_wait(mutex);
  42.         *s++ = ch;
  43.         sem_post(mutex);
  44.      }

  45.     //the below loop could be replaced by binary semaphore
  46.     while(*shm != '*')
  47.     {
  48.         sleep(1);
  49. }
  50.     sem_close(mutex);
  51.     sem_unlink(SEM_NAME);
  52.     shmctl(shmid, IPC_RMID, 0);
  53.     exit(0);
  54. }





client.c
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <semaphore.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>

  9. #define SHMSZ 27
  10. char SEM_NAME[]= "vik";

  11. int main()
  12. {
  13.     char ch;
  14.     int shmid;
  15.     key_t key;
  16.     char *shm,*s;
  17.     sem_t *mutex;

  18.     //name the shared memory segment
  19.     key = 1000;

  20.     //create & initialize existing semaphore
  21.     mutex = sem_open(SEM_NAME,0,0644,0);
  22.     if(mutex == SEM_FAILED)
  23.     {
  24.         perror("reader:unable to execute semaphore");
  25.         sem_close(mutex);
  26.         exit(-1);
  27.     }

  28.     //create the shared memory segment with this key
  29.     shmid = shmget(key,SHMSZ,0666);
  30.     if(shmid<0)
  31.     {
  32.         perror("reader:failure in shmget");
  33.         exit(-1);
  34.     }

  35.     //attach this segment to virtual memory
  36.     shm = shmat(shmid,NULL,0);

  37.     //start reading
  38.     s = shm;
  39.     for(s=shm;*s!=NULL;s++)
  40.     {
  41.         sem_wait(mutex);
  42.         putchar(*s);
  43.       sem_post(mutex);
  44.     }

  45.   //once done signal exiting of reader:This can be replaced by another semaphore
  46.     *shm = '*';
  47.     sem_close(mutex);
  48.     shmctl(shmid, IPC_RMID, 0);
  49.     exit(0);
  50. }



您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12949

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部