Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. Zad : Zaimplementować 4 procesy z których 3(PK1,PK2,PK3) pełni rolę konsumentów a 1 producenta(PP) który ma pobierać ciagi znaków z stdin i zapisywać je do pamięci współdzielonej
  2. ppkt 1: konsumenci ścigają się do zawartosci pamięci współdzielonej, tylko 1 ma odebrać ciąg znaków
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <semaphore.h>
  6. #include <sys/mman.h>
  7. #include <sys/wait.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include<fcntl.h>
  11.  
  12. pid_t wrap(void (*process_callback)(void))
  13. {
  14. pid_t pid = fork();
  15. switch(pid) {
  16. case -1:
  17. printf("Blad funkcji fork.");
  18. exit(1);
  19. case 0:
  20. process_callback();
  21. return 0;
  22. default:
  23. //ZWRACA PROCES ID
  24. return pid;
  25. }
  26. }
  27.  
  28. static char* komunikat;
  29. sem_t* semaphore;
  30. int* status_komunikatu;
  31.  
  32. void p1()
  33. {
  34.  
  35. while(1)
  36. {
  37.  
  38. sem_wait(semaphore);
  39.  
  40. if(*status_komunikatu)
  41. {
  42. printf("PK1, PID: %d, komunikat: %s\n", getpid(), komunikat);
  43. *status_komunikatu = 0;
  44. }
  45.  
  46. sem_post(semaphore);
  47. }
  48.  
  49. void p2()
  50. {
  51.  
  52. while(1)
  53. {
  54.  
  55. sem_wait(semaphore);
  56.  
  57. if(*status_komunikatu)
  58. {
  59. printf("PK2, PID: %d, komunikat: %s\n",getpid(),komunikat);
  60.  
  61. *status_komunikatu = 0;
  62.  
  63. }
  64.  
  65. sem_post(semaphore);
  66. }
  67. }
  68.  
  69. void p3()
  70. {
  71.  
  72. while(1)
  73. {
  74.  
  75. sem_wait(semaphore);
  76.  
  77. if(*status_komunikatu)
  78. {
  79.  
  80. printf("PK3, PID: %d, komunikat: %s\n",getpid(),komunikat);
  81.  
  82. *status_komunikatu = 0;
  83.  
  84. }
  85.  
  86. sem_post(semaphore); //odpowiada za inkrementacje
  87. }
  88. }
  89.  
  90. #define wspoldzielona_zmienna(name,type,num) name = (type*)mmap(NULL,sizeof(type)*num,PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1,0)
  91. int main(int argc, char const *argv[])
  92. {
  93.  
  94.  
  95.  
  96. wspoldzielona_zmienna(komunikat, char, 80);
  97. wspoldzielona_zmienna(status_komunikatu, int, 1);
  98. wspoldzielona_zmienna(semaphore, sem_t, 3);
  99.  
  100. sem_init(semaphore, 1, 1); /
  101.  
  102. *status_komunikatu = 0;
  103.  
  104. pid_t proces1 = wrap(p1);
  105. pid_t proces2 = wrap(p2);
  106. pid_t proces3 = wrap(p3);
  107.  
  108. int run = 1;
  109.  
  110. while(run)
  111. {
  112. scanf("%s",komunikat); // uzytkownik podaje z klawiatury
  113. if(strcmp(komunikat, "exit") == 0) run = 0; // jezeli string == 'exit' to wychodzimy z programu
  114. /*wypisuje komunikat*/
  115. printf("PP, PID: %d, komunikat: %s\n",getpid(),komunikat);
  116. *status_komunikatu = 1;
  117. }
  118.  
  119. kill(proces1, SIGTERM);
  120. kill(proces2, SIGTERM);
  121. kill(proces3, SIGTERM);
  122.  
  123. return 0;
  124. }
  125.  
  126. ppkt2: procesy potomne PK1 PK2 PK3 mają po kolei odbierać od procesu macierzystego PP komunikaty w kolejności PK1->PK2->PK3 (każdy proces odbiera 1 komunikat na raz tzn. komunikat1:PK1->komunikat2-:PK2->komunikat3:PK3->komunikat4:PK1 etc.
  127. #include <stdio.h>
  128. #include <stdlib.h>
  129. #include <sys/mman.h>
  130. #include <sys/types.h>
  131. #include <sys/shm.h>
  132. #include <errno.h>
  133. #include <semaphore.h>
  134. #include <string.h>
  135. #include <unistd.h>
  136. #include<sys/wait.h>
  137. #include<fcntl.h>
  138.  
  139. void* shmem;
  140. sem_t* sem1;
  141. sem_t* sem2;
  142. sem_t* sem3;
  143. sem_t* sem4;
  144.  
  145.  
  146. void* create_shared_memory(size_t size) {
  147. int protection = PROT_READ | PROT_WRITE;
  148. int visibility = MAP_ANONYMOUS | MAP_SHARED;
  149. return mmap(NULL, size, protection, visibility, 0, 0);
  150. }
  151.  
  152. pid_t wrap(void (*process_callback)(void))
  153. {
  154.  
  155. pid_t pid = fork();
  156. switch(pid) {
  157. case -1:
  158. printf("Blad funkcji fork.");
  159. exit(1);
  160. case 0:
  161. process_callback();
  162. return 0;
  163. default:
  164. return pid;
  165. }
  166. }
  167.  
  168. void p3()
  169. {
  170.  
  171. while(1)
  172. {
  173.  
  174. sem_wait(sem3);
  175.  
  176. char* x = shmem;
  177.  
  178. printf("PK3, PID: %d, komunikat: %s\n", getpid(), x);
  179.  
  180. sem_post(sem1);
  181. }
  182. }
  183.  
  184. void p2()
  185. {
  186.  
  187. while(1)
  188. {
  189.  
  190. sem_wait(sem4);
  191.  
  192. char* x = shmem;
  193.  
  194. printf("PK2, PID: %d, komunikat: %s\n", getpid(), x);
  195.  
  196. sem_post(sem1);//odpowiada za inkrementacje
  197. }
  198. }
  199.  
  200. void p1()
  201. {
  202.  
  203. while(1)
  204. {
  205.  
  206. sem_wait(sem2);
  207.  
  208. char* x = shmem;
  209.  
  210. printf("PK1, PID: %d, komunikat: %s\n", getpid(), x);
  211.  
  212. sem_post(sem1);
  213. }
  214. }
  215.  
  216. int main() {
  217.  
  218. char parent_message[100];
  219.  
  220. shmem = create_shared_memory(128);
  221.  
  222. char* name1 = "bartekk";
  223. char* name2 = "bartekkk";
  224. char* name3 = "bartekkkkk";
  225. char* name4 = "bartekkkkkk";
  226.  
  227. sem_unlink(name1);
  228. sem_unlink(name2);
  229. sem_unlink(name3);
  230. sem_unlink(name4);
  231.  
  232. sem1 = sem_open(name1, O_CREAT, 0777, 1);
  233. sem2 = sem_open(name2, O_CREAT, 0777, 0);
  234. sem3 = sem_open(name3, O_CREAT, 0777, 0);
  235. sem4 = sem_open(name4, O_CREAT, 0777, 0);
  236.  
  237. int count = 1;
  238. int run=0;
  239.  
  240. pid_t proces3 = wrap(p3);
  241. pid_t proces2 = wrap(p2);
  242. pid_t proces1 = wrap(p1);
  243.  
  244. while(run == 0)
  245. {
  246.  
  247. sem_wait(sem1);
  248.  
  249. scanf("%s",parent_message);
  250.  
  251. if(strcmp(parent_message, "exit") == 0) run = 1;
  252.  
  253. printf("PP, PID: %d, komunikat: %s\n", getpid(), parent_message);
  254.  
  255. memcpy(shmem, parent_message, sizeof(parent_message));
  256.  
  257. if(count == 1)sem_post(sem2);
  258.  
  259. if(count == 2)sem_post(sem4);
  260.  
  261. if(count == 3)
  262. {
  263. sem_post(sem3);
  264. count = 1;
  265. }
  266. else count += 1;
  267. }
  268.  
  269. kill(proces1, SIGTERM);
  270. kill(proces2, SIGTERM);
  271. kill(proces3, SIGTERM);
  272. sem_unlink(name1);
  273. sem_unlink(name2);
  274. sem_unlink(name3);
  275. sem_unlink(name4);
  276. return 0;
  277. }
  278.  
  279. ppkt3: procesy potomne PK1 PK2 PK3 mają odbierać od procesu macierzystego PP każdy komunikat w kolejności PK1->PK2->PK3
  280. #include <stdio.h>
  281. #include <stdlib.h>
  282. #include <sys/mman.h>
  283. #include <sys/types.h>
  284. #include <sys/shm.h>
  285. #include <errno.h>
  286. #include <semaphore.h>
  287. #include <string.h>
  288. #include <unistd.h>
  289. #include<sys/wait.h>
  290. #include<fcntl.h>
  291.  
  292. void* shmem;
  293. sem_t* sem1;
  294. sem_t* sem2;
  295. sem_t* sem3;
  296. sem_t* sem4;
  297. sem_t* sem5;
  298.  
  299.  
  300. void* create_shared_memory(size_t size) {
  301. int protection = PROT_READ | PROT_WRITE;
  302. int visibility = MAP_ANONYMOUS | MAP_SHARED;
  303. return mmap(NULL, size, protection, visibility, 0, 0);
  304. }
  305.  
  306. pid_t wrap(void (*process_callback)(void))
  307. {
  308.  
  309. pid_t pid = fork();
  310. switch(pid) {
  311. case -1:
  312. printf("Blad funkcji fork.");
  313. exit(1);
  314. case 0:
  315. process_callback();
  316. return 0;
  317. default:
  318. return pid;
  319. }
  320. }
  321.  
  322. void p3()
  323. {
  324.  
  325. while(1)
  326. {
  327.  
  328. sem_wait(sem5);
  329.  
  330. char* x = shmem;
  331.  
  332. printf("PK3, PID: %d, komunikat: %s\n", getpid(), x);
  333.  
  334. sem_post(sem1);
  335. }
  336. }
  337.  
  338. void p2()
  339. {
  340.  
  341. while(1)
  342. {
  343.  
  344. sem_wait(sem4);
  345.  
  346. char* x = shmem;
  347.  
  348. printf("PK2, PID: %d, komunikat: %s\n", getpid(), x);
  349.  
  350. sem_post(sem3);
  351. }
  352. }
  353.  
  354. void p1()
  355. {
  356.  
  357. while(1)
  358. {
  359.  
  360. sem_wait(sem2);
  361.  
  362. char* x = shmem;
  363.  
  364. printf("PK1, PID: %d, komunikat: %s\n", getpid(), x);
  365.  
  366. sem_post(sem3);
  367. }
  368. }
  369.  
  370. int main() {
  371.  
  372. char parent_message[100];
  373.  
  374. shmem = create_shared_memory(128);
  375.  
  376. char* name1 = "bartekk";
  377. char* name2 = "bartekkk";
  378. char* name3 = "bartekkkk";
  379. char* name4 = "bartekkkkk";
  380. char* name5 = "bartekkkkkk";
  381.  
  382. sem_unlink(name1);
  383. sem_unlink(name2);
  384. sem_unlink(name3);
  385. sem_unlink(name4);
  386. sem_unlink(name5);
  387.  
  388. sem1 = sem_open(name1, O_CREAT, 0777, 1);
  389. sem2 = sem_open(name2, O_CREAT, 0777, 0);
  390. sem3 = sem_open(name3, O_CREAT, 0777, 0);
  391. sem4 = sem_open(name4, O_CREAT, 0777, 0);
  392. sem5 = sem_open(name5, O_CREAT, 0777, 0);
  393.  
  394. int run=0;
  395.  
  396. pid_t proces3 = wrap(p3);
  397. pid_t proces2 = wrap(p2);
  398. pid_t proces1 = wrap(p1);
  399.  
  400.  
  401. while(run == 0)
  402. {
  403.  
  404. scanf("%s",parent_message);
  405.  
  406. if(strcmp(parent_message, "exit") == 0) run = 1;
  407.  
  408. printf("PP, PID: %d, komunikat: %s\n", getpid(), parent_message);
  409.  
  410. memcpy(shmem, parent_message, sizeof(parent_message));
  411.  
  412. sem_post(sem2);
  413.  
  414. sem_wait(sem3);
  415.  
  416. sem_post(sem4);
  417.  
  418. sem_wait(sem3);
  419.  
  420. sem_post(sem5);
  421. }
  422.  
  423. kill(proces1, SIGTERM);
  424. kill(proces2, SIGTERM);
  425. kill(proces3, SIGTERM);
  426. sem_unlink(name1);
  427. sem_unlink(name2);
  428. sem_unlink(name3);
  429. sem_unlink(name4);
  430. sem_unlink(name5);
  431. return 0;
  432. }
  433.  
  434. ppkt4: procesy potomne PK1 PK2 PK3 mają odbierać od procesu macierzystego PP po jednym komunikacie tak że PK1 ma dwa razy częściej odbierać komunikaty od PK2 który z kolei ma dwa razy częściej odebrać komunikaty niż PK3
  435.  
  436. #include <stdio.h>
  437. #include <stdlib.h>
  438. #include <sys/mman.h>
  439. #include <sys/types.h>
  440. #include <sys/shm.h>
  441. #include <errno.h>
  442. #include <semaphore.h>
  443. #include <string.h>
  444. #include <unistd.h>
  445. #include<sys/wait.h>
  446. #include<fcntl.h>
  447.  
  448. void* shmem;
  449. sem_t* sem1;
  450. sem_t* sem2;
  451. sem_t* sem3;
  452. sem_t* sem4;
  453.  
  454.  
  455. void* create_shared_memory(size_t size) {
  456. int protection = PROT_READ | PROT_WRITE;
  457. int visibility = MAP_ANONYMOUS | MAP_SHARED;
  458. return mmap(NULL, size, protection, visibility, 0, 0);
  459. }
  460.  
  461. pid_t wrap(void (*process_callback)(void))
  462. {
  463.  
  464. pid_t pid = fork();
  465. switch(pid) {
  466. case -1:
  467. printf("Blad funkcji fork.");
  468. exit(1);
  469. case 0:
  470. process_callback();
  471. return 0;
  472. default:
  473. return pid;
  474. }
  475. }
  476.  
  477. void p3()
  478. {
  479.  
  480. while(1)
  481. {
  482.  
  483. sem_wait(sem3);
  484.  
  485. char* x = shmem;
  486.  
  487. printf("PK3, PID: %d, komunikat: %s\n", getpid(), x);
  488.  
  489. sem_post(sem1);
  490. }
  491. }
  492.  
  493. void p2()
  494. {
  495.  
  496. while(1)
  497. {
  498.  
  499. sem_wait(sem4);
  500.  
  501. char* x = shmem;
  502.  
  503. printf("PK2, PID: %d, komunikat: %s\n", getpid(), x);
  504.  
  505. sem_post(sem1);//odpowiada za inkrementacje
  506. }
  507. }
  508.  
  509. void p1()
  510. {
  511.  
  512. while(1)
  513. {
  514.  
  515. sem_wait(sem2);
  516.  
  517. char* x = shmem;
  518.  
  519. printf("PK1, PID: %d, komunikat: %s\n", getpid(), x);
  520.  
  521. sem_post(sem1);
  522. }
  523. }
  524.  
  525. int main() {
  526.  
  527. char parent_message[100];
  528.  
  529. shmem = create_shared_memory(128);
  530.  
  531. char* name1 = "bartekk";
  532. char* name2 = "bartekkk";
  533. char* name3 = "bartekkkkk";
  534. char* name4 = "bartekkkkkk";
  535.  
  536. sem_unlink(name1);
  537. sem_unlink(name2);
  538. sem_unlink(name3);
  539. sem_unlink(name4);
  540.  
  541. sem1 = sem_open(name1, O_CREAT, 0777, 1);
  542. sem2 = sem_open(name2, O_CREAT, 0777, 0);
  543. sem3 = sem_open(name3, O_CREAT, 0777, 0);
  544. sem4 = sem_open(name4, O_CREAT, 0777, 0);
  545.  
  546. int count = 1
  547.  
  548. int run=0;
  549.  
  550. pid_t proces3 = wrap(p3);
  551. pid_t proces2 = wrap(p2);
  552. pid_t proces1 = wrap(p1);
  553.  
  554. while(run == 0)
  555. {
  556.  
  557. sem_wait(sem1);
  558.  
  559. scanf("%s",parent_message);
  560.  
  561. if(strcmp(parent_message, "exit") == 0) run = 1;
  562.  
  563. printf("PP, PID: %d, komunikat: %s\n", getpid(), parent_message);
  564.  
  565. memcpy(shmem, parent_message, sizeof(parent_message));
  566.  
  567. if(count == 1 || count == 2 || count == 4 || count == 5)sem_post(sem2);
  568.  
  569.  
  570. if(count == 3 || count == 6)sem_post(sem4);
  571.  
  572.  
  573. if(count == 7){ sem_post(sem3);count=1;}
  574.  
  575. else count +=1;
  576. }
  577.  
  578.  
  579. kill(proces1, SIGTERM);
  580. kill(proces2, SIGTERM);
  581. kill(proces3, SIGTERM);
  582. sem_unlink(name1);
  583. sem_unlink(name2);
  584. sem_unlink(name3);
  585. sem_unlink(name4);
  586. return 0;
  587. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement