Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/neutrino.h>
  5. #include <process.h>
  6. #include <ctype.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <sched.h>
  10. #include <process.h>
  11. #include <sys/wait.h>
  12. #include <sys/iomsg.h>
  13.  
  14. #define PR_POTOMNY "./klient"
  15. #define SIZE 256
  16.  
  17.  
  18. typedef struct {
  19. int typ; // Typ komunikatu
  20. int od; // Numer procesu
  21. char tekst[SIZE]; // Tekst komunikatu
  22. } kom_t;
  23.  
  24.  
  25. int main(int argc, char* argv[]) {
  26. int chid, pid, rcvid;
  27. int status;
  28. kom_t msg;
  29.  
  30. int il_proc_potomnych = atoi(argv[1]);
  31.  
  32. chid = ChannelCreate(0);
  33. pid = getpid();
  34. printf("Server's pid: %d, chid: %d\n", pid, chid);
  35.  
  36. int i = 0;
  37. for(i = 0; i < il_proc_potomnych; i++) {
  38. int pr_potomny_pid = fork();
  39. if (!pr_potomny_pid) {
  40. char pid_chars[20];
  41. char chid_chars[20];
  42. int base = 10;
  43.  
  44. itoa(pid, pid_chars, base);
  45. itoa(chid, chid_chars, base);
  46. execl (PR_POTOMNY, PR_POTOMNY, pid_chars, chid_chars, (char *)NULL);
  47. }
  48. }
  49.  
  50. while(1) {
  51. rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
  52. if(msg.typ == 0) {
  53. printf("\t Proces %d wyslal zakonczenie\n", msg.od);
  54. status = MsgReply(rcvid, EOK, &msg, sizeof(msg) );
  55. il_proc_potomnych--;
  56. if(il_proc_potomnych == 0) {
  57. break;
  58. }
  59. continue;
  60. }
  61. printf("%s", msg.tekst);
  62.  
  63. msg.od = getpid();
  64. msg.typ = 1;
  65.  
  66. status = MsgReply(rcvid, EOK, &msg, sizeof(msg) );
  67.  
  68. }
  69.  
  70. printf("po petli\n");
  71.  
  72. while((pid=wait(&status))!=-1) {
  73. printf("Proces %d zakonczony, status %d\n",pid,WEXITSTATUS(status));
  74. }
  75.  
  76. return 0;
  77. }
  78.  
  79. ////////////////////////////////////////////////////////////////////////////////////////////////
  80. ///////////////////////////////////////////////////////////////////////////////////////////////
  81.  
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <sys/neutrino.h>
  86. #include <sys/netmgr.h>
  87. #include <process.h>
  88. #include <sys/wait.h>
  89. #include <sys/iomsg.h>
  90.  
  91. #define SIZE 256
  92.  
  93. typedef struct {
  94. int typ; // Typ komunikatu
  95. int od; // Numer procesu
  96. char tekst[SIZE]; // Tekst komunikatu
  97. } kom_t;
  98.  
  99. int main(int argc, char* argv[])
  100. {
  101. int coid, server_pid, server_chid;
  102. kom_t msg;
  103. kom_t reply;
  104.  
  105. if(argc != 3) {
  106. printf("Server Pid\n");
  107. printf("Server Chid\n");
  108. exit(EXIT_FAILURE);
  109. }
  110.  
  111. server_pid = atoi(argv[1]);
  112. server_chid = atoi(argv[2]);
  113.  
  114. printf("Server pid: %d, chid %d\n", server_pid, server_chid);
  115. coid = ConnectAttach(ND_LOCAL_NODE, server_pid, server_chid, _NTO_SIDE_CHANNEL, 0);
  116.  
  117. int i = 0;
  118. while(i++ < 10) {
  119. msg.typ = 1;
  120. msg.od = getpid();
  121. sprintf(msg.tekst, "Proces %d, krok petli %d\n", getpid(), i);
  122.  
  123. MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
  124. sleep(1);
  125. }
  126.  
  127. msg.typ = 0;
  128. MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
  129.  
  130. exit(EXIT_SUCCESS);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement