Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1.  
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <stdio.h>
  8.  
  9. int main() {
  10.  
  11. int Len;
  12. int ShmId;
  13. int res;
  14. key_t Klucz=3120;
  15. //void ShowSMem(int);
  16. void * Addr;
  17. void * Napis;
  18.  
  19. ShmId=shmget(Klucz, 10000, IPC_CREAT | 0660);
  20.  
  21. if(ShmId<0){
  22. fprintf(stderr,"\nWystapil w trakcie kreowania segment pamieci wspolnej");
  23. exit(1);
  24. }
  25.  
  26. if(fork()==0) {
  27. Addr=shmat(ShmId,0 ,0);
  28. Napis=(char *)Addr;
  29. sprintf(Napis,"-----------Proces Potomny------------", setpid());
  30.  
  31. if(Addr<0){
  32. fprintf(stderr, "\nWystapil w trakcie dolaczania pamieci wspolnej");
  33. exit(1);
  34. }
  35. fprintf(stderr,"\nShared memory attached from %x to %x", Addr, Addr+1000);
  36. res=shmdt(Addr);
  37.  
  38. if(res<0){
  39. fprintf(stderr,"\nWystapil w trakcie odlaczania segmentu pamieci wspolnej");
  40. exit(1);
  41. }
  42.  
  43. sleep(10);
  44. printf("\nProces potomny zakonczony");
  45. exit(0);
  46. }
  47.  
  48. wait(0);
  49. Addr=shmat(ShmId,0,0);
  50. Napis=(char *)Addr;
  51. printf("\nKOMUNIKAT:%s",Napis);
  52.  
  53. if(Addr<0){
  54. fprintf(stderr,"\nWystapil w trakcie dolaczania amieci wspolnej");
  55. exit(1);
  56. }
  57. fprintf(stderr, "\nShared memory attached from %x to %x", Addr, Addr+1000);
  58. res=shmdt(Addr);
  59. if(res<0){
  60. fprintf(stderr, "\nWystapil w trakcie odlaczana segmentu pamieci wspolnej");
  61. exit(1);
  62. }
  63. //ShowSMem(ShmId);
  64. res=shmctl(ShmId, IPC_RMID,0);
  65. if(res<0) {
  66. fprintf(stderr,"\nWystapil w czasie usuwania pamieci wspolnej");
  67. exit(1);
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement