Guest User

Untitled

a guest
Jan 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int msg[2];
  8. const int MAX = 100;
  9.  
  10. int main(int argc, char *argv[]) {
  11.  
  12. int master, i, child, num, pid, ppid, counter, loops;
  13. char buffer[MAX];
  14. num = atoi(argv[1]);
  15. loops = atoi(argv[2]);
  16. counter = 0;
  17.  
  18. master = (int)getpid();
  19.  
  20. //check num arguments
  21. if (argc != 4) {
  22. fprintf(stderr, "%sn", "incorrect # arguments");
  23. }
  24. pipe(msg); //create pipe
  25. dup2(msg[0], STDIN_FILENO); //duplicate pipes
  26. dup2(msg[1], STDOUT_FILENO);
  27. close(msg[0]); //close ends of pipe
  28. close(msg[1]);
  29.  
  30. //create other processes
  31. for(i=1; i<num; i++) {
  32. pipe(msg); //create new pipe
  33.  
  34. //create new process
  35. child = fork(); //parent has child id
  36. pid = (int)getpid(); //has own pid
  37. ppid = (int)getppid(); //has parent pid
  38.  
  39. //if parent, fix output
  40. if(child > 0){
  41. dup2(msg[1], STDOUT_FILENO);
  42. } else {
  43. dup2(msg[0], STDIN_FILENO);
  44. }
  45. close(msg[0]);
  46. close(msg[1]);
  47. if(child){
  48. break;
  49. }
  50. }
  51.  
  52. //simple output
  53. fprintf(stderr, "process %d with id %d and parent id %dn", i, pid, ppid);
  54.  
  55. //message passing
  56. //if master, establish trasnfer
  57. if (pid == master) {
  58. //parent
  59. close(msg[0]); //closes its read end
  60. char buffer[MAX];
  61. fprintf(stderr, "Parent: Waiting for inputn");
  62. while(1) {
  63. scanf("%s", buffer);
  64. if (strcmp(buffer, "exit")==0) {
  65. break;
  66. }
  67. write(msg[1], buffer, MAX);
  68. }
  69. close(msg[1]); //closes it write end
  70. } else {
  71. //child
  72. close(msg[1]); //closes its write end
  73. char buffer[MAX];
  74. fprintf(stderr, "Child: Waiting for pipen");
  75. while(read(msg[0], buffer, MAX) > 0) {
  76. fprintf(stderr, "Received: %sn", buffer);
  77. buffer[0] = '';
  78. }
  79. close(msg[0]); //closes its read end
  80. }
  81.  
  82. //special stuff for master node
  83. if(master == pid){
  84. fprintf(stderr, "%sn", "i am the master");
  85. //special stuff
  86. } else {
  87. fprintf(stderr, "%sn", "i am a child");
  88. //nothing really?
  89. }
  90.  
  91. wait(2); //let all processes finish.
  92. exit(0);
  93. }
  94.  
  95. if (argc != 4) {
  96. ...
  97. }
  98.  
  99. int num = atol(argv[1]);
  100.  
  101. C:+----------+-----------+
  102. | | |
  103. | Read End | Write End |
  104. | | |
  105. +----------+-----------+
  106. |
  107.  
  108.  
  109.  
  110.  
  111. P:+----------+-----------+
  112. | | |
  113. | Read End | Write End |
  114. | | |
  115. +----------+-----------+ |
  116. /| |
  117. | |
  118. +-----------------------+
  119.  
  120. C2:+----------+-----------+
  121. | | |
  122. | Read End | Write End |
  123. | | |
  124. +----------+-----------+
  125. |
  126.  
  127.  
  128.  
  129.  
  130. C1:+----------+-----------+ |
  131. | | | |
  132. | Read End | Write End | |
  133. | | | |
  134. +----------+-----------+ |
  135. | |
  136. |
  137. |
  138. |
  139. |
  140. P:+----------+-----------+ |
  141. | | | |
  142. | Read End | Write End | |
  143. | | | |
  144. +----------+-----------+ |
  145. /| |
  146. | |
  147. +-----------------------+
  148.  
  149. C2:+----------+-----------+
  150. | | |
  151. | Read End | WriteEnd3 |
  152. | | |
  153. +----------+-----------+
  154. |
  155.  
  156.  
  157.  
  158. C1:+----------+-----------+ +----------+-----------+ |
  159. | | | | | | |
  160. | Read End | WriteEnd1 | | Read End | WriteEnd2 | |
  161. | | | | | | |
  162. +----------+-----------+ +----------+-----------+ |
  163. | /| |
  164. | |
  165. +------------------+
  166.  
  167.  
  168. P:+----------+-----------+
  169. | | |
  170. | Read End | Write End |
  171. | | |
  172. +----------+-----------+ |
  173. /| |
  174. | |
  175. +-----------------------+
  176.  
  177. #include <stdio.h>
  178. #include <stdlib.h>
  179. #include <string.h>
  180. #include <unistd.h>
  181.  
  182. #define BUF_LEN 32
  183.  
  184. int input_pipe[2];
  185. int output_pipe[2];
  186.  
  187. char buf[BUF_LEN];
  188. const char msg[] = "hello world.n";
  189. int str_len = sizeof(msg);
  190.  
  191. int status;
  192.  
  193. int main(int argc, char **argv) {
  194. /* master input channel */
  195. int master_input;
  196. int ring_pid = 0;
  197.  
  198. if (argc != 2) {
  199. fprintf(stdout, "Argument error!n");
  200. fflush(stdout);
  201.  
  202. exit(-1);
  203. }
  204.  
  205. int total_processes = atoi(argv[1]);
  206.  
  207. if (pipe(input_pipe) || pipe(output_pipe)) {
  208. fprintf(stdout, "Cannot create pipes at master. Program exits.n");
  209. fflush(stdout);
  210.  
  211. exit(-1);
  212. } else {
  213. /* fork child process */
  214. pid_t pid = fork();
  215. if (pid) {
  216. /* dup read end of input pipe and write end of output pipe. */
  217. int input_fd = dup(input_pipe[0]);
  218. int output_fd = dup(output_pipe[1]);
  219.  
  220. close(input_pipe[0]);
  221. close(input_pipe[1]);
  222. close(output_pipe[0]);
  223. close(output_pipe[1]);
  224.  
  225. /* parent process */
  226. fprintf(stdout, "Send message to downstream: %s", msg);
  227. fflush(stdout);
  228. write(output_fd, msg, str_len + 1);
  229.  
  230. read(input_fd, buf, BUF_LEN);
  231. fprintf(stdout, "Recieve message from upstream: %s", buf);
  232. fflush(stdout);
  233.  
  234. waitpid(pid, &status, 0);
  235. } else {
  236. /* child process */
  237. /* write end of master's input pipe */
  238. master_input = dup(input_pipe[1]);
  239.  
  240. int input_fd = dup(output_pipe[0]);
  241. int output_fd = -1;
  242.  
  243. /* increase ring number */
  244. while (++ring_pid < total_processes) {
  245. int tmp_pipe[2];
  246.  
  247. if (pipe(tmp_pipe)) {
  248. fprintf(stdout, "Cannot create pipes at master. Program exits.n");
  249. fflush(stdout);
  250.  
  251. exit(-1);
  252. } else {
  253. /* pid of sub-process's child */
  254. pid_t spid = fork();
  255. if (spid) {
  256. /* drop read end of the pipe, we read from parent process */
  257. close(tmp_pipe[0]);
  258. /* output of last process directs to master's input */
  259. if (ring_pid == total_processes - 1) {
  260. output_fd = dup(master_input);
  261. } else {
  262. output_fd = dup(tmp_pipe[1]);
  263. }
  264. close(tmp_pipe[1]);
  265. /* receive and send message */
  266. read(input_fd, buf, BUF_LEN);
  267. fprintf(stdout, "Read from upstream: %s", buf);
  268.  
  269. fprintf(stdout, "Write to downstream: %s", buf);
  270. write(output_fd, buf, BUF_LEN);
  271.  
  272. waitpid(spid, &status, 0);
  273. break;
  274. } else {
  275. /* for child process, the input is read end of the new pipe. */
  276. input_fd = dup(tmp_pipe[0]);
  277. close(tmp_pipe[0]);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284.  
  285. child_pid = fork();
  286. if (child_pid==0) {
  287. childProcess(); // isolate the child process logic
  288. _exit(0); // make sure the execution doesn't escape the if statement
  289. }
  290.  
  291. input(stdin)
  292. v
  293. +> A -+
  294. | v
  295. D B
  296. ^ |
  297. +- C <+
  298.  
  299. #define MAXCHILD 50
  300. #define NO_OF_CHILDREN 20
  301. #define MAXBUF 100
  302.  
  303. void child(int readend, int writeend);
  304. int main()
  305. {
  306. int pipefd[MAXCHILD + 1][2];
  307. int noofchildren = 0;
  308. int nchild = NO_OF_CHILDREN;
  309. int ret;
  310. int i ;
  311. char buf[MAXBUF];
  312. for(i = 0; i <= nchild; i++)
  313. {
  314. ret = pipe(pipefd[i]);
  315. if (0 > ret)
  316. {
  317. printf("Pipe Creation failedn");
  318. exit(1);
  319. }
  320. }
  321. printf("Pipes createdn");
  322.  
  323. for(i = 0; i < nchild; i++)
  324. {
  325.  
  326. ret = fork();
  327. if (0 == ret)
  328. {
  329. child(pipefd[i][0], pipefd[i + 1][1]);
  330. }
  331. //These ends are no longer needed in parent.
  332. close (pipefd[i][0]);
  333. close(pipefd[i + 1][1]);
  334. }
  335.  
  336. printf("%d Children Createdn", nchild);
  337.  
  338. while(1)
  339. {
  340. printf("Enter Some datan");
  341. //Now the parent/master reads from the standard input
  342. fgets(buf, MAXBUF, stdin);
  343. //Write to the pipe which is connected to the first child.
  344. write(pipefd[0][1],buf, strlen(buf) + 1);
  345. //Read from the pipe, which is attached to the last child created
  346. read(pipefd[nchild][0], buf, MAXBUF);
  347. printf("Master Read from the pipe - %sn", buf);
  348. if (strcmp(buf, "exitn") == 0)
  349. {
  350. break;
  351. }
  352. }
  353. //By this time all other children, would have exited
  354. //Let's wait to see this
  355. while(1)
  356. {
  357. ret = wait(NULL);
  358. if (0 > ret) //Returns -ve when no more child is left.
  359. {
  360. break;
  361. }
  362. noofchildren++;
  363. }
  364. printf("My %d children terminated! Now I am exiting", noofchildren);
  365. exit(0);
  366. }
  367.  
  368. void child(int readend, int writeend)
  369. {
  370. char buf[MAXBUF];
  371. int ret;
  372. //set up environment
  373. dup2(readend, 0);
  374. dup2(writeend,1);
  375. while(1)
  376. {
  377. ret = read(0, buf, MAXBUF );
  378. if (strcmp(buf, "exitn") == 0)
  379. {
  380. write(1, buf, ret);
  381. exit(0);
  382. }
  383. write(1, buf, ret);
  384. }
  385. }
Add Comment
Please, Sign In to add comment