Lecture 6
Lecture 6
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 2
Carnegie Mellon
Processes
Definition: A process is an instance of a program (in
binary format) that is executing
▪ Not the same as “program”: there can be several instances of
the same program running
Memory
Two key properties ensured by the OS: Stack
Heap
Data
▪ “Logical” control flow Code
Each process seems to have exclusive use of the CPU incl.
▪
its registers CPU
▪ Virtual memory Registers
▪ Each process seems to have exclusive use of main
memory.
executes
Each process executes for some time and then is preempted (=temporarily
suspended) while other processes take their turns (multitasking). Each process
seems to have exclusive use of the processor.
user code
user code
PROCESS CONTROL:
THE ROLE OF THE OS
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All
rightsandreserved.
Bryant O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13
Carnegie Mellon
Process creation
Process Termination
Conditions which terminate a process:
1. Normal exit (voluntary):
• Returning from the main() routine
• Calling the exit() function (a system call) without an error
code (convention: normal return status is 0, nonzero on
error)
2. Error exit (voluntary)
• Calling the exit() function with error code
3. Fatal exception error (involuntary): illegal instruction (e.g.
versus execution mode), division by zero, etc
4. Killed by another process (involuntary from killed process’
perspective).
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All
rightsandreserved.
Bryant O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15
Carnegie Mellon
Process Creation
When are processes created ?
1. System initialization: e.g., on Unix the first system process init
(systemd in Linux) loads other daemons like cron, sshd
2. Execution by a running process of a system call to create
another process : e.g., creation of a process in a command
shell by the user entering a command Our focus
In UNIX, there is only one system call to create a new process: fork. This
call creates an exact clone of the calling process – except that parent’s
and child’s process have different PIDs
Nb: process creation is slightly different under Windows but same principles apply
Creating Processes
Parent process creates a new running child
process by calling fork()function
fork is confusing because it is called once but returns
twice ! Once in the calling process, and once in the newly
created child process
int fork(void)
▪ Returns 0 to the child process, child’s PID to parent process
▪ Child is almost identical to parent:
▪ Child get an identical (but separate) copy of the parent’s virtual
address space.
▪ Child gets identical copies of the parent’s open file descriptors
▪ Child has a different PID than the parent
fork Example
int main() Call once, return twice
{
pid_t pid;
Concurrent execution
int x = 1; ▪ Can’t predict execution
order of parent and child
pid = fork();
if (pid == 0) { /* Child */ Duplicate but separate
printf("child : x=%d\n", ++x);
exit(0);
address space
} ▪ x has a value of 1 when
fork returns in parent and
/* Parent */
printf("parent: x=%d\n", --x); child
exit(0); ▪ Subsequent changes to x
} fork.c are independent
/* Parent */
printf("parent: x=%d\n", --x);
exit(0);
}
fork.c
void fork5()
{ L2 Bye
printf("L0\n"); printf printf
if (fork() == 0) { L1 Bye
printf("L1\n"); print fork printf
L0 f
Bye
if (fork() == 0) {
printf("L2\n"); printf fork printf
}
}
printf("Bye\n"); Feasible output: Infeasible output:
} forks.c L0 L0
Bye Bye
L1 L1
L2 Bye
Bye Bye
Bye L2
void forks() {
Zombie if (fork() == 0) {
/* Child */
printf("Terminating Child, PID = %d\n", getpid());
Example exit(0);
} else {
printf("Running Parent, PID = %d\n", getpid());
while (1)
; /* Infinite loop */
}
linux> ./forks & } forks.c
[1] 6639
Running Parent, PID = 6639
Terminating Child, PID = 6640
linux> ps
PID TTY TIME CMD
6585 ttyp9 00:00:00 tcsh
6639 ttyp9 00:00:03 forks
ps shows child process as
6640 ttyp9 00:00:00 forks <defunct> “defunct” (i.e., a zombie)
6641 ttyp9 00:00:00 ps
linux> kill 6639
[1] Terminated When killing parent process, the
linux> ps child is reaped by init
PID TTY TIME CMD
6585 ttyp9 00:00:00 tcsh
6642 ttyp9 00:00:00 ps
void forks()
Child may not {
if (fork() == 0) {
terminate /* Child */
printf("Running Child, PID = %d\n",
execve Example
Executes “/bin/ls –lt /usr/include” in current process:
Null-terminated array of pointers to character strings
myargv[3] = NULL
myargv[2] “/usr/include”
myargv[1] “-lt”
myargv myargv[0] “/bin/ls”
envp[n] = NULL
envp[n-1] “PWD=/usr/joe”
…
envp[0] “USER=joe”
envp
…
if (execve(myargv[0], myargv, envp) < 0) {
printf("%s: Command not found.\n", myargv[0]);
exit(1);
}
/* control flow will never reach this point if call to execve is successful */
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31
Carnegie Mellon
execve summary
Recap: programs vs. processes
▪ a program is code + data typically stored in an object (=binary) file
▪ a process is a specific instance of a program in execution
▪ fork runs the same program in a new child process
▪ execve loads and runs a new program in the context of the current process
How to run a different program from the current process without terminating
the current process ? like command shells do
if (fork() == 0) { /* Child */
if (execve( myargv[0], myargv, envp) < 0) { /* replaces child */
printf("%s: Command not found.\n", myargv[0]);
exit(-1);
}
}
Summary
Creating processes
▪ Call fork
▪ One call, two returns
Process completion
▪ Call exit
▪ One call, no return
Reaping and waiting for processes
▪ Call wait (or variant)
Loading and running programs
▪ Call execve (or variant)
▪ One call, (normally) no return
SCHEDULING PROCESSES:
DECIDING THE EXECUTION ORDER
The dispatcher
The dispatcher gives control of the CPU to the process
selected by scheduler; this involves:
1. switching context (e.g., saving/restoring registers)
2. jumping to the proper location in the code to restart that
process (can be considered part of the context switch as
instruction pointer register holds the next address to execute)
Dispatch latency – time it takes for the dispatcher to
interrupt one process and start another
THREADS
What is a thread ?
A “lightweight” process within a process which does not
have its own address space but serves the same purpose of
running activities in parallel
Enables to define multiple flows of control within a process
(a) Three processes each with one thread. (b) One process
with three threads.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 49
Carnegie Mellon
Each thread has its own stack but resides in the same address
space. Processes start with a single thread (the main thread) which
is going to create new threads by calling a function in a library.
Each thread is identified by a unique Id.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 50
Carnegie Mellon
…
stack 1 stack 2
run-time heap
read/write data
Thread 1 context: Thread 2 context:
Data registers Data registers read-only code/data
SP1 SP2 0
PC1 PC2
… …
SP1: stack pointer of thread #1
PC1: program counter of thread #1
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 51
Carnegie Mellon
T5 T3 foo
bar
Disadvantages
▪ No memory protection between threads, one thread can modify the
other threads’ data and kill the entire process
▪ Interaction between threads can be tricky (“What happens if one
thread closes a file while another one is still reading from it?”)
▪ More difficult to know exactly when threads will execute because of
hierarchical scheduling (a process is selected for execution then a
thread within that process)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 55
Carnegie Mellon
Tanenbaum & Bos, Modern Operating Systems: 4th ed., Global Edition (c) 2015 Pearson Education Limited. All
rights
Bryant andreserved.
O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 56
Carnegie Mellon
END OF LECTURE 6