Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.32 KB | None | 0 0
  1. /////////////////////////  1
  2.  
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #include <sys/file.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <time.h>
  13.  
  14. void *funkcja1(void *a);
  15. void *funkcja2(void *a);
  16. void *funkcja3(void *a);
  17.  
  18. int liczby1[10], liczby2[10];
  19.  
  20.  
  21. pthread_t watek1, watek2, watek3;
  22.  
  23. int main(){
  24.     srand(time(NULL));
  25.  
  26.     pthread_create(&watek1, NULL, funkcja1, NULL);
  27.     pthread_create(&watek2, NULL, funkcja2, NULL);
  28.     pthread_create(&watek3, NULL, funkcja3, NULL);
  29.  
  30.    
  31.     printf("Watek 1 zaczyna losowac liczby\n");
  32.     pthread_join(watek1, NULL);
  33.  
  34.     printf("Watek 2 zaczyna losowac liczby\n");
  35.     pthread_join(watek2, NULL);
  36.  
  37.     pthread_join(watek3, NULL);
  38.  
  39.     return 0;
  40. }
  41.  
  42. void *funkcja1(void *a){
  43.     int i;
  44.     for(i = 0; i < 10; i++){
  45.         int a = rand()%100;
  46.         while(a%2!=0)
  47.         {
  48.             a = rand()%100;
  49.         }
  50.         liczby1[i] = a;
  51.         sleep(3);  
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
  57. void *funkcja2(void *a){
  58.     int i;
  59.     for(i = 0; i < 10; i++){
  60.         int a = rand()%100;
  61.         while(a%2==0)
  62.         {
  63.             a = rand()%100;
  64.         }
  65.         liczby2[i] = a;
  66.         sleep(2);  
  67.     }
  68.  
  69.     return 0;
  70. }
  71.  
  72. void *funkcja3(void *a){
  73.     sleep(2*10+3*10);
  74.     int i, suma = 0;
  75.     printf("Watek 1 wylosowal liczby: ");
  76.     for(i = 0; i < 10; i++) {
  77.         printf("%d ", liczby1[i]);
  78.         suma += liczby1[i];
  79.     }
  80.     printf("\n");
  81.     printf("Watek 2 wylosowal liczby: ");
  82.     for(i = 0; i < 10; i++) {
  83.         printf("%d ", liczby2[i]);
  84.         suma += liczby2[i];
  85.     }
  86.     printf("\n");
  87.  
  88.     printf("Suma liczb wynosi: %d\n", suma);
  89.     return 0;
  90. }
  91.  
  92. //////////////////  2
  93.  
  94. #include <sys/types.h>
  95. #include <sys/ipc.h>
  96. #include <sys/msg.h>
  97. #include <string.h>
  98. #include <sys/stat.h>
  99. #include <fcntl.h>
  100. #include <pthread.h>
  101. #include <string.h>
  102. #include <sys/file.h>
  103. #include <stdlib.h>
  104. #include <stdio.h>
  105. #include <unistd.h>
  106. #include <time.h>
  107.  
  108. void *glowny1(void *a);
  109. void *glowny2(void *a);
  110. void *poboczny1(void *a);
  111. void *poboczny2(void *a);
  112.  
  113. pthread_t g1, g2, p1, p2;
  114. int tok1, tok2, msg1, msg2;
  115.  
  116. struct msgbuf {
  117.     long mtype;
  118.     char komunikat[10];
  119. };
  120.  
  121. struct msgbuf bufor1, bufor2, bufor3, bufor4;
  122.  
  123. struct msqid_ds *buf;
  124.  
  125. void *glowny1(void *a){
  126.     int i;
  127.     for(i = 1; i <= 10; i++){
  128.         bufor1.mtype = 1;
  129.         char *m = "watek1";
  130.         strcpy(bufor1.komunikat, m);
  131.  
  132.         msgsnd(msg1, &bufor1, sizeof(bufor1.komunikat), IPC_NOWAIT);
  133.         sleep(3);
  134.     }
  135.    
  136.     return 0;
  137. }
  138.  
  139. void *glowny2(void *a){
  140.     int i;
  141.     for(i = 1; i <= 10; i++){
  142.         bufor2.mtype = 2;
  143.         char *m = "watek2";
  144.         strcpy(bufor2.komunikat, m);
  145.  
  146.         msgsnd(msg2, &bufor2, sizeof(bufor2.komunikat), IPC_NOWAIT);
  147.         sleep(2);
  148.     }
  149.    
  150.     return 0;
  151. }
  152.  
  153. void *poboczny1(void *a){
  154.     int i;
  155.     for(i = 1; i <= 10; i++){
  156.         msgrcv(msg1, &bufor3, sizeof(bufor3.komunikat), 1, IPC_NOWAIT);
  157.         printf("Komunikat odebrany przez poboczny 1: %s\n", bufor3.komunikat);
  158.         sleep(3);
  159.     }
  160.    
  161.     return 0;
  162. }
  163.  
  164. void *poboczny2(void *a){
  165.     int i;
  166.     for(i = 1; i <= 10; i++){
  167.         msgrcv(msg2, &bufor4, sizeof(bufor4.komunikat), 2, IPC_NOWAIT);
  168.         printf("Komunikat odebrany przez poboczny 2: %s\n", bufor4.komunikat);
  169.         sleep(2);
  170.     }
  171.    
  172.     return 0;
  173. }
  174.  
  175. int main(){
  176.     tok1 = ftok("watek1",120);
  177.     msg1 = msgget(tok1, IPC_CREAT|0660);
  178.  
  179.     tok2 = ftok("watek2",120);
  180.     msg2 = msgget(tok2, IPC_CREAT|0660);
  181.  
  182.     pthread_create(&g1, NULL, glowny1, NULL);
  183.     pthread_create(&g2, NULL, glowny2, NULL);
  184.     pthread_create(&p1, NULL, poboczny1, NULL);
  185.     pthread_create(&p2, NULL, poboczny2, NULL);
  186.  
  187.     pthread_join(g1, NULL);
  188.     pthread_join(g2, NULL);
  189.     pthread_join(p1, NULL);
  190.     pthread_join(p2, NULL);
  191.  
  192.     msgctl(msg1, IPC_RMID, buf);
  193.     msgctl(msg2, IPC_RMID, buf);
  194.     return 0;
  195. }
  196.  
  197. //////////////////////// 3
  198.  
  199.  
  200. #include <stdlib.h>
  201. #include <stdio.h>
  202. #include <sys/types.h>
  203. #include <sys/stat.h>
  204. #include <fcntl.h>
  205. #include <time.h>
  206. #include <pthread.h>
  207. #include <sched.h>
  208. #include <wait.h>
  209. #include <string.h>
  210. #include <sys/ipc.h>
  211. #include <sys/msg.h>
  212. #include <unistd.h>
  213.  
  214. void *funkcja1(void *a);
  215. void *funkcja2(void *a);
  216. void *funkcja3(void *a);
  217.  
  218. pthread_t watek1, watek2, watek3;
  219.  
  220. int fifo1, fifo2;
  221.  
  222. int main(){
  223.     srand(time(NULL));
  224.  
  225.     pthread_create(&watek1, NULL, funkcja1, NULL);
  226.     pthread_create(&watek2, NULL, funkcja2, NULL);
  227.     pthread_create(&watek3, NULL, funkcja3, NULL);
  228.  
  229.     fifo1 = mkfifo("programf1", 0600);
  230.     if(fifo1==-1)
  231.         perror("mkfifo1");
  232.  
  233.     fifo2 = mkfifo("programf2", 0600);
  234.     if(fifo2==-1)
  235.         perror("mkfifo2");
  236.  
  237.     pthread_join(watek1, NULL);
  238.  
  239.     sleep(1);
  240.  
  241.     pthread_join(watek2, NULL);
  242.  
  243.     pthread_join(watek3, NULL);
  244.  
  245.     unlink("programf1");
  246.     unlink("programf2");
  247.  
  248.     return 0;
  249. }
  250.  
  251. void *funkcja1(void *a){
  252.     sleep(1);
  253.     srand(time(NULL));
  254.     int liczba, i, lacze1, lacze2, w1, w2;
  255.     //char liczba2[10];
  256.     for(i = 1; i <= 20; i++){
  257.         liczba = rand() % (256-64) + 64;
  258.         //sprintf(liczba2, "%d", liczba);
  259.         lacze1 = open("programf1", O_WRONLY);
  260.         lacze2 = open("programf2", O_WRONLY);
  261.         if(write(lacze1, &liczba, sizeof(liczba)) == -1)
  262.             printf("Blad write1\n");
  263.         else w1 = 1;
  264.         if(write(lacze2, &liczba, sizeof(liczba)) == -1)
  265.             printf("Blad write2\n");
  266.         else w2 = 1;
  267.         if(w1 && w2) printf("%d. Wyslano %d do watkow\n", i, liczba);
  268.         close(lacze1);
  269.         close(lacze2);
  270.         sleep(3);
  271.     }
  272.  
  273.     return 0;
  274. }
  275.  
  276. void *funkcja2(void *a){
  277.     sleep(1);
  278.     int liczba;
  279.     int suma = 0, i, lacze;
  280.     //char liczba2[10];
  281.     for(i = 1; i <= 20; i++){
  282.         lacze = open("programf1", O_RDONLY);
  283.         if(read(lacze, &liczba, sizeof(liczba)) == -1)
  284.             printf("Blad read1\n");
  285.         suma += liczba;
  286.         close(lacze);
  287.         sleep(3);
  288.     }
  289.  
  290.     printf("Suma liczb: %d\n", suma);
  291.  
  292.     return 0;
  293. }
  294.  
  295. void *funkcja3(void *a){
  296.     sleep(1);
  297.     int liczba;
  298.     int suma = 1, i, lacze;
  299.     for(i = 1; i <= 20; i++){
  300.         lacze = open("programf2", O_RDONLY);
  301.         if(read(lacze, &liczba, sizeof(liczba)) == -1)
  302.             printf("Blad read2\n");
  303.         suma *= liczba;
  304.         close(lacze);
  305.         sleep(3);
  306.     }
  307.  
  308.     printf("Iloczyn liczb: %d\n", suma);
  309.  
  310.     return 0;
  311. }
  312.  
  313. ////////////////////// 4
  314.  
  315. #include <stdio.h>
  316. #include <stdlib.h>
  317. #include <pthread.h>
  318. #include <unistd.h>
  319. #include <sys/shm.h>
  320. #include <sys/types.h>
  321. #include <sys/ipc.h>
  322.  
  323. pthread_mutex_t xmut, xmut2, xmut3;
  324. pthread_cond_t zm_w, zm_w2, zm_w3;
  325.  
  326. void *watek_glowny(void *p)
  327. {
  328.     int i; int s = 0;
  329.     int il = 1; int d;
  330.    
  331.     int key = ftok("4.c",123);
  332.     int mem_idw = shmget(key,0,IPC_CREAT|0666);
  333.     int *buf;
  334.     buf = shmat(mem_idw,0,0);
  335.    
  336.         for (i = 0;i<5;i++)
  337.         {
  338.        
  339.             pthread_cond_wait(&zm_w,&xmut); //czkam na wateki pomocnicze
  340.            
  341.             d = buf[1];
  342.             s = s + d;
  343.             printf("Watek glowny: Odebrano liczbe od Watek_1: %d, suma: %d\n",d,s);
  344.  
  345.             d = buf[2];
  346.             il = il * d;
  347.             printf("Watek glowny: Odebrano liczbe od Watek_2: %d, iloczyn: %d\n",d,il);
  348.            
  349.             pthread_cond_signal(&zm_w2); // wysylam ze juz odebralem           
  350.         }          
  351.             pthread_mutex_unlock(&xmut);
  352.             pthread_mutex_unlock(&xmut2);
  353.             pthread_mutex_unlock(&xmut3);
  354.             printf("Watek glowny: Koniec\n");
  355.        
  356.         shmdt(buf);
  357.         return 0;
  358. }
  359. void *watek_pom(void *p)
  360. {
  361.     sleep(1);
  362.     int i;
  363.     int t[5];
  364.     for (i = 0; i<5; i++)
  365.     {
  366.         t[i] = (10 ? (rand() % 10 +1) : 1);
  367.     }
  368.     int key = ftok("4.c",123);
  369.     int mem_idw = shmget(key,0,IPC_CREAT|0666);
  370.     int *buf;
  371.     srand(123);
  372.     buf = shmat(mem_idw,0,0);
  373.        
  374.         for (i =0;i<5;i++)
  375.         {
  376.            
  377.            
  378.             buf[1] = t[i]; //rand() %(10 - 1) + 1;
  379.            
  380.             printf("Watek 1: Wyslano liczbe: %d\n", buf[1]);
  381.            
  382.             pthread_cond_signal(&zm_w3);    //wysyla do procesu 2 pomociniczego
  383.             pthread_cond_wait(&zm_w2,&xmut2);   //czeka na proces glowny           
  384.                    
  385.         }  
  386.     printf("Watek1: Koniec\n");
  387.    
  388.     shmdt(buf);
  389.     return 0;  
  390. }
  391.  
  392. void *watek_pom_2(void *p)
  393. {  
  394.     int i;
  395.     int t[5];
  396.     for (i = 0; i<5; i++)
  397.     {
  398.         t[i] = (10 ? (rand() % 10 +1) : 1);
  399.     }
  400.     int key = ftok("4.c",123);
  401.     int mem_idw = shmget(key,0,IPC_CREAT|0666);
  402.     int *buf;
  403.     buf = shmat(mem_idw,0,0);
  404.     srand(432);
  405.  
  406.         for (i =0;i<5;i++)
  407.         {
  408.             pthread_cond_wait(&zm_w3,&xmut3);       //czeka na proces 1 pomocniczy
  409.             buf[2] = t[i]; // rand() %(10 - 1) + 1;
  410.            
  411.             printf("Watek 2: Wyslano liczbe: %d\n", buf[2]);
  412.             pthread_cond_signal(&zm_w);         //wysyla do procesu glownego
  413.         }  
  414.     printf("Watek2: Koniec\n");
  415.    
  416.     shmdt(buf);
  417.     return 0;      
  418. }
  419.  
  420. int main()
  421. {
  422.     pthread_t w1, w2, w3;
  423.     pthread_cond_init(&zm_w,NULL);
  424.     pthread_cond_init(&zm_w2,NULL);
  425.     pthread_cond_init(&zm_w3,NULL);
  426.    
  427.     int key = ftok("4.c",123);
  428.     int mem_id = shmget(key,200,IPC_CREAT|0666);
  429.  
  430.     pthread_create(&w1,NULL,&watek_glowny,NULL);
  431.    
  432.     pthread_create(&w3,NULL,&watek_pom_2, NULL);
  433.     pthread_create(&w2,NULL,&watek_pom, NULL);
  434.    
  435.     pthread_join(w1,NULL);
  436.     pthread_join(w2,NULL);
  437.     pthread_join(w3,NULL);
  438.    
  439.     shmctl(mem_id,IPC_RMID,0);     
  440. }
  441.  
  442. /////////////////////////////// 5_1
  443.  
  444. #include <stdlib.h>
  445. #include <stdio.h>
  446. #include <sys/types.h>
  447. #include <sys/stat.h>
  448. #include <fcntl.h>
  449. #include <sys/ipc.h>
  450. #include <sys/unistd.h>
  451. #include <pthread.h>
  452.  
  453. void *funkcja1(void *a);
  454.  
  455. pthread_t watek1;
  456.  
  457. int fifo1;
  458.  
  459. int main(){
  460.     srand(time(NULL));
  461.  
  462.     pthread_create(&watek1, NULL, funkcja1, NULL);
  463.  
  464.     do {fifo1 = mkfifo("programf1", 0600);}
  465.     while(fifo1 != -1);
  466.  
  467.     pthread_join(watek1, NULL);
  468.  
  469.     return 0;
  470. }
  471.  
  472. void *funkcja1(void *a){
  473.     sleep(1);
  474.     int i, lacze;
  475.     char komunikat[20];
  476.  
  477.     printf("Wysylam komunkaty\n");
  478.     for(i = 1; i <=10; i++){
  479.         sprintf(komunikat, "komunikat %d", i);
  480.         lacze = open("programf1", O_WRONLY);
  481.         write(lacze, komunikat, 20);
  482.         close(lacze);
  483.         sleep(3);
  484.     }
  485.     printf("Wyslano komunkaty\n");
  486.  
  487.     return 0;
  488. }
  489.  
  490. //////////////////////////// 5_2
  491.  
  492.  
  493. #include <stdlib.h>
  494. #include <stdio.h>
  495. #include <sys/types.h>
  496. #include <sys/stat.h>
  497. #include <fcntl.h>
  498. #include <sys/ipc.h>
  499. #include <sys/unistd.h>
  500. #include <pthread.h>
  501.  
  502. void *funkcja1(void *a);
  503.  
  504. pthread_t watek1;
  505.  
  506. int fifo1;
  507.  
  508. int main(){
  509.     srand(time(NULL));
  510.  
  511.     pthread_create(&watek1, NULL, funkcja1, NULL);
  512.  
  513.     pthread_join(watek1, NULL);
  514.  
  515.     return 0;
  516. }
  517.  
  518. void *funkcja1(void *a){
  519.     sleep(1);
  520.     int i, lacze;
  521.     char komunikat[20];
  522.  
  523.     printf("Odbieram komunkaty\n");
  524.     for(i = 1; i <=10; i++){
  525.         lacze = open("programf1", O_RDONLY);
  526.         read(lacze, komunikat, 20);
  527.         close(lacze);
  528.         printf("Odebrano: %s\n", komunikat);
  529.         sleep(3);
  530.     }
  531.     printf("Odebrano komunkaty\n");
  532.  
  533.     unlink("programf1");
  534.  
  535.     return 0;
  536. }
  537.  
  538. //////////////////////////// 6
  539.  
  540.  
  541. #include <sys/types.h>
  542. #include <sys/ipc.h>
  543. #include <sys/msg.h>
  544. #include <string.h>
  545. #include <sys/stat.h>
  546. #include <fcntl.h>
  547. #include <pthread.h>
  548. #include <string.h>
  549. #include <sys/file.h>
  550. #include <stdlib.h>
  551. #include <stdio.h>
  552. #include <unistd.h>
  553. #include <time.h>
  554.  
  555. void *fglowna(void *a);
  556. void *fpoboczna1(void *a);
  557. void *fpoboczna2(void *a);
  558.  
  559. pthread_t watek1, watek2, watek3;
  560.  
  561. int tok1, msg1;
  562.  
  563. struct msgbuf {
  564.     long mtype;
  565.     int liczba;
  566. };
  567.  
  568. struct msgbuf bufor1, bufor2, bufor3, bufor4;
  569.  
  570. struct msqid_ds *buf;
  571.  
  572. int main(){
  573.     tok1 = ftok("watek1",120);
  574.     msg1 = msgget(tok1, IPC_CREAT|0660);
  575.  
  576.     pthread_create(&watek1, NULL, fglowna, NULL);
  577.     pthread_create(&watek2, NULL, fpoboczna1, NULL);
  578.     pthread_create(&watek3, NULL, fpoboczna2, NULL);
  579.    
  580.     pthread_join(watek1, NULL);
  581.     pthread_join(watek2, NULL);
  582.     pthread_join(watek3, NULL);
  583.    
  584.     msgctl(msg1, IPC_RMID, buf);
  585.  
  586.     return 0;
  587. }
  588.  
  589. void *fglowna(void *a){
  590.     srand(time(NULL));
  591.  
  592.     int liczba, i;
  593.  
  594.     for(i = 1; i <= 20; i++){
  595.         liczba = rand() % (256-128) + 128;
  596.  
  597.         bufor1.mtype = 1;
  598.         bufor1.liczba = liczba;
  599.         bufor2.mtype = 2;
  600.         bufor2.liczba = liczba;
  601.  
  602.         msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
  603.         msgsnd(msg1, &bufor2, sizeof(bufor2.liczba), IPC_NOWAIT);
  604.         printf("\n%d. Wyslano %d do watkow\n", i, liczba);
  605.  
  606.         sleep(3);
  607.     }
  608.  
  609.     return 0;
  610. }
  611.  
  612. void *fpoboczna1(void *a){
  613.     int suma = 0, i;
  614.  
  615.     for(i = 1; i <= 20; i++){
  616.         msgrcv(msg1, &bufor3, sizeof(bufor3.liczba), 1, 0);
  617.         suma += bufor3.liczba;
  618.         printf("%d. Poboczny1 odebral %d\n", i, bufor3.liczba);
  619.     }
  620.  
  621.     printf("\nSuma liczb: %d, srednia arytmetyczna: %.2f\n", suma, (double)suma / 20.0);
  622.  
  623.     return 0;
  624. }
  625.  
  626. void *fpoboczna2(void *a){
  627.     int i;
  628.     unsigned long long int iloczyn;
  629.     for(i = 1; i <= 20; i++){
  630.         msgrcv(msg1, &bufor4, sizeof(bufor4.liczba), 2, 0);
  631.         iloczyn *= bufor4.liczba;
  632.         printf("%d. Poboczny2 odebral %d\n", i, bufor4.liczba);
  633.     }
  634.  
  635.     printf("\nIloczyn liczb: %llu\n", iloczyn);
  636.  
  637.     return 0;
  638. }
  639.  
  640. //////////////////////////////// 7
  641.  
  642. #include <sys/types.h>
  643. #include <sys/ipc.h>
  644. #include <sys/msg.h>
  645. #include <string.h>
  646. #include <sys/stat.h>
  647. #include <fcntl.h>
  648. #include <pthread.h>
  649. #include <string.h>
  650. #include <sys/file.h>
  651. #include <stdlib.h>
  652. #include <stdio.h>
  653. #include <unistd.h>
  654. #include <time.h>
  655.  
  656. void *fglowna(void *a);
  657. void *fpoboczna1(void *a);
  658. void *fpoboczna2(void *a);
  659.  
  660. int parzyste[10] = {2,4,6,8,10,12,14,16,18,20};
  661. int nieparzyste[10] = {1,3,5,7,9,11,13,15,17,19};
  662. int wynik1, wynik2;
  663.  
  664. pthread_t watek1, watek2, watek3;
  665.  
  666. int tok1, msg1;
  667.  
  668. struct msgbuf {
  669.     long mtype;
  670.     char liczba[2];
  671. };
  672.  
  673. struct msgbuf bufor1, bufor2, bufor3;
  674.  
  675. struct msqid_ds *buf;
  676.  
  677. int main(){
  678.     tok1 = ftok("watek1",120);
  679.     msg1 = msgget(tok1, IPC_CREAT|0660);
  680.  
  681.     pthread_create(&watek1, NULL, fglowna, NULL);
  682.     pthread_create(&watek2, NULL, fpoboczna1, NULL);
  683.     pthread_create(&watek3, NULL, fpoboczna2, NULL);
  684.  
  685.     pthread_join(watek1, NULL);
  686.     pthread_join(watek2, NULL);
  687.     pthread_join(watek3, NULL);
  688.  
  689.     msgctl(msg1, IPC_RMID, buf);
  690.     return 0;
  691. }
  692.  
  693. void *fpoboczna1(void *a){
  694.     srand(time(NULL));
  695.     int i;
  696.     for(i = 0; i < 10; i++){
  697.         bufor1.mtype = 1;
  698.         sprintf(bufor1.liczba, "%d", parzyste[rand()%10]);
  699.  
  700.         msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
  701.  
  702.         printf("Poboczny 1 wysÅ‚aÅ‚ %s\n", bufor1.liczba);
  703.  
  704.         sleep(3);  
  705.     }
  706.  
  707.     return 0;
  708. }
  709.  
  710. void *fpoboczna2(void *a){
  711.     srand(time(NULL));
  712.     int i;
  713.     for(i = 0; i < 10; i++){
  714.         bufor2.mtype = 1;
  715.         sprintf(bufor2.liczba, "%d", nieparzyste[rand()%10]);
  716.  
  717.         msgsnd(msg1, &bufor2, sizeof(bufor2.liczba), IPC_NOWAIT);
  718.  
  719.         printf("Poboczny 2 wysÅ‚aÅ‚ %s\n", bufor2.liczba);
  720.  
  721.         sleep(2);  
  722.     }
  723.  
  724.     return 0;
  725. }
  726.  
  727. void *fglowna(void *a){
  728.     int i, suma = 0;
  729.    
  730.     for(i = 0; i < 20; i++){
  731.         msgrcv(msg1, &bufor3, sizeof(bufor3.liczba), 1, 0);
  732.  
  733.         printf("Glowny odebral %s\n", bufor3.liczba);
  734.  
  735.         suma += atoi(bufor3.liczba);
  736.     }
  737.  
  738.     printf("Suma liczb wynosi: %d\n", suma);
  739.     return 0;
  740. }
  741.  
  742. ///////////////////////////////////////// zad1
  743.  
  744. #include <sys/types.h>
  745. #include <sys/ipc.h>
  746. #include <sys/msg.h>
  747. #include <string.h>
  748. #include <sys/stat.h>
  749. #include <fcntl.h>
  750. #include <pthread.h>
  751. #include <string.h>
  752. #include <sys/file.h>
  753. #include <stdlib.h>
  754. #include <stdio.h>
  755. #include <unistd.h>
  756.  
  757.  
  758. void *funkcja1(void *a);
  759. void *funkcja2(void *a);
  760. int tok1, msg1;
  761.  
  762. pthread_t watek1, watek2;
  763.  
  764. struct msgbuf {
  765.     long mtype;
  766.     int liczba;
  767. };
  768.  
  769. struct msgbuf bufor1, bufor2;
  770.  
  771. struct msqid_ds *buf;
  772.  
  773. int globalna = 0;
  774.  
  775. void *funkcja1(void *a){
  776.     int liczba;
  777.     scanf("%d", &liczba);
  778.     bufor1.mtype = 1;
  779.         bufor1.liczba = liczba;
  780.         msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
  781.     globalna = 1;
  782.     while(liczba >= 0)
  783.     {
  784.         scanf("%d", &liczba);
  785.         bufor1.mtype = 1;
  786.         bufor1.liczba = liczba;
  787.         msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
  788.         globalna = 1;
  789.     }
  790.     return 0;
  791. }
  792.  
  793. void *funkcja2(void *a){
  794.     int liczba = 1;
  795.     while(liczba >= 0)
  796.     {
  797.         if(globalna ==1)
  798.         {
  799.             msgrcv(msg1, &bufor2, sizeof(bufor2.liczba), 1, IPC_NOWAIT);
  800.             liczba = bufor2.liczba;
  801.             if(liczba >=0)printf("\nWatek 2: %d\n", liczba);
  802.             globalna = 0;
  803.         }
  804.     }
  805.    
  806.     return 0;
  807. }
  808.  
  809. int main(){
  810.  
  811.     tok1 = ftok("watek1",120);
  812.     msg1 = msgget(tok1, IPC_CREAT|0660);
  813.     pthread_create(&watek1, NULL, funkcja1, NULL);
  814.     pthread_create(&watek2, NULL, funkcja2, NULL);
  815.  
  816.    
  817.     pthread_join(watek1, NULL);
  818.  
  819.     pthread_join(watek2, NULL);
  820.     msgctl(msg1, IPC_RMID, buf);
  821.  
  822.     return 0;
  823. }
  824.  
  825. ////////////////////////// zad 2_klient
  826.  
  827. #include <stdio.h>
  828. #include <unistd.h>
  829. #include <sys/socket.h>
  830. #include <sys/types.h>
  831. #include <netinet/ip.h>
  832. #include <arpa/inet.h>
  833. #include <errno.h>
  834.  
  835. #define PORT 1090
  836. #define ADRES "127.0.0.1"
  837.  
  838. void socket_name(int socket_descriptor)
  839. {
  840.     struct sockaddr_in server = {
  841.         .sin_family = AF_INET,
  842.         .sin_port = PORT,
  843.         .sin_addr = {INADDR_ANY}
  844.     };
  845.     if(bind(socket_descriptor, (struct sockaddr *)&server, sizeof(server))<0)
  846.         perror("bind");
  847. }
  848.  
  849. void get_message(int socket_descriptor)
  850. {
  851.     int liczba;
  852.     int suma = 0;
  853.     struct sockaddr_in client;
  854.     socklen_t adress_length = sizeof(client);
  855.     do
  856.     {
  857.         int recive = recvfrom(socket_descriptor, &liczba, sizeof(liczba), 0 , (struct sockaddr *)&client, &adress_length);
  858.         if(recive <0)
  859.             perror("recvfrom");
  860.         suma +=liczba;
  861.         printf("%d\n", liczba);
  862.        
  863.     }while(liczba>=0);
  864.     printf("Suma wynosi: %d", suma);
  865.    
  866. }
  867.  
  868. int main()
  869. {
  870.     int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  871.     if(socket_descriptor < 0)
  872.         perror("socket");
  873.     socket_name(socket_descriptor);
  874.     get_message(socket_descriptor);
  875.     if(close(socket_descriptor)<0)
  876.         perror("close");
  877.     return 0;
  878. }
  879.  
  880.  
  881. //////////////////////////// zad 2_serwer
  882.  
  883. #include <stdio.h>
  884. #include <unistd.h>
  885. #include <sys/socket.h>
  886. #include <sys/types.h>
  887. #include <netinet/ip.h>
  888. #include <arpa/inet.h>
  889. #include <errno.h>
  890.  
  891. #define PORT 1090
  892. #define ADRES "127.0.0.1"
  893.  
  894. void send_message(int socket_descriptor)
  895. {
  896.     struct in_addr network;
  897.     if(!inet_aton(ADRES, &network))
  898.         perror("inet_aton");
  899.     struct sockaddr_in server =
  900.         {
  901.             .sin_family = AF_INET,
  902.             .sin_port = PORT,
  903.             .sin_addr = network
  904.         };
  905.         int liczba;
  906.         do
  907.         {
  908.             scanf("%d", &liczba);
  909.             if(sendto(socket_descriptor, &liczba, sizeof(liczba), 0, (struct sockaddr *)&server, sizeof(server))<0)
  910.             {
  911.                 perror("sendto");
  912.             }
  913.         }while(liczba >=0);
  914.        
  915. }
  916.  
  917. int main()
  918. {
  919.     int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  920.     if(socket_descriptor < 0)
  921.         perror("socket");
  922.     send_message(socket_descriptor);
  923.     if(close(socket_descriptor)<0)
  924.         perror("close");
  925.     return 0;
  926. }
  927.  
  928.  
  929.  
  930. //////////////////////// zad3
  931.  
  932. #include <sys/types.h>
  933. #include <sys/ipc.h>
  934. #include <sys/msg.h>
  935. #include <string.h>
  936. #include <sys/stat.h>
  937. #include <fcntl.h>
  938. #include <pthread.h>
  939. #include <string.h>
  940. #include <sys/file.h>
  941. #include <stdlib.h>
  942. #include <stdio.h>
  943. #include <unistd.h>
  944. #include <stdio.h>
  945.  
  946.  
  947. void *funkcja1(void *a);
  948. void *funkcja2(void *a);
  949. int tok1, msg1;
  950.  
  951. pthread_t watek1, watek2;
  952.  
  953. struct msgbuf {
  954.     long mtype;
  955.     char liczba[21];
  956. };
  957.  
  958. struct msgbuf bufor1, bufor2;
  959.  
  960. struct msqid_ds *buf;
  961.  
  962. int globalna = 0;
  963.  
  964. void *funkcja1(void *a){
  965.     char liczba[21];
  966.     scanf("%s", liczba);
  967.     char koniec[] = "koniec";
  968.     bufor1.mtype = 1;
  969.     strcpy(bufor1.liczba, liczba);
  970.     msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
  971.     globalna = 1;
  972.     while(strncmp(liczba, koniec,5)!=0)
  973.     {
  974.         if(strncmp(liczba, koniec,5 )==0)
  975.         {
  976.             break;
  977.         }
  978.         scanf("%s", liczba);
  979.         bufor1.mtype = 1;
  980.         strcpy(bufor1.liczba, liczba);
  981.         msgsnd(msg1,&bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
  982.         globalna = 1;
  983.     }
  984.     return 0;
  985. }
  986.  
  987. void *funkcja2(void *a){
  988.     char liczba[21];
  989.     liczba[0] = 'm';
  990.     char koniec[] = "koniec";
  991.     while(strncmp(liczba, koniec,5)!=0)
  992.     {
  993.         if(globalna ==1)
  994.         {
  995.             if(strncmp(liczba, koniec,5 )==0)
  996.             {
  997.                 break;
  998.             }
  999.             msgrcv(msg1, &bufor2, sizeof(bufor2.liczba), 1, IPC_NOWAIT);
  1000.             strcpy(liczba, bufor2.liczba);
  1001.             if(strcmp(liczba, "koniec" ))printf("\nWatek 2: %s\n", liczba);
  1002.             globalna = 0;
  1003.         }
  1004.     }
  1005.    
  1006.     return 0;
  1007. }
  1008.  
  1009. int main(){
  1010.  
  1011.     tok1 = ftok("watek2",120);
  1012.     msg1 = msgget(tok1, IPC_CREAT|0660);
  1013.     pthread_create(&watek1, NULL, funkcja1, NULL);
  1014.     pthread_create(&watek2, NULL, funkcja2, NULL);
  1015.  
  1016.    
  1017.     pthread_join(watek1, NULL);
  1018.  
  1019.     pthread_join(watek2, NULL);
  1020.     msgctl(msg1, IPC_RMID, buf);
  1021.  
  1022.     return 0;
  1023. }
  1024.  
  1025.  
  1026. //////////////////////////////// zad4  klient
  1027.  
  1028. #include <stdio.h>
  1029. #include <sys/socket.h>
  1030. #include <stdlib.h>
  1031. #include <netinet/in.h>
  1032. #include <string.h>
  1033. #include <unistd.h>
  1034. #include <errno.h>
  1035. #include <arpa/inet.h>
  1036. #define PORT 8080
  1037.  
  1038. int main(int argc, char const *argv[])
  1039. {
  1040.     struct sockaddr_in address;
  1041.     int sock = 0;
  1042.     struct sockaddr_in serv_addr;
  1043.     char *hello = "Hello from client";
  1044.     char buffer[1024] = {0};
  1045.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  1046.     {
  1047.         printf("\n Socket creation error \n");
  1048.         return -1;
  1049.     }
  1050.  
  1051.     memset(&serv_addr, '0', sizeof(serv_addr));
  1052.  
  1053.     serv_addr.sin_family = AF_INET;
  1054.     serv_addr.sin_port = htons(PORT);
  1055.      
  1056.     // Convert IPv4 and IPv6 addresses from text to binary form
  1057.     if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
  1058.     {
  1059.         printf("\nInvalid address/ Address not supported \n");
  1060.         return -1;
  1061.     }
  1062.  
  1063.     if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  1064.     {
  1065.         printf("\nConnection Failed \n");
  1066.         return -1;
  1067.     }
  1068.     int liczba;
  1069.     do
  1070.     {
  1071.         scanf("%d", &liczba);
  1072.         send(sock, &liczba, sizeof(liczba),0);
  1073.     }while(liczba >=0);
  1074.    
  1075.     close(sock);
  1076.     return 0;
  1077. }
  1078.  
  1079.  
  1080.  
  1081. ////////////////////////// zad4 serwer
  1082.  
  1083. #include <stdio.h>
  1084. #include <sys/socket.h>
  1085. #include <stdlib.h>
  1086. #include <netinet/in.h>
  1087. #include <string.h>
  1088. #include <unistd.h>
  1089. #include <sys/types.h>
  1090. #include <netinet/ip.h>
  1091. #include <arpa/inet.h>
  1092. #include <errno.h>
  1093. #define PORT 8080
  1094. int main(int argc, char const *argv[])
  1095. {
  1096.     int server_fd, new_socket;
  1097.     struct sockaddr_in address;
  1098.     int opt = 1;
  1099.     int addrlen = sizeof(address);
  1100.     char buffer[1024] = {0};
  1101.     char *hello = "Hello from server";
  1102.      
  1103.     // Creating socket file descriptor
  1104.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  1105.     {
  1106.         perror("socket failed");
  1107.         exit(EXIT_FAILURE);
  1108.     }
  1109.      
  1110.     // Forcefully attaching socket to the port 8080
  1111.     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
  1112.                                                   &opt, sizeof(opt)))
  1113.     {
  1114.         perror("setsockopt");
  1115.         exit(EXIT_FAILURE);
  1116.     }
  1117.     address.sin_family = AF_INET;
  1118.     address.sin_addr.s_addr = INADDR_ANY;
  1119.     address.sin_port = htons( PORT );
  1120.      
  1121.     // Forcefully attaching socket to the port 8080
  1122.     if (bind(server_fd, (struct sockaddr *)&address,
  1123.                                  sizeof(address))<0)
  1124.     {
  1125.         perror("bind failed");
  1126.         exit(EXIT_FAILURE);
  1127.     }
  1128.     if (listen(server_fd, 3) < 0)
  1129.     {
  1130.         perror("listen");
  1131.         exit(EXIT_FAILURE);
  1132.     }
  1133.     if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
  1134.                        (socklen_t*)&addrlen))<0)
  1135.     {
  1136.         perror("accept");
  1137.         exit(EXIT_FAILURE);
  1138.     }
  1139.     int liczba=2;
  1140.     do
  1141.     {
  1142.         read(new_socket, &liczba, 1024);
  1143.         printf("%d\n", liczba);
  1144.     }while(liczba >=0);
  1145.     close(server_fd);
  1146.     return 0;
  1147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement