151 lines
4 KiB
Text
151 lines
4 KiB
Text
__________________
|
|
|
|
LAB 03 QUESTIONS
|
|
__________________
|
|
|
|
|
|
- Name: Michael Zhang
|
|
- NetID: zhan4854
|
|
|
|
Answer the questions below according to the lab specification. Write
|
|
your answers directly in this text file and submit it to complete Lab01.
|
|
|
|
|
|
PROBLEM 1 `wait-fork.c'
|
|
=======================
|
|
|
|
A
|
|
~
|
|
|
|
Compile and run the program in `wait-loop.c'. Does the loop in the
|
|
program ever end? How must one "exit" the program?
|
|
|
|
The program doesn't end, since there's a while loop that continuously asks the
|
|
user for input, without any option that makes it exit. The only way to "exit"
|
|
the program is to terminate it either through sending the signal using ^C, or
|
|
else opening another terminal window and killing the process by id.
|
|
|
|
|
|
B
|
|
~
|
|
|
|
Alter the code so that when the child has completed execution, the
|
|
program breaks out of the loop and the program ends normally. Paste in
|
|
the code you used for this.
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
int main(void) {
|
|
// Make sure to compile sleep_print first:
|
|
// gcc -o sleep_print sleep_print.c
|
|
char *child_argv[] = {"./sleep_print", "5", "CHILD: Awake and Done", NULL};
|
|
pid_t child_pid = fork();
|
|
if (child_pid == 0) {
|
|
execvp(child_argv[0], child_argv);
|
|
perror("errno indicates");
|
|
printf("Couldn't run '%s': is it compiled??\n", child_argv[0]);
|
|
exit(1);
|
|
}
|
|
|
|
while (1) {
|
|
printf("Type text then press enter to check on child:\n");
|
|
char buffer[1024];
|
|
char *input = fgets(buffer, 1024, stdin);
|
|
input[strlen(input) - 1] = '\0'; // Eliminate newline in buffer
|
|
printf("Entered text: '%s'\n", input);
|
|
|
|
printf("Waiting\n");
|
|
int status;
|
|
pid_t pid = waitpid(child_pid, &status, 0);
|
|
if (pid == child_pid) {
|
|
printf("CHILD FINISHED: ");
|
|
printf("\n");
|
|
break;
|
|
} else {
|
|
printf("Child not finished: wait returned %d\n", pid);
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("Exiting wait-loop\n");
|
|
return 0;
|
|
}
|
|
|
|
|
|
C
|
|
~
|
|
|
|
Adjust the code so that the exit status of the child process is
|
|
printed when it finishes. Make sure to use the macros
|
|
`WIFEXITED(status)' and `WEXITSTATUS(status)' to deal with the
|
|
`status' set by `waitpid()'. Paste the code you added for your
|
|
answer.
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
printf("CHILD FINISHED: %d", WEXITSTATUS(status));
|
|
printf("\n");
|
|
break;
|
|
}
|
|
|
|
D
|
|
~
|
|
|
|
Make changes so that if the user types in the string `quit', the
|
|
program exits immediately without waiting further for the
|
|
child. Example:
|
|
,----
|
|
| > a.out
|
|
| Type text then press enter to check on child:
|
|
| quit
|
|
| Entered text: 'quit'
|
|
| Quitting
|
|
| Exiting wait-loop
|
|
| > CHILD: Awake and Done
|
|
`----
|
|
Note that the child eventually prints output to the screen which is
|
|
fine.
|
|
|
|
You will need to check the user input using the `strncmp()'
|
|
function. Do some research on this function as it will prove generally
|
|
useful.
|
|
|
|
Paste the code you used below.
|
|
|
|
|
|
E
|
|
~
|
|
|
|
The current call to `waitpid()' blocks, pausing execution of the
|
|
parent process until the child finishes. Look up the option to pass to
|
|
`waitpid()' that will allow it to do a non-blocking wait which returns
|
|
immediately if the child is not finished. A sample behavior is below.
|
|
,----
|
|
| > a.out
|
|
| Type text then press enter to check on child:
|
|
| stuff
|
|
| Entered text: 'stuff'
|
|
| Waiting
|
|
| Child not finished: wait returned 0
|
|
|
|
|
| Type text then press enter to check on child:
|
|
| more stuff
|
|
| Entered text: 'more stuff'
|
|
| Waiting
|
|
| Child not finished: wait returned 0
|
|
|
|
|
| Type text then press enter to check on child:
|
|
| CHILD: Awake and Done
|
|
| Looks like you're finally up
|
|
| Entered text: 'Looks like you're finally up'
|
|
| Waiting
|
|
| CHILD FINISHED: Exit status 5
|
|
| Exiting wait-loop
|
|
| >
|
|
`----
|
|
|
|
Paste your entire code for `wait-loop.c' below.
|