Quote:
Originally Posted by p00ndawg
Code:
pid = fork();
pid1 = fork();
|
OK, first off, you've got a total of
four processes here, not three... (Is that what you wanted? For the process I call A to spawn off three children? Or did you want three processes total?)
Process A (original process): pid = B, pid1 = C
Process B (first child of A): pid = 0, pid1 = D
Process C (second child of A): pid = B, pid1 = 0
Process D (child of B): pid = 0, pid1 = 0
Quote:
Code:
if(pid | pid1 == 0)//process A
|
This if statement will actually run for three of the four processes: A, C, and D:
Process A: (pid | pid1 == 0) = (B | C == 0) = (B | 0) = true
Process B: (pid | pid1 == 0) = (0 | D == 0) = (0 | 0) = false
Process C: (pid | pid1 == 0) = (B | 0 == 0) = (B | true) = true
Process D: (pid | pid1 == 0) = (0 | 0 == 0) = (0 | true) = true
(where
true is some nonzero value...)
Quote:
Code:
{
close(p[0]);
write(p[1], message, MSGSIZE);
read(p[1], message, MSGSIZE);
write(p[2], message, MSGSIZE);
}
|
p[2] is uninitialized.
pipe() only creates two file descriptors:
p[0], the read end of the pipe, and
p[1], the write end of the pipe.
Also, from the problem description it sounded like you wanted each of the three communicating processes to have the ability to write messages to and receive messages from each of the other two communicating processes. This suggests you need to create at least three pipes (three invocations of pipe() in the parent process). I say "at least three pipes" because if these processes are running concurrently, allowing two processes to write to the same file descriptor could have unpredictable results. (Using write() to write a single, whole message will probably be safe - but if the message were large it's possible the write would block waiting for the reader to read some data out - in which case I think it's possible the other process's write() could wind up getting in before the first write() finishes its message. (Not sure, though. write() is a system call so the kernel may synchronize it...)
Remember that pipe() creates two file descriptors but
just one pipe. A pipe with an input end and an output end. A file descriptor you get from pipe() is either read-only or write-only - you can never read and write from the same file descriptor, when the file descriptor was obtained with pipe().