Advertisement
Guest User

SIAGA

a guest
Jan 29th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.87 KB | None | 0 0
  1. Stwórz w programie dwa wątki, które wypiszą swój identyfikator i identyfikator procesu
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <pthread.h>
  6. #include <string.h>
  7. #include <sys/file.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12.  
  13.  
  14. void *fun_watek1(void * p);
  15. void *fun_watek2(void * p);
  16.  
  17. int main() {
  18. pthread_t watek1;
  19. pthread_t watek2;
  20.  
  21. pthread_create(&watek1, NULL, fun_watek1, (void *)&watek1);
  22. pthread_create(&watek2, NULL, fun_watek2, (void *)&watek2);
  23.  
  24. pthread_join(watek1, NULL);
  25. pthread_join(watek2, NULL);
  26.  
  27.  
  28. return 0;
  29. }
  30.  
  31. void *fun_watek1(void * p) {
  32. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  33. return 0;
  34. }
  35.  
  36. void *fun_watek2(void * p) {
  37. printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
  38. return 0;
  39. }
  40.  
  41.  
  42. Stwórz dwa wątki w programie. Każdemu z nich przekaż przez parametr funkcji dwie liczby. Pierw-szy wątek niech policzy sumę tych liczb, drugi różnicę. Obie wartości należy wypisać na ekranw wątkach.
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <pthread.h>
  47. #include <string.h>
  48. #include <sys/file.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <unistd.h>
  52. #include <time.h>
  53.  
  54.  
  55. void *fun_watek1(void * p);
  56. void *fun_watek2(void * p);
  57. struct liczby{
  58. int x;
  59. int y;
  60. };
  61.  
  62. struct liczby message;
  63.  
  64. int main() {
  65. message.x = 4;
  66. message.y = 2;
  67.  
  68. pthread_t watek1;
  69. pthread_t watek2;
  70.  
  71. pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
  72. pthread_create(&watek2, NULL, fun_watek2, (void *)&message);
  73.  
  74. pthread_join(watek1, NULL);
  75. pthread_join(watek2, NULL);
  76.  
  77.  
  78. return 0;
  79. }
  80.  
  81. void *fun_watek1(void * p) {
  82. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  83.  
  84. struct liczby * l = (struct liczby*) p;
  85.  
  86. printf("%d + %d = %d\n",l->x, l->y, l->x+l->y);
  87. return 0;
  88. }
  89.  
  90. void *fun_watek2(void * p) {
  91. printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
  92.  
  93. struct liczby * l = (struct liczby*) p;
  94.  
  95. printf("%d - %d = %d\n",l->x, l->y, l->x-l->y);
  96. return 0;
  97. }
  98. Napisz program, w którym stworzysz jeden wątek łączny i jeden wątek rozdzielny oraz zademon-strujesz różnicę w działaniu tych wątków.
  99.  
  100. #include <sys/types.h>
  101. #include <sys/stat.h>
  102. #include <fcntl.h>
  103. #include <pthread.h>
  104. #include <string.h>
  105. #include <sys/file.h>
  106. #include <stdlib.h>
  107. #include <stdio.h>
  108. #include <unistd.h>
  109. #include <time.h>
  110.  
  111.  
  112. void *fun_watek1(void * p);
  113. void *fun_watek2(void * p);
  114.  
  115. char* message;
  116.  
  117. pthread_attr_t attr;
  118.  
  119. int main() {
  120.  
  121. pthread_t watek1;
  122. pthread_t watek2;
  123.  
  124. pthread_attr_init(&attr);
  125.  
  126. pthread_create(&watek1, &attr, fun_watek1, (void *)&message);
  127.  
  128. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  129. pthread_create(&watek2, &attr, fun_watek2, (void *)&message);
  130.  
  131. pthread_join(watek1, NULL);
  132. //tu
  133. pthread_join(watek2, NULL);
  134.  
  135. pthread_attr_destroy(&attr);
  136. return 0;
  137. }
  138.  
  139. void *fun_watek1(void * p) {
  140. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  141.  
  142. return 0;
  143. }
  144.  
  145. void *fun_watek2(void * p) {
  146. printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
  147.  
  148. return 0;
  149. }
  150.  
  151. Zademonstruj działanie funkcjipthread_kill() wysyłając do wątku sygnał, dla którego będzie onmiał własną procedurę obsługi. Do podmiany procedury obsługi wykorzystaj funkcjęsigaction().
  152.  
  153. #include <sys/types.h>
  154. #include <sys/stat.h>
  155. #include <fcntl.h>
  156. #include <pthread.h>
  157. #include <string.h>
  158. #include <sys/file.h>
  159. #include <stdlib.h>
  160. #include <stdio.h>
  161. #include <signal.h>
  162. #include <unistd.h>
  163. #include <time.h>
  164.  
  165.  
  166. void *fun_watek1(void * p);
  167.  
  168. char* message;
  169.  
  170.  
  171. int main() {
  172.  
  173. pthread_t watek1;
  174.  
  175. pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
  176. sleep(2);
  177. pthread_kill(watek1, SIGHUP);
  178.  
  179. pthread_join(watek1, NULL);
  180.  
  181. return 0;
  182. }
  183.  
  184. void *fun_watek1(void * p) {
  185. int koniec = 0;
  186.  
  187. void syghup(int a)
  188. {
  189. printf("Otrzymalem sygnal. Koniec\n");
  190. koniec=1;
  191. }
  192.  
  193. signal(SIGHUP, &syghup);
  194. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  195.  
  196. for(;;){
  197. if(koniec==1) break;
  198. };
  199.  
  200. return 0;
  201. }
  202. Zademonstruj sposób użycia semafora, którego wartość początkowa jest większa od jeden
  203. #include <sys/types.h>
  204. #include <sys/stat.h>
  205. #include <fcntl.h>
  206. #include <pthread.h>
  207. #include <string.h>
  208. #include <sys/file.h>
  209. #include <stdlib.h>
  210. #include <stdio.h>
  211. #include <signal.h>
  212. #include <unistd.h>
  213. #include <time.h>
  214.  
  215. static sigset_t signal_mask;
  216.  
  217. void *fun_watek1(void * p);
  218.  
  219. char* message;
  220. int rc;
  221.  
  222. int main() {
  223. sigemptyset (&signal_mask);
  224. sigaddset (&signal_mask, SIGHUP);
  225. rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
  226. if (rc != 0) {
  227. printf("Blad blokowania sygnalu");
  228. }
  229.  
  230.  
  231. pthread_t watek1;
  232. rc = pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
  233. if (rc != 0) {
  234. printf("Blad tworzenia");
  235. }
  236. sleep(2);
  237.  
  238. pthread_kill(watek1, SIGHUP);
  239.  
  240. pthread_join(watek1, NULL);
  241.  
  242. return 0;
  243. }
  244.  
  245. void *fun_watek1(void * p) {
  246. int sig;
  247. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  248.  
  249. sigwait(&signal_mask, &sig);
  250. printf("Sygnal %d Koniec\n", sig);
  251. return 0;
  252. }
  253. Zademonstruj działanie funkcji sprzątających
  254.  
  255. #include <sys/types.h>
  256. #include <sys/stat.h>
  257. #include <fcntl.h>
  258. #include <pthread.h>
  259. #include <string.h>
  260. #include <sys/file.h>
  261. #include <stdlib.h>
  262. #include <stdio.h>
  263. #include <signal.h>
  264. #include <unistd.h>
  265. #include <time.h>
  266.  
  267. void cleanupHandler(void *arg)
  268. {
  269. printf("Funkcja sprzatajaca\n");
  270. }
  271.  
  272. void *fun_watek1(void * p) {
  273. printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
  274.  
  275. pthread_cleanup_push(cleanupHandler, NULL);
  276. while (1) {
  277. pthread_testcancel();
  278. sleep(1);
  279. }
  280. pthread_cleanup_pop(0);
  281. return 0;
  282. }
  283.  
  284.  
  285. char* message;
  286. int rc;
  287.  
  288. int main() {
  289.  
  290. pthread_t watek1;
  291. rc = pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
  292. if (rc != 0) {
  293. printf("Blad tworzenia");
  294. }
  295. sleep(2);
  296.  
  297. rc = pthread_cancel(watek1);
  298. if (rc != 0) {
  299. printf("Blad blokowania sygnalu");
  300. }
  301.  
  302. sleep(2);
  303. //pthread_join(watek1, NULL);
  304.  
  305. return 0;
  306. }
  307. Napisz program, w którym stworzysz 20 wątków wykonujących tę samą czynność. W momenciekiedy jeden z nich ją zakończy pozostałe powinny być anulowane w sposób asynchroniczny.
  308. #include <sys/types.h>
  309. #include <sys/stat.h>
  310. #include <fcntl.h>
  311. #include <pthread.h>
  312. #include <string.h>
  313. #include <sys/file.h>
  314. #include <stdlib.h>
  315. #include <stdio.h>
  316. #include <signal.h>
  317. #include <unistd.h>
  318. #include <time.h>
  319.  
  320.  
  321. void *fun_watek(void * p) {
  322. int oldtype, rc;
  323. int x=2,l;
  324.  
  325. printf("Watek: %lu, PID: %d\n", pthread_self(), getpid());
  326.  
  327. rc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
  328. if (rc != 0) {
  329. printf("Blad ustawienia cancel");
  330. }
  331. for(l=0; l<1000000; l++){
  332. x*=2;
  333. }
  334.  
  335. printf("Koniec watku %lu\n",pthread_self());
  336. return 0;
  337. }
  338.  
  339.  
  340. int rc;
  341.  
  342. int main() {
  343. int i;
  344. pthread_t watek[20];
  345.  
  346. for(i=0; i<20;i++){
  347. rc = pthread_create(&watek[i], NULL, fun_watek, NULL);
  348. if (rc != 0) {
  349. printf("Blad tworzenia");
  350. }
  351. }
  352. //sleep(6);
  353.  
  354. pthread_join(watek[0], NULL);
  355. for(i=0; i<20; i++) pthread_cancel(watek[i]);
  356.  
  357.  
  358. printf("Koniec programu\n");
  359. return 0;
  360. }
  361. Napisz program, który sprawdzi, czy z deskryptorem standardowego wejścia (stdin_filenolubwartość0) jest związany terminal. Jeśli tak, to program powinien wypisać nazwę pliku urządzeniatego terminala, oraz kilka jego bieżących ustawień.
  362. #include <termios.h>
  363. #include <unistd.h>
  364. #include <stdio.h>
  365.  
  366. int main (void)
  367. {
  368. struct termios temp;
  369. int atr_l, atr_i, atr_o, atr_c;
  370. char * ciag;
  371. if(tcgetattr(STDIN_FILENO, &temp) != 0)
  372. {
  373. perror("tcgetattr");
  374. return -1;
  375. }
  376. atr_i=temp.c_iflag;
  377. atr_o=temp.c_oflag;
  378. atr_c=temp.c_cflag;
  379. atr_l=temp.c_lflag;
  380.  
  381. if(isatty(STDIN_FILENO) == 1)
  382. {
  383. printf("Deskryptor odnosi sie do terminala\n");
  384. ciag = ttyname(STDIN_FILENO);
  385. if(ciag != NULL)
  386. {
  387. printf("nazwa pliku terminala: %s\n", ciag);
  388. printf("ECHO: \t\t%s\n", atr_l & ECHO?"włączone":"wyłączone");
  389. printf("ICANON: \t%s\n", atr_l & ICANON?"włączone":"wyłączone");
  390. printf("\n");
  391. printf("INPCK: \t%s\n", atr_i & INPCK?"włączone":"wyłączone");
  392. printf("IGNCR: \t%s\n", atr_i & IGNCR?"włączone":"wyłączone");
  393. printf("\n");
  394. printf("OPOST: \t%s\n", atr_o & OPOST?"włączone":"wyłączone");
  395. printf("OFILL: \t%s\n", atr_o & OFILL?"włączone":"wyłączone");
  396. printf("\n");
  397. printf("CREAD: \t\t%s\n", atr_c & CREAD?"włączone":"wyłączone");
  398. printf("CLOCAL: \t%s\n", atr_c & CLOCAL?"włączone":"wyłączone");
  399. }
  400. else
  401. {
  402. perror("ttyname");
  403. return -1;
  404. }
  405. }
  406. else
  407. {
  408. printf("Dekstryptor nie odnosi sie.");
  409. perror("isatty: ");
  410. return -1;
  411. }
  412. return 0;
  413. }
  414. Napisz program, który wyłączy echo, czyli wypisywanie znaków wprowadzonych z klawiatury naekran. Po ponownym uruchomieniu program powinien przywrócić echo.
  415. #include <termios.h>
  416. #include <unistd.h>
  417. #include <stdio.h>
  418.  
  419. int main (void)
  420. {
  421. struct termios temp;
  422. int atr;
  423. if(tcgetattr(STDIN_FILENO, &temp) != 0)
  424. {
  425. perror("tcgetattr: ");
  426. return -1;
  427. }
  428. atr=temp.c_lflag & ECHO;
  429. if(atr == 0)
  430. {
  431. printf("ECHO ON \n");
  432. temp.c_lflag = temp.c_lflag | ECHO;
  433. if(tcsetattr(STDIN_FILENO, TCSANOW, &temp) != 0)
  434. {
  435. perror("tcsetattr: ");
  436. return -1;
  437. }
  438.  
  439. }
  440. else
  441. {
  442. printf("ECHO OFF \n");
  443. temp.c_lflag = temp.c_lflag & ~(ECHO);
  444. if(tcsetattr(STDIN_FILENO, TCSANOW, &temp) != 0)
  445. {
  446. perror("tcsetattr: ");
  447. return -1;
  448. }
  449. }
  450. return 0;
  451. }
  452. Napisz program, po którego uruchomieniu terminal będzie wypisywał wszystkie informacje dużymiliterami (niezależnie od ustawienia CapsLock). Efekt ten powinno usuwać ponowne uruchomienieprogramu.
  453. #include <termios.h>
  454. #include <unistd.h>
  455. #include <stdio.h>
  456.  
  457. int main (void)
  458. {
  459. struct termios term;
  460. int x;
  461. tcgetattr(STDIN_FILENO, &term);
  462.  
  463. x=term.c_oflag & OLCUC;
  464. if(x == 0)
  465. {
  466. term.c_oflag |= OLCUC;
  467. tcsetattr(STDIN_FILENO, TCSANOW, &term);
  468. }
  469.  
  470. else
  471. {
  472. term.c_oflag &= ~(OLCUC);
  473. tcsetattr(STDIN_FILENO, TCSANOW, &term);
  474. }
  475.  
  476. return 0;
  477. }
  478.  
  479. Napisz program, który przepisze z istniejącego pliku tekstowego czterdzieści znaków z początkui czterdzieści znaków z końca do nowego pliku.
  480. #include <sys/types.h>
  481. #include <sys/stat.h>
  482. #include <fcntl.h>
  483. #include <stdlib.h>
  484. #include <stdio.h>
  485. #include <unistd.h>
  486.  
  487. int main()
  488. {
  489. int desk = open("emg", 0666);
  490. if(desk == -1) perror("open");
  491. int fd = creat("z", S_IRWXU);
  492. if(fd == -1) perror("create");
  493. int tekst[41];
  494. int a = read(desk, (void *)tekst, 40 * sizeof(char));
  495. if(a == -1) perror("read");
  496. else
  497. printf("Odczytano %d znaków\n", a);
  498. a = write(fd, tekst, 40 * sizeof(char));
  499. if(a == -1) perror("write");
  500. else printf("Zapisano %d bajtów\n", a);
  501. lseek(desk, -(sizeof(int) * 40), SEEK_END);
  502. a = read(desk, (void *)tekst, 40 * sizeof(char));
  503. if(a == -1) perror("read2");
  504. else printf("Odczytano %d bajtów", a);
  505. lseek(fd, 1, SEEK_END);
  506. a = write(fd, tekst, 40 * sizeof(char));
  507. if(a == -1) perror("write2");
  508. else printf("Zapisano %d bajtów\n", a);
  509.  
  510. if(desk != -1) close(desk);
  511. if(fd != -1) close(fd);
  512. return 0;
  513. }
  514. Stwórz plik tekstowy wielkości 512 bajtów, z „dziurą” w środku.
  515. #include <sys/types.h>
  516. #include <sys/stat.h>
  517. #include <fcntl.h>
  518. #include <unistd.h>
  519. #include <stdlib.h>
  520. #include <stdio.h>
  521. #include <string.h>
  522.  
  523. int main()
  524. {
  525. int d1 = creat("z2", S_IRWXU);
  526. if(d1 == -1) perror("Creat");
  527.  
  528. int d2 = dup(d1);
  529. if(d2 == -1) printf("Nie udało się skopiować deskryptora pliku\n");
  530. else printf("Skopiowano deskryptor pliku\n");
  531. char t1[] ="Zapis t1\n";
  532. int a = write(d1, t1, strlen(t1));
  533. if(a == -1) perror("write1");
  534. else printf("Zapisano %d\n", a);
  535. char t2[] = "Zapis t2\n";
  536. a = write(d2, t2, strlen(t2));
  537. if(a == -1) perror("Write2");
  538. else printf("Zapisano d2 %d\n", a);
  539. int d3;
  540. close(d3);
  541. if(dup2(d1, d3) == -1) printf("Nie udało się skopiować desk pliku\n");
  542. else printf("Skopiowano desk pliku\n");
  543. char t3[] = "Zapis t3";
  544. a = write(d3, t3, strlen(t3));
  545. if(a == -1) perror("Write3");
  546. else printf("Zapisano d3 %d\n", a);
  547. close(d3);
  548. close(d2);
  549. close(d1);
  550.  
  551. return 0;
  552. }
  553. Zademonstruj działanie funkcji replikującej deskryptory plików.
  554. #include <sys/types.h>
  555. #include <sys/stat.h>
  556. #include <fcntl.h>
  557. #include <pthread.h>
  558. #include <string.h>
  559. #include <sys/file.h>
  560. #include <stdlib.h>
  561. #include <stdio.h>
  562.  
  563. void *zap(void *);
  564. int d;
  565.  
  566. int main()
  567. {
  568. d = creat("z3", S_IRWXU);
  569. pthread_t tid[20];
  570. int i;
  571. for(i = 0; i < 20; i++)
  572. {
  573. pthread_create(&tid[i], NULL, zap, (void *)&i);
  574. pthread_join(tid[i], NULL);
  575. }
  576. close(d);
  577.  
  578. return 0;
  579. }
  580.  
  581. void *zap(void * p)
  582. {
  583. int a;
  584. memcpy((void *)&a, p, sizeof(int));
  585. printf("Wątek %d\n", a);
  586. char c = a + 48;
  587. flock(d, LOCK_EX | LOCK_NB);
  588. int r = write(d, &c, sizeof(char));
  589. if(r == -1) printf("Nie udało się zapisać w wątku %d\n", a);
  590. else printf("Zapisanow w wątku %d %d bajtów\n", a, r);
  591. flock(d, LOCK_UN);
  592. return NULL;
  593. }
  594. Napisz program, który przeczyta i wypisze na ekran treść pliku tekstowego o dowolnej wielkości.Nazwę pliku należy przekazywać jako argument wywołania programu
  595. #include <stdlib.h>
  596. #include <stdio.h>
  597. #include <sys/types.h>
  598. #include <sys/stat.h>
  599. #include <fcntl.h>
  600. #include <string.h>
  601.  
  602. int main(int argc, char *argv[])
  603. {
  604.  
  605. if(argc != 3)
  606. {
  607. printf("Podaj jako parametr ścieżkę do pliku\n");
  608. exit(0);
  609. }
  610. //printf("%s", argv[1]);
  611. char *plik = (char *)calloc(strlen(argv[1]), sizeof(char));
  612. strcpy(plik, argv[1]);
  613. char *plik2 = (char *)calloc(strlen(argv[2]), sizeof(char));
  614. strcpy(plik2, argv[2]);
  615. char p[] = "Pliki: ";
  616. char newLine[] = "\n";
  617. write(1, p, strlen(p));
  618. write(1, plik, strlen(plik));
  619. write(1, plik2, strlen(plik2));
  620. write(1, newLine, strlen(newLine));
  621. int d = open(plik, 0444);
  622. if(d == -1) perror("open\n");
  623. free(plik);
  624. off_t roz = lseek(d, 0, SEEK_END);
  625. printf("Rozmiar pliku %d bajtów\n", (int)roz);
  626. char *tmp = (char *)malloc(roz);
  627. lseek(d, 0, SEEK_SET);
  628. int r = read(d, tmp, roz);
  629. if(r == -1) perror("read");
  630. else printf("Odczytano %d bajtów\n\n", r);
  631. r = write(1, tmp, roz);
  632. if(r == -1) perror("write wys");
  633. else printf("\nWyświetlono %d bajtów\n", r);
  634. int d1 = creat(plik2, S_IRWXU);
  635. if(d1 == -1) perror("creat");
  636. free(plik2);
  637. r = write(d1, tmp, roz);
  638. if(r == -1) perror("write zap");
  639. else printf("\nZapisono %d bajtów\n", r);
  640.  
  641. free(tmp);
  642. if(d != -1) close(d);
  643. if(d1 != -1) close(d1);
  644. return 0;
  645.  
  646. }
  647. Zmodyfikuj przykładowe programy tak, aby serwer odsyłał do klienta komunikat potwierdzającyodebranie komunikatu.
  648. //client
  649. #include<stdio.h>
  650. #include<unistd.h>
  651. #include<sys/socket.h>
  652. #include<sys/types.h>
  653. #include<string.h>
  654. #include<netinet/ip.h>
  655. #include<arpa/inet.h>
  656.  
  657. #define SERVER_PORT 1096
  658.  
  659. struct sockaddr_in server_address =
  660. {
  661. .sin_family = AF_INET,
  662. .sin_port = SERVER_PORT,
  663. .sin_addr = {INADDR_ANY}
  664. };
  665.  
  666. void get_and_print_message(int socket_descriptor)
  667. {
  668. char buffer[512];
  669.  
  670. socklen_t address_length = sizeof(server_address);
  671.  
  672. int received_bytes = recvfrom(socket_descriptor,(void *)buffer,sizeof(buffer),0,(struct sockaddr*)&server_address,&address_length);
  673. if(received_bytes<0)
  674. perror("recvfrom");
  675. else
  676. {
  677. buffer[received_bytes]='\0';
  678. puts(buffer);
  679. }
  680. }
  681.  
  682. void send_message(int socket_descriptor)
  683. {
  684. const char *message = "Komunikat przesłany przez sieć.";
  685. if(sendto(socket_descriptor,message,strlen(message),0,(struct sockaddr *)&server_address,sizeof(server_address))<0)
  686. perror("sendto");
  687. }
  688.  
  689. SERVER
  690.  
  691. int main(void)
  692. {
  693. int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  694. if(socket_descriptor<0)
  695. perror("socket");
  696.  
  697. send_message(socket_descriptor);
  698. get_and_print_message(socket_descriptor);
  699.  
  700. if(close(socket_descriptor)<0)
  701. perror("close");
  702. return 0;
  703. }
  704.  
  705. //server
  706. #include<stdio.h>
  707. #include<unistd.h>
  708. #include<sys/types.h>
  709. #include<sys/socket.h>
  710. #include<netinet/ip.h>
  711. #include<string.h>
  712.  
  713. #define SERVER_PORT 1096
  714. #define SERVER_IP_ADDRESS "127.0.0.1"
  715.  
  716. struct sockaddr_in client_address;
  717. struct sockaddr_in server_address =
  718. {
  719. .sin_family = AF_INET,
  720. .sin_port = SERVER_PORT,
  721. .sin_addr = {INADDR_ANY}
  722. };
  723.  
  724. void name_socket(int socket_descriptor)
  725. {
  726. if(bind(socket_descriptor,(struct sockaddr*)&server_address,sizeof(server_address))<0)
  727. perror("bind");
  728. }
  729.  
  730. void get_and_print_message(int socket_descriptor)
  731. {
  732. char buffer[512];
  733.  
  734. socklen_t address_length = sizeof(client_address);
  735.  
  736. int received_bytes = recvfrom(socket_descriptor,(void *)buffer,sizeof(buffer), 0,(struct sockaddr*)&client_address,&address_length);
  737. if(received_bytes<0)
  738. perror("recvfrom");
  739. else
  740. {
  741. buffer[received_bytes]='\0';
  742. puts(buffer);
  743. }
  744. }
  745.  
  746. void send_message(int socket_descriptor)
  747. {
  748. const char *message = "Komunikat zostal odebrany.";
  749. if(sendto(socket_descriptor,message,strlen(message),0,(struct sockaddr *)&client_address,sizeof(server_address))<0)
  750. perror("sendto");
  751. }
  752.  
  753. int main(void)
  754. {
  755. int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  756. if(socket_descriptor<0)
  757. perror("socket");
  758.  
  759. name_socket(socket_descriptor);
  760. get_and_print_message(socket_descriptor);
  761. send_message(socket_descriptor);
  762.  
  763. if(close(socket_descriptor)<0)
  764. perror("close");
  765. return 0;
  766. }
  767.  
  768. Napisz programy, które będą realizował polecenie zawarte w zadaniu pierwszym, ale w oparciuo protokółTCP/IP.
  769. KLIENT
  770. #include <stdio.h>
  771. #include <stdlib.h>
  772. #include <unistd.h>
  773. #include <errno.h>
  774. #include <string.h>
  775. #include <sys/types.h>
  776. #include <sys/socket.h>
  777. #include <netinet/in.h>
  778. #include <netdb.h>
  779. #include <sys/types.h>
  780. #include <arpa/inet.h>
  781. #include <sys/sendfile.h>
  782. #include <sys/stat.h>
  783. #include <fcntl.h>
  784.  
  785. struct sockaddr_in do_kogo_addr, moj_addr;
  786. int socket_fd, socket_accept;
  787. char moje_ip[20]="127.0.0.1";
  788. char odbiorca_ip[20]="127.0.0.1";
  789.  
  790. int main(void){
  791.  
  792.  
  793.  
  794.  
  795. int desk = open("plik.gts", 0666);
  796. if(desk == -1) perror("open");
  797. else printf(" opened file ");
  798.  
  799.  
  800. socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  801. if(socket_fd == -1){
  802. perror("socket: ");
  803. return -1;
  804. }
  805. else printf("\nSocket");
  806. do_kogo_addr.sin_family = AF_INET;
  807. do_kogo_addr.sin_port = htons(4000);
  808. do_kogo_addr.sin_addr.s_addr = inet_addr(odbiorca_ip);
  809.  
  810.  
  811. if ( connect(socket_fd, (struct sockaddr*)&do_kogo_addr, sizeof(struct sockaddr)) == -1){
  812. perror("connect: ");
  813. close(socket_fd);
  814. return -1;
  815. }
  816. else printf("\nconnect");
  817.  
  818. char bufor[100]="wiadomosc od klienta do servera";
  819. int costam=0;
  820. int numbytes = 0;
  821. do{
  822. costam=sendfile(socket_fd, desk,NULL,50);
  823. //numbytes += send(socket_fd,&bufor,sizeof(bufor),0);
  824. }while(costam==50);
  825.  
  826. close(socket_fd);
  827. close(desk);
  828.  
  829. return 0;
  830. }
  831.  
  832. SERVER
  833. #include <stdio.h>
  834. #include <stdlib.h>
  835. #include <stdio.h>
  836. #include <stdlib.h>
  837. #include <unistd.h>
  838. #include <errno.h>
  839. #include <string.h>
  840. #include <sys/types.h>
  841. #include <sys/socket.h>
  842. #include <netinet/in.h>
  843. #include <netdb.h>
  844. #include <sys/types.h>
  845. #include <arpa/inet.h>
  846. #include <sys/sendfile.h>
  847. #include <sys/stat.h>
  848. #include <fcntl.h>
  849.  
  850. struct sockaddr_in od_kogo_addr, moj_addr;
  851. int socket_fd, socket_accept;
  852. char moje_ip[20]="127.0.0.1";
  853. char odbiorca_ip[20]="127.0.0.1";
  854.  
  855. int main(void){
  856. socklen_t _size;
  857.  
  858. int desk2 = creat("plik2.gts", 0666);
  859. if(desk2 == -1) perror("open");
  860. else printf(" opened file ");
  861.  
  862. socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  863.  
  864. if(socket_fd == -1){
  865. perror("socket: ");
  866. return -1;
  867. }
  868. else printf("\n socket");
  869. moj_addr.sin_family = AF_INET;
  870. moj_addr.sin_port = htons(4000);
  871. moj_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  872.  
  873.  
  874.  
  875. if ( (bind(socket_fd, (struct sockaddr*)&moj_addr, sizeof(struct sockaddr))) == -1){
  876. close(socket_fd);
  877. perror("bind: ");
  878. }
  879. else printf("\n bind: ");
  880. if(listen(socket_fd, SOMAXCONN) == -1) {
  881. close(socket_fd);
  882. perror("listen: ");
  883. }
  884. else printf("\n listen: ");
  885.  
  886.  
  887. _size = sizeof(struct sockaddr_in);
  888.  
  889.  
  890. socket_accept = accept (socket_fd, (struct sockaddr*)&od_kogo_addr, &_size);
  891. if(socket_accept == -1){
  892. close(socket_fd);
  893. close(socket_accept);
  894. perror("\naccept: ");
  895. return -1;
  896. }
  897. else printf("accept\n");
  898. char bufor[100];
  899. int numbytes=1;
  900. int a=0;
  901. //do{
  902. numbytes = recv(socket_accept, bufor, sizeof(bufor), 0);
  903.  
  904. printf("\n %d,\n %s\n\n",numbytes,bufor);
  905. a += write(desk2, bufor, 100 * sizeof(char));
  906. //}while(numbytes != 0);
  907. close(desk2);
  908. return 0;
  909. }
  910. Stwórz serwer współbieżny, który będzie obsługiwał połączenia od wielu klientów, również napi-sanych przez Ciebie - mogą one przesyłać np. pseudolosowe liczby do serwera, który będzie jewyświetlał na ekranie. Połączenia powinny być obsługiwane przez procesy potomne. Aby uniknąćtworzenia procesów zombie, proces macierzysty powinien ignorować sygnały o zakończeniu proce-sów potomnych. Użyj protokołu połączeniowego.8.Wykonaj polecenie z poprzedniego zadania, używając tym razem wątków zamiast procesów.
  911. KLIENT
  912. #include <arpa/inet.h>
  913. #include <netinet/in.h>
  914. #include <stdio.h>
  915. #include <sys/types.h>
  916. #include <sys/socket.h>
  917. #include <unistd.h>
  918. #include <stdlib.h>
  919. #include <string.h>
  920.  
  921. #define BUFLEN 512
  922. #define NPACK 3
  923. #define PORT 54321
  924. #define SRV_IP "127.0.0.1"
  925.  
  926. void blad(char *s)
  927. {
  928. perror(s);
  929. exit(1);
  930. }
  931.  
  932. int main(void)
  933. {
  934. struct sockaddr_in si_other;
  935. int gniazdo, i;
  936. socklen_t slen = sizeof(struct sockaddr);
  937. char buf[BUFLEN];
  938.  
  939. if ((gniazdo = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
  940. blad("socket: ");
  941.  
  942. memset((char *) &si_other, 0, sizeof(si_other));
  943. si_other.sin_family = AF_INET;
  944. si_other.sin_port = htons(PORT);
  945. si_other.sin_addr.s_addr = inet_addr(SRV_IP);
  946.  
  947. connect(gniazdo, (struct sockaddr*)&si_other, sizeof(struct sockaddr));
  948.  
  949. for (i = 0; i < NPACK; i++) {
  950. //wysylanie
  951. printf("Wiadomosc nr: %d\n ", i+1);
  952. scanf("%s", buf);
  953. printf("\n");
  954. if (sendto(gniazdo, buf, BUFLEN, 0, (struct sockaddr *)&si_other, slen) == -1)
  955. blad("sendto: ");
  956. }
  957.  
  958. close(gniazdo);
  959. return 0;
  960. }
  961. SERVER
  962. #include <arpa/inet.h>
  963. #include <netinet/in.h>
  964. #include <stdio.h>
  965. #include <sys/types.h>
  966. #include <sys/socket.h>
  967. #include <unistd.h>
  968. #include <stdlib.h>
  969. #include <string.h>
  970. #include <pthread.h>
  971.  
  972. #define BUFLEN 512
  973. #define NPACK 3
  974. #define PORT 54321
  975.  
  976. int gniazdo, i;
  977. socklen_t slen = sizeof(struct sockaddr);
  978. char buf[BUFLEN];
  979.  
  980. struct sockaddr_in si_me, si_other;
  981.  
  982. void blad(char *s)
  983. {
  984. perror(s);
  985. exit(1);
  986. }
  987.  
  988. void *odbierz(void *arg)
  989. {
  990. for (i = 0; i < NPACK; i++) {
  991. //odbieranie
  992. if (recvfrom(gniazdo, buf, BUFLEN, 0,(struct sockaddr*) &si_other, &slen) == -1)
  993. blad("perror: ");
  994. printf("Wiadomosc od %s:%d\n %s\n\n",
  995. inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
  996. }
  997. }
  998.  
  999. int main(void)
  1000. {
  1001. pthread_t klient_watek;
  1002.  
  1003. if ((gniazdo = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  1004. blad("socket: ");
  1005. memset((char *) &si_me, 0, sizeof(si_me));
  1006. si_me.sin_family = AF_INET;
  1007. si_me.sin_port = htons(PORT);
  1008. si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  1009. if (bind(gniazdo, (struct sockaddr*)&si_me, sizeof(si_me)) == -1)
  1010. blad("bind: ");
  1011.  
  1012. int j = 0;
  1013.  
  1014. for(j = 0; j < 2; j++) {
  1015. listen(gniazdo, 1);
  1016. int list = accept(gniazdo, (struct sockaddr*) &si_other, &slen);
  1017. if ( pthread_create( &klient_watek, NULL, odbierz, NULL))
  1018. blad("pthread_create: ");
  1019. if ( pthread_join ( klient_watek, NULL))
  1020. blad("pthread_join: ");
  1021. }
  1022.  
  1023. close(gniazdo);
  1024. return 0;
  1025. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement