Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. zad1 lab2
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7.  
  8. int main ()
  9. {
  10. pid_t potomek;
  11.  
  12. potomek = fork ();
  13. if (potomek != 0) {
  14. printf ("RODZIC: ID procesu biezacego: %d\n", getpid ());
  15. printf ("ID dziecka: %d\n", potomek);
  16. }
  17. else {
  18. printf ("DZIECKO: ID procesu biezacego: %d\n", getpid ());
  19. printf ("ID rodzica: %d\n", getppid ());
  20. }
  21.  
  22. return 0;
  23. }
  24.  
  25. zad2
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29.  
  30. int main ()
  31. {
  32. pid_t potomek;
  33.  
  34. //Tworzenie procesu potomnego
  35. potomek = fork ();
  36.  
  37. if (potomek > 0) {
  38. //To jest proces rodzica, ktory spi przez 60 sec
  39. sleep (10);
  40. }
  41. else {
  42. //To jest proces dziecka, ktory konczy sie natychmiast
  43. exit (0);
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. zad 3
  50. #include <stdio.h>
  51. #include <sys/types.h>
  52. #include <unistd.h>
  53. #include <sys/wait.h>
  54. #include <stdlib.h>
  55.  
  56. int main ()
  57. {
  58. int status;
  59. int potomek;
  60.  
  61. potomek = fork ();
  62. //proces macierzysty
  63. if (potomek != 0) {
  64. wait(&status);
  65. if (WIFEXITED(status) > 0) {
  66. printf("status: %d\n", WEXITSTATUS(status));
  67. }
  68. else
  69. printf("Cos jest nie tak");
  70. }
  71. //proces potomny
  72. else {
  73. printf("proces potomny...\n");
  74. exit(0);
  75. }
  76.  
  77. return 0;
  78. }
  79.  
  80.  
  81.  
  82. zad4
  83. #include <stdio.h>
  84. #include <sys/types.h>
  85. #include <unistd.h>
  86. #include <sys/wait.h>
  87. #include <stdlib.h>
  88.  
  89. void dziecko(int a);
  90.  
  91. int main (int argc, char *argv[])
  92. {
  93. int status;
  94. int pid;
  95. pid_t potomek;
  96. int i, j;
  97.  
  98. for(i = 1; i < atoi(argv[1])+1; i++) {
  99. potomek = fork ();
  100. if (potomek == 0) {
  101. pid = getpid();
  102. for(j = 0; j < 4; j++)
  103. printf("Potomek: %d PID: %d PPID: %d\n", i, getpid(), getppid());
  104. sleep(i);
  105. exit(0);
  106. }
  107. }
  108.  
  109. while ((pid = wait(&status)) > 0)
  110. printf("\nPID: %d, status: %d\n", pid, status);
  111.  
  112. return 0;
  113. }
  114.  
  115. void dziecko(int a)
  116. {
  117.  
  118. }
  119.  
  120. zad5
  121. #include <stdio.h>
  122. #include <unistd.h>
  123. #include <stdlib.h>
  124. #include <sys/wait.h>
  125.  
  126. int main ()
  127. {
  128. int status;
  129. int potomek;
  130.  
  131. potomek = fork ();
  132. if (potomek == 0) {
  133. execlp("./zad5a", "zad5a", 0);
  134. }
  135. wait(&status);
  136.  
  137. return 0;
  138. }
  139.  
  140.  
  141.  
  142. zad 6
  143.  
  144. #include <stdio.h>
  145. #include <unistd.h>
  146. #include <sys/types.h>
  147. #include <signal.h>
  148.  
  149. void obsluz(int sygnal)
  150. {
  151. printf("------------------------\n");
  152. printf("Otrzymano sygnal nr: %d\n", sygnal);
  153. printf("------------------------\n");
  154. }
  155.  
  156. int main(int argc, char **argv)
  157. {
  158. signal(SIGALRM, obsluz);
  159. int pid = getpid();
  160. kill(pid, SIGALRM);
  161. printf("Koniec programu.\n");
  162. return 0;
  163. }
  164.  
  165.  
  166. zad 7
  167.  
  168. #include <stdio.h>
  169. #include <unistd.h>
  170. #include <sys/types.h>
  171. #include <signal.h>
  172. #include <sys/wait.h>
  173.  
  174. void obsluz(int sygnal)
  175. {
  176. printf("------------------------\n");
  177. printf("Otrzymano sygnal nr: %d\n", sygnal);
  178. printf("------------------------\n");
  179. }
  180.  
  181. int main(int argc, char **argv)
  182. {
  183. int potomek;
  184. int status;
  185. signal(SIGINT, obsluz);
  186.  
  187. potomek = fork();
  188. if (potomek == 0) {
  189. kill(getppid(), SIGINT);
  190. }
  191. else {
  192. wait(&status);
  193. printf("Koniec programu.\n");
  194. }
  195. return 0;
  196. }
  197.  
  198.  
  199. zad 8
  200.  
  201. #include <stdio.h>
  202. #include <unistd.h>
  203. #include <sys/types.h>
  204. #include <signal.h>
  205. #include <sys/wait.h>
  206.  
  207. void obsluz(int sygnal)
  208. {
  209. printf("Otrzymano sygnal nr: %d\n", sygnal);
  210. }
  211.  
  212. int main(int argc, char **argv)
  213. {
  214. int p1, p2, p3, p4;
  215. int a = 4, b = 4, c = 4, d = 4;
  216. int status;
  217.  
  218. p1 = fork();
  219. p2 = fork();
  220.  
  221. if(p1 == 0) {
  222. signal(SIGINT, obsluz);
  223. while(1) {
  224. printf("proces 1...\n");
  225. pause();
  226. sleep(1);
  227. kill(p2, SIGALRM);
  228. a--;
  229. }
  230. }
  231.  
  232. if(p2 == 0) {
  233. signal(SIGALRM, obsluz);
  234. while(1) {
  235. printf("proces 2...\n");
  236. pause();
  237. sleep(1);
  238. kill(p1, SIGINT);
  239. b--;
  240. }
  241. }
  242. wait(&status);
  243. return 0;
  244. }
  245.  
  246.  
  247. zad 9
  248.  
  249. #include <stdio.h>
  250. #include <sys/types.h>
  251. #include <unistd.h>
  252. #include <sys/wait.h>
  253. #include <stdlib.h>
  254.  
  255. int main(int argc, char **argv)
  256. {
  257. int potomek;
  258. int status;
  259. int dana = 5;
  260.  
  261. potomek = fork();
  262. if (potomek == 0) {
  263. printf("Potomek: %d\n", dana);
  264. printf("Potomek: %d\n", dana += 5);
  265. }
  266. else {
  267. wait(&status);
  268. printf("Rodzic: %d\n", dana);
  269. printf("Rodzic: %d\n", dana += 5);
  270. }
  271. return 0;
  272. }
  273.  
  274.  
  275. zad 10
  276. #include <stdio.h>
  277. #include <unistd.h>
  278. #include <sys/types.h>
  279. #include <signal.h>
  280. #include <sys/wait.h>
  281.  
  282. void obsluz(int sygnal)
  283. {
  284. printf("------------------------\n");
  285. printf("Funkcja obslugujaca sygnal SIGINT\n");
  286. printf("Otrzymano sygnal nr: %d czyli SIGINT\n", sygnal);
  287. printf("------------------------\n");
  288. }
  289.  
  290. void skomplikowana_funkcja()
  291. {
  292. printf("UWAGA !!! Skomplikowana operacja.\n");
  293. }
  294.  
  295. int main(int argc, char **argv)
  296. {
  297. int potomek;
  298. int status;
  299. signal(SIGINT, obsluz);
  300.  
  301. potomek = fork();
  302. if (potomek == 0) {
  303. kill(getppid(), SIGINT);
  304. }
  305. else {
  306. wait(&status);
  307. skomplikowana_funkcja();
  308. printf("Koniec programu.\n");
  309. }
  310. return 0;
  311. }
  312.  
  313.  
  314.  
  315. zad 11
  316.  
  317. #include <stdio.h>
  318. #include <unistd.h>
  319. #include <sys/types.h>
  320. #include <signal.h>
  321. #include <sys/wait.h>
  322.  
  323. int main(int argc, char **argv)
  324. {
  325. int potomek;
  326. int status;
  327. sigblock(SIGINT);
  328.  
  329. potomek = fork();
  330. if (potomek == 0) {
  331. kill(getppid(), SIGINT);
  332. }
  333. else {
  334. wait(&status);
  335. printf("Koniec programu.\n");
  336. }
  337. return 0;
  338. }
  339.  
  340.  
  341. zad 5b
  342.  
  343. #include <stdio.h>
  344.  
  345. int main(int argc, char **argv)
  346. {
  347. printf("\nAlbert lubi lowic ryby\n\n");
  348. return 0;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement