Lecture 02 Process
Lecture 02 Process
2
Review Quiz
Which of the following is incorrect about a time sharing OS?
A. Allow multiple processes to run on a single CPU machine
B. Utilize resources more effectively
C. Only utilize CPU more effectively
D. Even suitable for multi-CPU machines
3
Review Quiz
Which of the following is incorrect about a batch OS?
A. A simple type of operating systems
B. It works in first-come-first-served order
C. Allow multiple users to use the system concurrently
D. Not the same as multiprogramming systems
4
Review Quiz
Which of the following devices DOESN’T have an
embedded system?
A. Mp3 player
B. TV
C. Calculator
D. Laptop
5
Process
Materials
§ Textbook:
• A. Silberschatz, P. B. Galvin, and G. Gagne: Operating System Concepts,
10th edition, John Wiley & Sons, 2018.
• Chapter 3
§ Futher reading:
• W. Stallings: Operating Systems: Internals and Design Principles, 9th
edition, Pearson Education Limited, 2018.
• Chapter 3
• A. S. Tanenbaum and H. Bos: Modern Operating Systems, 4th edition,
Pearson Prentice Hall, 2015.
• Chapter 2
7
Contents
§ Process concepts
§ Process scheduling
§ Operations on processes
8
Contents
§ Process concepts
§ Process scheduling
§ Operations on processes
9
Question
What is a process?
A. A file on disk
B. An application
C. A program running on the system
D. A library
10
Process concept
§ An OS executes a variety of programs that run as a process.
§ Process – a program in execution; process execution must
progress in sequential fashion. No parallel execution of
instructions of a single process (single-threaded process).
§ A process has multiple parts:
• The program code, also called text section
• Current activity including program counter (PC), processor registers
• Stack containing temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during running time
11
Process concept (cont.)
§ Program is a passive entity stored on disk (executable
file); process is active
• A program becomes a process when an executable file is loaded
into memory
12
Process in memory
13
Process classification
§ System processes
• Created by system accounts
• Run essential services
§ User processes
• Created by user accounts
• Usually are application processes (Word, Excel, Messenger, etc.)
14
Process information: CPU statistics
15
Process information: memory statistics
16
Memory layout of a C program
17
Process states
As a process executes, it changes state
§ New: the process is being created.
§ Running: the process’s instructions are being executed.
§ Waiting: the process is waiting for some event to occur.
§ Ready: the process is waiting to be assigned to a processor.
§ Terminated: the process has finished execution.
18
Diagram of process state changing
19
Process control block (PCB)
§ Information associated with each process
(also called task control block)
• Process state: running, waiting, ready, etc.
• Program counter (PC): location of instruction to
next execute
• CPU registers: contents of all process-centric
registers
• CPU scheduling information: priorities, scheduling
queue pointers
• Memory-management information: memory
allocated to the process
• Accounting information: CPU used, clock time
elapsed since started, time limits
• I/O status information: I/O devices allocated to
process, list of open files
20
Process representation in Linux kernel
21
CPU and I/O bursts
§ Burst: a time span (duration)
§ Two burst types:
• I/O burst
• CPU burst
22
CPU-bound and I/O-bound
§ CPU-bound processes
• Use CPU a lot (for computation)
§ I/O-bound processes
• Perform I/O a lot (e.g., disk read/write, …)
23
Contents
§ Process concepts
§ Process scheduling
§ Operations on processes
24
Process scheduling
§ Process scheduler selects among available processes for
next execution on CPU core
§ Goal: maximize CPU use, quickly switch processes onto
CPU core
§ Maintains scheduling queues of processes
• Ready queue: set of all processes residing in main memory, ready
and waiting to execute
• Wait queues: set of processes waiting for an event (i.e., I/O)
• Processes migrate among the various queues
25
Ready and wait queues
26
Representation of process scheduling
27
CPU switch from process to process
§ A context switch occurs when the CPU switches from
one process to another
28
Context switch
§ When CPU switches to another process, the system must
save the state of the old process and load the saved
state for the new process via a context switch
§ Context of a process represented in its PCB
§ Context switch time is pure overhead; the system does no
useful work while switching
• The more complex the OS and the PCB ➝ the longer the context
switch
30
Operations on processes
System must provide mechanism for:
§ Process creation
§ Process termination
31
Process creation
§ Parent process creates children processes, which, in turn
create other processes, forming a tree of processes
§ Generally, process identified and managed via a process
identifier (pid)
§ Resource sharing options:
• Parent and children share all resources,
• Children share subset of parent’s resources,
• Parent and children share no resources
§ Execution options:
• Parent and children execute concurrently,
• Parent waits until children terminate to continue
32
Process creation (cont.)
§ Address space
• Child duplicate of parent
• Child has a program loaded into it
§ Unix examples
• fork() system call creates a new process (i.e., a new child)
• exec() system call used after a fork() to replace the (child) process’
memory space with a new program
• Parent process call wait() to wait for the child process to terminate
33
A tree of processes in Linux
34
C program forking a separate (child) process
35
Process termination
§ Process executes last statement and then asks the
operating system to delete it using the exit() system call
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
36
Process termination (cont.)
§ Some operating systems do not allow child to exists if its
parent has terminated. If a process terminates, then all its
children must also be terminated.
• Cascading termination: all children, grand-children, etc. are terminated.
• The termination is initiated by the operating system.
§ The parent process may wait for termination of a child process
by using the wait() system call. The call returns status
information and the pid of the terminated process
pid = wait(&status);
38
Parent and child processes (fork1.c)
39
Question
What will the output of fork1.c look like?
40
fork1.c - output
41
Parent and child processes (fork2.c)
42
Question
What will the output of fork2.c look like?
43
fork2.c - output
44
Parent and child processes (fork3.c)
45
Question
What will the output of fork3.c look like?
46
fork3.c - output
47
Quiz
§ What is the correct relation among application, process,
and program concepts?
A. An application may have multiple processes, a process may have
multiple programs
B. An application only has one program, a program only has one
process
C. An application may have multiple programs, a program may have
multiple processes
D. An application may have many programs, a program only has one
process
48
Summary
§ What is a process
§ Process vs. program vs. application
§ Process structure
§ Process states
§ Process control block (PCB)
§ Types of processes
§ Context switch
§ Process creation and termination
49