/* * CSE 190, spring 2000. Created Apr 10, 2000, bsy. * * Assignment 1. Run this program interactively. Observe its * behavior. Run this program with input redirected from a file. * Observe its behavior. * * Based on your observations and -a priori- knowledge of Unix, come * up with a theory that explains all that you observed. Use your * theory to predict what would happen if our experiment did not * include the first call to fgets(3). * * Your writeup should include your observations, your theory, and * additional candidate experiments to test the various aspects of * your theory. Can these experiments falsify your theory, i.e., * could their outcome force you to abandon or revise your theory? */ #include #include #include #include #include #define LINESIZE 1024 int main(void) { char linebuf[LINESIZE]; int pid, status; printf("Before fork, please enter a line of text: "); if (!fgets(linebuf,sizeof linebuf,stdin)) { fprintf(stderr,"Got EOF\n"); return 1; } printf("Got(parent,1):\n\"%s\"\n",linebuf); switch (pid = fork()) { case 0: printf("I'm the child. Please enter another line: "); if (!fgets(linebuf,sizeof linebuf,stdin)) { fprintf(stderr,"Got EOF child fgets\n"); return 1; } printf("Got(child):\n\"%s\"\n",linebuf); return 0; case -1: fprintf(stderr,"fork failed, \"%s\"; errno: %d\n",strerror(errno),errno); return 1; default: printf("I'm the parent of %d; waiting.\n",pid); /* * suppose the child returned vital information * as exit status. is this secure? */ (void) wait(&status); printf("Child exited with status %d\n",status); printf("Parent. Please enter another line of text\n"); if (!fgets(linebuf,sizeof linebuf,stdin)) { fprintf(stderr,"Got EOF parent 2nd fgets\n"); return 1; } printf("Got(parent,2):\n\"%s\"\n",linebuf); } return 0; }