Advertisement
Guest User

lab 4 so sosoosososososàso

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. Przykład 2
  2.  
  3.  
  4. #include<stdio.h>
  5. #include<sys/ipc.h>
  6. #include<sys/msg.h>
  7. #include<sys/types.h>
  8. struct msgbuf {long type;
  9.  
  10. char mtext[50];
  11. void receive_message(int mqid)
  12.  
  13. struct msgbuf buffer;if(msgrcv(mqid,&buffer,sizeof(buffer.mtext),1,0)<0)
  14.  
  15. }; {
  16.  
  17. else
  18.  
  19. int main(void) {
  20.  
  21. perror("msgrcv");
  22. printf("Odebrany komunikat: %s\n",buffer.mtext);
  23.  
  24. }
  25.  
  26. int key = ftok("/tmp",8);if(key<0)
  27.  
  28. perror("ftok");
  29. int id = msgget(key,0600|IPC_CREAT|IPC_EXCL);if(id<0)
  30.  
  31. perror("msgget");
  32. receive_message(id);
  33. if(msgctl(id,IPC_RMID,0)<0) perror("msgctl");
  34.  
  35. return 0;
  36.  
  37. }
  38.  
  39. Pytanie 2
  40. Zakończone
  41. Nie oceniono
  42. Nie oznaczony flagąOflaguj pytanie
  43. Treść pytania
  44. Przykład 3
  45.  
  46. #include<stdio.h>
  47. #include<string.h>
  48. #include<sys/ipc.h>
  49. #include<sys/msg.h>
  50. #include<sys/types.h>
  51. #define TEXT_LENGTH 50
  52. struct msgbuf {long type;
  53.  
  54. char mtext[TEXT_LENGTH];
  55. void send_message(int mqid, char message[])
  56.  
  57. }; {
  58.  
  59. struct msgbuf buffer;
  60.  
  61. buffer.type = 1; memset(buffer.mtext,0,sizeof(buffer.mtext)); strncpy(buffer.mtext,message,TEXT_LENGTH-1);
  62.  
  63. if(msgsnd(mqid,&buffer,sizeof(buffer.mtext),0)<0) perror("msgsnd");
  64.  
  65. }
  66.  
  67. int main(void) {
  68.  
  69. int key = ftok("/tmp",8);if(key<0)
  70.  
  71. perror("ftok");
  72. int id = msgget(key,0600);if(id<0)
  73.  
  74. perror("msgget"); send_message(id,"Systemy Operacyjne");return 0;
  75.  
  76. }
  77.  
  78. Pytanie 3
  79. Zakończone
  80. Nie oceniono
  81. Nie oznaczony flagąOflaguj pytanie
  82. Treść pytania
  83. Zad 1
  84.  
  85. #include<sys/types.h>
  86.  
  87. #include<sys/ipc.h>
  88.  
  89. #include<sys/msg.h>
  90.  
  91. #include<stdio.h>
  92.  
  93.  
  94.  
  95. struct msqid_ds *buf;//zmienna przez ktora przekazywane sa parametry operacji
  96.  
  97.  
  98.  
  99. struct msgbuf {//struktura komunikatu
  100.  
  101. long mtype;//typ komunikatu
  102.  
  103. char *mtext;//tresc komunikatu
  104.  
  105. };
  106.  
  107.  
  108.  
  109. struct msgbuf bufor1, bufor2;
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. int main()
  118.  
  119. {
  120.  
  121. int id_msgget;//identyfikator kolejki
  122.  
  123. char *wiadomosc = "message ...";//tresc komunikat
  124.  
  125. id_msgget = msgget(IPC_PRIVATE, IPC_CREAT|0600);
  126.  
  127. printf("Id msgget %d\n", id_msgget);
  128.  
  129.  
  130.  
  131. bufor1.mtype = 1;//typ komunikatu
  132.  
  133. bufor1.mtext = wiadomosc;//tresc komunikatu
  134.  
  135.  
  136.  
  137. int rozmiar = sizeof(struct msgbuf) - sizeof(bufor1.mtype);//rozmiar komunikatu
  138.  
  139.  
  140.  
  141. int wysylanie = msgsnd (id_msgget, &bufor1, rozmiar, 0);//wyslanie komunikatu
  142.  
  143. if (wysylanie == -1)
  144.  
  145. perror("Blad wyslania");
  146.  
  147.  
  148.  
  149. msgrcv(id_msgget, &bufor2, rozmiar, 1, 0);//odebranie komunikatu
  150.  
  151. printf("Odebrany komunikat: %s\n", bufor2.mtext);
  152.  
  153.  
  154.  
  155. msgctl(id_msgget, IPC_RMID, buf);//usuniecie kolejki
  156.  
  157. return0;
  158.  
  159. }
  160.  
  161.  
  162.  
  163. Pytanie 4
  164. Zakończone
  165. Nie oceniono
  166. Nie oznaczony flagąOflaguj pytanie
  167. Treść pytania
  168. Zad 2
  169.  
  170.  
  171. #include<sys/types.h>
  172.  
  173. #include<sys/ipc.h>
  174.  
  175. #include<sys/msg.h>
  176.  
  177. #include<stdio.h>
  178.  
  179. #include <unistd.h>
  180.  
  181. #include <stdlib.h>
  182.  
  183. #include <sys/wait.h>
  184.  
  185.  
  186.  
  187. struct msqid_ds *buf;//zmienna przez ktora przekazywane sa parametry operacji
  188.  
  189.  
  190.  
  191. struct msgbuf {//struktura komunikatu
  192.  
  193. long mtype;//typ komunikatu
  194.  
  195. char *mtext;//tresc komunikatu
  196.  
  197. };
  198.  
  199.  
  200.  
  201. struct msgbuf bufor1, bufor2;
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. int main()
  210.  
  211. {
  212.  
  213. int id_msgget;//identyfikator kolejki
  214.  
  215. char *wiadomosc = "message ...";//tresc komunikat
  216.  
  217. int rozmiar = sizeof(struct msgbuf) - sizeof(bufor1.mtype);//rozmiar komunikatu
  218.  
  219. pid_t pid;
  220.  
  221.  
  222.  
  223. id_msgget = msgget(IPC_PRIVATE, IPC_CREAT|0600);
  224.  
  225.  
  226.  
  227. if ((pid = fork()) == 0) {//potomek
  228.  
  229. printf("Id msgget %d\n", id_msgget);
  230.  
  231.  
  232.  
  233. bufor1.mtype = 1;//typ komunikatu
  234.  
  235. bufor1.mtext = wiadomosc;//tresc komunikatu
  236.  
  237.  
  238.  
  239. int wysylanie = msgsnd (id_msgget, &bufor1, rozmiar, 0);//wyslanie komunikatu
  240.  
  241. if (wysylanie == -1)
  242.  
  243. perror("Blad wyslania");
  244.  
  245. exit(0);
  246.  
  247. }
  248.  
  249. else {//rodzic
  250.  
  251. wait(0);
  252.  
  253. msgrcv(id_msgget, &bufor2, rozmiar, 1, 0);//odebranie komunikatu
  254.  
  255. printf("Odebrany komunikat: %s\n", bufor2.mtext);
  256.  
  257.  
  258.  
  259. msgctl(id_msgget, IPC_RMID, buf);//usuniecie kolejki
  260.  
  261.  
  262.  
  263. return0;
  264.  
  265. }
  266.  
  267. }
  268.  
  269. Pytanie 5
  270. Zakończone
  271. Nie oceniono
  272. Nie oznaczony flagąOflaguj pytanie
  273. Treść pytania
  274. Zad 3
  275.  
  276. #include<sys/types.h>
  277.  
  278. #include<sys/ipc.h>
  279.  
  280. #include<sys/msg.h>
  281.  
  282. #include<stdio.h>
  283.  
  284.  
  285.  
  286. struct msgbuf {//struktura komunikatu
  287.  
  288. long mtype;//typ komunikatu
  289.  
  290. int mnumber;//tresc komunikatu
  291.  
  292. };
  293.  
  294.  
  295.  
  296. struct msgbuf bufor;
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304. int main()
  305.  
  306. {
  307.  
  308. int id_msgget, id_ftok;//identyfikator kolejki
  309.  
  310.  
  311.  
  312. id_ftok = ftok("zad3a.c",120);
  313.  
  314. id_msgget = msgget(id_ftok, IPC_CREAT|0660);
  315.  
  316.  
  317.  
  318. printf("Id msgget %d\n", id_msgget);
  319.  
  320.  
  321.  
  322. bufor.mtype = 1;//typ komunikatu
  323.  
  324. bufor.mnumber = 5;//tresc komunikatu
  325.  
  326. int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);//rozmiar komunikatu
  327.  
  328.  
  329.  
  330. int wyslanie = msgsnd (id_msgget, &bufor, rozmiar, 0);//wyslanie komunikatu
  331.  
  332. if (wyslanie == -1)
  333.  
  334. perror("Blad wyslania");
  335.  
  336. printf("Wyslano komunikat: %d\n", bufor.mnumber);
  337.  
  338.  
  339.  
  340. return0;
  341.  
  342. }
  343.  
  344.  
  345.  
  346. Pytanie 6
  347. Zakończone
  348. Nie oceniono
  349. Nie oznaczony flagąOflaguj pytanie
  350. Treść pytania
  351. Zad 4
  352.  
  353. #include<sys/types.h>
  354.  
  355. #include<sys/ipc.h>
  356.  
  357. #include<sys/msg.h>
  358.  
  359. #include<stdio.h>
  360.  
  361.  
  362.  
  363. struct msgbuf {//struktura komunikatu
  364.  
  365. long mtype;//typ komunikatu
  366.  
  367. int mnumber;//tresc komunikatu
  368.  
  369. };
  370.  
  371.  
  372.  
  373. struct msgbuf bufor;
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381. int main()
  382.  
  383. {
  384.  
  385. int id_msgget, id_ftok;//identyfikator kolejki
  386.  
  387. int i, wyslanie;
  388.  
  389.  
  390.  
  391. id_ftok = ftok("zad3a.c", 269);
  392.  
  393. id_msgget = msgget(id_ftok, IPC_CREAT|0660);
  394.  
  395.  
  396.  
  397. printf("Id msgget %d\n", id_msgget);
  398.  
  399.  
  400.  
  401. bufor.mtype = 1;//typ komunikatu
  402.  
  403. bufor.mnumber = 5;//tresc komunikatu
  404.  
  405. int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);//rozmiar komunikatu
  406.  
  407. for(i = 0; i < 10000; i++) {
  408.  
  409. wyslanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT);//wyslanie komunikatu
  410.  
  411. ++bufor.mnumber;
  412.  
  413. }
  414.  
  415. if (wyslanie == -1)
  416.  
  417. perror("Blad wyslania");
  418.  
  419. printf("Wyslano komunikat: %d\n", bufor.mnumber);
  420.  
  421.  
  422.  
  423. return0;
  424.  
  425. }
  426.  
  427.  
  428.  
  429. Pytanie 7
  430. Zakończone
  431. Nie oceniono
  432. Nie oznaczony flagąOflaguj pytanie
  433. Treść pytania
  434. Zad 5
  435.  
  436. #include <stdlib.h>
  437.  
  438. #include <time.h>
  439.  
  440. #include<sys/types.h>
  441.  
  442. #include<sys/ipc.h>
  443.  
  444. #include<sys/msg.h>
  445.  
  446. #include<stdio.h>
  447.  
  448.  
  449.  
  450. struct msgbuf {//struktura komunikatu
  451.  
  452. long mtype;//typ komunikatu
  453.  
  454. int mnumber;//tresc komunikatu
  455.  
  456. };
  457.  
  458.  
  459.  
  460. struct msgbuf bufor;
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468. int main()
  469.  
  470. {
  471.  
  472. srand(time(NULL));
  473.  
  474. int id_msgget, id_ftok;//identyfikator kolejki
  475.  
  476. int i, wyslanie;
  477.  
  478. id_ftok = ftok("zad3a.c",120);
  479.  
  480. id_msgget = msgget(id_ftok, IPC_CREAT|0660);
  481.  
  482.  
  483.  
  484. printf("Id msgget %d\n", id_msgget);
  485.  
  486.  
  487.  
  488. int rozmiar = sizeof(struct msgbuf) - sizeof(bufor.mtype);//rozmiar komunikatu
  489.  
  490.  
  491.  
  492. for(i = 1; i < 8; i++) {
  493.  
  494. bufor.mtype = 1+random()%5;
  495.  
  496. bufor.mnumber = bufor.mtype;
  497.  
  498. wyslanie = msgsnd (id_msgget, &bufor, rozmiar, IPC_NOWAIT);//wyslanie komunikatu
  499.  
  500. if (wyslanie == -1)
  501.  
  502. perror("Blad wyslania");
  503.  
  504. printf("Wyslano komunikat: %d\n", bufor.mnumber);
  505.  
  506. }
  507.  
  508.  
  509.  
  510. return0;
  511.  
  512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement