Guest User

Untitled

a guest
Apr 28th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <sys/stat.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <pwd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <sys/shm.h>
  14. #include <sys/sem.h>
  15. #include <sys/msg.h>
  16. #include <stdio.h>
  17. #include <sys/shm.h>
  18. #include <sys/sem.h>
  19.  
  20. #define SEGSIZE 100
  21.  
  22.  
  23.  
  24. int main ()
  25. {
  26. int id_ftok = ftok("1.c",129);
  27. int id_shmget = shmget(id_ftok, SEGSIZE, IPC_CREAT | 0660 );
  28. if (id_shmget == -1)
  29. perror("shmget");
  30.  
  31. char *attach_segment = shmat(id_shmget, 0, 0);
  32. int procpid = fork();
  33. if(procpid == -1) exit(EXIT_FAILURE);
  34. if(procpid == 0) {
  35. /*tu kod potomka*/
  36. printf("W pamieci wspoldzielonej znajduje sie: %s\n", attach_segment);
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. } else {
  46. /* tu kod rodzica*/
  47.  
  48. strcpy(attach_segment, "przykladowy tekst");
  49. if(wait(0)==-1)
  50. perror("wait");
  51.  
  52.  
  53. if ( shmdt(attach_segment) == -1)
  54. {
  55. perror("shmdt");
  56. exit(-1);
  57. }
  58.  
  59.  
  60. if ( shmctl(id_shmget, IPC_RMID, NULL) == -1 )
  61. {
  62. perror("shmctl");
  63. exit(-1);
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment