0% found this document useful (0 votes)
59 views46 pages

Module-1 1

This document discusses processes, threads, and concurrency in operating systems. It covers key topics like: - Processes have program counters, stacks, and other components represented by process control blocks. Processes transition between states like ready, running, waiting. - Threads allow for parallel execution within a process by sharing the process's resources. User threads are managed in libraries while kernel threads have OS support. - Processes communicate through interprocess communication and synchronize actions using message passing models with mailboxes or direct naming. - Multithreading provides benefits like responsiveness, resource sharing, and utilization of multiprocessors. Various threading models map threads to processes differently.
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)
59 views46 pages

Module-1 1

This document discusses processes, threads, and concurrency in operating systems. It covers key topics like: - Processes have program counters, stacks, and other components represented by process control blocks. Processes transition between states like ready, running, waiting. - Threads allow for parallel execution within a process by sharing the process's resources. User threads are managed in libraries while kernel threads have OS support. - Processes communicate through interprocess communication and synchronize actions using message passing models with mailboxes or direct naming. - Multithreading provides benefits like responsiveness, resource sharing, and utilization of multiprocessors. Various threading models map threads to processes differently.
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/ 46

UNIT 1: PROCESSES AND

THREADS
TOPICS

Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
Introduction to Threads
Single and Multi-threaded processes and its benefits
User and Kernel threads
Multithreading models
PROCESS CONCEPT

Process – a program in execution; process execution must


progress in sequential fashion
A process includes:
⚫ program counter
⚫ stack
⚫ data section
⚫ Text section
⚫ heap
PROCESS STATE

As a process executes, it changes state


⚫ new: The process is being created
⚫ running: Instructions are being executed
⚫ waiting: The process is waiting for some event to occur
⚫ ready: The process is waiting to be assigned to a processr
⚫ terminated: The process has finished execution
⚫ Only one process can be running on any processor at any instant,
although many processes may be ready and waiting
DIAGRAM OF PROCESS STATE
PROCESS CONTROL BLOCK (PCB)
Each process is represented in OS by PCB, also known as TCB
Process state
Program counter
CPU registers (accumulators, index registers, stack pointers, and
general-purpose registers)
CPU scheduling information (includes a process priority, pointers to
scheduling queues)
Memory-management information (value of the base and limit
registers, the page tables, or the segment tables)
Accounting information (amount of CPU and real time used, time
limits, account numbers, job or process numbers)
I/O status information (list of I/O devices allocated to this process, a
list of open files)
PROCESS CONTROL BLOCK (PCB)
CPU SWITCH FROM PROCESS TO
PROCESS
PROCESS SCHEDULING QUEUES
The objective of multiprogramming is to have some process running at
all times, so as to maximize CPU utilization.
The objective of time-sharing is to switch the CPU among processes
so frequently that users can interact with each program while it is
running
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory, ready
and waiting to execute
A ready-queue header contains pointers to the first and final PCBs in
the list
Device queues – set of processes waiting for an I/O device
Each device has its own device queue
Processes migrate among the various queues
READY QUEUE AND VARIOUS I/O DEVICE QUEUES
REPRESENTATION OF PROCESS
SCHEDULING
SCHEDULERS
The operating system must select, processes for scheduling
purposes, from the queues in some fashion
Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU
Some operating systems, such as time-sharing systems, may
introduce an additional, intermediate level of scheduling
ADDITION OF MEDIUM TERM
SCHEDULING
SCHEDULERS (CONT.)
Short-term scheduler is invoked very frequently (milliseconds)
⇒ (must be fast)
Long-term scheduler is invoked very infrequently (seconds,
minutes) ⇒ (may be slow)
Processes can be described as either:
⚫ I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
⚫ CPU-bound process – spends more time doing
computations; few very long CPU bursts
PROCESS CREATION
Parent process create children processes, which, in turn create
other processes, forming a tree of processes
Resource sharing
⚫ Parent and children share all resources
⚫ Children share subset of parent’s resources
⚫ Parent and child share no resources
Execution
⚫ Parent and children execute concurrently
⚫ Parent waits until children terminate
PROCESS CREATION (CONT.)
Address space
⚫ Child duplicate of parent
⚫ Child has a program loaded into it
UNIX examples
⚫ fork system call creates new process
⚫ exec system call used after a fork to replace the process’
memory space with a new program
PROCESS CREATION
A TREE OF PROCESSES ON A TYPICAL
SOLARIS
PROCESS TERMINATION
Process executes last statement and asks the operating system to delete
it (exit)
⚫ Output data from child to parent (via wait)
⚫ Process’ resources are deallocated by operating system
Parent may terminate execution of children processes (abort)
⚫ Child has exceeded allocated resources
⚫ Task assigned to child is no longer required
⚫ If parent is exiting
Some operating system do not allow child to continue if its
parent terminates
All children terminated - cascading termination
COOPERATING PROCESSES
Independent process cannot affect or be affected by the execution of
another process
Cooperating process can affect or be affected by the execution of
another process (any process that shares data with other processes)
Advantages of process cooperation
⚫ Information sharing (concurrent access to same resources)
⚫ Computation speed-up (If we want a particular task to run faster,
we must break it into subtasks, each of which will be executing in
parallel)
⚫ Modularity (dividing the system functions into separate processes
or threads)
⚫ Convenience (user may have many tasks on which to work at one
time)
COMMUNICATIONS MODELS
MESSAGE-PASSING SYSTEM
Mechanism for processes to communicate and to synchronize
their actions
Naming: IPC facility provides two operations:
⚫ send(message) – message size fixed or variable
⚫ receive(message)
Processes that want to communicate must have a way to refer
to each other.
They can use either direct or indirect communication
Direct Communication
Processes must name each other explicitly:
⚫ send (P, message) – send a message to process P
⚫ receive(Q, message) – receive a message from process Q
Indirect Communication
Messages are directed and received from mailboxes (also
referred to as ports)
⚫ Each mailbox has a unique id
⚫ Processes can communicate only if they share a mailbox
INDIRECT COMMUNICATION
Operations
⚫ create a new mailbox
⚫ send and receive messages through mailbox
⚫ destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
SYNCHRONIZATION
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
⚫ Blocking send has the sender block until the message is received
⚫ Blocking receive has the receiver block until a message is
available
Non-blocking is considered asynchronous
⚫ Non-blocking send has the sender send the message and continue
⚫ Non-blocking receive has the receiver receive a valid message or
null
BUFFERING
messages exchanged by communicating processes reside in a
temporary queue
Queue of messages attached to the link; implemented in one of
three ways
1. Zero capacity – 0 messages
Sender must wait for receiver
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits
DIFFERENCE BETWEEN PROCESS AND
THREAD

Process Thread
Process is heavy weight. light weight taking lesser
resources than a process.
Process switching needs
interaction with operating Thread switching does not
system. need to interact with operating
system.
Multiple processes without
using threads use more Multiple threaded processes
resources. use fewer resources.
If one process is blocked While one thread is blocked
then no other process can another thread can run.
execute until the first One thread can read, write or
process is unblocked. change another thread's data.
In multiple processes each
process operates
independently of the others.
SINGLE AND MULTITHREADED
PROCESSES
BENEFITS OF MULTITHREADED
PROGRAMMING

Responsiveness (allow a program to continue running even


if part of it is blocked or is performing a lengthy operation )
Resource Sharing (threads share the memory and the
resources of the process to which they belong)
Economy (Allocating memory and resources for process
creation is costly)
Utilization of multiprocessor Architectures (each thread
may be running in parallel on a different processor)
USER LEVEL THREADS
Thread management done by user-level threads library
Fast to create and manage
supports for thread creation, scheduling, and management with
no support from the kernel
Three primary thread libraries:
⚫ POSIX Pthreads
⚫ Win32 threads
⚫ Java threads
ADVANTAGES

Thread switching does not require Kernel mode


privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level
thread.
User level threads are fast to create and manage
More efficient
DISADVANTAGES

In a typical operating system, most system calls are


blocking.
There is a lack of coordination between threads and
operating system kernel
KERNEL LEVEL THREADS
Supported by the Kernel
Kernel performs thread creation, scheduling, and management
in kernel space
Kernel threads are generally slower to create and manage than
are user threads
Examples
⚫ Windows XP/2000
⚫ Solaris
⚫ Linux
⚫ Tru64 UNIX
⚫ Mac OS X
ADVANTAGES

Kernel can simultaneously schedule multiple threads


from the same process on multiple processes.
If one thread in a process is blocked, the Kernel can
schedule another thread of the same process.
DISADVANTAGES

Kernel threads are generally slower to create and


manage than the user threads.
Transfer of control from one thread to another within
same process requires a mode switch to the Kernel.
User Level Threads Kernel Level Thread
faster to create and manage. slower to create and manage.
Implementation is by a thread Operating system supports
library at the user level. creation of Kernel threads.
User level thread is generic and Kernel level thread is specific
can run on any operating to the operating system.
system.
MULTITHREADING MODELS
Many-to-One

One-to-One

Many-to-Many
MANY-TO-ONE
Many user-level threads mapped to single kernel thread
Thread management is done in user space
entire process will block if a thread makes a blocking system
call
only one thread can access the kernel at a time, multiple
threads are unable to run in parallel on multiprocessors
Examples:
⚫ Solaris Green Threads
⚫ GNU Portable Threads
MANY-TO-ONE MODEL
ONE-TO-ONE
Each user-level thread maps to kernel thread
Provides more concurrency than the many-to-one model by
allowing another thread to run when a thread makes a blocking
system call
Allows multiple threads to run in parallel on multiprocessors
creating a user thread requires creating the corresponding kernel
thread
Examples
⚫ Windows NT/XP/2000
⚫ Linux
⚫ Solaris 9 and later
ONE-TO-ONE MODEL
MANY-TO-MANY MODEL
Allows many user level threads to be mapped to a smaller or
equal number of kernel threads
Allows the operating system to create a sufficient number of
kernel threads
corresponding kernel threads can run in parallel on a
multiprocessor
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
MANY-TO-MANY MODEL
TWO-LEVEL MODEL
Similar to M:M, except that it allows a user thread to be
bound to kernel thread
Examples
⚫ IRIX
⚫ HP-UX
⚫ Tru64 UNIX
⚫ Solaris 8 and earlier
TWO-LEVEL MODEL

You might also like