0% found this document useful (0 votes)
6 views4 pages

Understanding Processes and Threads

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)
6 views4 pages

Understanding Processes and Threads

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

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:

Common questions

Powered by AI

A process is a program in execution, representing a basic unit of work within an operating system, while a thread is the smallest unit of processing that can be scheduled by an operating system . Processes are independent and have their own memory space, while threads within the same process share resources like memory and open files, which makes them more economical to create and manage . Understanding these differences is crucial because processes commonly have more overhead due to the necessity of separate memory management, whereas threads provide efficient communication and resource sharing due to their shared address space . However, multithreaded processes may face complexity issues like synchronization problems .

Operating systems optimize resource sharing and communication between threads by allowing them to share the same address space, including memory, data, and file descriptors, which facilitates efficient inter-thread communication . This shared environment significantly reduces the overhead associated with context switching and process management . According to the design, multithreading improves the responsiveness and performance of applications by allowing concurrent execution of tasks, enabling better resource utilization, and supporting faster and simpler communication between tasks sharing data within the same application . These features are crucial in designing scalable and high-performance systems as they ensure efficient resource management and responsiveness, allowing applications to run smoothly even under a high load.

Scheduling queues are mechanisms used by the operating system to manage the execution of processes by organizing them into different queues based on their states, such as ready, waiting, or blocked . The OS maintains a separate queue for each process state, enabling efficient state transitions and reducing the complexity of process management . These queues are integral for the OS to decide which process should be executed next and help in implementing efficient CPU scheduling strategies by keeping track of all processes and their PCBs . Proper management of scheduling queues is essential to handle process prioritization, ensure resource allocation fairness, and optimize CPU utilization, thereby impacting the overall effectiveness of the operating system.

Processes can terminate due to normal exits, error exits, fatal errors, or being killed by another process. Normal exits occur when a process completes its operations successfully and informs the OS using system calls like exit in UNIX . Error exits happen when errors like missing files occur, causing the process to terminate . Fatal errors are due to issues such as illegal instructions or memory access violations, prompting the OS to terminate the process or signal it for handling these errors . Lastly, a process can be terminated by another through system calls such as kill in UNIX, where the OS handles the request by stopping the targeted process .

Multithreading in a multiprocessor architecture allows each thread of a process to run on different processors simultaneously, thus enhancing concurrency and system throughput . This architecture supports parallel execution of threads, leading to increased computational speed and more efficient use of processor resources . It also improves program responsiveness, as multithreading can prevent a single thread from blocking the entire process . These advantages make multithreading particularly beneficial in high-performance computing environments where managing parallel tasks efficiently is essential.

The Process Control Block (PCB) is a crucial data structure used by the operating system to manage processes. It contains vital information about the process, including its current state, process ID, priority, program counter, and CPU registers, among other data . The PCB allows the OS to keep track of a process's state and ensure that it can be switched in and out of execution correctly, making it possible to manage multiple processes effectively and efficiently .

The many-to-one model maps many user level threads to a single kernel thread, with thread management performed in user space, which improves efficiency but limits concurrency since only one thread can access the kernel at a time . This leads to blocking issues if one thread makes a blocking system call . Conversely, the many-to-many model allows many user-level threads to be represented by an equal or smaller number of kernel threads, offering better concurrency as the kernel can schedule another thread to run if one is blocked . However, it introduces overhead for the operating system, and performance can be lower if not implemented efficiently .

The long-term scheduler determines which processes are admitted to the system for processing, operating infrequently to control the degree of multiprogramming . The short-term scheduler, or CPU scheduler, selects processes from the ready queue and allocates CPU to them, invoked frequently to enhance system responsiveness . The medium-term scheduler temporarily removes processes from memory (swapping) to manage the extent of multiprogramming and ensure efficient memory usage . Each scheduler plays a vital role; the long-term scheduler manages workload, the short-term scheduler optimizes CPU performance, and the medium-term scheduler balances CPU and memory usage to maintain overall system performance.

Context switching involves saving the state of a currently executing process or thread and loading the state of the next scheduled process to run . This is done by storing CPU registers, including the program counter, into the PCB of the active process and updating to the PCB of the next process . Although necessary for multitasking, context switching adds overhead due to the time taken in saving and loading a process's state, which can impact system performance by reducing CPU availability for actual execution, especially in systems requiring frequent switches like time-sharing systems .

User level threads are often faster and more efficient to create and manage due to reduced kernel intervention, making them portable and low-cost . However, they cannot take full advantage of multiprocessing since the kernel is not aware of individual user threads, potentially leading to process-wide blocking . In contrast, kernel level threads, although slower to create and manage, allow the operating system to schedule multiple threads simultaneously across multiple processors, enhancing concurrency and performance in a multiprocessor environment . The choice between these depends on the specific needs for system performance and portability versus the necessity for sophisticated resource management and multiprocessing support.

You might also like