ash_wath

Shared Memory

Sep 19th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. Creating a shared memory using Server Process:
  2.  
  3. #include<sys/shm.h>
  4. #include<sys/ipc.h>
  5. #include<stdio.h>
  6. #include<sys/types.h>
  7. #include<stdlib.h>
  8. #define SHMSZ 27
  9. main()
  10. {
  11. char c;
  12. int shmid;
  13. key_t key;
  14. char *shm,*s;
  15. key=1004;
  16. if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
  17. {
  18. perror("shmget");
  19. exit(1);
  20. }
  21. if((shm=shmat(shmid,NULL,0))==(char *)-1)
  22. {
  23. perror("shmat");
  24. exit(1);
  25. }
  26. s=shm;
  27. for(c='a';c<='z';c++)
  28. *s++=c;
  29. *s='\0';
  30. printf("Shared Memory Contents:%s\n",shm);
  31. while(*shm!='*')
  32. sleep(1);
  33. printf("Shared Memory Contents:%s\n",shm);
  34. exit(0);
  35. }
  36.  
  37.  
  38. Client Program:
  39.  
  40. #include<sys/shm.h>
  41. #include<sys/ipc.h>
  42. #include<stdio.h>
  43. #include<sys/types.h>
  44. #include<stdlib.h>
  45. #define SHMSZ 27
  46. main()
  47. {
  48. char c;
  49. int shmid;
  50. key_t; key
  51. char *shm,*s;
  52. key=1004;
  53. if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
  54. {
  55. perror("shmget");
  56. exit(1);
  57. }
  58. if((shm=shmat(shmid,NULL,0))==(char *)-1)
  59. {
  60. perror("shmat");
  61. exit(1);
  62. }
  63. for(s=shm;*s!='\0';s++)
  64. putchar('\n');
  65. *shm='*';
  66. exit(0);
  67. }
Add Comment
Please, Sign In to add comment