Chapter 2 Processes and Threads
*PROCESS define & CONCEPT:- A process is defined as, "an entity which represents the basic unit of work to be
implemented in the system". Aprocess is a program in execution. [Link] process concept helps to explain, understand
and organize execution of programs in an operating system. [Link] is an instance of an application execution. [Link]
execution of a process must progress in a sequential fashion. An OS uses processes to organize execution of
programs. [Link] and Threads e A process is defined as, an entity which represents the basic unit of work to be
implemented in the system. e When a program is loaded into the memory and it becomes a process. [Link] process
contains program counter which specify the next instruction to be executed.
*Process States:-
1. New: A process is said to be new state if it is being created. 2. Ready: A process is
said to be ready state if it is ready for the execution and waiting for the CPU to be allocated to it. 3. Running State: A
process is said to be in running state if the CPU has been allocated to it and it is currently being executed. 4. Waiting
or Blocked: A process is said to be in waiting state if it has been blocked by some event. Unless that event occurs the
process cannot continue it execution. 5. Terminated: A process is said to be in waiting state if it has completed its
execution normally or it has been terminated abnormally by the OS because of some error or killed by some other
processes.
*Process Control Block (PCB):- Pointer: The pointer has an address of the next PCB, whose process state is
ready.
2. Process State: Process state specifies the current state of the process. The current state of the process
may be new, ready, running, waiting, terminated and so on. 3. Process Number: Each process is identified
by its process number called Process Identification Number (PID). The process-id is provided by the
operating system. 4. Priority: The process is assigned the priority at the time of its creation. Process with
the highest priority is allocated the CPU first among all the processes. 5. Program Counter: It is a pointer
indicates the address of the next instruction to be executed for the process. 6. CPU Registers: Various CPU
registers like accumulators, index registers, stack pointers, and general-purpose register etc. where process
need to be stored for execution for running state. 7. CPU Scheduling Information: This includes process
priority and other scheduling information which is required to schedule the process.
*Scheduling Queue:-1. In multiprogramming, several processes are there in ready or waiting state. [Link]
processes form a queue. [Link] operating system maintains all PCBs in process scheduling queues.4. The
operating system maintains a separate queue for each of the process states and PCBs of all processes in the
same execution state are placed in the same queue. [Link] the state of a process is changed, its PCB is
unlinked from its current queue and moved to its new state queue. [Link] two or more processes
compete for the CPU at the same time then choice has to be made which process to allocate the CPU
next.7. This procedure of determining the next process to be executed on the CPU is called process
scheduling and the module of operating system that makes this decision is called the scheduler. [Link]
process scheduler is the component of the operating system that is responsible for deciding whether the
currently running process should continue running and, if not, which process should run next.
*type of schedulers:-
1. Long-term Scheduler (Job Scheduler or Admission Scheduler):- [Link] long-term scheduler selects the job or
process to be executed from job pool on a secondary storage device and loads them into memory for execution.
[Link] long-term scheduler executes less frequently. [Link] long-term scheduler is invoked when the process leaves
the system. Because of the longer duration between executions. 4. The long-term scheduler can afford to take more
time to decide which process should be selected for execution. 5. A long-term scheduler determines which programs
are admitted to the system for processing.
2. Short-term Scheduler (CPU Scheduler or Process Scheduler): [Link]-term scheduler selects a job from ready queue and
submits it to CPU. As the short-term scheduler selects only one job at a time, it is invoked very frequently. 2. The main objective
of short-term scheduler is to increase system performance in accordance with the chosen set of criteria. [Link] is the change of
ready state to running state of the process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them. Short-term schedulers, also known as dispatchers, make the decision of which process to execute
next.4. Short-term schedulers are faster than long-term schedulers.
[Link]-term Scheduler: [Link] OSs, such as time-sharing systems, may introduce an additional, intermediate
level of scheduling (medium term scheduler). [Link] term scheduling is a part of swapping so it is also Known
swapper. [Link] is term associated with the medium-term scheduler by which processes are temporarily
removed and then brought back to the ready queue. [Link] medium term scheduler temporarily removes processes
from main memory and places them on secondary memory (such as a disk drive) or vice versa. [Link] is commonly
referred to as "swapping out” or "swapping in" Medium-term scheduling removes the processes from the memory. It
reduces the degree of Swap multiprogramming.
*Context Switch steps:-1 First, the context switching needs to save the state of process Py in the form of the program
counter and the registers to the PCB, which is in the running state. 2. Now update PCB, to process Py and moves the
process to the appropriate queue, such as the ready queue, I/O queue and waiting queue. 3. After that, another
process gets into the running state, or we can select a new process from the ready state, which is to be executed, or
the process has a high priority to execute its task. 4. Now, we have to update the PCB for the selected process P;. It
includes switching the process state from ready to running state or from another state like blocked, exit, or suspend.
5. If the CPU already executes process P;, we need to get the status of process P, to resume its execution at the same
time point where the system interrupt occurs.
*Process Termination:-1. Due to Normal exit: When compiler has compiled the program, the compiler executes a
system call to inform the operating system that task has finished. This call is exit in UNIX and Exit Process in windows.
2. Error Exit: Another reason for termination is that process finds fatal error, suppose user types the command
sample.c and no such files present, the compiler simply exits. [Link] Error: The third reason for termination is an
error caused by the process, often due to a program bug. Examples include executing an illegal instruction,
referencing nonexistent memory, or dividing by zero. In some systems (e.g., UNIX), a process can tell the operating
system that it wishes to handle certain errors itself, in which case the process is signaled (interrupted) instead of
terminated when one of the errors occurs. [Link] by another Process: The fourth reason a process might terminate
is that the process executes a system call telling the operating system to kill some other process. In UNIX this call is
kill. The corresponding Win32 function is Terminate Process.
*Threads:- [Link] Level Threads:- [Link] threads implemented at the user level are known as user threads.
In user level thread, thread management is done by the application; the kernel is not aware of existence of
threads. [Link] threads are supported above the kernel and are implemented by a thread library at the
user level. [Link] library provides support for thread creation, scheduling and management with no support
from the kernel 4. Because the kernel us unaware of user level threads, all thread creation and scheduling
are down in user space without the need for kernel intervention. [Link], user level threads are
generally fast to create and manage. [Link] thread library contains code for creating and destroying threads,
for passing message and data between threads, for scheduling thread execution and for saving and restoring
thread contexts. User thread libraries include POSIX PThreads, Mach C-threads, and Solaris 2 UIthreads.
[Link] Level Threads: [Link] threads implemented at kernel level are known as kernel threads. The thread
management is done by the Kernel. [Link] is no thread management code in the application area. Kernel
threads are supported directly by the operating system.3 The kernel performs thread creation, scheduling
and management in kernel space. [Link] thread management is done by the operating system, kernel
threads are generally slower to create and manage than are user threads. 5 However, since the kernel is
managing the threads, if a thread performs a blocking system call, the kernel can schedule another thread
in the application for execution. 6 Also in a multiprocessor environment, the kernel can schedule threads
on different processors. e Most contemporary operating systems - including Windows NT, Windows 2000,
Solaris 2, BeOS and Tru64 UNIX - support kernel threads.
*Advantages of User Level Threads: (i) User level thread can run on any operating system. (ii) A user thread
does not require modification to operating systems. (iii) User level threads are fast to create and manage.
(iv) User thread library easy to portable. (v) Low cost thread operations are possible.
*Disadvantages of User Level Threads: (i) Multithreaded applications cannot take advantage of
multiprocessing. (ii) At most, one user level thread can be in operation at one time, which limits the degree
of parallelism. (iti) If a user thread is blocked in the kernel, the entire processes (all threads of that process)
are blocked.
*Advantages of Kernel Level Threads: (i) Kernel can simultaneously schedule multiple threads from the
same process on multiple processes. (ii) If one thread in a process is blocked, the Kernel can schedule
another thread of the same process. (iii) Kernel routines themselves can multithreaded.
*Disadvantages of Kernel Level Threads: (i) The kernel-level threads are slow and inefficient. For instance,
threads operations are hundreds of times slower than that of user-level threads. (ii) Transfer of control
from one thread to another within same process requires a mode switch to the Kernel.
*Benefits:- [Link] Sharing: All the threads of a process share its resources such as memory, data, files etc. A
single application can have different threads within the same address space using resource sharing.
2. Responsiveness: Program responsiveness allows a program to run even if part of it is blocked using multithreading.
This can also be done if the process is performing a lengthy operation. For example, a web browser with
multithreading can use one thread for user contact and another for image loading at the same time.
3. Proper Utilization of Multiprocessor Architecture: In a multiprocessor architecture, each thread can run on a
different processor in parallel using multithreading. This increases concurrency of the system. This is in direct
contrast to a single processor system, where only one process or thread can run on a processor ata time. 2.26 SKT
Operating Systems - I Processes and Threads 4. Economical: Thread creation is more economical than process
creation. It is more economical to use threads as they share the process resources. Comparatively, it is more
expensive and time-consuming to create processes as they require more memory and resources. The overhead for
process creation and management is much higher than thread creation and management.
5. Efficient Communication: Communication between multiple threads is easier, as the threads shares common
address space. 6. Computational Speedup: On a single processor system, a process can be executed speedily by the
creating multiple threads in the process.
*Multithreading Models:- [Link]-to-One Model:-
[Link]-to-One Model:- 1. The many-to-one model maps many user level threads to one kernel thread. 2. Thread
management is done in user space, so it is efficient, but the entire process will block if a thread makes a blocking
system call. 3. Only one thread can access the Kernel at a time. Multiple threads are unable to run in parallel on
multiprocessors. 4. Green threads a thread library available for Solaris 2- uses many-to-one thread model. e Fig. 2.16
shows many-to-one model concept.
*Advantages:1 One kernel thread controls multiple user threads. 2. Itis efficient
because thread management is done by thread library. 3. Used in language systems,
portable libraries.
*Disadvantages: [Link] disadvantage is entire process will block if a thread makes
blocking system call. 2. Multiple threads are not able to run in parallel since only one
thread can be accessed by kernel at a time.
*Many-to-Many Model:- [Link] this model, many user level threads multiplex to the Kernel thread of smaller or equal
numbers. [Link] number of Kernel threads may be specific to either a particular application or a particular machine.
3. Solaris 2, IRIX, HP-UX and Tru64 UNIX support many to many thread model.
*Advantages: 1. Many threads can be created as per user’s requirement. 2.
Provides the best accuracy on concurrency. 3. When a thread performs a
blocking system call, the kernel can schedule another thread for execution. 4.
Multiple kernel or equal to user threads can be created.
*Disadvantages: 1. Multiple threads of kernel are an overhead for OS. 2. Low
performance. 3. True concurrency cannot be achieved.
*Difference between Process and Thread: