Made some modifications to your program but ain't sure if it cuts it.
Code:#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { pid_t childpid; int count[2]; int fd[2]; int m; pipe(fd); if ((childpid = fork()) == -1) { perror("fork"); exit(1); } else if (childpid == 0) { dup2(fd[0], 0); read(fd[0], count, sizeof count); printf("child process: no. of multiples of 5 are %d" " and those of 3 are %d\n", count[0], count[1]); } else if (childpid > 0) { close(fd[0]); dup2(fd[1], 1); for (m = 0; m < 10001; m++) { if ((m%5) == 0) count[0]++; if ((m%3) == 0) count[1]++; } write(fd[1], count, sizeof count); } return 0; }
Last edited by itCbitC; 03-19-2009 at 06:42 PM.
The outputs I get are "1075122321 and those of 3 are 1073836614". I was wondering if having a larger buffer would make this program work right but I noticed that you took it off. What could be making this number so large? its pretty much the same weird output I was talking about initially.
Is that from the code posted here??
Edit: I get this output "child process: no. of multiples of 5 are 2001 and those of 3 are 3334"
Last edited by itCbitC; 03-19-2009 at 06:49 PM.
Perhaps something got lost in the copy 'n paste. What compiler are you using? Post the code you're compiling.
This is the code. I'm using PuTTy.Code:#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { pid_t childpid; int count[2]; int fd[2]; int m; pipe(fd); if ((childpid = fork()) == -1) { perror("fork"); exit(1); } else if (childpid == 0) { dup2(fd[0], 0); read(fd[0], count, sizeof count); printf("child process: no. of multiples of 5 are %d" " and those of 3 are %d\n", count[0], count[1]); } else if (childpid > 0) { close(fd[0]); dup2(fd[1], 1); for (m = 0; m < 10001; m++) { if ((m%5) == 0) count[0]++; if ((m%3) == 0) count[1]++; } write(fd[1], count, sizeof count); } return 0; }
PuTTy is not a compiler but a software for terminal emulation.
Oh. I guess I should ask you what you used and try that out.
What platform and OS are you on?
Windows Vista.
Sorry for the double post but can someone tell me why the outputs I get are extremely large for the last code posted?
What command are you issuing to compile your C program, and please learn the difference between a compiler and a terminal emulator.
*PuTTY is not a compiler*
I'm using the gcc command and executing it with ./a.out.