Download Processes - Lecture Slides - Computer Systems Program | CS 201 and more Study notes Computer Science in PDF only on Docsity!
CS 201
Processes
Gerson Robboy
Portland State University
Review
Definition: A Definition: A process process is an instance of a runningis an instance of a running
program. program.
One of the most fundamental concepts in computer science.
Not the same as “program” or “processor”
A A programprogram is a set of instructions and initialized data inis a set of instructions and initialized data in
a file, usually found on a disk. a file, usually found on a disk.
A A processprocess is an instance of that program while it isis an instance of that program while it is
running, along with the state of all the CPU registers running, along with the state of all the CPU registers
and the values of data in memory. and the values of data in memory.
A single program can correspond to many processes; A single program can correspond to many processes;
for example, several users can be running a shell. for example, several users can be running a shell.
Logical Control Flows
Time
Process A Process B Process C
Each process has its own logical control flow
Context Switching
Processes are managed by a shared chunk of OS code Processes are managed by a shared chunk of OS code
called the called the kernel kernel
The kernel is not a separate process, but rather runs as part
of some user process
Control flow passes from one process to another via a Control flow passes from one process to another via a
context switch.context switch.
Process A code Process B code
user code
kernel code
user code
kernel code
user code
Time context switch context switch
How do processes get created?
The The fork() fork() system call creates a new process.system call creates a new process.
Every process is created by another process. Every process is created by another process.
With one exception, the very first process…
fork() creates a duplicate of the process that called it. fork() creates a duplicate of the process that called it.
fork : Creating new processes
int int fork(void)fork(void)
creates a new process (child process) that is
identical to the calling process (parent process)
Fork is called once but returns in two separate
processes.
The processes are identical except for one detail:
fork returns 0 to the child process fork returns the child’s pid to the parent process
Fork
Key Points Key Points
Parent and child both run the same code
Distinguish parent from child by return value from fork
Both processes, after fork, have identical state
Including shared open file descriptors Relative ordering of their print statements undefined The two processes will go their separate ways without synchronizing
This is important: Separate memory spaces.
Fork Example
void fork1() { int x = 1; pid_t pid = fork(); if (pid == 0) { printf("Child has x = %d\n", ++x); } else { printf("Parent has x = %d\n", --x); } printf("Bye from process %d with x = %d\n", getpid(), x); }
What does this program do? What does this program do?
Exercise
void doit()
fork();
fork();
printf(“hello\n");
return;
int main()
doit();
printf(“hello\n”);
exit(0);
What does this program print? What does this program print?
Exercise: What does this program print?
static int j = 0;
do_child(int i)
int pid;
if (i < 2){
pid = fork();
if(pid == -1)
exit(0);
else if(pid == 0){
do_child(i+1);
} else {
j++;
printf("This is process %d, j=%d\n", i, j);
} else {
j++;
printf("This is process %d, j=%d\n", i, j);
main()
do_child(0);
exec : Running new programs
A family of related functions: execv, execp, execl A family of related functions: execv, execp, execl
int int execlexecl(char(char path,path, charchar arg0,arg0, charchar arg1,arg1, ……,, 0)0)
loads and runs executable at path with args arg0 , arg1 , …
path is the complete path of an executable arg0 becomes the name of the process “real” arguments to the executable start with arg1 , etc. list of args is terminated by a (char *)0 argument
Here Here’’s what they all do:s what they all do:
Overwrite the calling process with a new program
Does not create a new process Runs a new program
returns -1 if error, otherwise doesn’t return
Why doesn’t it return?
Example
A program that creates a child process, the child A program that creates a child process, the child
executes executes /usr/bin/ls /usr/bin/ls, and then the parent prints, and then the parent prints
“ “done.done.””
main() {
if (fork() == 0) {
execl("/usr/bin/ls", “ls”, 0);
wait(0); // This is the parent
printf(“done\n”);
exit(0);
wait : Synchronizing with children
int int wait(wait(intint child_status)child_status)
suspends current process until one of its children
terminates
return value is the pid of the child process that terminated
If the child has already terminated, then wait returns its pid
immediately
If child_status != NULL , then the object it points to will
be set to a status indicating why the child process
terminated
wait : Synchronizing with children
void fork9() { int child_status; if (fork() == 0) { printf("HC: hello from child\n"); } else { printf("HP: hello from parent\n"); wait(&child_status); printf("CT: child has terminated\n"); } printf("Bye\n"); exit(); }
HP
HC Bye
CT Bye