0% found this document useful (0 votes)
13 views44 pages

Processes Threads

The document provides an overview of processes and threads in operating systems, detailing their definitions, models, hierarchies, creation, termination, and states. It discusses the benefits of multithreading, including responsiveness and resource sharing, as well as various implementation methods such as user space and kernel space. Additionally, it covers the complexities and advantages of hybrid implementations of threads, emphasizing their role in enhancing system performance and efficiency.

Uploaded by

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

Processes Threads

The document provides an overview of processes and threads in operating systems, detailing their definitions, models, hierarchies, creation, termination, and states. It discusses the benefits of multithreading, including responsiveness and resource sharing, as well as various implementation methods such as user space and kernel space. Additionally, it covers the complexities and advantages of hybrid implementations of threads, emphasizing their role in enhancing system performance and efficiency.

Uploaded by

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

Processes & Threads

Processes

Kiều Trọng Khánh


Review
• Terminology
– Kernel mode, User mode
– Latency time
• Switch user mode  kernel mode
• Switch CPU a process to other process together
• System Calls
Review
• Process:
– A program in execution
– A program loaded into memory and executing
– Associated with each process is set of resources such as
executable code, data, stack, CPU registers value, PC, and
other information needing to run a program
– Associated with each process is its address space (i.e., all
memory locations that the process can read and write)
• Process table (array or linked list) stores all the
information of processes
• A process hierarchy (tree)

Tanenbaum, Fig. 1-13.


Objectives
• Processes
– Definition
– The process model
– Process creation
– Process hierarchies
– Process Termination
– Process States
– Transition States
– Implementation of Processes
– Degree of multiprogramming
Objectives…
• Threads
– Overview
– Models
– Benefit
– Implementing threads in User Space
– Implementing threads in the Kernels
– Hybrid Implementations
– Scheduler Activations
– Pop-Up threads
– Making Single Threaded Code Multithreaded
Processes
Definition
• OS abstraction to support the ability to have (pseudo)
concurrent operation even when there is only one CPU
available
− An OS consists of a collection of processes: By switching the
CPU between processes, the OS can make the computer more
productive
• An instance of an executing program (PCB), including
the current values of PC, registers, variables, etc.
− A process is not a program on the disk (this is just a file).
• Compare with a cake recipe (program) and the activity involving reading
the recipe, fetching ingredients, and baking the cake! (process)
− A process is a sequential stream of execution in its own
address space
Processes
The process model
• Early computers allowed only one program to be
executed at a time. (quantum or time slice)
• The program is a static (inactive) entity, whereas the
process is an active one
• A process is an activity of some kind
• A process has a program, input, output, and a state
• There are two parts of a process
– sequential execution: no concurrency inside a process;
everything happens sequentially (There is only one CPU and
one physical program counter)
– process state: everything that process interacts with (registers,
memory, files, etc)
Example

Tanenbaum, Fig. 2-1.


Processes
The process model (cont)
• Single-processor systems
– Pseudo-parallelism
• Multiprogramming
– Switching among processes (The CPU switches back and forth
from process to process)
• A single processor may be shared among several processes
– Scheduling algorithm
• Context switch
– Switching the CPU to another process requires saving the state
of the old process and loading the save state for the new process
– Context switch time is pure overhead because the system does not
useful work while switching. It‘s varies depending on the
hardware machine
– Has become a performance bottle neck
Processes
Process Creation
• In a general-purpose OS, the principal events that may cause
process creation are:
– System initialization
– Execution of a process creation system call by a running process
– A user request to create a new process
– Initiation of a batch job
• Automatically by the system
– At system initialization
– Foreground (application) and background (services) processes
• By another process
– A system call for process creation
– Situations
• A process needs help doing some computation
• A user action – interaction with the shell
• In general, a process (parent process) may create new processes
(children) via a create-process system call:
– fork() and execve() in UNIX
– CreateProcess in Win32
Processes
Process Hierarchies
• Process hierarchy
– Parent–Child Relationship
– The child can itself create new processes
– Tree of processes
• In UNIX OS
– A process creates another process, the parent process and child
process continue to be associated in certain ways
– A process and all of its children and further descendants
together form a process group
• In Windows OS
– All processes are equal (has no concept of process hierarchy)
– The parent is given a special token (called a handle) that it can
use to control the child
Processes
Process Termination
• After a process has been created, it may terminate
usually due to one of the following:
– Normal exit (task accomplished) (voluntary)
– Error exit (voluntary)
• Ex: inexistent files, insufficient or incorrect input
– Fatal error (involuntary)
• Ex: illegal instructions, division by zero etc.
– Killed by another process (involuntary)
• kill system call in Unix, or TerminateProcess in Win32.(in
some systems, if the parent terminates)
• Voluntary – using a special system call
• Involuntary – receiving an interruption
Processes
Process States
• Running
– Using the CPU at that instant (executed by the CPU) (or
Instructions are being executed)
• Ready
– Runnable; temporarily stopped to let another process run; but
the CPU available (or The process is waiting to be assigned to a
process)
• Blocked
– Unable to run until some external event happens (or The
process is waiting for some event to occur)
• New (optional)
– Waiting for same resources to be allocated (or The process is
being created)
• Terminated (optional)
– Keeping same information about the exit state (or The process
has finished execution)
Processes
Transition States
• New to Ready or Running
• Ready to Running (dispatch)
– its turn comes again
New Terminated
– selected by the scheduler (optional) (optional)

• Running to Ready (interrupt)


– suspended by the scheduler Running
– time slice expired
• Running to Blocked (block) Blocked Ready
– wait for some event to occur
• Blocked to Ready (ready)
– the awaited event occurs
• Running to Terminated (exit)
Processes
Transition States – Example
Processes
Transition States – Example
Processes
Transition States – Example
Processes
Implementation of Processes
• OS maintains a process table
– each process is allocate an entry – PCB (Process Control Block)
(Where process is located)
– used as a repository when the process is suspended or blocked
• A PCB contains all the information, that needed (varies
from system to system) so that a process can be restarted
as if it had never been stopped, such as
– Process ID
– process state (new, ready, running …)
– the value of the CPU registers (PC, Stack Pointer, other register)
– CPU scheduling information (process priority etc.)
– memory management information (pointers etc.)
– accounting information (the amount of CPU and real time used, time limits
etc.)
– I/O status information (outstanding I/O req., I/O devices allocated, list of
open files etc.)
– Process parent Id, process group ...
Processes
Implementation of Processes (cont)
• A PCB block
Processes
Implementation of Processes (cont)
• Example
Processes
Implementation of Processes (cont)
• Interrupt handling and scheduling are summarized

Tanenbaum, Fig. 2-5.


Processes
Degree of multiprogramming
• Shows the CPU utilization
CPU utilization = 1 - pn
– p: time waiting for I/O to complete
– n: number of processes
• Example
– A computer has 512MB of memory, with OS taking
128 MB and each user program also taking up 128MB
with an 80% average I/O wait
• CPU utilization = 1 – 80%((512-128)/128) = 49%
– Adding another 512MB,
• CPU utilization = 1 – 80%((1024-128)/128) = 79%
Threads
Context
• Each process has an address space
• The CPU is allocated only one process at one time
• Context switching
• Problems
– First, (in Network Services)
• We want to search something using the Google Web
• Our request is transferred to web server that is busy with serving many
client concurrently
→ So, the server can serve only one client at a time
– Second, (in Word processor)
• We uses the word processor to type the document
• The word processor supports some of features as automatically saving the
entire file in every 5 minutes and display the graphics. Besides, the user
read, he/she types on the keyboards,
→ So, when the automatically saving is executed, the reading or display
can be not progressed
Threads
Overview
• It is desirable to have multiple threads of control in the
same address space running in quasi-parallel, as though
they were separate processes
Threads
Single and Multithreaded Processes

PC

PC PC PC

25
Threads
Models
• Threads of one process (mini-process)
– Describe an sequential execution within a process
– Share the same address space and resources of the process
– Each thread has its own PC, registers and stack of execution
– There is no protection between threads in one process
– Lightweight processes (contains some properties of processes)
– Have its own stack
– Multithreading (multiple threads in same process)
• Having multiple threads running concurrently within a
process is analogous to having multiple processes running
in parallel in one computer
Threads
Model (cont)

Tanenbaum, Fig. 2-11, 2-13.


Three processes each with one thread One process with three threads
Multiprogramming Multithreading
Threads
Model – Example
Threads
Model – Example
Threads
Benefits
• Responsiveness and better resource sharing
– A program may continue running even if part of it is blocked.
– The application’s performance may improve since we can overlap I/O and
CPU computation.
• Economy:
– Allocating memory and resources for process creation (faster, easier) is costly.
– Thread creation may be up to 100 times faster! (Takes less time to create a new
thread than a process)
• Useful on systems with multiple CPUs.
• Less time to terminate a thread than a process
• Less time to switch between two threads within the same process
(serve many task with the same purpose)
• Since threads within the same process share memory and files, they
can communicate with each other without invoking the kernel
• But, they introduce a number of complications:
– E.g., since they share data, one thread may read and another may write the same
location – care is needed!
Threads
Multithreading
• Operating system supports multiple threads of
execution within a single process
• MS-DOS supports a single thread
• UNIX supports multiple user processes but only
supports one thread per process
• Windows 2000, Solaris, Linux, Mach, and OS/2
support multiple threads
• Multithread is effective in multiprocessors
because the thread can execute in concurrently
• …
Threads
Implementing Threads in User Space
• The kernel knows nothing about threads
– The approach is suitable for OS that does not support threads
– Threads are implemented by a user-level library (with code and data structure)
• The threads run on top of a runtime system (which is a collection of
procedures that manage threads)
• Each process has its own thread table
• Advantages
– Thread switching and scheduling is faster (because it’s done at user mode)
than to trapping the kernel mode
– Each process can have its own customized scheduling algorithm
– Scale better (can vary process table space and thread stack space in flexibility)
• Disadvantages
– The implementation of blocking system calls is complexity → instead of
blocking thread, the process is blocked
– The need that a thread voluntarily gives up the CPU → The OS doesn’t know
this, so if any user-level thread is blocked, the entire process is blocked
– The developer want threads precisely in applications → make system call
constantly
Threads
Implementing Threads in User Space (cont)

Tanenbaum, Fig. 2-16.


Threads
Implementing Threads in User Space (cont)
• Many to one model
– Thread management is done in user space.
– When thread makes a blocking system call, the entire process will be blocked.
Only one thread can access the Kernel at a time, so multiple threads are
unable to run in parallel on multiprocessors

Operating System - Mul


ti-Threading (tutorialsp
oint.com)
Threads
Implementing Threads in User Space (cont)

Cooperative scheduling
Implementing threads :: Operating systems 2018 (uu.se)
Threads
Implementing Threads in User Space (cont)

Preemptive scheduling
Implementing threads :: Operating systems 2018 (uu.se)
Threads
Implementing Threads in the Kernel
• The kernel knows about the threads and manage the
threads (no run-time system is needed)
• The kernel schedules all the threads
• The kernel has a thread table (using kernel call to create
or destroy thread)
• Advantages
– The kernel can switch between threads belonging to different
processes
– No problem with blocking system calls
– Useful if multiprocessor support is available (multiple CPUs)
• Disadvantages
– Greater cost (time and resources to manage threads create and
terminate) → Solution: recycling
– Thread creation, saving is slow (needs system call)
Threads
Implementing Threads in the Kernel (cont)

Tanenbaum, Fig. 2-16.


Threads
Implementing Threads in the Kernel (cont)
• One to one model
– Allows another thread to run when a thread makes a blocking system call.
– It supports multiple threads to execute in parallel on microprocessors.

Operating System - Multi-Threading (tutorialspoint.com)


Threads
Libraries
• There are 3 primitive libraries
• POSIX Pthreads.
– May be provided as either a user-level or kernel-level
library.
• Win32 threads.
– Kernel-level library, available on Windows systems.
• Java threads.
– JVM is running on top of a host operating system, the
implementation depends on the host system.
• On Windows systems, Java threads are
implemented using the Win32 API;
• UNIX-based systems often use Pthreads.
Threads
Hybrid Implementations
• Combine the advantages of user-level threads with
kernel-level threads
– Using kernel-level threads and then multiplex user-level
threads onto some or all of the kernel threads (ultimate in
flexibility)

Tanenbaum, Fig. 2-17.


Threads
Hybrid Implementations
• Many to many model
– precisely can create as many user threads as necessary and the
corresponding Kernel threads can run in parallel on a multiprocessor
machine.
– It is the best accuracy on concurrency and when a thread performs a
blocking system call, the kernel can schedule another thread for execution.

Operating System - M
ulti-Threading (tutorial
spoint.com)
Summary
• Processes
• Threads

Q&A
Next Lecture
• Threads Implementation
• InterProcess Communication

You might also like