Advertisement
Ortodecimal

Untitled

Dec 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #define BUF 50
  10.  
  11. int fd[2];
  12. FILE* config_file;
  13. int father_pid;
  14. int semaphor = 1;
  15.  
  16. int count_of_args(char* value);
  17. int make_children(int num);
  18. int check_files(int num, int children, char** files);
  19. void SigWrite(int sig_num);
  20. void SigRead(int sig_num);
  21. void KillChild(int sig_num);
  22.  
  23. int main(int argc, char** argv)
  24. {
  25. int num_of_child;
  26. int i;
  27.  
  28. if (argc < 2)
  29. {
  30. printf("Not enough arguments\n");
  31. return -1;
  32. }
  33.  
  34. num_of_child = count_of_args(argv[1]);
  35. if (num_of_child < 2)
  36. {
  37. printf("I need more children!!\n");
  38. return -1;
  39. }
  40.  
  41. make_children(num_of_child);
  42.  
  43. return 0;
  44. }
  45.  
  46. int make_children(int num)
  47. {
  48. int i, j, child;
  49. pid_t pides[num - 1];
  50. pid_t pid;
  51. char conf_ch[10];
  52.  
  53. printf("Starting make children\n");
  54.  
  55. father_pid = getpid();
  56.  
  57. pipe(fd);
  58.  
  59. signal(SIGUSR1, SigWrite);
  60. signal(SIGUSR2, SigRead);
  61. signal(SIGTERM, KillChild);
  62.  
  63. for (i = 1; i < num; i++)
  64. {
  65. switch (pid = (fork()))
  66. {
  67. case -1:
  68. printf("Killing all children\n");
  69. for (j = 0; j < (i-1); j++)
  70. kill(pides[j], SIGTERM);
  71. printf("error fork\n");
  72. return -1;
  73. break;
  74. case 0:
  75. printf("son num %d is born\n", i);
  76. sprintf(conf_ch, "config_%d", i);
  77. printf("%s\n", conf_ch);
  78. config_file = fopen(conf_ch ,"r");
  79. if (config_file == NULL);
  80. {
  81. semaphor = 0;
  82. printf("The error is - %s\n", strerror(errno));
  83. printf("No file %d found\n", i);
  84. }
  85. kill(father_pid, SIGUSR1);
  86. printf("Signal %d was sent!\n", i);
  87. while(1);
  88. break;
  89. default:
  90. pides[i-1] = pid;
  91. while(semaphor == 1);
  92. semaphor = 1;
  93. signal(SIGUSR1, SigWrite);
  94. break;
  95. }
  96. }
  97.  
  98.  
  99. for (i = 1; i < num; i++)
  100. {
  101. printf("send signal1\n");
  102. kill(pides[i-1], SIGUSR1);
  103. while(semaphor == 1);
  104. read(fd[1], &child, sizeof(int));
  105. printf("Messag to son %d\n", child);
  106. if ((child > 0) && (child < num))
  107. {
  108. printf("send signal2\n");
  109. printf("son %d is reading\n", child);
  110. kill(pides[child-1], SIGUSR2);
  111. while(semaphor == 0);
  112. signal(SIGUSR1, SigWrite);
  113. signal(SIGUSR2, SigRead);
  114. }
  115. else if(child < (num - 1))
  116. printf("No son %d\n",child);
  117. printf("itteration %d done\n", i);
  118. }
  119.  
  120. printf("KILL CHILDREN!!\n");
  121. for (i = 0; i < (num-1); i++)
  122. kill(pides[i], SIGTERM);
  123.  
  124. close(fd[0]);
  125. close(fd[1]);
  126.  
  127. return 0;
  128. }
  129.  
  130. void SigWrite(int sig_num)
  131. {
  132. int child, i = 0;
  133. char c;
  134. char byte[BUF];
  135. int no_message = -100;
  136. if(getpid() != father_pid)
  137. {
  138. printf("Start WRITE!!!\n");
  139. if (semaphor == 1)
  140. {
  141. if(fscanf(config_file, "%d", &child) != 1)
  142. {
  143. printf("no message %d\n", no_message);
  144. write(fd[1], &no_message, sizeof(int));
  145. }
  146. else
  147. {
  148. c = fgetc(config_file);
  149. if (c == EOF)
  150. write(fd[1], &no_message, sizeof(int));
  151. else
  152. {
  153. write(fd[1], &child , sizeof(int));
  154. while (c != EOF)
  155. {
  156. byte[i] = c;
  157. if (i == BUF)
  158. {
  159. write(fd[1], byte, sizeof(char)*i);
  160. i = 0;
  161. }
  162. else i++;
  163. c = fgetc(config_file);
  164. }
  165. if (i != 0)
  166. write(fd[1], byte, strlen(byte)*sizeof(char));
  167. }
  168. }
  169. fclose(config_file);
  170. }
  171. else
  172. write(fd[1], &no_message, sizeof(int));
  173. kill(father_pid, SIGUSR1);
  174. }
  175. else
  176. semaphor = 0;
  177.  
  178. }
  179. void SigRead(int sig_num)
  180. {
  181. char byte[1024];
  182. if(getpid() != father_pid)
  183. {
  184. printf("START READ!!\n");
  185. read(fd[0], byte, 1024*sizeof(char));
  186. printf("%s\n", byte);
  187. kill(father_pid, SIGUSR2);
  188. }
  189. else
  190. semaphor = 1;
  191. }
  192.  
  193. void KillChild(int sig_num)
  194. {
  195. close(fd[0]);
  196. close(fd[1]);
  197. exit(0);
  198. }
  199.  
  200. int count_of_args(char* value)
  201. {
  202. int num = 0;
  203. while (*value != '\0')
  204. {
  205. if ((*value < '0') || (*value > '9'))
  206. return -1;
  207. num = num * 10 + *value - '0';
  208. value++;
  209. }
  210. return num;
  211. }
  212.  
  213. int check_files(int num, int children, char** files)
  214. {
  215. FILE* file;
  216. int i;
  217. int value;
  218. char c;
  219. for (i = 0; i < num; i++)
  220. {
  221. file = fopen(files[i + 2], "r");
  222. if (file == NULL)
  223. {
  224. fclose(file);
  225. return -1;
  226. }
  227. if(fscanf(file, "%d", &value) == 1)
  228. {
  229. if ((value > children) || (value < 1))
  230. {
  231. fclose(file);
  232. return -1;
  233. }
  234. }
  235. else if ((c = fgetc(file)) != EOF)
  236. {
  237. fclose(file);
  238. return -1;
  239. }
  240. fclose(file);
  241. }
  242. return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement