0% found this document useful (0 votes)
10 views7 pages

Processes and Threads Ch2

The document provides an overview of processes and threads in operating systems, detailing their states, creation, and termination. It distinguishes between uniprogramming and multiprogramming, explaining how processes are managed and scheduled by the OS. Additionally, it covers the concept of threads, their benefits, and the structure of process and thread control blocks.

Uploaded by

Anthony D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Processes and Threads Ch2

The document provides an overview of processes and threads in operating systems, detailing their states, creation, and termination. It distinguishes between uniprogramming and multiprogramming, explaining how processes are managed and scheduled by the OS. Additionally, it covers the concept of threads, their benefits, and the structure of process and thread control blocks.

Uploaded by

Anthony D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2/28/2019

What is a process
• Informally: program in execution
• Process state
– The program code, also called text section
Processes and Threads – Registers (including PC, SP)
– Stack containing temporary data
– Data section containing global variables
– Heap containing memory dynamically allocated during
run time
– Open file tables
Some slides and/or pictures are adapted from • Key concept: processes are separated: no
• Operating System Concepts, 9th edition, by Silberschatz, Galvin,
Gagne, John Wiley & Sons, 2013 process can directly affect the state of another
• Lecture notes by Dr. Prof. John Kubiatowicz (Berkeley)
1 process. 2

1 2

Process in Memory
Examples of Processes
• Shell: creates a process to execute
command
> ls foo
(shell creates process that executes “ls”)
• When you execute a program you have
just compiled, the OS generates a process
to run the program.
• Your WWW browser is a process.
4

3 4

Uniprogramming vs
Process vs. Program
Multiprogramming
• Program: static • Uniprogramming
• Process: dynamic – Only one process exists at any point
• Example: – Makes designing OS easier
– A user opens two browser windows: same – User can’t do two things at once (MS-DOS)
program, different processes; • Multiprogramming
– Multiple processes exist (only one runs at a
time)
– Requires protection, scheduling, etc

5 6

5 6

1
2/28/2019

The Basic Problem of Concurrency What happens during execution?


Addr 232-1
• The basic problem of concurrency involves R0
resources: … …
– Hardware: single CPU, single DRAM, single I/O
R31
Fetch Data1
F0
devices … Exec Data0
– Multiprogramming: users think they have exclusive F30 Inst237
access to shared resources PC Inst236

• OS has to coordinate all activity
Inst5
– Multiple users, I/O interrupts, … • Execution sequence:
Inst4
– How can it keep all these things straight? – Fetch Instruction at PC Inst3 PC
• Basic Idea: Use Virtual Machine abstraction – Decode Inst2 PC
– Decompose hard problem into simpler ones – Execute (possibly using registers) Inst1 PC
– Abstract the notion of an executing program – Write results to registers/mem Inst0 PC
– Then, worry about multiplexing these abstract – PC = Next Instruction(PC)
Addr 0
machines – Repeat

7 8

How can we give the illusion of multiple


processors? Process State
CPU1 CPU2 CPU3
CPU1 CPU2 CPU3 CPU1 CPU2 • As a process executes, it changes state
Shared Memory Time – new: The process is being created
• Assume a single processor. How do we provide the illusion of – running: Instructions are being executed
multiple processors?
– Multiplex in time! – waiting: The process is waiting for some
• Each virtual “CPU” needs a structure to hold: event to occur
– Program Counter (PC), Stack Pointer (SP)
– Registers (Integer, Floating point, others…?) – ready: The process is waiting to be
• How switch from one CPU to the next?
– Save PC, SP, and registers in current state block assigned to a processor
– Load PC, SP, and registers from new state block
• What triggers switch? – terminated: The process has finished
– Timer, voluntary yield, I/O, other things execution
• Preemptive
– OS runs one process for a while, then takes the CPU away from that
process and lets another process run.

9 10

Diagram of Process State Process Creation


• Parent process create 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 child share no resources
• Execution options
– Parent and children execute concurrently
– Parent waits until children terminate

11 12

2
2/28/2019

A Tree of Processes in Linux More on Processes


init
pid = 1

• To start a process
login
pid = 8415
kthreadd sshd
pid = 3028
– Load code and data (from a file)
pid = 2

– Initialize PC to start of code


bash
pid = 8416
khelper
pid = 6
pdflush
pid = 200
sshd
pid = 3610
• Registers and Stack used while process
runs
ps emacs tcsch
– Both under compiler control
pid = 9204 pid = 4005
pid = 9298

• Files can be opened, closed, read, etc

14

13 14

Creating a Process(Cont.) C Program Forking Separate Process


• Must somehow specify code, data, files, stack, registers
• UNIX examples
– fork() system call creates new process
• Semantics of Unix fork() are that the child process gets a
complete copy of the parent memory and I/O state
– (returns 0 to indicate child process)
• Originally very expensive
• Much less expensive with “copy on write”
– exec() system call used after a fork() to replace the
process’ memory space with a new program

15 16

How many new processes are


What will the program print?
created in the program below?
int main(void)
int main(void)
{
{ char** argv = (char**) malloc(3*sizeof(char*));
argv[0] = "/bin/ls";
for (int i = 0; i < 3; i++) { argv[1] = ".";
pid_t pid = fork(); argv[2] = NULL;
for (int i = 0; i < 10; i++) {
if (pid > 0) printf("%d\n", i);
if (i == 3)
printf("Process id: %d\n", pid);
execv("/bin/ls", argv);
} }
}
}
17 18

17 18

3
2/28/2019

Process Termination
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()) Conditions which terminate processes
– Process’ resources are deallocated by operating system
• Parent may terminate the execution of children 1. Normal exit (voluntary)
processes using the abort() system call. Some
reasons for doing so: 2. Error exit (voluntary)
– Child has exceeded allocated resources 3. Fatal error (involuntary)
– Task assigned to child is no longer required
– The parent is exiting and the operating systems does not 4. Killed by another process (involuntary)
allow a child to continue if its parent terminates

20

19 20

Process Control Block (PCB)


Context Switch
Information associated with each
process
• Switch from running one process to • Process state – running, waiting, etc
running another process • Program counter – location of
instruction to next execute
• Solution: save and restore hardware state • CPU registers – contents of all
on a context switch. process-centric registers
• CPU scheduling information-
• Save the state in Process Control Block priorities, scheduling queue pointers
(PCB) • Memory-management information –
memory allocated to the process
• What is in PCB? • Accounting information – CPU
used, clock time elapsed since start,
time limits
• I/O status information – I/O devices
allocated to process, list of open
files
21

21 22

CPU Switch From Process to Process


Decomposing a Process
• Process: everything needed to run a
program
• Consists of:
– Thread(s)
– Address space

23 24

23 24

4
2/28/2019

Thread Address Space


• Sequential stream of execution • Consists of
• More concretely – Code
– Program counter (PC) – Data
– Register set – Open files
– Stack • Address space can have > 1 thread
• Sometime called lightweight process – threads share code, data, files
– threads have separate stacks, register set

25 26

25 26

Single and Multithreaded Processes


Run-time Stack
A(int x) {
int y = x; y(0)
if (x == 0) return 0; x(0)
else return A(y-1) + 1; ret
}
y(1)
main () {
int z; x(1)
A(1); ret
} z

27

27 28

Threads, Address Spaces Thread Usage (1)

• Threads provide concurrency


– Each thread is sequential, but can have > 1
thread
• Address spaces provide protection
– Don’t want my error to clobber your data

Modern OS’s generally have threads,


address spaces
A word processor with three threads
29 30

29 30

5
2/28/2019

Thread Usage (2) Benefits of multithreaded programming


• Responsiveness
– Ex: a multithreaded web browser could still allow user
interaction in one thread while an image is being
loaded in another thread
• Resource sharing
– Threads share the same address space
• Economy
– More economical to create and context switch threads
• Utilization of multiprocessor architectures
• Simplify Programming
– Having a separate thread for each activity.
Programmer does not have to deal with the complexity
A multithreaded Web server 31
of interleaving multiple activities on the same thread.
32

31 32

Classification Memory Footprint of Two-Thread Example


spaces:
# of addr

• This process has


One Many
# threads – Two sets of CPU registers
Stack 1
Per AS: – Two sets of Stacks
One
MS/DOS, early
Macintosh
Traditional UNIX • Questions:
Stack 2

Address Space
Embedded systems Mach, OS/2, Linux, – How do we position stacks
(Geoworks, VxWorks, Win 95?, Mac OS X, relative to each other?
Many JavaOS,etc)
Win NT to XP, Solaris, – What maximum size should we
JavaOS, Pilot(PC) HP-UX choose for the stacks? Heap
• Real operating systems have either – What happens if threads violate
this? Global Data
– One or many address spaces
– One or many threads per address space – How might you catch violations? Code

33 34

Per Thread State Lifecycle of a Thread (or Process)


• Each Thread has a Thread Control Block (TCB)
– Execution State: CPU registers, program counter,
pointer to stack
– Scheduling info: State (more later), priority, CPU
time
– Accounting Info
– Various Pointers (for implementing scheduling • As a thread executes, it changes state:
queues) – new: The thread is being created
– Pointer to enclosing process? (PCB)? – ready: The thread is waiting to run
– running: Instructions are being executed
– Etc (add stuff as you find a need)
– waiting: Thread waiting for some event to occur
• OS Keeps track of TCBs in protected memory – terminated: The thread has finished execution
– In Array, or Linked List, or … • “Active” threads are represented by their TCBs
– TCBs organized into queues based on their state

35 36

6
2/28/2019

Ready Queue And Various I/O Device Queues Representation of Thread Scheduling
• Thread not running  TCB is in some scheduler queue
– Separate queue for each device/signal/condition
– Each queue can have a different scheduler policy  Queueing diagram represents queues, resources, flows

Ready Head Link Link Link


Queue Tail Registers Registers Registers
Other Other Other
Tape Head State State State
TCB9 TCB6 TCB16
Unit 0 Tail

Disk Head Link Link


Unit 0 Tail Registers Registers
Other Other
Disk Head State State
Unit 2 TCB2 TCB3
Tail
Link
Ether Head
Netwk 0 Registers
Tail Other
State
TCB8

37 38

Scheduler Scheduler Regaining Control


• Decides which thread to run • Internal Events (voluntary)
– From ready list only – ex. Disk request, wait for another thread
• Choose from some algorithms • External Events (involuntary)
• Tries to provide fairness, good – Ex. Timer
performance, quality of service (real time), • Scheduler chooses new thread
or some other metric (depends on system – Now must switch from old thread to new
goals) thread

39 40

39 40

You might also like