Download Understanding Processes in Operating Systems and more Study notes Operating Systems in PDF only on Docsity!
Processes
Indranil Gupta
Lecture 3
Aug 29, 2005
CS423UG Operating Systems
CS 423UG - Operating Systems, Indranil Gupta 2
Overview
“Process”? Hey, what’s that?
What’s in a process?
Processes: pronounced as “process-ees”, rhymes with “armies”; to avoid confusion with “processors”.
CS 423UG - Operating Systems, Indranil Gupta 3
OS: A Reminder
OS
Manages resources
Involves asynchronous and sometimes parallel
activities
But how does it do this?
Through several abstractions
Let’s look at one such abstraction: the Process
CS 423UG - Operating Systems, Indranil Gupta 4
Users, Programs, Processes
Users have accounts on the system
Users write programs, then launch
programs
Different users may launch same program
One user may launch many instances of the
same program
Process:
= an executing program
= a program in action
= a running program
CS 423UG - Operating Systems, Indranil Gupta 5
Analogy
Program: steps for attending the lecture
1. walk to Siebel Center Building
2. enter 1404 Lecture Room
3. find a seat
4. listen and take notes
Process: instance of above program in action
= attending the lecture
Action
Right now, you are in the middle of this process!
You have a “condition” that changes every second!
Different processes from the same program may behave
differently
CS 423UG - Operating Systems, Indranil Gupta 6
An OS runs many processes
simultaneously
Listing all running processes Windows: Task Manager Unix/Linux> “ps –all” PID TTY TIME CMD 1625 pts/6 0:02 tcsh 17246 pts/1 0:35 pine 1272 pts/7 0:00 ps 8865 pts/3 0:01 emacs
Examples of running processes: Command Shell (e.g., tcsh) Editors: pine (also emacs, vim, etc.) Others: Firefox windows (each firefox window is a separate process) And “ps” itself appears as a process!
CS 423UG - Operating Systems, Indranil Gupta 7
Concretely! Can we define the
term “process”?
OK, a process consists of:
1. The program code (does not change, ideally)
2. The Data
3. The Program Counter
4. The Stack
5. The Register values
And anything else that defines the process’ current
condition
CS 423UG - Operating Systems, Indranil Gupta 8
A Process, Pictorially
A process in a machine with 4 registers
PC (hex): 00d
R1 R2 R3 R
CS 423UG - Operating Systems, Indranil Gupta 9
Hmm… how does one “play”
around with a process?
A process is an abstraction for sequence of
operations that implement a computation.
There are 2 types of processes:
OS processes executing system code
User processes executing user code
A user process may be given kernel privilege sometimes (e.g., once it executes a TRAP)
Regardless of type, a process may be manipulated,
suspended, scheduled and terminated
By whom? By the OS, as well as by other processes!
CS 423UG - Operating Systems, Indranil Gupta 10
“New Rules” for Process
Management (with apologies to
Bill Maher):
1. One CPU can execute only one process at a time.
If there are 4 CPU’s in a machine, how many processes
could be executed simultaneously by the machine?
2. The OS manages the CPU’s selection of which
process is the lucky one in (1). The OS may
change its mind as time goes along!
That’s called scheduling – we’ll study it later!
3. The OS may be told by other processes (through
“subtle” system calls) how to make the decision in
Is it a good idea to allow a system call that explicitly
selects which process CPU will run next? Consider two
“colluding” processes that can hog the CPU.
CS 423UG - Operating Systems, Indranil Gupta 11
Process Creation
Processes are created and deleted dynamically (by
the OS or by other processes)
System initialization, e.g., boot or reboot
User request to create a new process
By command line process (type in or double-click)
Initiation of a batch job, e.g., through cron
Execution of a process creation system call, e.g.,
fork() system call in Unix, CreateProcess() in
Windows
OS assigns each process a unique id called pid
(process id). No two processes on same system can
have identical pid’s.
CS 423UG - Operating Systems, Indranil Gupta 12
Forks
int parentpid; int childpid;
if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes / printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }
PC
PID: 837
Suppose the OS is running this, and it executes the fork() instruction
CS 423UG - Operating Systems, Indranil Gupta 19
Process: A Manipulatable
Object
OS manipulates a process as if it is an
object with a condition
The operations that OS performs on the
process change the condition of the
process.
An important component of a process’
condition: its state
CS 423UG - Operating Systems, Indranil Gupta 20
Process State
Possible process states
Running (occupy CPU)
Blocked
Ready (does not occupy CPU)
Other states: suspended, terminated
Notice the transitions between states
Question: in a single processor machine, how
many processes can be in running state?
“state” as in “state of the union”, not as in “state of Idaho”
CS 423UG - Operating Systems, Indranil Gupta 21
The Process Model
4 independent, sequential processes in a 1 CPU machine
Only one program active at any instant
Real life analogy?
You doing homeworks for 4 different courses
(as seen by OS)
(as seen by CPU)
(as seen in memory)
CS 423UG - Operating Systems, Indranil Gupta 22
Well, so I lied. A process’
condition also consists of ...
Process State new, ready, running, waiting, halted; Program Counter the address of the next instruction to be executed for this process; CPU Registers index registers, stack pointers, general purpose registers; CPU Scheduling Information process priority and pointer; Memory Management Information base/limit information; Accounting Information time limits, process number; owner I/O Status Information list of I/O devices allocated to the process;
All present in OS-maintained data structure called PCB=process control block, for each process that has not been terminated
CS 423UG - Operating Systems, Indranil Gupta 23
Process Control Block (PCB)
Fields of a process table entry
e.g.: Linux: struct task_struct (^) CS 423UG - Operating Systems, Indranil Gupta 24
Remember This?
Application
Portable OS Layer
Libraries
Machine-dependent layer
User space/level
Kernel space/level
CS 423UG - Operating Systems, Indranil Gupta 25
Main Memory/
One (common)
approach
Kernel is high memory
User is low memory
What restrictions
apply?
read(f, buf, nbytes)
Main Memory
PID 901
(code,data, stack) PID 902 (code,data, stack)
1GB
Set aside for OS/kernel use only
CS 423UG - Operating Systems, Indranil Gupta 26
Process Address Space
Program segments
Text=static
Data=static data
Heap=dynamic data
Stack=dynamic
Lots of flexibility
Allows stack growth
Allows heap growth
No predetermined division
0xffff….
Kernel space
Stack
Heap
Code & Data
0x0000…
Why is this needed? Remember system call libraries?
PID 901
(code,data, stack)
CS 423UG - Operating Systems, Indranil Gupta 27
Process Scheduling
Objective of multiprogramming – maximal
CPU utilization, i.e., have always a process
running
Objective of time-sharing – switch CPU
among processes frequently enough so that
different users can use the single CPU as if
they had their own
Need Context Switching
CS 423UG - Operating Systems, Indranil Gupta 28
Context Switch
Switch CPU from one process to another
Performed by scheduler (later this week)
It does the following:
1. saves PCB of the old process;
2. loads PCB of the new process;
3. flushes memory cache;
4. change memory mapping (TLB) – more later on this
Context switch is expensive(1-1000 microseconds)
No useful work is done (pure overhead)
Can become a bottleneck
Real life analogy?
Your switching back and forth betw. HW’s for different courses
Needs hardware support
CS 423UG - Operating Systems, Indranil Gupta 29
Process Context Switch
Save PCB-
Reload from PCB-
Save into PCB-
Reload from PCB-
exec
exec
idle
PCB-0 PCB-
idle
exec
idle
Interrupt/system call
Interrupt/ System call
OS jobs
CS 423UG - Operating Systems, Indranil Gupta 30
“Process”? Hey, Ah That’s it!
It’s one executing instance of a “program”
It’s separate from other instances
It can started/manipulated/killed by other processes
It can be started/manipulated/killed by by them
Also covered
Process model, fork, PCB, context switch
This lecture read: 2.
Next lecture: 2.