http://blog.csdn.net/yanghaoran321/article/details/7872722

程序要求:

创建一个写端和一个读端,写端写入数据后读端才开始读,读端读完数据后,写端才可以开始写,这样的同步采用信号机制实现,并且写端与读端打开顺序不同也能实现功能;

程序如下:

(1)write.c(写端)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <errno.h>
  8. #include "sem.h"
  9. typedef struct
  10. {
  11. char buf[1024];
  12. }memory;
  13. int main(int argc, const char *argv[])
  14. {
  15. key_t key;
  16. memory *p = NULL;
  17. int shmid;
  18. int create_flag = 0;
  19. int sem_id;
  20. if ((key = ftok(".", 'a')) < 0)
  21. {
  22. perror("failed to get key");
  23. exit(-1);
  24. }
  25. if ((sem_id = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL)) < 0)
  26. {
  27. if (errno == EEXIST)
  28. {
  29. if ((sem_id = semget(key, 1, 0666)) < 0)
  30. {
  31. perror("failed to semget");
  32. exit(-1);
  33. }
  34. }
  35. }
  36. init_sem(sem_id, 0);
  37. if ((shmid = shmget(key, sizeof(memory), 0666 | IPC_CREAT | IPC_EXCL)) < 0)
  38. {
  39. if (errno == EEXIST)
  40. {
  41. if ((shmid = shmget(key, sizeof(memory), 0666)) < 0)
  42. {
  43. perror("failed to shmget memory");
  44. exit(-1);
  45. }
  46. }
  47. else
  48. {
  49. perror("failed to shmget");
  50. exit(-1);
  51. }
  52. }
  53. else
  54. create_flag = 1;
  55. if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))
  56. {
  57. perror("failed to shmat memory");
  58. exit(-1);
  59. }
  60. while(1)
  61. {
  62. printf(">");
  63. fgets(p->buf, sizeof(p->buf), stdin);
  64. p->buf[strlen(p->buf) - 1] = 0;
  65. sem_v(sem_id);
  66. if (strncmp(p->buf, "quit", 4) == 0)
  67. break;
  68. }
  69. if (create_flag == 1)
  70. {
  71. if (shmdt(p) < 0)
  72. {
  73. perror("failed to shmdt memory");
  74. exit(-1);
  75. }
  76. if (shmctl(shmid, IPC_RMID, NULL) == -1)
  77. {
  78. perror("failed to delete share memory");
  79. exit(-1);
  80. }
  81. delete_sem(sem_id);
  82. }
  83. return 0;
  84. }

(2)read.c(读端)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <errno.h>
  8. #include "sem.h"
  9. typedef struct
  10. {
  11. char buf[1024];
  12. }memory;
  13. int main(int argc, const char *argv[])
  14. {
  15. key_t key;
  16. int shmid;
  17. memory *p = NULL;
  18. int create_flag = 0;
  19. int sem_id;
  20. if ((key = ftok(".", 'a')) < 0)
  21. {
  22. perror("failed to get key");
  23. exit(-1);
  24. }
  25. if ((sem_id = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL)) < 0)
  26. {
  27. if (errno == EEXIST)
  28. {
  29. if ((sem_id = semget(key, 1, 0666)) < 0)
  30. {
  31. perror("failed to semget");
  32. exit(-1);
  33. }
  34. }
  35. }
  36. init_sem(sem_id, 0);
  37. if ((shmid = shmget(key, sizeof(memory), 0666 | IPC_CREAT | IPC_EXCL)) < 0)
  38. {
  39. if (errno == EEXIST)
  40. {
  41. if ((shmid = shmget(key, sizeof(memory), 0666)) < 0)
  42. {
  43. perror("failed to create share memory");
  44. exit(-1);
  45. }
  46. }
  47. else
  48. {
  49. perror("failed to shmget");
  50. exit(-1);
  51. }
  52. }
  53. else
  54. create_flag = 1;
  55. if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))
  56. {
  57. perror("failed to shmat");
  58. exit(-1);
  59. }
  60. while(1)
  61. {
  62. sem_p(sem_id);
  63. if (strncmp(p->buf, "quit", 4) == 0)
  64. break;
  65. printf("recv: %s\n", p->buf);
  66. }
  67. if (create_flag == 1)
  68. {
  69. if (shmdt(p) < 0)
  70. {
  71. perror("failed to shmdt");
  72. exit(-1);
  73. }
  74. if (shmctl(shmid, IPC_RMID, NULL) == -1)
  75. {
  76. perror("failed to delete share memory");
  77. exit(-1);
  78. }
  79. delete_sem(sem_id);
  80. }
  81. return 0;
  82. }

关于封装信号量函数的头文件:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/sem.h>
  6. #include <unistd.h>
  7. void init_sem(int , int );
  8. void delete_sem(int );
  9. void sem_p(int );
  10. void sem_v(int );
  11. union semun
  12. {
  13. int val;
  14. struct semid_ds *buf;
  15. unsigned short *array;
  16. };
  17. void init_sem(int sem_id, int init_value)
  18. {
  19. union semun sem_union;
  20. sem_union.val = init_value;
  21. if (semctl(sem_id, 0, SETVAL, sem_union) < 0)
  22. {
  23. perror("failed to init_sem");
  24. exit(-1);
  25. }
  26. return ;
  27. }
  28. void delete_sem(int sem_id)
  29. {
  30. union semun sem_union;
  31. if (semctl(sem_id, 0, IPC_RMID, sem_union) < 0)
  32. {
  33. perror("failed to delete_sem");
  34. exit(-1);
  35. }
  36. return ;
  37. }
  38. void sem_p(int sem_id)
  39. {
  40. struct sembuf sem_b;
  41. sem_b.sem_num = 0;
  42. sem_b.sem_op = -1;
  43. sem_b.sem_flg = SEM_UNDO;
  44. if (semop(sem_id, &sem_b, 1) < 0)
  45. {
  46. perror("failed to sem_p");
  47. exit(-1);
  48. }
  49. return;
  50. }
  51. void sem_v(int sem_id)
  52. {
  53. struct sembuf sem_b;
  54. sem_b.sem_num = 0;
  55. sem_b.sem_op = 1;
  56. sem_b.sem_flg = SEM_UNDO;
  57. if (semop(sem_id, &sem_b, 1) < 0)
  58. {
  59. perror("failed to sem_v");
  60. exit(-1);
  61. }
  62. return ;
  63. }

<转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)的更多相关文章

  1. Linux系统编程 —共享内存之mmap

    共享内存概念 共享内存是通信效率最高的IPC方式,因为进程可以直接读写内存,而无需进行数据的拷备.但是它没有自带同步机制,需要配合信号量等方式来进行同步. 共享内存被创建以后,同一块物理内存被映射到了 ...

  2. Linux环境进程间通信: 共享内存

    Linux环境进程间通信: 共享内存 第一部分 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进 ...

  3. Linux IPC之共享内存C 事例

    Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报  分类: Linux(3)  读书札记(3)  版权 ...

  4. Linux进程间通信—使用共享内存

    Linux进程间通信-使用共享内存 转自: https://blog.csdn.net/ljianhui/article/details/10253345 下面将讲解进程间通信的另一种方式,使用共享内 ...

  5. VC++ 共享内存读写操作

    此解决方案含两个工程文件,一个是写操作工程文件,即把任意字符串写入创建的共享内存里,另外一个读操作工程文件,则是读取共享内存里的数据,从而实现了进程之间的共享内存读写操作. 源码下载

  6. Linux 系统编程 学习:11-线程:线程同步

    Linux 系统编程 学习:11-线程:线程同步 背景 上一讲 我们介绍了线程的属性 有关设置.这一讲我们来看线程之间是如何同步的. 额外安装有关的man手册: sudo apt-get instal ...

  7. Linux IPC POSIX 共享内存

    模型 #include <unistd.h> //for fstat() #include <sys/types.h> //for fstat() #include <s ...

  8. linux进程间通信之共享内存篇

    本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...

  9. Linux下进程间通信--共享内存:最快的进程间通信方式

    共享内存: 一.概念: 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间. 进程A可以即时看到进程B ...

随机推荐

  1. 《DSP using MATLAB》示例 Example 9.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. Appium+python (3) 异常处理

    有时候定位时会发现无法定位到具体的元素,右侧元素定位处只告诉你这是一个网页视图: 点击里面的具体元素是无法选中的,船长的做法是回到App里点一下元素,再返回要定位的页面,重新点一下Device Scr ...

  3. nginx 获取请求头,URL参数

    获取url参数 在 ngx_lua 中访问 Nginx 内置变量 ngx.var.arg_PARAMETER 即可获得GET参数PARAMETER的内容. 在 nginx配置中,通过$arg_PARA ...

  4. anycast简单总结

    一针见血,言简意赅的总结 bgp+anycast就是不同服务器用了相同的ip地址 anycast 技术特点 bgp+anycast就是多个主机使用相同ip地址的一种技术,当报文发给该地址时,根据路由协 ...

  5. cockpit 使用(集成docker && k8s 管理)

    1. yum 安装 sudo yum install cockpit 2. 允许启动 sudo systemctl enable --now cockpit.socket 3. 可选的插件 cockp ...

  6. Cucumber 使用例子

    1. junit 配置 @RunWith(Cucumber.class) @CucumberOptions(format ={"pretty","html:target/ ...

  7. 学习 Git 玩转 GitHub

    原文地址:学习 Git 玩转 GitHub 博客地址:http://www.extlight.com 一.基本了解 1.1 什么是版本控制系统 版本控制系统是一种记录一个或若干个文件内容变化,以便将来 ...

  8. apache编译参数详解

    常用编译参数: ./configure     //配置源代码树–prefix=/usr/local/apache    //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录.–e ...

  9. 【备忘】mysql主从设置

    主(master)192.168.1.10机器设置: [root@vm-vagrant mysql]# vi my.cnf [mysqld]节点下添加以下配置server-id=1log-bin=my ...

  10. erlang的websocket例子

    创建工程 rebar-creator create-app websocket_demo 文件列表 route_helper.erl -module(route_helper). -export([g ...