Advertisement
Pierciro_98

Untitled

Jul 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <pthread.h>
  5. #include <sys/types.h>
  6. #include <sys/ipc.h>
  7. #include <sys/sem.h>
  8. #include <sys/mman.h>
  9. #include <sys/sem.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/shm.h>
  15.  
  16. struct sembuf buf;
  17. char ** filepath;
  18. int fd;
  19. int sid;
  20. FILE * file;
  21. char _buf[4096];
  22. char * path;
  23. int fd;
  24.  
  25. char _handler[4096];
  26. void handler(int sig)
  27. {
  28. printf("\n");
  29. printf("Nel buffer \n");
  30. sprintf(_handler,"cat %s",filepath[1]);
  31. system(_handler);
  32. }
  33.  
  34.  
  35. void consuma()
  36. {
  37. printf("Figlio numero %d\n",getpid());
  38. int ok;
  39. sigset_t sig;
  40. struct sigaction s_a;
  41. sigfillset(&sig);
  42. s_a.sa_handler = handler;
  43. s_a.sa_mask = sig;
  44. sigaction(SIGINT,&s_a,NULL);
  45. fd = open(path,O_RDWR | O_CREAT | O_TRUNC,0666);
  46. file = fdopen(fd,"w");
  47. while(1)
  48. {
  49. printf("Attendo \n");
  50. _retry:
  51. buf.sem_num = 1;
  52. buf.sem_op = -1;
  53. buf.sem_flg = 0;
  54.  
  55. if((ok = semop(sid,&buf,1)) == -1 && errno == EINTR)
  56. {
  57. goto _retry;
  58. }
  59. fprintf(file,"%s",_buf);
  60. fflush(file);
  61. printf("Ok %d\n",errno);
  62. _retry2:
  63. buf.sem_num = 0;
  64. buf.sem_op = 1;
  65. buf.sem_flg = 0;
  66. if((ok = semop(sid,&buf,1)) == -1 && errno == EINTR)
  67. {
  68. goto _retry2;
  69. }
  70. }
  71. }
  72.  
  73.  
  74. int main(int argc,char ** argv)
  75. {
  76.  
  77. printf("Padre %d\n",getpid());
  78. sigset_t sig_set;
  79. struct sigaction sig_act;
  80. if(sigfillset(&sig_set) == -1)
  81. {
  82. printf("Errore sigfillset\n");
  83. exit(-1);
  84. }
  85. sig_act.sa_handler = handler;
  86. sig_act.sa_mask = sig_set;
  87. if(sigaction(SIGINT,&sig_act,NULL) == -1)
  88. {
  89. printf("Errore sigaction\n");
  90. exit(-1);
  91. }
  92. filepath = argv; // salvo il pathname dei file
  93.  
  94. path = filepath[1];
  95. fd = open(path,O_RDWR | O_CREAT | O_TRUNC,0666);
  96. file = fdopen(fd,"w");
  97.  
  98.  
  99. if((sid = semget(IPC_PRIVATE,2,IPC_CREAT | 0666)) == -1)
  100. {
  101. printf("semget error \n");
  102. exit(-1);
  103. }
  104. if(semctl(sid,0,SETVAL,1) == -1)
  105. {
  106. printf("semctl error\n");
  107. exit(-1);
  108. }
  109. if(semctl(sid,1,SETVAL,0) == -1)
  110. {
  111. printf("Errore semctl \n");
  112. exit(-1);
  113. }
  114.  
  115. pid_t pid;
  116. pid = fork();
  117. if(pid == 0) //figlio
  118. {
  119. consuma();
  120. }
  121.  
  122. int ret;
  123. while(1)
  124. {
  125. retry:
  126. buf.sem_num = 0;
  127. buf.sem_op = -1;
  128. buf.sem_flg = 0;
  129. if((ret = semop(sid,&buf,1)) == -1 && errno == EINTR)
  130. {
  131. goto retry;
  132. }
  133. printf("Immettere stringa \n");
  134. retry_4:
  135. if((ret = scanf("%s",_buf)) == EOF && errno == EINTR)
  136. {
  137. goto retry_4;
  138. }
  139. retry2:
  140. buf.sem_num = 1;
  141. buf.sem_op = 1;
  142. buf.sem_flg = 0;
  143. if((ret = semop(sid,&buf,1)) == -1 && errno == EINTR)
  144. {
  145. goto retry2;
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement