3. PROCESS:
⢠Process is the program in execution. Process is the unit of work.
⢠The execution of a process must progress in a sequential fashion.
⢠A process is defined as an entity which represents the basic unit of work to be
implemented in the system. A process is a dynamic instance of a computer program.
⢠When a program is loaded into the memory and it becomes a process, it can be
divided into four sections â stack, heap, text and data.
ďProcess stack- The process Stack contains the temporary data such as
method/function parameters, return address and local variables.
ďHeap- This is dynamically allocated memory to a process during its run time
ďText- This includes the current activity represented by the value of Program
Counter and the contents of the processor's registers.
ďData section: contains global and static variables
5. Process states:
⢠Process changes its state during execution.
⢠The current activity of that process is determined as state of a
process.
⢠Each process may be in one of the following states:
ďNew: the new process is created
ďRunning: Instructions are 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.
7. Process Control Block
⢠Each process has its own process control block(PCB). It contains the entire information
about a process.
⢠It is also called as task control block. A Process Control Block is a data structure
maintained by the Operating System for every process.
⢠The PCB is identified by an integer process ID (PID). A PCB keeps all the information
needed to keep track of a process as listed below.
ďProcess state-The current state of the process i.e., whether it is ready, running,
waiting, or whatever.
ďProcess privileges-This is required to allow/disallow access to system resources.
ďProcess ID-Unique identification for each of the process in the operating system.
ďPointer-A pointer to parent process.
ďProgram counter- the address of the next instruction to be executed.
ďCPU registers- where process need to be stored for execution (running state).
Depending on the computer architecture, the registers vary in the number and type.
ďCPU scheduling information-includes process priority, pointers to scheduling queues,
and other scheduling parameters.
8. ď Memory management information- includes base and limit registers, the page
tables, or the segment tables.
ď Accounting information- includes the amount of CPU, time limits, account
numbers, job or process numbers.
ď Status information- includes the list of I/O devices, a list of open files.
⢠The PCB is maintained for a process throughout its
lifetime, and is deleted once the process terminates.
PROCESS CONTROL BLOCK
10. Process scheduling:
⢠The objective of multiprogramming is to have some process running at all times,
so as to maximize CPU utilization.
⢠The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another
process on the basis of a particular strategy.
⢠OS maintains all PCBâs in process scheduling queues.
⢠Scheduling queues:
1. Job queue- new process that enter the system are put into a job queue
2. Ready queue- the processes that are residing in the main memory and are
ready and waiting to execute are kept on the queue
3. Device queue- the processes that are waiting for an I/O device is kept in a
device queue.
11. ⢠A new process is initially put in the ready queue.
⢠It waits in the ready queue until it is selected for the execution(or
dispatched)
⢠Once the process is given with the CPU and is executing , the following
events occur,
ďThe process issue an I/O request, and then be placed in an I/O queue.
ďThe process creates a new sub-process and wait for its termination.
ďThe processes are removed forcibly from the CPU due to an interrupt,
and it is put back in the ready queue.
⢠The OS can use different policies to manage each queue (FIFO, SJF, Round
robin, priority etc.)
12. Two state process model:
⢠Two state process model refers to running and non-running states.
ďRunning- when a new process is created, it enters into the system as in the running
state.
ďNot running- Processes that are not running are kept in queue, waiting for their
turn to execute.
⢠Each entry in the queue is the pointer to a particular process. Queue is
implemented by using linked list.
⢠Dispatcher- when a process is interrupted, that process is transferred in the waiting
queue. If the process has completed or aborted, the process is discarded. In either
case, the dispatcher then selects a process from queue to execute.
14. Process scheduler:
⢠A process migrates between the scheduling queues throughout its lifetime.
⢠The selection process is carried out by the appropriate scheduler.
⢠There are three different types of schedulers. They are
1. Long term scheduler or job scheduler
2. Short term scheduler or CPU scheduler
3. Medium term scheduler
16. Long term scheduler:
⢠It is also called as job scheduler
⢠It selects processes from the pool and loads them into main memory for execution.
⢠It is invoked very infrequently.
⢠It controls the degree of multiprogramming
Short term scheduler:
⢠It is also called as CPU scheduler.
⢠It selects processes from among the processes that are ready to execute, and allocates the
CPU to one of them.
⢠It is invoked very frequently.
⢠Processes can be described as either I/O bound or CPU bound.
ďAn I/O bound process spends more time on I/O operation than it spends doing
computation.
ďA CPU bound process spends more time on doing computation than an I/O bound process.
I/O requests is generated infrequently.
17. Medium term scheduler:
⢠Medium term scheduling is a part of swapping.
⢠It removes process from main memory and thus reduces the degree of
multiprogramming.
⢠Medium-term scheduler is in-charge of handling the swapped out
processes.
⢠A running process may become suspended if it makes an I/O request. A
suspended process cannot make any progress towards completion.
⢠In this condition, to remove process from memory and make space for
other processes, the suspended process is moved to secondary storage.
This process is called swapping. The process can be reintroduced into main
memory and its execution can be continued where it left off.
19. Context switch:
⢠A context is the content of a CPUâs registers and program counter at
any point of time.
⢠Switching the CPU to another process requires performing a state
save of the current process and a state restore of a different process.
This task is known as a context switch.
⢠A register is a small and very fast memory inside the CPU, that is used
to speed the execution of a computer programs by providing quick
access to commonly used values.
⢠Context switch can occur only in kernel mode.
21. 1. Process creation:
⢠During the process execution, a process may create several new processes.
⢠The creating process is called parent process. The new process is called
child process.
⢠Two possibilities exist in terms of execution:
1. The parent process continues to execute concurrently with its children.
2. The parent process waits until some or all of its children have
terminated.
⢠Two possibilities in terms of address space:
1. The child process is a duplicate of the parent process.
2. The child process has a program loaded into it.
⢠A new process is created by the fork system call.
22. 2. Process termination:
⢠A process execution is terminated when it finishes executing its final
statement and ask the OS to delete it by using the exit system call.
⢠The process may return output data to its parent process by calling the
wait system call.
⢠A parent process may terminate the execution of one of its child process
due to,
1. The child process has exceeded its usage of some of the resources that it
has been allocated.
2. The task assigned to the child process is no longer required.
3. If the parent process is exiting, the OS does not allow a child to continue.
4. If a process terminates normally or abnormally, then all its child
processes must also be terminated. This phenomenon, referred to as
cascading termination.
23. 3.Cooperating processes:
⢠The concurrent processes executing in the OS may be either
independent processes or cooperating processes.
⢠A process is independent if it cannot affect or be affected by the
other processes executing in the system.
⢠A process is cooperating if it can affect or be affected by the other
processes executing in the system.
⢠The benefits of cooperating processes are
1. Information sharing
2. Computation speedup
3. Modularity
4. Convenience
25. ⢠IPC provides a mechanism to allow processes to communicate and to
synchronize their actions.
⢠This communication could involve a process letting another process
know that some event has occurred or the transferring of data from
one process to another.
⢠Two models
ďShared memory model
ďMessage passing system
26. Synchronization in Interprocess Communication
⢠Synchronization is a necessary part of interprocess communication.
⢠It is either provided by the interprocess control mechanism or handled by the
communicating processes.
⢠Some of the methods to provide synchronization are as follows â
ďSemaphore-A semaphore is a variable that controls the access to a common
resource by multiple processes. The two types of semaphores are binary
semaphores and counting semaphores.
ďMutual Exclusion-Mutual exclusion requires that only one process thread can
enter the critical section at a time. This is useful for synchronization and also
prevents race conditions.
ďBarrier-A barrier does not allow individual processes to proceed until all the
processes reach it. Many parallel languages and collective routines impose barriers.
ďSpinlock-This is a type of lock. The processes trying to acquire this lock wait in a
loop while checking if the lock is available or not. This is known as busy waiting
because the process is not doing any useful operation even though it is active.
28. Shared memory model:
⢠Processes shares a memory with other process.
Example âProducer Consumer Problem:
⢠There are two processes: Producer and Consumer.
⢠The producer produces some items and the Consumer consumes that
item. The two processes share a common space or memory location
known as a buffer where the item produced by the Producer is stored
and from which the Consumer consumes the item if needed.
⢠To allow producer and consumer processes to run concurrently, a
buffer of items are available that can be filled by the producer and
emptied by the consumer.
⢠Unbounded-buffer: places no practical limit on the size of the buffer.
⢠Bounded buffer: assumes that there is a fixed buffer size.
29. ⢠There are two versions of this problem: the first one is known as the unbounded
buffer problem in which the Producer can keep on producing items and there is
no limit on the size of the buffer.
⢠The second one is known as the bounded buffer problem in which the Producer
can produce up to a certain number of items before it starts waiting for
Consumer to consume it.
⢠Bounded buffer problem-First, the Producer and the Consumer will share some
common memory, then the producer will start producing items. If the total
produced item is equal to the size of the buffer, the producer will wait to get it
consumed by the Consumer.
⢠Similarly, the consumer will first check for the availability of the item. If no item is
available, the Consumer will wait for the Producer to produce it. If there are
items available, Consumer will consume them.
31. ⢠In the above code, the Producer will start producing again when the
(free_index+1) mod buff_max will be free.
⢠if it not free, this implies that there are still items that can be
consumed by the Consumer so there is no need to produce more.
⢠Similarly, if free index and full index point to the same index, this
implies that there are no items to consume.
32. ii) Messaging Passing Method
⢠In this method, processes communicate with each other by message passing.
⢠A communication link must exist between them.
⢠If two processes p1 and p2 want to communicate with each other, they proceed as
follows:
ďEstablish a communication link (if a link already exists, no need to establish it again.)
ďStart exchanging messages using basic primitives.
We need at least two primitives:
â send(message, destination) or send(message)
â receive(message, host) or receive(message)
33. ⢠The message size can be of fixed size or of variable size.
⢠If it is of fixed size, it is easy for an OS designer but complicated for a
programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer.
⢠A standard message can have two parts: header and body.
⢠The header part is used for storing message type, destination id,
source id, message length, and control information.
⢠The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is
sent using FIFO style.
34. ⢠There are several methods for logically implementing a link and the
operations.
ďź Direct or indirect communication
ďź Symmetric or asymmetric communication
ďź Automatic or explicit buffering
ďź Send by copy or send by reference
ďź Fixed sized or variable sized messages.
⢠Naming- processes that want to communicate must have a way to
refer to each other.
35. Direct and Indirect Communication link
⢠While implementing the link, there are some questions that need to be kept in
mind like :
1. How are links established?
2. Can a link be associated with more than two processes?
3. How many links can there be between every pair of communicating processes?
4. What is the capacity of a link? Is the size of a message that the link can
accommodate fixed or variable?
5. Is a link unidirectional or bi-directional?
36. ⢠A link has some capacity that determines the number of messages that can
reside in it temporarily for which every link has a queue associated with it
which can be of zero capacity, bounded capacity, or unbounded capacity.
⢠In zero capacity, the sender waits until the receiver informs the sender that
it has received the message.
⢠In non-zero capacity(bounded/unbounded) cases, a process does not know
whether a message has been received or not after the send operation.
⢠For this, the sender must communicate with the receiver explicitly.
Implementation of the link depends on the situation, it can be either a
direct communication link or an in-directed communication link.
⢠Direct Communication are implemented when the processes use a specific
process identifier for the communication
⢠In-direct Communication is done via a shared mailbox (port), which
consists of a queue of messages. The sender keeps the message in mailbox
and the receiver picks them up.
37. Direct communication:
⢠Each process that wants to communicate must explicitly name the recipient or sender of the
communication
⢠A communication link has the following properties
1. A link is established automatically between every pair of the processes that want to
communicate. The processes need to know only each others identity to communicate.
2. A link is associated with exactly two processes.
3. Exactly one link exists between each pair of processes.
⢠There are two ways of addressing namely
⢠Symmetry in addressing- the sender and receive primitives are defined as
Send(A, message)-send a message to process A
Receive(B, message)-receive a message from B
⢠Asymmetry in addressing- the sender and receive primitives are defined as
Send(A, message)-send a message to process A
Receive(id, message)-receive a message from any process with the corresponding id.
38. In-direct Communication
⢠With indirect communication, the messages are sent to and received from mailboxes, or ports.
⢠The sender and receive primitives are defined as
Send(M, message)-send a message to mailbox M
Receive(M, message)-receive a message from mailbox M.
⢠The communication link has the following properties
1. A link is established between a pair of processes only if both members of the pair have a shared
mailbox.
2. A link is may be associated with more than two processes.
3. A number of different links may exists between each pair of communicating processes, with each
link corresponding to one mailbox.
39. Buffering:
⢠A link has some capacity that determines the number of messages that can reside in it
temporarily.
⢠Zero capacity- queue length of maximum is 0. no messages is waiting in a queue. The
sender must wait until the recipient receives the messages.
⢠Bounded capacity- the queue has finite length n. thus at most n messages can reside in it.
⢠Unbounded capacity- the queue has potentially infinite length. Thus any number of
messages can wait in it.
40. Synchronization:
⢠Message passing may be either blocking or non blocking.
⢠Blocking send- the sender blocks itself till the message sent by it is
receive by the receiver.
⢠Non- Blocking send- the sender does not block itself after sending the
messages but continues with its normal operation.
⢠Blocking receive- the receiver block itself until it receives the
message.
⢠Non- Blocking receive- the receiver does not block itself.
42. Threads- Overview:
⢠A thread is the basic unit of CPU utilization. A thread provides away to improve
application performance through parallelism.
⢠It is sometimes called as light weight process.
⢠It is the flow of execution through the process code, thread ID, a program
counter, a register set and a stack.
⢠Each thread belongs to any of one process and no thread can exist outside a
process.
43. ⢠It share with other threads belonging to the same process its code section, data
section, and resources such as open files and signals.
⢠A traditional or heavy weight process has a single thread of control.
⢠If the process has multiple threads of control, it can do more than one task at a
time.
44. Process Threads
Process is a heavy weight or resource
intensive
Thread is light weight taking lesser
resources than a process.
Process switching needs interaction with
the OS
Thread switching does not need
interaction with the OS
In multiprocessing environments each
process executes the same code but has
its own memory and file resources
All threads can share same set of open
files, child processes.
If one process is blocked, then no other
process can execute until the first
process is unblocked
When one thread is blocked and waiting,
second thread in the same task can run.
Multiple processes without using the
thread use more resources
Multiple threaded process uses fewer
resources
In multiple process, each process
operates independently of the others.
One thread can read, write or change
another threadâs data.
45. Benefits of Multithreads
⢠Enhanced throughput of the system: When the process is split into many
threads, and each thread is treated as a job, the number of jobs done in the unit
time increases. That is why the throughput of the system also increases.
⢠Effective Utilization of Multiprocessor system: When you have more than one
thread in one process, you can schedule more than one thread in more than one
processor.
⢠Faster context switch: The context switching period between threads is less than
the process context switching. The process context switch means more overhead
for the CPU.
⢠Responsiveness: When the process is split into several threads, and when a
thread completes its execution, that process can be responded to as soon as
possible.
⢠Communication: Multiple-thread communication is simple because the threads
share the same address space, while in process, we adopt just a few exclusive
communication strategies for communication between two processes.
⢠Resource sharing: Resources can be shared between all threads within a process,
such as code, data, and files. Note: The stack and register cannot be shared
between threads. There is a stack and register for each thread.
46. Need of Thread:
⢠It takes less time to create a new thread in an existing process than to
create a new process.
⢠Threads can share the common data, they do not need to use Inter-
Process communication.
⢠Context switching is faster when working with threads.
⢠It takes less time to terminate a thread than a process.
Types of Threads
⢠In the Operating system, there are two types of threads.
ďKernel level thread
ďUser-level thread
47. User-level thread
⢠The OS does not recognize the user-level thread.
⢠User threads can be easily implemented by the user. If a user performs a user-level thread blocking
operation, the whole process is blocked.
⢠The kernel level thread does not know nothing about the user level thread. The kernel-level thread
manages user-level threads as if they are single-threaded processes.
Advantages of User-level threads
⢠The user threads can be easily implemented than the kernel thread.
⢠User-level threads can be applied to such types of OS that do not support threads at the kernel-level.
⢠It is faster and efficient.
⢠Context switch time is shorter than the kernel-level threads.
⢠It does not require modifications of the operating system.
⢠User-level threads representation is very simple. The register, PC, stack, and mini thread control
blocks are stored in the address space of the user-level process.
⢠It is simple to create, switch, and synchronize threads without the intervention of the process.
Disadvantages of User-level threads
⢠User-level threads lack coordination between the thread and the kernel.
⢠If a thread causes a page fault, the entire process is blocked.
48. Kernel level thread
⢠The kernel thread recognizes the operating system.
⢠There is a thread control block and process control block in the system for each thread
and process in the kernel-level thread.
⢠The kernel-level thread is implemented by the operating system. The kernel knows about
all the threads and manages them.
⢠The kernel-level thread offers a system call to create and manage the threads from user-
space. The implementation of kernel threads is more difficult than the user thread.
⢠Context switch time is longer in the kernel thread.
49. Advantages of Kernel-level threads
⢠The kernel-level thread is fully aware of all threads.
⢠The scheduler may decide to spend more CPU time in the process of
threads being large numerical.
⢠The kernel-level thread is good for those applications that block the
frequency.
Disadvantages of Kernel-level threads
⢠The kernel thread manages and schedules all threads.
⢠The implementation of kernel threads is difficult than the user thread.
⢠The kernel-level thread is slower than user-level threads.
51. ⢠Multicore programming helps to create concurrent systems for deployment on multicore
processor and multiprocessor systems.
⢠A multicore processor system is basically a single processor with multiple execution cores
in one chip.
⢠A Field-Programmable Gate Array (FPGA) is might be included in a multiprocessor
system. A FPGA is an integrated circuit containing an array of programmable logic blocks
and a hierarchy of reconfigurable interconnects.
⢠Input data is processed by to produce outputs. It can be a processor in a multicore or
multiprocessor system, or a FPGA.
Advantages:
⢠Multicore and FPGA processing helps to increase the performance of an embedded
system.
⢠Also helps to achieve scalability, so the system can take advantage of increasing numbers
of cores and FPGA processing power over time.
⢠Concurrent systems that we create using multicore programming have multiple tasks
executing in parallel. This is known as concurrent execution. When multiple parallel tasks
are executed by a processor, it is known as multitasking.
Disadvantages:
⢠A CPU scheduler, handles the tasks that execute in parallel. The CPU implements tasks
using OS threads. So that tasks can have some data transfer between them, such as data
transfer between a data acquisition module and controller for the system. Data transfer
occurs when there is a data dependency.
54. Multithreading models:
⢠Multithreading allows the application to divide its task into individual threads.
⢠In multi-threads, the same process or task can be done by the number of threads.
⢠With the use of multithreading, multitasking can be achieved.
⢠The main drawback of single threading systems is that only one task can be performed at a
time, so to overcome the drawback of this single threading, there is multithreading that
allows multiple tasks to be performed.
55. There exists three established multithreading models
ďMany to one multithreading model
ďOne to one multithreading model
ďMany to Many multithreading models
56. Many to one multithreading model:
⢠The many to one model maps many user levels threads
to one kernel thread.
⢠This type of relationship facilitates an effective context-
switching environment, easily implemented even on the
simple kernel with no thread support.
Disadvantage
⢠since there is only one kernel-level thread schedule at
any given time, this model cannot take advantage of
the hardware acceleration offered by multithreaded
processes or multi-processor systems.
⢠In this, all the thread management is done in the user
space. If blocking comes, this model blocks the whole
system.
57. One to one multithreading model
⢠The one-to-one model maps a single user-level
thread to a single kernel-level thread.
⢠This type of relationship facilitates the running of
multiple threads in parallel.
⢠The generation of every new user thread must
include creating a corresponding kernel thread
causing an overhead, which can hinder the
performance of the parent process.
⢠Windows series and Linux operating systems try to
tackle this problem by limiting the growth of the
thread count.
58. Many to Many Model multithreading model
⢠In this type of model, there are several user-level threads
and several kernel-level threads.
⢠The number of kernel threads created depends upon a
particular application.
⢠The developer can create as many threads at both levels but
may not be the same.
⢠The many to many model is a compromise between the
other two models.
⢠In this model, if any thread makes a blocking system call, the
kernel can schedule another thread for execution. Also, with
the introduction of multiple threads, complexity is not
present as in the previous models.
⢠Though this model allows the creation of multiple kernel
threads, true concurrency cannot be achieved by this
model. This is because the kernel can schedule only one
process at a time.
59. Threading issues:
1. Fork and exec system call:
⢠Fork system call is used to create a separate duplicate process.
⢠If one thread in a program calls fork system calls, then
ď Creating a new process that duplicates all threads.
ď Creating a new process that duplicates only one thread that invoked
the fork.
⢠If a thread invokes Exec system call, the program specified in the
parameter to exec will replace the entire process including all
threads.
60. Factors for Differentiation fork() exec()
Invoking fork() creates a new duplicate child
process of the process that invoked
fork()
exec() replaces a process that
invokes it with a new process
provided in its parameter.
Process id The child process and parent
process have unique process id.
The new process and the replaced
process have the same process id.
Execution The parent and child process start
simultaneous execution from the
instruction just after fork().
The currently running process is
terminated and the exec() start
execution of the new process from
its entry point.
Arguments No arguments are passed to fork()
system call.
Basically, three or more arguments
are passed to the exec() system call.
Format Pid=fork(); exec(cont char *filename, char*
const argv[], char* const envp[])
61. 2. Cancellation:
⢠Thread cancellation means terminating a thread before it has completed.
⢠A thread that is to be cancelled is referred to as target thread.
⢠Asynchronous cancellation- one thread immediately terminates the target thread.
⢠Deferred cancellation- the target thread can periodic check if it should terminate,
allowing an opportunity to terminate itself in an orderly fashion.
3. Signal handling:
⢠A signal is used to notify a process that a particular event has occurred.
⢠A signal may be received either synchronous or asynchronous .
⢠Every signal may be handled by one of the two handlers.
⢠Default signal handler- run by the kernel.
⢠User defined handler-run by the user defined functions.
⢠Delivering the signal is more complicated in multithreading program.
62. 4. Thread pools:
⢠When creating a separate thread, multithread server has some potential
problems.
⢠Amount of time required to create the thread prior servicing the request.
⢠Discarding the thread once it completes the task.
⢠There is no bound on the number of threads concurrently active in the
system.
⢠Create number of threads at process startup and place them into a pool,
where they sit and wait for work.
⢠When a server receives a request, it awakens a thread from this pool, if one
is available.
⢠If thread completes its service, it returns to the pool.