I am trying to use two child processes fork () and synchronizing them, so that they Can be able to display one and five times infinitely. .
The process is the process of process 1 ... process 2 is the process (pd) ... process 3 is the process (pd) ... process 1 (pid ) ... ...
I know that it can be simplified using 3 pipes, but I was wondering if it would be possible to do it with 2. There is never a chance to run the first process for any reason. Keep in mind that I can only use pipes to solve this problem.
#include & lt; Sys / types.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Unistd.h & gt; Int main () {pid_t p, p1, mypid; Int i; Int a = 1; Int b = 1; Int FD [2]; Int FD1 [2]; Pipe (FD); P = fork (); If (p & lt; 0) {mirror ("fork"); Exit (1); } And if (p == 0) {mypid = getpid (); While (1) {closed (FD [1]); If (FD [0], A, size (int)) == 0 (for (i = 0; i & lt; 5; i ++) {printf ("process 2 is process% d \ n" , Mypid);} closed (FD [0]); A = 1; write (FD [1], and one, size (int))}}} {{P = fork ()); If (p & lt; 0) {mirror ("fork"); Exit (1); } Pipes (FD1); If (P & gt;) {mypid = getpid (); While (1) {closed (FD1 [1]); If (read (fd1 [0], & amp; b, sizeof (int)) == 1) {for (i = 0; i & lt; 5; i ++) {printf ("process 1 process% d \ N ", mypid); } Closed (FD [0]); A = 0; Write (FD [1], and one, size (int)); } Closed (FD [1]); If (FD1 [0], and B, size (int)) == 0) & amp; ((FD [0], A, read the size (int)) = 1) Close (FD1 [0]); B = 0; Write (FD1 [1], and B, size (int)); }}} And {while (1) {closed (FD1 [1]); If (read (FD1 [0], and B, size (int)) == 0} {mypid = getpid (); For (i = 0; i <5; i ++) {printf ("Process 3 process is% d \ n", mypid is); } Pass (FD1 [0]); B = 1; Write (FD1 [1], and B, size (int)); }} Return (1); }
- This is a project for my UNIX class so I really appreciate it if you have pointed me towards instead of solving my mistakes - I think I Pipe concept is misunderstood / read Please tell me that I am missing something and possibly link to source to get more specific details. - There may be some minor mistakes in the program (missing {} etc.).
With a pipe, the two file descriptors connected to each other by pipes when you process a child If the file descriptors are produced, then the file descriptors are copied in the hair process. For the purpose of the IPC, two processes must agree with what process the end of the pipe is. If the parent dies and the end of child 1 ends, or vice versa, it does not matter, the important thing is that they agree. Picture it as a physical pipe connecting it with two rooms If you are in a room, you can put the bag in the pipe or take the baggage, but you can only do it at one end.
In your program, both parents and children are trying to stop the same end of the pipe, and then trying to consume the opposite end of the pipe. Your pipe is a bucket :) To improve it, in your children, stop the end of the pipe that you read / write in parent. In the process of parents, stop the end of the pipe reading / writing in children. I suspect you will get more success.
Comments
Post a Comment