#include #include #include #include #include #include /* / Eliasz Zurawka / Sept 29/07 / PROG40000_Fall2007_Asg1 / Question 2 */ int main(int argc, char **argv) { if(argc != 2) { printf("usage: elFork filename"); } char *outputPath = argv[1]; int output; if((output = open(outputPath, O_RDONLY)) == -1) { printf("Could not open %s\n", outputPath); exit(2); } pid_t pid1, pid2; int status1, status2; if((pid1 = fork())< 0) { printf("error forking, exiting"); exit(1); } if(pid1 == 0) { system("ps"); } else { pid2 = fork(); if(pid2 == 0) { char readBuf[10]; memset(readBuf, 0, 10); ssize_t r1 = read(output, readBuf, 9); printf("\n\n Child Read: %s", readBuf); } else { char readBuf[10]; memset(readBuf, 0, 10); ssize_t r1 = read(output, readBuf, 9); printf("\n\nParent Read: %s", readBuf); while((waitpid(pid1, &status1, WNOHANG) == 0) || (waitpid(pid2, &status2, WNOHANG) == 0)) { //do Nothing } r1 = read(output, readBuf, 9); printf("\n\nParent Read: %s", readBuf); exit(0); } } exit(0); }