HW 7: Process Exit and Creation
Instructions
You must turn in a sheet of paper that is neatly typed or written answering the questions below. (You are strongly encouraged to type your homework.)
You can use the following tex file to get started with preparing your homework.
- This homework is graded out of 100 points. Point values are associated to each question.
Questions
- (10 points) What is the difference between
_exit()
andexit()
and_Exit()
? - (5 points) When a process successfully returns from
main()
, which of the three different exit calls is actually used? What exit value is typically used for the process when it returns frommain()
and why? - (5 points) What is the difference between unbuffered, line buffered, and fully buffered with respect to output streams?
- (20 points) Consider the following program snippets. What are the outputs of
each? Explain your answer!
int main(){ fprintf(stdout, "Hello World!"); return 0; }
int main(){ fprintf(stdout, "Hello World!"); exit(0); }
int main(){ fprintf(stdout, "Hello World!"); _Exit(0); }
int main(){ fprintf(stderr, "Hello World!"); _exit(0); }
(10 point) Why does the following code snippet properly check for a failed call to
execv()
?int main(){ char * ls_args[2] = { "/bin/ls", NULL} ; execv( ls_args[0], ls_args); perror("execve failed"); exit(1); //failure }
(10 points) Consider setting up an
argv
array to be passed to execv() for the execution of following command:ls –l –a /bin /usr/bin Fill in
Complete the
argv
deceleration in codechar * argv[] = { /* what goes here? */ } ;
- (5 points) The
fork()
system call is the only function that returns twice. Explain why this is? (5 points) If you were to compile and run the following program in the shell, which process'es
pid
would print to the screen? Explainint main(){ printf("Parent pid: %d\n", getppid()); }
- (5 points) The
wait()
system call will return when a child's status change of a child. What is the most typical status change that would make the system call return? - (15 points) Using the manual page, provide a brief description of each of
the status macros below:
WIFEXITED()
WIFEXITSTATUS()
WIFSIGNALED()
(10 points) Assume you were writing a program that checked if a file existed by using
ls
. (This is a silly way to do this, but just for the sake of argument)Recall that
ls
returns an exit status of 2 when the file does not exist and it cannot list it, andls
returns an exit status of 0 when the file does exist and can be listed. Complete thewait()
portion of the program below. The output should be EXISTS! if the file specified inargv[1]
exists and DOES NOT EXIST! If the file specified inargv[1]
does not exist.( hint: actually try and complete the program on your computer )
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/wait.h> #include <sys/types.h> int main(int argc, char * argv[]){ pid_t cid; char * ls_args[] = {"ls", NULL, NULL}; if(argc == 2){ ls_args[1] = argv[1]; } cid = fork(); if( cid == 0 ){ /*child*/ execvp(ls_args[0],ls_args); exit(1); /*error*/ } /*parent*/ int status; wait(&status); /* FINISH THIS PROGRA */ }