Advertisement
Guest User

Miaone

a guest
Jul 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <sys/shm.h>
  6.  
  7.  
  8. void produttore(key_t key){
  9. int memZone;
  10. if (memZone=shmget(key,128,IPC_CREAT|IPC_EXCL|0666)==-1){
  11. printf("Errore...");
  12. exit(0);
  13. }
  14. void *robe;
  15. if ((robe= shmat(memZone,NULL,0)) == (void *) -1) {
  16. printf("Errore del produttore. Impossibile agganciare la memoria condivisa.\n");
  17. shmctl(memZone, IPC_RMID, NULL);
  18. exit(1);
  19. }
  20. scanf("%[^\n]",(char *)robe);
  21. shmdt(robe);
  22. }
  23.  
  24.  
  25. void consumatore(key_t key){
  26. int memZone=shmget(key,128,0);
  27. void *robine=shmat(memZone,NULL,0);
  28. printf("|%s|\n",(char *)robine);
  29. shmctl(memZone, IPC_RMID, NULL);
  30. shmdt(robine);
  31. }
  32.  
  33.  
  34. int main(){
  35.  
  36. int status;
  37. key_t key = 1324;
  38. int son=fork();
  39.  
  40. if (son==0){
  41. produttore(key);
  42. exit(0);
  43. }
  44.  
  45. wait(&status);
  46. consumatore(key);
  47. return 1;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement