Operating Systems 2020W: Assignment 1
Jump to navigation
Jump to search
Please submit the answers to the following questions via CULearn by 2:30 PM on February 7, 2020. There are 20 points in 8 questions.
Submit your answers as a gzipped tarball “
The tarball you submit must contain the following:
1. A plaintext file containing your solutions to all questions, including explanations. Further details are provided below.
2. A README.txt file listing the contents of your submission as well as any commands the TAs should use to compile your code.
3. The source code for your modified version of 3000run. This file should include all required changes.
No other formats will be accepted. Submitting in another format will likely result in your assignment not being graded and you receiving no marks for this assignment. In particular do not submit an MS Word, OpenOffice, or PDF file as your answers document!
Empty or corrupted tarballs may be given a grade of zero, so please double check your submission by downloading and extracting it.
Don’t forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources. You do not need to list help from the instructor, TA, or information found in the textbook.
Contents
1
Contents of Plaintext File
2
Creating a Gzipped Tarball
3
Background
4
Questions
5
Code
Contents of Plaintext File
The plaintext file you submit with your assignment must include a detailed explanation of your reasoning for all questions.
For coding questions, explain the changes you made to the original file, including why and how these changes work. Your explanations should make reference to the source file you include with your submission. You may wish to include diffs against the original code to augment your explanation. Your grade for coding questions will be based partly on the provided explanation, and partly on whether your code compiles, runs, and does what it is supposed to do.
For all questions, answers should be detailed and reflect an understanding of course material.
Creating a Gzipped Tarball
You can create gzipped tarballs on the course VM with the command tar cvzf dest.tar.gz source.
For example, if my name was John Doe and I had an assignment folder named johndoe-assignment1, I could create a gzipped tarball of that folder as follows:
tar cvzf johndoe-comp3000-assign1.tar.gz johndoe-assignment1
Before submitting your assignment, you should test to see that your gzipped tarball actually has the correct contents. Following the above John Doe example, I can extract my tarball by running tar xvzf johndoe-comp3000-assign1.tar.gz and then inspect the contents.
Background
The questions in this assignment are based on 3000run.c. Its code is listed below. 3000run takes a program binary (full pathname), a filename for standard input, and a filename for standard output. It then runs the specified binary in a new process and waits for it to finish, outputting the results of execution.
For example, to run ls, with standard input coming from file A and standard output coming from file B, do the following:
./3000run /bin/ls A B
(Note that the current version does not implement redirecting standard input.)
Questions
1. [1] Complete support for redirecting standard input.
2. [1] To run nano with standard in and out being set to A and B, we can type “./3000run /bin/nano A B” in a terminal. What should A and B be so that nano is run and interacts properly with the current terminal?
3. [1] How does envp[] in main() compare to environ in child()? Be precise. How did you compare them?
4. [5] Which library function calls in 3000run.c generate one or more system calls? List each library call and what system call (if any) it produces (once main() starts and until main() exits). Note the library calls are as follows: close, creat, dup2, execve, exit, fork, fprintf, printf, puts, snprintf, wait
5. [2] Add support for running binaries without specifying the full pathname. Implement only using code and methods shown in class (lecture or tutorial) except for any execve variants (don’t change the call to execve). Other solutions will not be accepted.
6. [2] Add support for reporting when the child terminates with a signal and the signal number that caused the termination. (Note that processes with custom signal handlers may terminate normally using exit, so be sure to test with a program that actually terminates with a signal.)
7. [2] Add support for command line arguments. For example, to run ls -l /tmp with input from A and output to B, you would type “./3000run /bin/ls A B -l /tmp”.
8. [6] Change the parent process so that it only allows the child to run for three seconds before sending it a SIGTERM, as follows:
◦ [1] Replace the call to wait in parent() with a sleep for three seconds.
◦ [2] Call wait in a signal handler for SIGCHLD. This signal handler stores result_status and result_pid in global variables. It also sets a global flag child_exited to 1 (this flag defaults to 0).
◦ [2] If the child has not exited after the sleep (as indicated by child_exited), send the child a SIGTERM signal, then sleep for another three seconds.
◦ [1] The parent terminates as before, conditionally on result_pid (but it is now global, not local).
Code
1
/* 3000run.c */
2
3
#include
4
#include
5
#include
6
#include
7
#include
8
#include
9
#include
10
11
int parent(pid_t child_pid, char *command, char *input_fn, char *output_fn)
12
{
13
int result_status;
14
pid_t result_pid;
15
int count = 0;
16
const int bufsize = 256;
17
char buf[bufsize];
18
19
result_pid = wait(&result_status);
20
21
if (result_pid == child_pid) {
22
count += snprintf(buf + count, bufsize,
23
“Child %d terminated”, child_pid);
24
if (WIFEXITED(result_status)) {
25
count += snprintf(buf + count, bufsize,
26
” normally with status %d”,
27
WEXITSTATUS(result_status));
28
}
29
30
puts(buf);
31
return 0;
32
} else if (result_pid == -1) {
33
printf(“No child to wait for.\n”);
34
return -1;
35
} else {
36
printf(“wait returned unknown value %d\n”, result_pid);
37
return result_pid;
38
}
39
}
40
41
int child(char *command, char *input_fn, char *output_fn)
42
{
43
extern char **environ;
44
char *argv[2];
45
int fd;
46
47
argv[0] = command;
48
argv[1] = NULL;
49
50
close(1);
51
fd = creat(output_fn, 0644);
52
if (fd == -1) {
53
fprintf(stderr, “Could not create %s for output.\n”,
54
output_fn);
55
exit(-3);
56
}
57
dup2(fd, 1);
58
59
execve(command, argv, environ);
60
fprintf(stderr, “execve of %s failed.\n”, command);
61
return(-1);
62
}
63
64
int main(int argc, char *argv[], char *envp[])
65
{
66
char *command;
67
char *input_fn;
68
char *output_fn;
69
70
pid_t pid;
71
72
if (argc < 4) {
73
fprintf(stderr,
74
"Usage: %s
Navigation menu
Log in
Page
Discussion
Read
View source
View history
Search
Main page
OS 2020W
Tools
What links here
Related changes
Special pages
Printable version
Permanent link
Page information
This page was last edited on 3 February 2020, at 10:23.
Content is available under GNU Free Documentation License 1.2 unless otherwise noted.
Privacy policy
About Soma-notes
Disclaimers