Advertisement
szilard-dobai

SO - Test Procese

Dec 8th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. labs.cs.upt.ro/~septimiu.dinulica/s7.html
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7.  
  8. int main (int argc, char* argv[]) {
  9. int pid;
  10. int status;
  11.  
  12. int N = atoi(argv[1]);
  13. int i;
  14.  
  15. if (argc < 3) {
  16. printf("wrong arguments\n");
  17. exit(-1);
  18. }
  19.  
  20. for(i=0; i<N; i++) {
  21. if ((pid = fork()) < 0) {
  22. printf("error forking\n");
  23. exit(-2);
  24. }
  25.  
  26. if (pid == 0) {
  27. printf("Eu sunt %d si am parinte pe %d. Am fost pornit al %d-lea.\n", getpid(), getppid(), i+1);
  28. exit(i);
  29. }
  30.  
  31. if (wait(&status) != -1) {
  32. printf("Parinte, terminat proces fiu %d cu valoarea de exit %d.\n", pid, WEXITSTATUS(status));
  33. }
  34. }
  35.  
  36. for(i=2; i < argc; i++) {
  37. if ((pid=fork()) < 0) {
  38. printf("error forking 2\n");
  39. exit(-3);
  40. } else if (pid == 0) {
  41. printf("Numar cuvinte fisier %s:\n", argv[i]);
  42. execlp("wc", "wc", "-w", argv[i], NULL);
  43. }
  44. wait(&status);
  45. }
  46. exit(5);
  47.  
  48.  
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. labs.cs.upt.ro/~septimiu.dinulica/s8.html
  57. #include <stdlib.h>
  58. #include <stdio.h>
  59. #include <sys/types.h>
  60. #include <sys/wait.h>
  61. #include <unistd.h>
  62. #include <signal.h>
  63.  
  64.  
  65. int pid_copil;
  66. int status;
  67. char litera;
  68.  
  69. void rutinaAlarma(int a) {
  70. alarm(2);
  71. if(kill(pid_copil, SIGUSR1)!=0) {
  72. printf("error SIGUSR1\n");
  73. }
  74. }
  75.  
  76. void rutinaUSR1(int a) {
  77. if(litera > 'c') {
  78. exit(1);
  79. } else {
  80. printf("%c \n", litera);
  81. litera++;
  82. }
  83. }
  84.  
  85. void rutinaSC (int a) {
  86. pid_copil = wait(&status);
  87. printf("child with pid %d ended with code %d\n", pid_copil, WEXITSTATUS(status));
  88. exit(0);
  89. }
  90.  
  91. int main (int argc, char* argv[]) {
  92. struct sigaction sigActAlarm;
  93. sigActAlarm.sa_handler = rutinaAlarma;
  94. sigActAlarm.sa_flags = 0;
  95.  
  96. struct sigaction sigActSC;
  97. sigActSC.sa_handler = rutinaSC;
  98. sigActSC.sa_flags = 0;
  99.  
  100. struct sigaction sigActUSR1;
  101. sigActUSR1.sa_handler = rutinaUSR1;
  102. sigActUSR1.sa_flags = 0;
  103.  
  104. if ((pid_copil = fork()) < 0) {
  105. printf("error forking\n");
  106. exit(-1);
  107. } else if (pid_copil == 0) {
  108. sigaction(SIGUSR1, &sigActUSR1, NULL);
  109. litera = 'a';
  110. while(1);
  111. } else {
  112. sigaction(SIGALRM, &sigActAlarm, NULL);
  113. sigaction(SIGCHLD, &sigActSC, NULL);
  114. /*
  115. if (raise (SIGALRM) == 0) {
  116. printf("succ raised alarm\n");
  117. } else {
  118. printf("err raising alarm\n");
  119. }
  120. */
  121. alarm(2);
  122. while(1);
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. labs.cs.upt.ro/~septimiu.dinulica/s9.html
  132. #include <stdlib.h>
  133. #include <stdio.h>
  134. #include <sys/types.h>
  135. #include <sys/wait.h>
  136. #include <unistd.h>
  137. #include <signal.h>
  138. #include <string.h>
  139. #include <fcntl.h>
  140.  
  141.  
  142. int pid_c1;
  143. int pid_c2;
  144. int pid_c;
  145. int status;
  146. char litera;
  147.  
  148.  
  149. int main (int argc, char* argv[]) {
  150. int stat_cifre[10] = {0,0,0,0,0,0,0,0,0,0};
  151. int pfd1[2], pfd2[2], pfd3[2];
  152. int fd = open(argv[1], O_RDONLY);
  153. char buff[1000];
  154. int buff_size;
  155. int i;
  156.  
  157. if (pipe(pfd1) < 0) {
  158. printf("error pipe 1\n");
  159. exit(-1);
  160. }
  161. if (pipe(pfd2) < 0) {
  162. printf("error pipe 2\n");
  163. exit(-2);
  164. }
  165. if (pipe(pfd3) < 0) {
  166. printf("error pipe 3\n");
  167. exit(-3);
  168. }
  169.  
  170. if((pid_c1 = fork()) < 0) {
  171. printf("error forking 1\n");
  172. exit(-4);
  173. } else if (pid_c1 == 0) {
  174. close(pfd1[1]); // 0 = citire, 1 = scriere
  175. close(pfd2[0]);
  176. close(pfd3[0]);
  177. close(pfd3[1]);
  178.  
  179. char buff_str[1];
  180.  
  181. while(buff_size = read(pfd1[0], buff, 1000)) {
  182. for(i=0; i<buff_size; i++) {
  183. if(buff[i] >= '0' && buff[i] <= '9') {
  184. //printf("char: %c\n", buff[i]);
  185. //sprintf(buff_str, "%c", buff[i]);
  186. buff_str[0] = buff[i];
  187. //printf("str: %s\n", buff_str);
  188. write(pfd2[1], buff_str, 1);
  189. }
  190. }
  191. }
  192.  
  193. close(pfd2[1]);
  194. exit(1);
  195. } else if ((pid_c2 = fork()) < 0) {
  196. printf("error forking 2\n");
  197. exit(-5);
  198. } else if (pid_c2 == 0) {
  199. close(pfd1[0]);
  200. close(pfd1[1]);
  201. close(pfd2[1]);
  202. close(pfd3[0]);
  203.  
  204. char c[10];
  205. int cifre_dist = 0;
  206. char cifre_str[100];
  207.  
  208. while(read(pfd2[0], c, 1)) {
  209. //printf("%s\n", c);
  210. stat_cifre[atoi(c)]++;
  211. }
  212.  
  213. FILE* out = fopen("output.txt", "w");
  214. for(i=0; i<10; i++) {
  215. sprintf(buff, "%d: %d\n", i, stat_cifre[i]);
  216. fprintf(out, "%s",buff);
  217. if(stat_cifre[i] != 0)
  218. cifre_dist++;
  219. }
  220. fclose(out);
  221.  
  222. sprintf(cifre_str, "%d", cifre_dist);
  223. write(pfd3[1], cifre_str, 100);
  224.  
  225. close(pfd3[1]);
  226. exit(2);
  227. } else {
  228. close(pfd1[0]);
  229. close(pfd2[0]);
  230. close(pfd2[1]);
  231. close(pfd3[1]);
  232.  
  233. while(buff_size = read(fd, buff, 1000)) {
  234. write(pfd1[1], buff, buff_size);
  235. }
  236. close(pfd1[1]);
  237.  
  238. while(read(pfd3[0], buff, 1000)) {
  239. printf("cifre distincte: %s\n", buff);
  240. }
  241. close(pfd3[0]);
  242.  
  243. for(i=0; i<2; i++) {
  244. pid_c = wait(&status);
  245. printf("child %d ended with %d\n", pid_c, WEXITSTATUS(status));
  246. }
  247.  
  248. }
  249.  
  250. }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. labs.cs.upt.ro/~septimiu.dinulica/s10.html
  259. #include <stdlib.h>
  260. #include <stdio.h>
  261. #include <sys/types.h>
  262. #include <sys/wait.h>
  263. #include <unistd.h>
  264. #include <signal.h>
  265. #include <string.h>
  266. #include <fcntl.h>
  267.  
  268.  
  269. int pid_c1;
  270. int pid_c2;
  271. int pid_c;
  272. int status;
  273. char litera;
  274.  
  275.  
  276. int main (int argc, char* argv[]) {
  277. int pfd1[2], pfd2[2], pfd3[2];
  278. FILE *stream;
  279. int fd;
  280. int numar_cuvinte = 0;
  281.  
  282. if (pipe(pfd1) < 0) {
  283. printf("error pipe 1\n");
  284. exit(-1);
  285. }
  286. if (pipe(pfd2) < 0) {
  287. printf("error pipe 2\n");
  288. exit(-2);
  289. }
  290. if (pipe(pfd3) < 0) {
  291. printf("error pipe 3\n");
  292. exit(-3);
  293. }
  294.  
  295. if((pid_c1 = fork()) < 0) {
  296. printf("error forking 1\n");
  297. exit(-4);
  298. } else if (pid_c1 == 0) {
  299. //close(pfd1[1]); // 0 = citire, 1 = scriere
  300. close(pfd2[0]);
  301. close(pfd3[0]);
  302. close(pfd3[1]);
  303.  
  304. dup2(pfd2[1],1);
  305. execlp("ps", "ps", "ax", NULL);
  306. printf("\n");
  307. close(pfd2[1]);
  308.  
  309. exit(1);
  310. } else if ((pid_c2 = fork()) < 0) {
  311. printf("error forking 2\n");
  312. exit(-5);
  313. } else if (pid_c2 == 0) {
  314. //close(pfd1[0]);
  315. //close(pfd1[1]);
  316. close(pfd2[1]);
  317. close(pfd3[0]);
  318.  
  319. dup2(pfd2[0], 0);
  320. dup2(pfd3[1], 1);
  321.  
  322. execlp("wc", "wc", "-w", NULL);
  323.  
  324. close(pfd3[1]);
  325. close(pfd2[0]);
  326. exit(2);
  327. } else {
  328. //close(pfd1[0]);
  329. close(pfd2[0]);
  330. close(pfd2[1]);
  331. //close(pfd3[1]);
  332.  
  333. stream = fdopen(pfd3[0], "r");
  334. fscanf(stream, "%d", &numar_cuvinte);
  335.  
  336. printf("numar cuvinte numarate: %d\n", numar_cuvinte);
  337.  
  338. close(pfd3[1]);
  339. close(pfd3[0]);
  340.  
  341. for(int i=0; i<2; i++) {
  342. pid_c = wait(&status);
  343. printf("child %d ended with status %d\n", pid_c, WEXITSTATUS(status));
  344. }
  345.  
  346. }
  347.  
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement