CS 350 Operating Systems
Spring 2021
Labs for processes
1 Practice 1: fork()
• Download the source code – $ wget http://www.cs.binghamton.edu/~huilu/teaching/lecture3/cpu-api.tar.gz – $ tar xvf cpu-api.tar.gz – $ cd cpu-api – $ make • Run the first program p1 – $ ./p1 • Read “p1.c” and make sure you understand what the code does
2 Practice 2: wait()
• Run the second program p2 – $ ./p2 • Read “p2.c” and answer the questions: – Why is there a delay for the parent process to print out its message? – What is the return value of the wait()? • Please use “waitpid()” to replace wait()? – Use “man waitpid” command in terminal to check out the usage of waitpid – What is the return value and of waitpid()? – What is the return status of the child process? – Can the parent process wait on other processes (not the child process)? • Try it out and see the result.
3 Practice 3: exec()
• Run the third program p3 – $ ./p3 • Read “p3.c” and answer the questions: – Does “execvp()” return? What is the evidence to back your answer? – In which case, “execvp()” returns? Please make a case if possible.
4 Practice 4: Redirection
• Run the fourth program p4 – $ ./p4 • Read “p4.c” and answer the questions: – Where is the result stored? – Why does this happen?
5 Practice 5: Orphan process
• An orphan process is the one that its parent dies (i.e., finishes its execution) • Please modify “p1” to make such a case – to generate an orphan process – Note you can use the following command to check the parent process id – $ ps -o ppid= -p $pid_of_child_process – Or – $ ps -l – Note that, the orphan process’s parent is systemd with pid of 1 – Make sure you kill the orphan process: – $ killall p1
pid pid of the parent
6 Practice 6: Zombie process
• A zombie process is the one that dies (i.e., finishes its execution) but its parent does not check on it via wait() • Please modify “p1” to make such a case – to generate a zombie process – $ ps -l – Make sure you kill the orphan process: You catch – $ killall p1 the zombie
7 Practice 7: Address space
• Run the fourth program p5 – $ ./p5 • Read “p5.c” and answer the questions: – Why do the parent and child processes print out the different values for the “same” variables?
8 Practice 8
• Write a program that opens a file (with the open() system call) and then calls fork() to create a new process. • Can both the child and parent access the file descriptor returned by open()? • What happens when they are writing to the file concurrently, i.e., at the same time?
9 Practice 1-8
● Try your best to finish practice 1-8 from lab slides (download from MyCourses).
● To check the manual of unknown system call functions, please type “man
● We will discuss answers and solutions in the second half of the lab.
10 Practice 1: fork()
#include
11 Practice 2: wait()
• Run the second program p2 – $ ./p2 • Read “p2.c” and answer the questions: – Why is there a delay for the parent process to print out its message? Wait for the child process and child process sleep for 2 seconds – What is the return value of the wait()? Child process pid • Please use “waitpid()” to replace wait()? – Use “man waitpid” command in terminal to check out the usage of waitpid – What is the return value and of waitpid()? Child process pid – What is the return status of the child process? Stored in wstatus with value 0, means normally terminated – Can the parent process wait on other processes (not the child process)? • Try it out and see the result. No, can only wait for its children
12 Practice 3: exec()
• Run the third program p3 – $ ./p3 • Read “p3.c” and answer the questions: – Does “execvp()” return? What is the evidence to back your answer? No, execvp will replace address space to another program, normally it won’t return.
– In which case, “execvp()” returns? Please make a case if possible. When execvp runs failed, such as the program does not exist.
13 Practice 4: Redirection
• Run the fourth program p4 – $ ./p4 • Read “p4.c” and answer the questions: – Where is the result stored? To the file named “p4.output” – Why does this happen?
write-only, offset: 0
This will point to some other files, in our case, p4.output
14 Practice 5: Orphan process
• An orphan process is the one that its parent dies (i.e., finishes its execution) • Please modify “p1” to make such a case – to generate an orphan process – Note you can use the following command to check the parent process id – $ ps -o ppid= -p $pid_of_child_process – Or – $ ps -l – Note that, the orphan process’s parent is systemd with pid of 1 – Make sure you kill the orphan process: – $ killall p1
pid pid of the parent
15 Practice 6: Zombie process
• A zombie process is the one that dies (i.e., finishes its execution) but its parent does not check on it via wait() • Please modify “p1” to make such a case – to generate a zombie process – $ ps -l – Make sure you kill the orphan process: You catch – $ killall p1 the zombie
16 Practice 7: Address space
• Run the fourth program p5 – $ ./p5 • Read “p5.c” and answer the questions: – Why do the parent and child processes print out the different values for the “same” variables?
17 Practice 8
• Write a program that opens a file (with the open() system call) and then calls fork() to create a new process. • Can both the child and parent access the file descriptor returned by open()? • What happens when they are writing to the file concurrently, i.e., at the same time?
18 References
• Chapter 4&5 of OSTEP book (please read) – http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-intro.pdf – http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-api.pdf
• Man pages for different system calls
– Try “man 2
• E.g. man 2 exec – Syscalls are normally listed in section 2 of the man page
19