Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6.  
  7. pid_t *childs; //array for storing childs pids
  8. int number_of_childs;//variable for the number of childs
  9. int exit_count=0;
  10. int *fd;
  11.  
  12. /*Handler for the SIGINT signal*/
  13.  
  14. void control_handler(int sig)
  15. {
  16. if (sig==SIGINT){
  17.  
  18. int j,bytes;
  19. char message[512];
  20. /*Sending SIGUSR1*/
  21.  
  22. for (j=0;j<number_of_childs;j++)
  23. {
  24. kill(childs[j],SIGUSR1);
  25. }
  26.  
  27. /*Reading from PIPES*/
  28.  
  29. for (j=0;j<number_of_childs;j++)
  30. {
  31. close(fd[(2*j)+1]);
  32. bytes = read(fd[(2*j)], message, sizeof(message));
  33. printf("Read from: %sn",message);
  34. close(fd[2*j]);
  35. }
  36. }
  37.  
  38. if (sig==SIGUSR2)
  39. {
  40. exit_count++;
  41. }
  42.  
  43. }
  44.  
  45. main (int argc,char *argv[]){
  46.  
  47. int i,child_status;
  48. char cast[512];
  49. char cast2[512];
  50. char cast3[512];
  51. int pid;
  52. number_of_childs=atoi(argv[1]);
  53.  
  54. signal(SIGINT,control_handler);
  55. signal(SIGUSR2,control_handler);
  56.  
  57. /*Creating array for children pipes*/
  58. fd=(int*)malloc((2*number_of_childs)*sizeof(int));
  59.  
  60.  
  61. /*array that holds the pids for every child used in sending signals*/
  62. childs=malloc(number_of_childs*sizeof (pid_t));
  63.  
  64.  
  65.  
  66. for (i=0;i<number_of_childs;i++){
  67. pid=fork();
  68. /*Create pipes to communicate with all children*/
  69.  
  70. if(pipe(fd+(2*i))==-1)
  71. {
  72. perror("pipe");exit(1);
  73. }
  74.  
  75. /*Fathers code goes here*/
  76.  
  77. if(pid!=0)
  78. {
  79. printf("Parent process: PID= %d,PPID=%d, CPID=%d n",getpid(),getppid(),pid);
  80. childs[i]=pid; // Keep all your childs in an array
  81. printf("Child:%dn",childs[i]);
  82. }
  83.  
  84. /*If you are a child*/
  85.  
  86. else
  87. {
  88. /*Change the code for the childs and set the time of execution*/
  89. sprintf(cast,"%d",i+1); // make the time char
  90. sprintf(cast2,"%d",(2*i)+1); //make the pipe char
  91. sprintf(cast3,"%d",number_of_childs);
  92. execl("./Child.out","",cast,cast2,cast3,NULL);
  93. }
  94. }
  95. /*Father should never terminate*/
  96. while (exit_count!=number_of_childs);
  97. printf("Father pospastex!!n");
  98.  
  99. }
  100.  
  101. #include <stdio.h>
  102. #include <stdlib.h>
  103. #include <signal.h>
  104. #include <unistd.h>
  105. #include <sys/types.h>
  106. #include <time.h>
  107. #include <string.h>
  108. #define WRITE 0
  109. #define READ 1
  110.  
  111.  
  112. /*Global declerations*/
  113.  
  114. int alarmflag=0;
  115. double result=0;
  116. int count=0;
  117. int global_pipe;
  118. int *fd;
  119.  
  120. /*Handler for the alarm and SIGUSR1 signal*/
  121. void signal_handler (int sig)
  122. {
  123.  
  124. if(sig==SIGALRM)
  125. {
  126. printf("Im child with pid:%d im going to die my value is %lf n",getpid(),result);
  127. alarmflag=1;
  128. }
  129.  
  130. if(sig==SIGUSR1)
  131. {
  132. count++;
  133. char message[512];
  134.  
  135. if(count==1)
  136. {
  137. close(fd[global_pipe-1]);
  138. sprintf(message,"%d,%lf",getpid(),result);
  139. write(fd[global_pipe],message,strlen(message)+1);
  140. close(fd[global_pipe]);
  141. //printf("PID:%d report: %lfn",getpid(),result);
  142. }
  143. if(count==2)
  144. {
  145. close(fd[global_pipe-1]);
  146. sprintf(message,"%d,%lf",getpid(),result);
  147. write(fd[global_pipe],message,strlen(message)+1);
  148. close(fd[global_pipe]);
  149. //printf("PID:%d report2 : %lfn",getpid(),result);
  150. //kill(getppid(),SIGUSR2);
  151. //exit(0);
  152. }
  153. }
  154. if(sig==SIGINT)
  155. {
  156. /*Do nothing*/
  157. }
  158.  
  159. }
  160.  
  161.  
  162. double p_calculation ()
  163. {
  164. int i=2;
  165. result=3;
  166. double prosimo=-1;
  167.  
  168. while(!alarmflag)
  169. {
  170. prosimo=prosimo*(-1);
  171. result=result+(prosimo*(4/((double)i*((double)i+1)*((double)i+2))));
  172. i=i+2;
  173. }
  174.  
  175. }
  176.  
  177. main(int argc,char *argv[])
  178. {
  179. int pipe;
  180. int size_fd;
  181.  
  182. size_fd=(atoi(argv[3]));
  183. pipe=(atoi(argv[2]));
  184. global_pipe=pipe;
  185.  
  186. fd=(int*)malloc(size_fd*sizeof(int));
  187.  
  188. /*handling signals*/
  189. signal(SIGALRM,signal_handler);
  190. signal(SIGUSR1,signal_handler);
  191. signal(SIGINT,signal_handler);
  192.  
  193. /*Notify for execution time*/
  194. printf("PID : %d with PPID : %d executing for %d seconds n",getpid(),getppid(),atoi(argv[1]));
  195. //printf("pipe:%dn",pipe);
  196. /*end this after the value passed as argument*/
  197. alarm(atoi(argv[1]));
  198. p_calculation();
  199.  
  200. /*Notify for finish*/
  201.  
  202. printf("Done!!!n");
  203.  
  204. }
Add Comment
Please, Sign In to add comment