Linux 进程间通信(IPC)之三 --- 共享内存
2014-02-20 14:38
381 查看
场景:
两个进程,A进程创建共享内存并读取数据;B进程连接共享内存写入数据。输入 end 结束进程。
应用实例:
头文件:shm_com.h
消费者程序:shm_customer.h消费者程序:shm_customer.h消费者程序:shm_customer.h
生产者程序:shm_producer.c
生产者程序:shm_producer.c生产者程序:shm_producer.c生产者程序:shm_producer.c生产者程序:shm_producer.c生产者程序:shm_producer.c
两个进程,A进程创建共享内存并读取数据;B进程连接共享内存写入数据。输入 end 结束进程。
应用实例:
头文件:shm_com.h
01.#include <unistd.h> 02.#include <stdlib.h> 03.#include <stdio.h> 04.#include <sys/types.h> 05.#include <sys/ipc.h> 06.#include <sys/shm.h> 07. 08.#define TEXT_SZ 2048 09. 10.struct shared_use_st{ 11. int written_by_you; 12. char some_text[TEXT_SZ]; 13.};
消费者程序:shm_customer.h消费者程序:shm_customer.h消费者程序:shm_customer.h
01.#include "shm_com.h" 02. 03.int main(int argc,char **argv) 04.{ 05. int running = 1; 06. void *shared_memory = (void *)0; 07. struct shared_use_st *shared_stuff; 08. int shmid; 09. 10. srand((unsigned int)getpid()); 11. 12. shmid = shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT); 13. if(shmid == -1){ 14. fprintf(stderr,"shmget failed\n"); 15. exit(-1); 16. } 17. 18. shared_memory = shmat(shmid,(void *)0,0); 19. if(shared_memory == (void *)-1){ 20. fprintf(stderr,"shmat failed\n"); 21. exit(-1); 22. } 23. 24. shared_stuff = (struct shared_use_st *)shared_memory; 25. shared_stuff->written_by_you = 0; 26. while(running){ 27. if(shared_stuff->written_by_you){ 28. printf("You wrote: %s",shared_stuff->some_text); 29. sleep(rand()%4); 30. shared_stuff->written_by_you = 0; 31. if(strncmp(shared_stuff->some_text,"end",3) == 0){ 32. running = 0; 33. } 34. } 35. } 36. 37. if(shmdt(shared_memory) == -1){ 38. fprintf(stderr,"shmdt failed!\n"); 39. exit(-1); 40. } 41. 42. if(shmctl(shmid,IPC_RMID,0) == -1){ 43. fprintf(stderr,"shmctl(IPC_RMID) failed!\n"); 44. exit(-1); 45. } 46. 47. exit(0); 48.}
生产者程序:shm_producer.c
01.#include "shm_com.h" 02. 03.int main(int argc,char **argv) 04.{ 05. int running = 1; 06. void *shared_memory = (void *)0; 07. struct shared_use_st *shared_stuff; 08. char buffer[TEXT_SZ]; 09. int shmid; 10. 11. shmid = shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT); 12. if(shmid == -1){ 13. fprintf(stderr,"Shmget failed!\n"); 14. exit(-1); 15. } 16. 17. shared_memory = shmat(shmid,(void *)0,0); 18. if(shared_memory == (void *)-1){ 19. fprintf(stderr,"Shmat failed!\n"); 20. exit(-1); 21. } 22. 23. printf("Memory attached at %X\n",(int)shared_memory); 24. 25. shared_stuff = (struct shared_use_st *)shared_memory; 26. while(running){ 27. while(shared_stuff->written_by_you == 1){ 28. sleep(1); 29. printf("Waiting for client...\n"); 30. } 31. printf("Enter some text: "); 32. fgets(buffer,TEXT_SZ,stdin); 33. 34. strncpy(shared_stuff->some_text,buffer,TEXT_SZ); 35. shared_stuff->written_by_you = 1; 36. 37. if(strncmp(buffer,"end",3) == 0){ 38. running = 0; 39. } 40. } 41. 42. if(shmdt(shared_memory) == -1){ 43. fprintf(stderr,"Shmdt failed\n"); 44. exit(-1); 45. } 46. 47. exit(0); 48.}
生产者程序:shm_producer.c生产者程序:shm_producer.c生产者程序:shm_producer.c生产者程序:shm_producer.c生产者程序:shm_producer.c
相关文章推荐
- Linux进程间通信(IPC)编程实践(八)共享内存的使用-POSIX 共享内存(API)
- Linux 进程间通信 (IPC) //共享内存
- Linux进程间通信(IPC)编程实践(七)共享内存的使用-System V共享内存(API)
- linux 进程间通信(IPC)一共享内存
- 深入理解Linux进程间通信(IPC)-- 共享内存shared memory mmap
- 【Linux】进程间通信(IPC)之共享内存详解与测试用例
- linux进程间通信(IPC)--共享内存
- 唯快不破:linux进程间通信(IPC)--共享内存
- linux 进进程间通信(IPC) 之共享内存
- 深入理解Linux进程间通信(IPC)-- Posix共享内存
- Linux进程间通信(IPC)编程实践(六)共享内存的使用-mmap
- Linux进程间通信——IPC共享内存学习笔记
- Linux下网络编程(3)——进程间通信(IPC),共享内存,传递结构体数据
- Linux进程间通信源码剖析,共享内存(shmget()、shmat()、shmdt()及shmctl())
- 进程间通信IPC---共享内存
- Linux环境进程间通信系列(五):共享内存
- linux 进程间通信-----共享内存(上)
- Linux进程间通信(七)---共享内存之shmget()、shmat()、shmdt()及其基础实验
- Linux进程间通信源码剖析,共享内存(shmget()、shmat()、shmdt()及shmctl())
- linux下的多进程通信(IPC)原理及实现方案(管道、队列、信号量、共享内存)