Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main()
- {
- int i;
- pid_t pid;
- int status = 0;
- int fd[2];
- int runningTotal = 0;
- pipe(fd);
- int t;
- int r;
- for (i = 0; i < 10; i++) {
- pid = fork();
- if (pid == 0){
- close(fd[0]);
- t = ChildProcess();
- write(fd[1], &t, sizeof(t));
- exit(0);
- }
- close(fd[1]);
- read(fd[0], &r, sizeof(r));
- runningTotal = runningTotal + r;
- wait(&status);
- }
- printf("%in", runningTotal);
- }
- int ChildProcess() {
- int i;
- int total = 0;
- int r = 0;
- for (i = 0; i < 10000; i++) {
- r = rand() % 10; // 0 to 10
- total = total + r;
- }
- printf("%in", total);
- return total;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- int ChildProcess(void)
- {
- int total = 0;
- srand(time(0) + getpid());
- for (int i = 0; i < 10000; i++)
- {
- int r = rand() % 10; // 0 to 10
- total = total + r;
- }
- printf("%in", total);
- return total;
- }
- int main(void)
- {
- int fd[2];
- pipe(fd); // Missing error check
- for (int i = 0; i < 10; i++) {
- pid_t pid = fork();
- if (pid == 0){
- close(fd[0]);
- int t = ChildProcess();
- write(fd[1], &t, sizeof(t)); // Missing error check?
- exit(0);
- }
- // Print PID here? Error check?
- }
- close(fd[1]);
- int r;
- int runningTotal = 0;
- while (read(fd[0], &r, sizeof(r)) > 0) // Debugging opportunities here
- runningTotal = runningTotal + r;
- while (wait(0) > 0) // Lots of debugging opportunities here
- ;
- printf("%in", runningTotal);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement