Advertisement
DescendingBear

benis

Jan 28th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.85 KB | None | 0 0
  1. options.c
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5.  
  6. int main(int argc, char *argv[]) {
  7. int i;
  8. /*
  9. for (i=0; i < argc; i++) {
  10. printf("parametrem nr %2d jest: %s\n", i, argv[i]);
  11. }
  12. */
  13.  
  14. extern int opterr;
  15. opterr=0;
  16. extern char *optarg;
  17.  
  18. int ch;
  19.  
  20. while ((ch = getopt(argc, argv, "xf:")) != -1) {
  21. switch (ch) {
  22. case 'x':
  23. printf("Podano opcjÄ™ x\n");
  24. break;
  25. case 'f':
  26. printf ("opcja f o wartosci '%s'\n", optarg);
  27. break;
  28. case '?':
  29. default:
  30.  
  31. printf ("uzycie -x albo -f ...\n");
  32. }
  33. }
  34.  
  35. }
  36.  
  37. envvar.c
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41.  
  42. int main( int argc, char* argv[], char *envp[]) {
  43.  
  44. //while(*envp)
  45. //printf("%s\n",*envp++);
  46.  
  47. //char *user;
  48. //user = getenv("USER");
  49. //if (user!=NULL) printf("Username: %s\n", user);
  50.  
  51.  
  52. setenv("X", "ABC", 1);
  53. //char *X;
  54. //X = getenv("X");
  55. //if (X!=NULL) printf("X=%s\n", X);
  56.  
  57.  
  58. extern char **environ;
  59. while(*environ)
  60. printf("%s\n",*environ++);
  61. }
  62.  
  63. #include <stdio.h>
  64. #include <unistd.h>
  65. #include <sys/types.h>
  66.  
  67. int main(int argc, char *argv[]) {
  68. printf("uid=%d\n", getuid());
  69. printf("euid=%d\n", geteuid());
  70. printf("gid=%d\n", getgid());
  71. printf("egid=%d\n", getegid());
  72. }
  73.  
  74. #include <stdio.h>
  75. #include <sys/types.h>
  76. #include <sys/stat.h>
  77. #include <unistd.h>
  78.  
  79. int main (void) {
  80. struct stat buf;
  81. int x;
  82.  
  83. x = stat ("link", &buf);
  84. if (S_ISLNK(buf.st_mode)) printf (" stat mowi: link\n");
  85. if (S_ISREG(buf.st_mode)) printf (" stat mowi: plik\n");
  86.  
  87. x = lstat ("link", &buf);
  88. if (S_ISLNK(buf.st_mode)) printf ("lstat mowi: link\n");
  89. if (S_ISREG(buf.st_mode)) printf ("lstat mowi: plik\n");
  90.  
  91. return 0;
  92. }
  93.  
  94. #include <sys/stat.h>
  95. #include <fcntl.h>
  96. #include <unistd.h>
  97.  
  98. int main(int argc, char *argv[]) {
  99. int wej, wyj, n;
  100. char bufor[100];
  101.  
  102. wej=open(argv[1], O_RDONLY);
  103. wyj=open(argv[2], O_CREAT| O_WRONLY | O_TRUNC, S_IRWXU);
  104. //wyj=open(argv[2], O_CREAT| O_WRONLY | O_TRUNC);
  105.  
  106.  
  107. while ((n=read(wej, bufor, sizeof(bufor)))!=0) {
  108. //printf("%d", n);
  109. write(wyj, bufor, n);
  110. }
  111. close(wej);
  112. close(wyj);
  113. }
  114.  
  115. #include <stdio.h>
  116. #include <stdlib.h>
  117. #include <dirent.h>
  118. #include <limits.h>
  119.  
  120. void readdir2(char *name) {
  121.  
  122. DIR *dirp = opendir(name);
  123. struct dirent* entry;
  124. int newpath_length;
  125. char newpath[PATH_MAX];
  126.  
  127. if (dirp != NULL) {
  128. while ((entry = readdir(dirp)) != NULL) {
  129. if (entry->d_type == DT_DIR ) {
  130. printf("%s\n", entry->d_name);
  131. newpath_length = snprintf (newpath, PATH_MAX, "%s/%s", name, entry->d_name);
  132. if (newpath_length >= PATH_MAX) {
  133. perror("Zbyt dluga sciezkaa.\n");
  134. exit(EXIT_FAILURE);
  135. }
  136. if (!(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0))
  137. readdir2(newpath);
  138. }
  139. }
  140. }
  141. closedir(dirp);
  142. }
  143.  
  144. int main( void )
  145. {
  146. readdir2(".");
  147.  
  148. return EXIT_SUCCESS;
  149. }
  150.  
  151. #include <sys/stat.h>
  152. #include <fcntl.h>
  153. #include <unistd.h>
  154. #include <stdio.h>
  155.  
  156. int main(int argc, char *argv[]) {
  157. int wyj, n, newout;
  158. char bufor[100];
  159.  
  160. newout=dup(1);
  161. close(1);
  162. wyj=open("tajemniczyplik.txt", O_CREAT| O_WRONLY | O_TRUNC, S_IRUSR|S_IRWXU);
  163.  
  164. //printf("%d", wyj);
  165. while ((n=read(0, bufor, 100))!=0) {
  166. write(1, bufor, n);
  167. write(newout, bufor, n);
  168. }
  169. close(wyj);
  170. }
  171.  
  172. #include <stdio.h>
  173. #include <unistd.h>
  174. #include <fcntl.h>
  175. #include <sys/types.h>
  176. #include <sys/stat.h>
  177.  
  178. int main(int argc, char **argv)
  179. {
  180. int in, out;
  181. char *args[] = {"grep", "lancuch", NULL};
  182.  
  183. in = open("plikwej.txt", O_RDONLY);
  184. out = open("plikwyj.txt", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
  185.  
  186. dup2(in, 0);
  187.  
  188. dup2(out, 1);
  189.  
  190. close(in);
  191. close(out);
  192.  
  193. execvp("grep", args);
  194. }
  195.  
  196. #include <stdio.h>
  197. #include <signal.h>
  198. #include <stdlib.h>
  199. #include <unistd.h>
  200.  
  201. void obsluga(int sig) {
  202. printf("parent: otrzymalem sygnal %d\n", sig);
  203. //signal(SIGINT, obsluga);
  204. }
  205.  
  206. int main(void) {
  207.  
  208. pid_t pid;
  209.  
  210. printf("zaczyna przodek\n");
  211. signal(SIGINT, obsluga);
  212. printf("obsluga ustawiona\n");
  213. //sleep(10);
  214. if ((pid = fork())==0) {
  215. //potomek
  216. printf("zaczyna potomek\n");
  217. kill(getppid(), SIGINT);
  218. //kill(getppid(), SIGINT);
  219. printf("potomek konczy\n");
  220. } else if (pid>0) {
  221. //przodek
  222. waitpid(pid);
  223. printf("przodek konczy\n");
  224. return EXIT_SUCCESS;
  225. } else if (pid==-1) {
  226. printf("Nie mozna utworzyc procesu");
  227. exit(EXIT_FAILURE);
  228. }
  229. }
  230.  
  231. #include <stdio.h>
  232. #include <signal.h>
  233. #include <unistd.h>
  234.  
  235. void obsluga (int sig) {
  236. printf("budzik\n");
  237. }
  238.  
  239. int main() {
  240. signal(SIGALRM, obsluga);
  241. alarm(5);
  242. //alarm(4);
  243.  
  244.  
  245. while(1);
  246.  
  247. return 0;
  248. }
  249.  
  250. #include <signal.h>
  251. #include <stdlib.h>
  252. #include <stdio.h>
  253.  
  254. void obsluga(int sygnal) {
  255. printf("odebralem sygnal %d\n", sygnal);
  256. }
  257.  
  258. int main() {
  259. signal(SIGINT, obsluga);
  260.  
  261. printf("wysylam sygnal do siebie\n");
  262. raise(SIGINT);
  263.  
  264. return EXIT_SUCCESS;
  265. }
  266.  
  267. #include <signal.h>
  268. #include <stdio.h>
  269. #include <unistd.h>
  270. #include <stdlib.h>
  271.  
  272. void obsluga(int sygnal) {
  273. printf("Wcisnales ctl-c\n");
  274. }
  275.  
  276. int main() {
  277.  
  278. struct sigaction act, oldact;
  279. act.sa_handler = obsluga;
  280. sigemptyset(&act.sa_mask);
  281. act.sa_flags = 0;
  282. sigaction(SIGINT, &act, &oldact);
  283.  
  284. while(1);
  285.  
  286. return EXIT_SUCCESS;
  287.  
  288. }
  289.  
  290. #include <stdio.h>
  291. #include <signal.h>
  292. #include <unistd.h>
  293.  
  294. int main() {
  295. sigset_t sset;
  296.  
  297. sigemptyset(&sset);
  298. sigaddset(&sset, SIGQUIT);
  299. sigaddset(&sset, SIGALRM);
  300. sigaddset(&sset, SIGINT);
  301. sigprocmask(SIG_BLOCK, &sset, NULL);
  302.  
  303. alarm(2);
  304.  
  305. while(1);
  306.  
  307. return 0;
  308. }
  309.  
  310. #include <stdio.h>
  311. #include <signal.h>
  312. #include <unistd.h>
  313. #include <stdlib.h>
  314.  
  315. int i=10;
  316.  
  317. void obsluga (int sig) {
  318. printf("budzik %d\n", i);
  319. i--;
  320. if (i>=1) alarm(1); else exit(0);
  321. }
  322.  
  323. int main() {
  324.  
  325. struct sigaction act, oldact;
  326. act.sa_handler = obsluga;
  327. sigemptyset(&act.sa_mask);
  328. act.sa_flags = 0;
  329. sigaction(SIGALRM, &act, &oldact);
  330.  
  331. alarm(1);
  332.  
  333. while (1) pause();
  334.  
  335. return 0;
  336. }
  337.  
  338. #include<stdio.h>
  339. #include<string.h>
  340. #include<stdlib.h>
  341.  
  342. #define MAXLINE 255
  343.  
  344. main() {
  345. int n;
  346. pid_t pid;
  347. int lacze[2];
  348. char line[MAXLINE], command[MAXLINE]="test";
  349.  
  350. if (pipe(lacze)==-1) {
  351. perror("nie mozna utworzyc lacza");
  352. exit(1);
  353. }
  354.  
  355. if ((pid=fork())>0) {
  356. close(lacze[0]);
  357. //sleep(5);
  358. write(lacze[1], command, sizeof(command));
  359. } else if (pid==0) {
  360. close(lacze[1]);
  361. n=read(lacze[0], line, MAXLINE);
  362. line[n]='\0';
  363. //printf("potomek po read\n");
  364. printf("potomek: dane z pipe:%s\n", line);
  365. } else {
  366. perror("nie można utworzyc potomka");
  367. exit(2);
  368. }
  369.  
  370. exit(EXIT_SUCCESS);
  371. }
  372.  
  373. #include<stdio.h>
  374. #include<string.h>
  375. #include<stdlib.h>
  376.  
  377. #define MAXLINE 255
  378.  
  379. main() {
  380. int n;
  381. char line[MAXLINE], command[MAXLINE], expr[MAXLINE];
  382. FILE *fp;
  383.  
  384. scanf("%s", expr);
  385. sprintf(command, "echo \"");
  386. strcat(command, expr);
  387. strcat(command, "\"|bc -l");
  388. //printf("%s", command);
  389. if ( (fp = popen(command, "r")) == NULL)
  390. perror("popen error");
  391. fgets(line, MAXLINE, fp);
  392. line[strlen(line)-1]='\0';
  393.  
  394. if (ferror(fp))
  395. perror("fgets error");
  396.  
  397. printf("wynik: %s\n", line);
  398.  
  399. pclose(fp);
  400.  
  401. exit(EXIT_SUCCESS);
  402. }
  403.  
  404. #include<stdio.h>
  405. #include<string.h>
  406. #include<stdlib.h>
  407.  
  408. #define MAXLINE 255
  409.  
  410. main() {
  411. char line[MAXLINE], command[MAXLINE], expr[MAXLINE];
  412. FILE *fp;
  413.  
  414. scanf("%s", expr);
  415. sprintf(command, "bc");
  416. strcat(expr, "\n");
  417. if ( (fp = popen(command, "w")) == NULL)
  418. perror("popen error");
  419. fwrite(expr, sizeof(char), strlen(expr), fp);
  420.  
  421. if (ferror(fp))
  422. perror("fgets error");
  423.  
  424. pclose(fp);
  425.  
  426. exit(EXIT_SUCCESS);
  427. }
  428.  
  429. #include "naglowek.h"
  430.  
  431. main(argc, argv)
  432. int argc;
  433. char* argv[];
  434.  
  435. {
  436. char nazwa[MAXBUF], dane[MAXBUF], bufor[MAXBUF];
  437. int rdfifo, wrfifo; // To beda deskryptory
  438. int n;
  439. KOMUNIKAT komunikat;
  440.  
  441. // ----- Przygotowanie komunikatu do wyslania ------
  442.  
  443. printf("C: Wpisz teskst i zakoncz przez ^D\n");
  444. while ( fgets(bufor, MAXBUF, stdin) != NULL)
  445. sprintf(komunikat.txt, "%s", bufor);
  446. komunikat.pid = getpid();
  447.  
  448. // ----- Tworze fifo tymczasowe do czytania ------
  449.  
  450. sprintf(nazwa, "/tmp/jerzyfifo%d", getpid());
  451. //umask(0000);
  452. mkfifo(nazwa,0777);
  453.  
  454. // ----- Otwieram fifo serwera ---------
  455.  
  456. if ((wrfifo = open(FIFO, O_WRONLY)) < 0)
  457. perror("C: Nie moge otworzyc FIFO");
  458. else
  459. printf("C: Otworzylem: %s\n", FIFO);
  460.  
  461. // ------ Wyslanie i odebranie komunikatu -------
  462.  
  463. if ((n = write(wrfifo, &komunikat, sizeof(komunikat))) < 0)
  464. perror("C: Blad pisania do fifo");
  465. printf("\nC: Moj pid = %d, wysylam komunikat: %s\n", komunikat.pid, komunikat.txt);
  466.  
  467. // ----- Otwieram wlasne fifo ---------
  468.  
  469. if ((rdfifo = open(nazwa, O_RDONLY)) < 0)
  470. perror("C: Nie moge otworzyc fifo\n");
  471. else
  472. printf("C: Otworzylem: %s\n", nazwa);
  473.  
  474. if ((n = read(rdfifo, &komunikat, sizeof(komunikat))) < 0)
  475. perror("C: Blad czytania z fifo");
  476. printf("\nC: Odebralem komunikat: %s\n", komunikat.txt);
  477. if (unlink(nazwa)==-1) perror("C: Blad usuwania fifo.\n");
  478. else printf("C: Usunalem fifo: %s\n", nazwa);
  479. return 0;
  480. }
  481.  
  482. #include "naglowek.h"
  483. #include <sys/fcntl.h>
  484. #include <wait.h>
  485. #include <ctype.h>
  486. #include <string.h>
  487.  
  488. void children_handle(int sygnal) {
  489. pid_t pid;
  490. int stat;
  491. while (waitpid((pid_t)(-1), &stat, WNOHANG) > 0) {}
  492. }
  493.  
  494. void obsluga(KOMUNIKAT *komunikat) {
  495. int wrfifo; // deskryptor fifo do pisania
  496. char nazwa[MAXBUF];
  497.  
  498. printf("S: Otrzymalem komunikat: %s\n", komunikat->txt);
  499.  
  500. sprintf(nazwa, "/tmp/jerzyfifo%d", komunikat->pid);
  501. printf("S: Wysylam odpowiedz do %s\n", nazwa);
  502. if((wrfifo = open(nazwa, O_WRONLY)) < 0)
  503. perror("S: Nie moge otworzyc fifo klienta\n");
  504. else
  505. printf("S: Otworzylem: %s\n", nazwa);
  506. if(write(wrfifo, komunikat, sizeof(komunikat)) < 0)
  507. perror("S: Nie moge pisac do fifo\n");
  508. else
  509. printf("S: Wyslalem tekst: %s do %s\n", komunikat->txt, nazwa);
  510. close(wrfifo);
  511. }
  512.  
  513. serwer() {
  514. int pid, rdfifo, status, n;
  515. KOMUNIKAT komunikat;
  516.  
  517. // ----- Tworze i otwieram fifo do czytania -----
  518. printf("S: Czekam na klienta!!\n");
  519. // umask(0000);
  520. if (mkfifo(FIFO, 0666) < 0)
  521. perror("S: Nie moge stworzyc FIFO\n");
  522. else
  523. printf("S: Utworzylem fifo: %s\n", FIFO);
  524. if ((rdfifo = open(FIFO, O_RDONLY)) < 0)
  525. perror("S: Nie moge otworzyc FIFO\n");
  526. else
  527. printf("S: Otworzylem fifo do czytania\n");
  528.  
  529. // ----- Nieskonczona petla obslugujaca klientow -----
  530.  
  531. for(;;) {
  532.  
  533. // ----- Czytam komunikat z fifo -----
  534. n=read(rdfifo, &komunikat, sizeof(komunikat));
  535. if (n==0) {
  536. close(rdfifo);
  537. printf("S: Czekam na klienta!!\n");
  538. rdfifo = open(FIFO, O_RDONLY);
  539. } else
  540. if((pid = fork()) < 0)
  541. perror("S: nie moge stworzyc nowego procesu\n");
  542. else {
  543. if(pid == 0) {
  544. obsluga(&komunikat);
  545. //sleep(10);
  546. printf("S: Obsluzylem klienta!!\n");
  547. exit(0);
  548. }
  549. }
  550. }
  551. };
  552.  
  553. main() {
  554. pid_t pid1;
  555.  
  556. struct sigaction act, oldact;
  557.  
  558. //usuwamy potencjalne fifo z poprzedniego uruchomienia
  559. unlink(FIFO);
  560.  
  561. if((pid1 = fork()) < 0)
  562. perror("S: Nie moge stworzyc nowego procesu\n");
  563. else
  564. if(pid1 == 0) { //proces serwera
  565. //ustanawiamy obsluge syngnalu SIGCHLD
  566. act.sa_handler = children_handle;
  567. sigemptyset(&act.sa_mask);
  568. act.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  569. sigaction(SIGCHLD, &act, &oldact);
  570. //uruchamiamy funkcje glowna serwera
  571. serwer();
  572. }
  573. else printf("S: Serwer dziala w tle\n");
  574. }
  575.  
  576. #include <sys/types.h>
  577. #include <sys/stat.h>
  578. #include <sys/fcntl.h>
  579. #include <errno.h>
  580. #include <stdio.h>
  581. #include <wait.h>
  582. #include <ctype.h>
  583. #include <stdlib.h>
  584.  
  585. #define FIFO "/tmp/jerzy_fifo"
  586. #define MAXBUF 256
  587.  
  588. typedef struct
  589. {
  590. int pid;
  591. char txt[MAXBUF];
  592. } KOMUNIKAT;
  593.  
  594. #include <sys/ipc.h>
  595. #include <sys/msg.h>
  596. #include <sys/types.h>
  597. #include <stdio.h>
  598.  
  599. #define MAXBUF 256
  600. #define MSG_4_SERV 1
  601. #define KLUCZ 20160117L
  602.  
  603. typedef struct {
  604. long type; // typ komunikatu
  605. pid_t pid; // kto wysyla
  606. float a,b;
  607. char oper;
  608. } KOMUNIKAT;
  609.  
  610. #include "naglowek.h"
  611.  
  612. void wypisz_dane(int msqid)
  613. {
  614. struct msqid_ds dane;
  615.  
  616. if (msgctl(msqid, IPC_STAT, &dane) < 0)
  617. perror("Blad przy pobieraniu danych o kolejce\n");
  618. printf("Dane o kolejce o deskr %d:\n", msqid);
  619. printf("biez. l. bajtow w kolejce: %d\n", dane.msg_cbytes);
  620. printf("biez. l. komunikatow w kolejce: %d\n", dane.msg_qnum);
  621. printf("max. l. bajtow dla kolejki: %d\n", dane.msg_qbytes);
  622. printf("id. procesu, ktory ostatnio wywolal f-cje msgsnd: %d\n", dane.msg_lspid);
  623. printf("id. procesu, ktory ostatnio wywolal f-cje msgrcv: %d\n", dane.msg_lrpid);
  624. printf("czas ostat. wywol. f-cji msgrcv: %s", ctime(dane.msg_stime) );
  625. printf("czas ostat. wywol. f-cji msgsnd: %s", ctime(dane.msg_rtime) );
  626. printf("id. uzytkownika dla wlasciciela: %d\n", dane.msg_perm.uid);
  627. printf("id. grupy dla wlasciciela: %d\n", dane.msg_perm.gid);
  628. printf("id. uzytkownika dla tworcy: %d\n", dane.msg_perm.cuid);
  629. printf("id. grupy dla tworcy: %d\n", dane.msg_perm.cgid);
  630. //printf("Numer kolejny: %d\n", dane.msg_perm.seq);
  631. printf("Tryb dostepu do kolejki: %o\n", dane.msg_perm.mode & 0777);
  632. }
  633.  
  634. serwer()
  635. {
  636. int msqid ,rozmiar_kom;
  637. KOMUNIKAT komunikat;
  638. float licz1, licz2, wynik;
  639. char dzial;
  640.  
  641. rozmiar_kom = sizeof(komunikat)-sizeof(long);
  642.  
  643. if ((msqid=msgget(KLUCZ, 0666 | IPC_CREAT | IPC_EXCL) ) < 0)
  644. perror("S: Blad tworzenia kolejki\n");
  645. else printf("S: Kolejka utworzona z deskryptorem %d\n", msqid);
  646. wypisz_dane(msqid);
  647. //sleep(40);
  648. for(;;)
  649. {
  650. msgrcv(msqid, &komunikat, rozmiar_kom, MSG_4_SERV, 0);
  651. dzial=komunikat.oper;
  652. licz1=komunikat.a;
  653. licz2=komunikat.b;
  654. printf("S: Odebralem komunikat od klienta. Dzialanie %c\n", dzial);
  655. switch(dzial)
  656. {
  657. case '+' : wynik=licz1+licz2; break;
  658. case '-' : wynik=licz1-licz2; break;
  659. case '*' : wynik=licz1*licz2; break;
  660. case '/' : wynik=licz1/licz2; break;
  661. default : {
  662. printf("Nie znam dzialania: %c\n", dzial);
  663. wynik=0;
  664. }
  665. }
  666. komunikat.a=wynik;
  667. komunikat.type=komunikat.pid;
  668. msgsnd(msqid, &komunikat, rozmiar_kom, 0);
  669. printf("S: Obsluzylem klienta.\n");
  670. }
  671. /*
  672. if (msgctl(msqid, IPC_RMID, (struct msqid_ds *) 0) < 0)
  673. perror("S: Nie moge usunac kolejki\n");
  674. else printf("S: Kolejka usunieta\n");
  675. */
  676. }
  677.  
  678. main()
  679. {
  680. pid_t pid1;
  681.  
  682. if((pid1 = fork()) < 0)
  683. perror("S: Nie moge stworzyc nowego procesu\n");
  684. else
  685. if(pid1 == 0) serwer();
  686. else printf("S: Serwer dziala w tle\n");
  687. }
  688.  
  689. #include "naglowek.h"
  690.  
  691. main() {
  692. int msqid, rozmiar_kom;
  693. char dzial[MAXBUF];
  694. float liczba;
  695. KOMUNIKAT komunikat;
  696.  
  697. rozmiar_kom=sizeof(komunikat) - sizeof(long);
  698. if ((msqid = msgget(KLUCZ, 0666 | IPC_CREAT)) < 0)
  699. perror("C: Nie moge otworzyc kolejki serwera");
  700. else
  701. printf("C: Otworzylem kolejke serwera, id = %d.\n", msqid);
  702.  
  703. // ----- Przygotowanie komunikatu do wyslania ------
  704.  
  705. printf("C: Wpisz znak dzialania: ");
  706. if ( fgets(dzial, MAXBUF, stdin) == NULL) perror("C: Blad: fgets");
  707. komunikat.pid = getpid();
  708. komunikat.type = MSG_4_SERV;
  709. komunikat.oper=dzial[0];
  710. printf("C: Podaj liczbe a=");
  711. scanf("%f", &komunikat.a);
  712. printf("C: Podaj liczbe b=");
  713. scanf("%f", &komunikat.b);
  714. // ------ Wyslanie komunikatu do serwera -----------
  715.  
  716. msgsnd(msqid, &komunikat, rozmiar_kom, 0);
  717.  
  718. //------- Odebranie potwierdzenia ------------------
  719. msgrcv(msqid, &komunikat, rozmiar_kom, getpid(), 0);
  720. printf("C: Odebralem od serwera wynik: %f\n", komunikat.a);
  721.  
  722. return 0;
  723. }
  724.  
  725. /*********************************************
  726. *
  727. * program sem_create.c
  728. * tworzy semafor (jeden) o zadanym kluczu
  729. * wykonuje tez jedna operacje nadania mu wartosci 11
  730. *
  731. *********************************************/
  732.  
  733. #include <sys/types.h>
  734. #include <sys/ipc.h>
  735. #include <sys/sem.h>
  736. #include <stdio.h>
  737. #include <stdlib.h>
  738.  
  739. int semid;
  740.  
  741. int usun(int i) {
  742. if (semctl(semid, 0, IPC_RMID)!=-1)
  743. printf("Sygnal %d ....\n",i);
  744. else {
  745. printf("Nie mozna usunac semafora");
  746. exit(1);
  747. }
  748. }
  749.  
  750. main(int argc, char *argv[]) {
  751. int i, klucz;
  752. struct sembuf ustaw[1] = { 0, 11, 0 };
  753.  
  754. if (argc < 2) {
  755. printf ("Uzycie %s IPC_klucz\n", argv[0]);
  756. exit(1);
  757. }
  758.  
  759. for (i=0; i< 18; i++)
  760. signal(i, usun);
  761.  
  762. klucz = atoi(argv[1]);
  763.  
  764. if ( (semid = semget(klucz, 1, 0777 | IPC_CREAT | IPC_EXCL) ) < 0) {
  765. perror("Blad tworzenia");
  766. exit(1);
  767. }
  768.  
  769. printf("Semafor utworzony z identyfikatorem %d\n", semid);
  770.  
  771. if (semop(semid, &ustaw[0], 1) < 0)
  772. perror("blad semop");
  773.  
  774. pause();
  775. }
  776.  
  777. /*************************************************
  778. *
  779. * program sem_stat.c
  780. * drukuje wartosc semafora o zadanym kluczu
  781. * zmniejsza wartosc semafora o 1
  782. *
  783. ************************************************/
  784.  
  785. #include <sys/types.h>
  786. #include <sys/ipc.h>
  787. #include <sys/sem.h>
  788. #include <stdio.h>
  789. #include <stdlib.h>
  790.  
  791. main(argc, argv)
  792. int argc;
  793. char *argv[];
  794. {
  795. long semid, wart;
  796. key_t klucz;
  797. union semun {
  798. int val;
  799. struct semid_ds *buf;
  800. ushort *array;
  801. } arg;
  802.  
  803. if (argc < 2) {
  804. printf("Uzycie %s IPC_klucz\n", argv[0]);
  805. exit(1);
  806. }
  807.  
  808.  
  809. klucz = atoi(argv[1]);
  810.  
  811. if ( (semid = semget(klucz, 0, IPC_CREAT) ) < 0)
  812. {perror("Blad semget");
  813. exit(1);
  814. }
  815.  
  816. if ( ( wart = semctl(semid, 0,GETVAL, arg)) < 0) {
  817. perror("Blad semctl2");
  818. exit(3);
  819. }
  820. printf("Aktualna wartosc semafora: %d\n",wart);
  821.  
  822.  
  823. if ( ( wart = semctl(semid, 0,GETPID, arg)) < 0) {
  824. perror("Blad semctl");
  825. exit(3);
  826. }
  827. printf("PID ostatniego procesu: %d\n",wart);
  828.  
  829.  
  830. struct sembuf ustaw[1] = { 0, -1, 0 };
  831. if (semop(semid, &ustaw[0], 1) < 0)
  832. perror("blad semop");
  833.  
  834. if ( ( wart = semctl(semid, 0, GETVAL, arg)) < 0) {
  835. perror("Blad semctl2");
  836. exit(3);
  837. }
  838. printf("Aktualna wartosc semafora: %d\n",wart);
  839.  
  840.  
  841. if ( ( wart = semctl(semid, 0,GETPID, arg)) < 0) {
  842. perror("Blad semctl");
  843. exit(3);
  844. }
  845. printf("PID ostatniego procesu: %d\n",wart);
  846.  
  847.  
  848.  
  849. exit(0);
  850. }
  851.  
  852. #include <sys/shm.h>
  853. #include <sys/types.h>
  854. #include <stdlib.h>
  855. #include <stdio.h>
  856. #include <signal.h>
  857. #include <sys/fcntl.h>
  858. #include <string.h>
  859.  
  860. #define KLUCZ 20160107L
  861. #define L_KB 10
  862.  
  863. #include <sys/types.h>
  864. #include <sys/ipc.h>
  865. #include <sys/shm.h>
  866. #include "naglowek.h"
  867.  
  868. int shmid;
  869.  
  870. void wypisz_dane(int shmid) {
  871. struct shmid_ds dane;
  872.  
  873. if (shmctl(shmid, IPC_STAT, &dane) < 0)
  874. perror("Blad przy pobieraniu danych o kolejce\n");
  875. printf("Dane o pamieci wspolnej o deskr %d:\n", shmid);
  876.  
  877. printf("rozmiar segmentu: %d\n", dane.shm_segsz);
  878. printf("id. procesu dla ostatniej operacji %d\n", dane.shm_lpid);
  879. printf("id. procesu tworcy: %d\n", dane.shm_cpid);
  880. printf("dolaczony nr biezacy: %d\n", dane.shm_nattch);
  881. printf("id. uzytkownika dla wlasciciela: %d\n", dane.shm_perm.uid);
  882. printf("id. grupy dla wlasciciela: %d\n", dane.shm_perm.gid);
  883. printf("id. uzytkownika dla tworcy: %d\n", dane.shm_perm.cuid);
  884. printf("id. grupy dla tworcy: %d\n", dane.shm_perm.cgid);
  885. printf("Tryb dostepu do kolejki: %o\n", dane.shm_perm.mode & 0777);
  886. }
  887.  
  888. void obsluga_term() {
  889. if (shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0) < 0)
  890. perror("S: Nie moge usunac pamieci\n");
  891. else printf("S: Pamiec usunieta\n");
  892. exit(0);
  893. }
  894.  
  895. serwer() {
  896. int rozmiar_kom;
  897. char *dane, *dane_tmp;
  898. char temp[100];
  899. int pass, n, klient;
  900. int i=0;
  901.  
  902. signal(SIGTERM, obsluga_term); // ustawiam obsluge sygnalu TERM
  903.  
  904. if ((shmid=shmget(KLUCZ, L_KB*1024, 0777 | IPC_CREAT | IPC_EXCL) ) < 0)
  905. perror("S: Blad tworzenia pamieci\n");
  906. else printf("S: Pamiec utworzona z deskryptorem %d\n", shmid);
  907. if ( (dane = dane_tmp = (char *) shmat(shmid, (char *) 0, 0) ) == (char *) -1)
  908. perror("S: Nie moge przylaczyc pamieci\n");
  909. pass=open("/etc/passwd", O_RDONLY);
  910. do {
  911. n=read(pass, temp, 100);
  912. //write(1, temp, n);
  913. i+=n;
  914. if (i>= L_KB*1024) break;
  915. memcpy(dane_tmp, temp, n);
  916. dane_tmp+=n;
  917. } while(n == 100);
  918. printf("S: Wczytalem %d bajtow z pliku /etc/passwd do pamieci\n", i);
  919. close(pass);
  920. if (((int) shmdt(dane)) == -1)
  921. perror("S:Nie moge odlaczyc pamieci\n");
  922. for(;;);
  923. //wypisz_dane(shmid);
  924. }
  925.  
  926. main() {
  927. pid_t pid1;
  928.  
  929. if((pid1 = fork()) < 0)
  930. perror("S: Nie moge stworzyc nowego procesu\n");
  931. else
  932. if(pid1 == 0) serwer();
  933. else printf("S: Serwer dziala w tle\n");
  934. // serwer();
  935. }
  936.  
  937. #include "naglowek.h"
  938.  
  939. #define ILE_W 21
  940.  
  941. main() {
  942. int shmid;
  943. char *dane, *dane_tmp;
  944. int i=0,k,n;
  945.  
  946. if ((shmid=shmget(KLUCZ, L_KB*1024, 0666 | IPC_CREAT) ) < 0)
  947. perror("C: Blad uchwytu pamieci\n");
  948. printf("C: dolaczylem do pamieci o id %d\n", shmid);
  949. if ( (dane = dane_tmp = (char *) shmat(shmid, NULL, SHM_RDONLY) ) == (char *) -1)
  950. perror("C: Nie moge przylaczyc pamieci\n");
  951.  
  952. for(k=0; k < ILE_W; k++ ) {
  953. do {
  954. n=write(1, dane_tmp, 1);
  955. dane_tmp++;
  956. } while (*dane_tmp != '\n');
  957. }
  958. printf("\n");
  959. printf("C: Wypisalem %d wierszy pliku /etc/passwd\n", (int) ILE_W);
  960. if (shmdt((void *) dane) == -1)
  961. perror("C:Nie moge odlaczyc pamieci\n");
  962. }
  963.  
  964. #include <stdio.h>
  965. #include <mqueue.h>
  966.  
  967. main(int argc, char *argv[]) {
  968.  
  969. int res;
  970. int kom=77;
  971. mqd_t mq;
  972. char *mq_name="/jerzy";
  973.  
  974. // Utworzenie kolejki komunikatow
  975. if ((mq = mq_open(mq_name, O_RDWR | O_CREAT , 0660, NULL))==-1)
  976. perror("Blad tworzenia kolejki");
  977. // wysylanie komunikatu
  978. if ((res = mq_send(mq, (char *) &kom, sizeof(kom), 2)) == -1)
  979. perror("Nie moge wyslac komunikatu");
  980. printf("Wyslano komunikat: %d\n",kom);
  981. mq_close(mq);
  982. }
  983.  
  984. #include <stdio.h>
  985. #include <stdlib.h>
  986. #include <mqueue.h>
  987.  
  988. main(int argc, char *argv[]) {
  989.  
  990. int i;
  991. int res;
  992. char *kom;
  993. long sizemax;
  994. unsigned int prio;
  995. mqd_t mq;
  996. struct mq_attr attr;
  997. char *mq_name="/jerzy";
  998.  
  999. // Otwarcie kolejki komunikatow
  1000. if ((mq = mq_open(mq_name, O_RDWR , 0660, NULL ))==-1)
  1001. perror("Blad otwarcia kolejki");
  1002. //pobranie informacji o kolejce komunikatow
  1003. if (mq_getattr(mq, &attr)==-1)
  1004. perror("Blad odczytu atrybutow kolejki");
  1005. // pobranie maksymalnego rozmiaru komunikatu
  1006. sizemax=attr.mq_msgsize;
  1007. /* pobranie komunikatu */
  1008. if ((kom=malloc(sizemax)) == NULL) {
  1009. perror("Nie udalo sie przydzielic pamieci na komunikat\n");
  1010. exit(1);
  1011. }
  1012. if ((res = mq_receive(mq, kom, sizemax, &prio))==-1)
  1013. perror("Blad odczytu z kolejki");
  1014. else printf("Otrzymano komunikat: %d\n", (int) *kom);
  1015. free(kom);
  1016. // zamkniecie kolejki
  1017. mq_close(mq);
  1018. // usuniecie kolejki
  1019. if (res=mq_unlink(mq_name) !=-1 )
  1020. printf("Usunalem kolejke: %s\n", mq_name);
  1021. }
  1022.  
  1023. #include <stdio.h>
  1024. #include <stdlib.h>
  1025. #include <semaphore.h>
  1026.  
  1027. int main(int argc, char *argv[]) {
  1028.  
  1029. sem_t *sem;
  1030. int wartosc;
  1031. char *sem_name="/jerzy";
  1032.  
  1033. // otwarcie semafora
  1034. sem = sem_open(sem_name, 0);
  1035. // zwiekszenie wartosci semafora o jeden
  1036. sem_post(sem);
  1037. /* pobranie wartosci semafora */
  1038. sem_getvalue(sem , &wartosc);
  1039. printf("wartosc: %d\n", wartosc);
  1040. /* zamkniecie semafora */
  1041. sem_close(sem);
  1042. //sem_unlink(sem_name);
  1043. exit(0);
  1044. }
  1045.  
  1046. #include <stdio.h>
  1047. #include <stdlib.h>
  1048. #include <sys/fcntl.h>
  1049. #include <semaphore.h>
  1050.  
  1051. /* przyklad blokowania semafora */
  1052. int main(int argc, char *argv[]) {
  1053.  
  1054. sem_t *sem;
  1055. int wartosc;
  1056. char *sem_name="/jerzy";
  1057.  
  1058. // utworzenie semafora
  1059. sem = sem_open(sem_name, O_RDWR | O_CREAT, 0644, 0);
  1060. // oczekiwanie na zwiekszenie wartosci semafora
  1061. sem_wait(sem);
  1062. // pobranie wartosci semafora
  1063. sem_getvalue(sem, &wartosc);
  1064. printf("Wartosc: %d\n", wartosc);
  1065. // zamkniecie semafora
  1066. sem_close(sem);
  1067. exit(0);
  1068. }
  1069.  
  1070. #include <stdlib.h>
  1071. #include <stdio.h>
  1072. #include <sys/fcntl.h>
  1073. #include <sys/mman.h>
  1074. #include <sys/types.h>
  1075. #include <sys/stat.h>
  1076.  
  1077. int main(int argc,char *argv[]) {
  1078.  
  1079. int fd, i;
  1080. char *wsk;
  1081. char *shm_name="/jerzy";
  1082. size_t shm_length=1024;
  1083.  
  1084. // utworzenie i otwarcie pamieci wspolnej
  1085. fd = shm_open(shm_name, O_RDWR | O_CREAT , 0644);
  1086. ftruncate(fd, shm_length);
  1087. // odwzorowanie pamieci w obszar pamieci procesu
  1088. wsk = mmap(NULL, shm_length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  1089. // zapis do pamieci
  1090. for(i=0; i<shm_length; i++)
  1091. *wsk++ = i % 256;
  1092. //shm_unlink(shm_name); //usuniecie pamieci wspolnej
  1093. exit(0);
  1094. }
  1095.  
  1096. #include <stdlib.h>
  1097. #include <stdio.h>
  1098. #include <sys/fcntl.h>
  1099. #include <sys/mman.h>
  1100.  
  1101. int main(int argc,char * argv[]) {
  1102.  
  1103. int fd, mflagi, i;
  1104. struct stat shm_status;
  1105. unsigned char *wsk, c;
  1106. char *shm_name="/jerzy";
  1107.  
  1108. // otwarcie pamieci wspolnej
  1109. fd = shm_open(shm_name, O_RDONLY , 0644);
  1110. // pobranie informacji o pamieci wspolnej
  1111. fstat(fd, &shm_status);
  1112. // odwzorowanie pamieci fd w obszar pamieci procesu
  1113. wsk = mmap(NULL, shm_status.st_size, PROT_READ, MAP_SHARED, fd, 0);
  1114. //printf("Wielkosc pamieci: %d", shm_status.st_size);
  1115. // odczyt z pamieci
  1116. for(i=0; i< shm_status.st_size; i++) {
  1117. if ( (c = *wsk++) != (i % 256))
  1118. perror("Blad: ");
  1119. else printf("%c-", c%128);
  1120. }
  1121. //usuniecie pamieci wspolnej
  1122. if (i=shm_unlink(shm_name) !=-1 )
  1123. printf("Usunalem pamiec wspolna: %s\n", shm_name);
  1124.  
  1125. exit(0);
  1126. }
  1127.  
  1128. #include <sys/types.h>
  1129. #include <sys/socket.h>
  1130. #include <netinet/in.h>
  1131. #include <arpa/inet.h>
  1132. #include <stdio.h>
  1133. #include <stdlib.h>
  1134.  
  1135. main() {
  1136. int gniazdo_k, dlugosc_s, res;
  1137. struct sockaddr_in adres_serwera;
  1138.  
  1139. gniazdo_k=socket(AF_INET, SOCK_STREAM, 0);
  1140. adres_serwera.sin_family=AF_INET;
  1141. adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
  1142. adres_serwera.sin_port=htons(11199); //port serwera
  1143. dlugosc_s=sizeof(adres_serwera);
  1144.  
  1145. //polaczenie z serwerem
  1146.  
  1147. res=connect(gniazdo_k, (struct sockaddr *) &adres_serwera, dlugosc_s);
  1148.  
  1149. if (res==-1) {
  1150. perror("K: Blad polaczenia\n");
  1151. exit(1);
  1152. }
  1153.  
  1154. write(gniazdo_k, "a", 1);
  1155. //sleep(20);
  1156. close(gniazdo_k);
  1157.  
  1158. exit(0);
  1159. }
  1160.  
  1161. #include <sys/types.h>
  1162. #include <sys/socket.h>
  1163. #include <stdio.h>
  1164. #include <stdlib.h>
  1165. #include <netinet/in.h>
  1166. #include <arpa/inet.h>
  1167.  
  1168. void serwer() {
  1169. int gniazdo_s, gniazdo_k, dlugosc_s, dlugosc_k, res;
  1170. struct sockaddr_in adres_serwera, adres_klienta;
  1171.  
  1172. //utworzenie i dowiązanie gniazda
  1173. gniazdo_s=socket(AF_INET, SOCK_STREAM, 0);
  1174. adres_serwera.sin_family=AF_INET;
  1175. adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
  1176. adres_serwera.sin_port=htons(11199); //port serwera
  1177. dlugosc_s=sizeof(adres_serwera);
  1178. res=bind(gniazdo_s, (struct sockaddr *) &adres_serwera, dlugosc_s);
  1179. if (res==-1) {
  1180. perror("S: Blad bind\n");
  1181. exit(1);
  1182. }
  1183. //nasłuchiwanie i obsługa klientów
  1184. listen(gniazdo_s, 5);
  1185. while(1) {
  1186. char linia[255];
  1187. int n;
  1188. printf("S: Czekam na klienta:\n");
  1189. gniazdo_k=accept(gniazdo_s, (struct sockaddr *) &adres_klienta, &dlugosc_k);
  1190. n=read(gniazdo_k, linia, 255);
  1191. linia[n]='\0';
  1192. printf("%s\n", linia);
  1193. close(gniazdo_k);
  1194. }
  1195. }
  1196.  
  1197. main() {
  1198. pid_t pid1;
  1199.  
  1200. if((pid1 = fork()) < 0)
  1201. perror("S: Nie moge stworzyc nowego procesu\n");
  1202. else
  1203. if(pid1 == 0) serwer();
  1204. else printf("S: Serwer dziala w tle\n");
  1205. }
  1206.  
  1207. #include <sys/types.h>
  1208. #include <sys/socket.h>
  1209. #include <sys/un.h>
  1210. #include <stdio.h>
  1211. #include <stdlib.h>
  1212.  
  1213. main() {
  1214. int gniazdo_k, dlugosc_s, res;
  1215. struct sockaddr_un adres_serwera;
  1216. char *nazwa_gniazda="gniazdo_serwera";
  1217.  
  1218. gniazdo_k=socket(AF_UNIX, SOCK_STREAM, 0);
  1219. adres_serwera.sun_family=AF_UNIX;
  1220. strcpy(adres_serwera.sun_path, nazwa_gniazda);
  1221. dlugosc_s=sizeof(adres_serwera);
  1222.  
  1223. //polaczenie z serwerem
  1224.  
  1225. res=connect(gniazdo_k, (struct sockaddr *) &adres_serwera, dlugosc_s);
  1226.  
  1227. if (res==-1) {
  1228. perror("K: Blad polaczenia\n");
  1229. exit(1);
  1230. }
  1231.  
  1232. write(gniazdo_k, "b", 1);
  1233. //sleep(20);
  1234. close(gniazdo_k);
  1235.  
  1236. exit(0);
  1237. }
  1238.  
  1239. #include <sys/types.h>
  1240. #include <sys/socket.h>
  1241. #include <sys/un.h>
  1242. #include <stdio.h>
  1243. #include <stdlib.h>
  1244.  
  1245. void serwer() {
  1246. int gniazdo_s, gniazdo_k, dlugosc_s, dlugosc_k, res;
  1247. struct sockaddr_un adres_serwera, adres_klienta;
  1248. char *nazwa_gniazda="gniazdo_serwera";
  1249.  
  1250. //utworzenie i dowiązanie gniazda
  1251. unlink(nazwa_gniazda);
  1252. gniazdo_s=socket(AF_UNIX, SOCK_STREAM, 0);
  1253. adres_serwera.sun_family=AF_UNIX;
  1254. strcpy(adres_serwera.sun_path, nazwa_gniazda);
  1255. dlugosc_s=sizeof(adres_serwera);
  1256. res=bind(gniazdo_s, (struct sockaddr *) &adres_serwera, dlugosc_s);
  1257. if (res==-1) {
  1258. perror("S: Blad bind\n");
  1259. exit(1);
  1260. }
  1261. //nasłuchiwanie i obsługa klientów
  1262. listen(gniazdo_s, 5);
  1263. while(1) {
  1264. char linia[255];
  1265. int n;
  1266. printf("S: Czekam na klienta:\n");
  1267. gniazdo_k=accept(gniazdo_s, (struct sockaddr *) &adres_klienta, &dlugosc_k);
  1268. n=read(gniazdo_k, linia, 255);
  1269. linia[n]='\0';
  1270. printf("%s\n", linia);
  1271. close(gniazdo_k);
  1272. }
  1273. }
  1274.  
  1275. main() {
  1276. pid_t pid1;
  1277.  
  1278. if((pid1 = fork()) < 0)
  1279. perror("S: Nie moge stworzyc nowego procesu\n");
  1280. else
  1281. if(pid1 == 0) serwer();
  1282. else printf("S: Serwer dziala w tle\n");
  1283. }
  1284.  
  1285. #include <sys/types.h>
  1286. #include <sys/socket.h>
  1287. #include <netinet/in.h>
  1288. #include <arpa/inet.h>
  1289. #include <stdio.h>
  1290. #include <stdlib.h>
  1291.  
  1292. main() {
  1293. int gniazdo_k, dlugosc_s, res;
  1294. struct sockaddr_in adres_serwera;
  1295.  
  1296. gniazdo_k=socket(AF_INET, SOCK_STREAM, 0);
  1297. adres_serwera.sin_family=AF_INET;
  1298. adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
  1299. adres_serwera.sin_port=htons(22299); //port serwera
  1300. dlugosc_s=sizeof(adres_serwera);
  1301.  
  1302. //polaczenie z serwerem
  1303.  
  1304. res=connect(gniazdo_k, (struct sockaddr *) &adres_serwera, dlugosc_s);
  1305.  
  1306. if (res==-1) {
  1307. perror("K: Blad polaczenia\n");
  1308. exit(1);
  1309. }
  1310.  
  1311. write(gniazdo_k, "c", 1);
  1312. //sleep(20);
  1313. close(gniazdo_k);
  1314.  
  1315. exit(0);
  1316. }
  1317.  
  1318. #include <sys/types.h>
  1319. #include <sys/socket.h>
  1320. #include <stdio.h>
  1321. #include <stdlib.h>
  1322. #include <netinet/in.h>
  1323. #include <arpa/inet.h>
  1324.  
  1325. #include <sys/time.h>
  1326. #include <sys/ioctl.h>
  1327.  
  1328.  
  1329. void serwer() {
  1330. int gniazdo_s, gniazdo_k, dlugosc_s, dlugosc_k, res;
  1331. struct sockaddr_in adres_serwera, adres_klienta;
  1332. fd_set odczyt_z_gniazd, deskryptory_gniazd;
  1333.  
  1334. //utworzenie i dowiązanie gniazda
  1335. gniazdo_s=socket(AF_INET, SOCK_STREAM, 0);
  1336. adres_serwera.sin_family=AF_INET;
  1337. adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
  1338. adres_serwera.sin_port=htons(22299); //port serwera
  1339. dlugosc_s=sizeof(adres_serwera);
  1340. res=bind(gniazdo_s, (struct sockaddr *) &adres_serwera, dlugosc_s);
  1341. if (res==-1) {
  1342. perror("S: Blad bind\n");
  1343. exit(1);
  1344. }
  1345. //nasłuchiwanie i obsługa klientów
  1346. listen(gniazdo_s, 5);
  1347. FD_ZERO(&odczyt_z_gniazd); //zerowanie listy deskryptorow
  1348. FD_SET(gniazdo_s, &odczyt_z_gniazd); //dodanie deskryptora gniazda serwera do nasluchu dla select
  1349. while(1) {
  1350. char linia[255];
  1351. int n, l_gniazd, deskr, bajty_oczek;
  1352.  
  1353. deskryptory_gniazd=odczyt_z_gniazd;
  1354.  
  1355. printf("S: Czekam na klienta:\n");
  1356.  
  1357. l_gniazd=select(FD_SETSIZE, &deskryptory_gniazd, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);//wlaczamy naskuch na deskryptorach
  1358. if (l_gniazd < 1) {
  1359. perror("S: blad select");
  1360. exit(2);
  1361. }
  1362.  
  1363. for (deskr=0; deskr< FD_SETSIZE; deskr++) {
  1364. if (FD_ISSET(deskr, &deskryptory_gniazd)) {
  1365. if (deskr == gniazdo_s) { //do serwera zglosil sie nowy klient
  1366. gniazdo_k=accept(gniazdo_s, (struct sockaddr *) &adres_klienta, &dlugosc_k);
  1367. FD_SET(gniazdo_k, &odczyt_z_gniazd); //gniazdo klienta dodane do nasluchu przez select
  1368. printf("S: dodaje deskryptor gniazda klienta do nasluchu select\n");
  1369. }
  1370. else { //klient przyslal dane przez gniazdo
  1371. ioctl(deskr, FIONREAD, &bajty_oczek); //odczyt ile bajtow czeka w buforze do odczytu
  1372. if (bajty_oczek == 0) {
  1373. close(deskr); //zamkniecie gniazda
  1374. FD_CLR(deskr, &odczyt_z_gniazd); //usuniecie deskryptora gniazda z nasluchu select
  1375. printf("S: usuwam deskryptor gniazda klienta do nasluchu select\n");
  1376. }
  1377. else {//jest niezerowa ilosc bajtow do odczytu
  1378. n=read(gniazdo_k, linia, 255);
  1379. linia[n]='\0';
  1380. printf("S: dane od klienta: %s\n", linia);
  1381. }
  1382. }
  1383. }
  1384. }
  1385.  
  1386. //close(gniazdo_k);
  1387. }
  1388. }
  1389.  
  1390. main() {
  1391. pid_t pid1;
  1392.  
  1393. if((pid1 = fork()) < 0)
  1394. perror("S: Nie moge stworzyc nowego procesu\n");
  1395. else
  1396. if(pid1 == 0) serwer();
  1397. else printf("S: Serwer dziala w tle\n");
  1398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement