Using the program shown in Figure 3.30, explain what the output will be at LINE A.
#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> int value = 5; int main(void) { pid_t pid; pid = fork(); if (pid == 0) { value += 15; return(0); } else if (pid > 0) { wait(NULL); printf("PARENT: value = %d\n", value); /* Line A */ return(0); } }
Copy and paste this program into a C file on compsci. Compile it and run it 3 or 4 times. What will the following program print? Explain the output that was produced or make your best guess as to why you got this output.
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { pid_t child_pid; printf("The main program process ID is %d\n", (int)getpid()); child_pid = fork(); if (child_pid != 0) { printf("This is the parent process, with id %d\n", (int) getpid()); printf("The child\'s process ID is %d\n", (int) child_pid); } else printf("This is the child process, with id %d\n", (int)getpid()); return(0); }