Advertisement
Guest User

joniewiem

a guest
Oct 14th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. //zad1
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <process.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <sys/wait.h>
  10. #include <pthread.h>
  11. #include <unistd.h>
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. int pid1, pid2, stat1, stat2, i, p;
  16. int pid[100];
  17.  
  18. pid[1]=getpid();
  19. printf("Program stworzyl proces macierzysty (nr: 1) o PID: %d\n", pid[1]);
  20. for(i=2; i<argc && pid1!=0; i++)
  21. {
  22. pid1 = fork();
  23. if(pid1 == 0)
  24. {
  25. pid[i]=getpid();
  26. printf("Utworzylem proces nr: %d o PID: %d PPID: %d\n", i, pid[i], getppid());
  27. }
  28. }
  29.  
  30. sleep(1);
  31. if(pid1 != 0)printf("\n");
  32. sleep(1);
  33.  
  34. for(i=1; i<argc; i++)
  35. {
  36. // printf("TEST %d argc: %d\n", i, argc);
  37. for(p=1; p<= atoi(argv[i]) && pid[i]==getpid() ; p++)
  38. {
  39. //sleep(1);
  40. printf("PID = %d wykonay = %d PPID: %d\n", getpid(), p, getppid());
  41. sleep(1);
  42. if(atoi(argv[i])==p)
  43. {
  44. if(pid1 == 0)
  45. {
  46. printf("Zamykam proces PID = %d\n", getpid());
  47. }
  48. else
  49. {
  50. printf("Zamkniento proces macierzysty! o PID = %d\n", getpid());
  51. }
  52. exit(EXIT_SUCCESS);
  53. }
  54. }
  55. }
  56. return 0;
  57. }
  58.  
  59.  
  60.  
  61. /*
  62. exit(25);
  63. pid = wait(&status);
  64. int w = WEXITSTATUS(status);
  65. printf("Status zakonczenia procesu(%d): %d\n", pid, w);
  66. */
  67.  
  68.  
  69. //wywolanie funkcji
  70.  
  71. ./one 7 5 3 1
  72.  
  73. //zad2
  74.  
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #include <pthread.h>
  78.  
  79. void* function( void* arg )
  80. {
  81. while(1) {
  82. printf( "This is thread %d\n", pthread_self() );
  83. sleep(1);
  84. }
  85. pthread_exit( 0 );
  86. }
  87.  
  88. int main( void )
  89. {
  90. getchar();
  91. pthread_create( NULL, NULL, &function, NULL );
  92.  
  93. // while(1) {
  94. printf( "This is thread %d\n", pthread_self() );
  95. sleep(1);
  96. // }
  97. sleep( 5 );
  98. getchar();
  99. return EXIT_SUCCESS;
  100. }
  101.  
  102. //zad3
  103.  
  104. #include <stdio.h>
  105. #include <stdlib.h>
  106. #include <pthread.h>
  107.  
  108. void* function( void* arg )
  109. {
  110. printf( "This is thread %d\n", pthread_self() );
  111. sleep(1);
  112. sleep(40);
  113.  
  114. pthread_exit(0);
  115. }
  116.  
  117. int main( void )
  118. {
  119. pthread_t moj_watek;
  120.  
  121. // getchar();
  122. pthread_create( &moj_watek, NULL, &function, NULL );
  123.  
  124. printf( "This is thread %d\n", pthread_self() );
  125. sleep(1);
  126. // sleep(40);
  127. pthread_join(moj_watek,NULL);
  128.  
  129. // sleep( 5 );
  130. return EXIT_SUCCESS;
  131. }
  132.  
  133. //zad4
  134.  
  135. #include <stdio.h>
  136. #include <stdlib.h>
  137. #include <pthread.h>
  138.  
  139. void* function( void* arg )
  140. {
  141. int liczba;
  142. printf( "This is thread %d %d\n", pthread_self(), *((int*)arg) );
  143. liczba = *((int*)arg);
  144. liczba*=liczba;
  145. // sleep(3);
  146. pthread_exit((void*)liczba);
  147. }
  148.  
  149. int main( void )
  150. {
  151. int liczba;
  152. int wynik;
  153. pthread_t moj_watek;
  154. void* wynik_raw;
  155.  
  156. scanf("%d",&liczba);
  157. pthread_create( &moj_watek, NULL, &function, (void*)(&liczba) );
  158. printf( "This is thread %d\n", pthread_self() );
  159. // sleep(2);
  160.  
  161. pthread_join(moj_watek,&wynik_raw);
  162.  
  163. wynik = (int)wynik_raw;
  164. printf("%d\n",wynik);
  165. return EXIT_SUCCESS;
  166. }
  167.  
  168. //lab1
  169.  
  170. Kod:
  171. #include <stdio.h>
  172. #include <sys/types.h>
  173. #include <process.h>
  174.  
  175. int main( )
  176. {
  177. int potomny, i, nr_m, nr_p;
  178. potomny = fork();
  179. if(potomny==0) //potomny
  180. {
  181. for(i=0; i<10; i++)
  182. {
  183. nr_m=getpid();
  184. nr_p=getppid();
  185. printf("ID procesu macierzystego: %d \n", nr_m);
  186. printf("ID procesu potomnego: %d \n", nr_p);
  187. sleep(1);
  188. }
  189. }
  190. else //macierzysty
  191. {
  192. nr_m=getpid();
  193. printf("ID procesu macierzystego: %d \n", nr_m);
  194. getchar();
  195. }
  196. }
  197.  
  198.  
  199. Zad 2
  200. Kod:
  201. #include <stdio.h>
  202. #include <sys/types.h>
  203. #include <process.h>
  204. #include <sys/types.h>
  205. #include <sys/wait.h>
  206.  
  207. int main( )
  208. {
  209. int potomny, i, nr_m, nr_p, status, id;
  210. potomny = fork();
  211. if(potomny==0) //potomny
  212. {
  213. for(i=0; i<10; i++)
  214. {
  215. nr_m=getpid();
  216. nr_p=getppid();
  217. printf("ID procesu macierzystego: %d \n", nr_m);
  218. printf("ID procesu potomnego: %d \n", nr_p);
  219. sleep(1);
  220. }
  221. exit(1);
  222. }
  223. else //macierzysty
  224. {
  225. nr_m=getpid();
  226. printf("Czekam...\n");
  227. //sleep(10); //przypadek 2
  228. id = wait(&status);
  229. printf("ID procesu: %d \n", nr_m);
  230. }
  231. }
  232.  
  233.  
  234. Zad 3
  235. Kod:
  236. #include <stdio.h>
  237. #include <sys/types.h>
  238. #include <process.h>
  239. #include <sys/types.h>
  240. #include <sys/wait.h>
  241.  
  242. int main( )
  243. {
  244. int potomny, i, nr_m, nr_p, status, id;
  245. potomny = fork();
  246. if(potomny==0) //potomny
  247. {
  248. for(i=0; i<5; i++)
  249. {
  250. nr_m=getpid();
  251. nr_p=getppid();
  252. printf("ID procesu macierzystego: %d \n", nr_m);
  253. printf("ID procesu potomnego: %d \n", nr_p);
  254. sleep(1);
  255. }
  256. exit(25);
  257. }
  258. else //macierzysty
  259. {
  260. nr_m=getpid();
  261. printf("Czekam...\n");
  262. id = wait(&status);
  263. if(WIFEXITED(status)>0)
  264. {
  265. printf("Kod powrotu: %d \n", WEXITSTATUS(status));
  266. }
  267. else if(WIFSIGNALED(status)>0)
  268. {
  269. printf("Nr sygnalu: %d \n",WTERMSIG(status));
  270. }
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement