When the scheduler switches the CPU from executing one process to executing another, the context switcher saves the content of all processor registers for the process being removed from the CPU in its process descriptor.
The context of a process is represented in the process control block of a process. Context switch time is pure overhead.
Context switching can significantly affect performance, since modern computers have a lot of general and status registers to be saved. Content switch times are highly dependent on hardware support.
Context switch requires ( n + m ) b × K time units to save the state of the processor with “n” general registers, assuming “b” store operations are required to save register and each store instruction requires “K” time units. Some hardware systems employ two or more sets of processor registers to reduce the amount of context switching time.
Operation on Processes
Several operations are possible on the process. Process must be created and deleted dynamically. Operating system must provide the environment for the process operation. We discuss the two main operations on processes.
- Create a process: Operating system creates a new process with the specified or default attributes and identifier. A process may create several new sub processes. Syntax for creating new process is :
CREATE (processed, attributes)
Two names are used in the process they are parent process and child process. Parent process is a creating process. Child process is created by the parent process. Child process may create another subprocess. So it forms a tree of processes.
When a process creates a new process, two possibilities exist in terms of execution.
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
- Terminate a process : DELETE system call is used for terminating a process. A process may delete itself or by another process. A process can cause the termination of another process via an appropriate system call. TheDELETEservice is normally invoked as a part of orderly program termination.
Cooperating process is a process that can affect or be affected by the other processes while executing. If suppose any process is sharing data with other processes, then it is called cooperating process. Benefit of the cooperating processes are :
1. Sharing of information
2. Increases computation speed
A thread is a flow of execution through the process code, with its own program counter, system registers and stack. Threads are a popular way to improve application performance through parallelism. A thread is sometimes called a light weight process. Threads represent a software approach to improving performance of operating system by reducing the over head thread is equivalent to a classical process. Each thread belongs to exactly one process and no thread can exist outside a process.
Types of Thread
Threads is implemented in two ways :
1. User Level – In a user thread, all of the work of thread management is done by the application and the kernel is not aware of the existence of threads.
The 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.
The application begins with a single thread and begins running in that thread.
1. Thread switching does not require Kernel mode privileges.
2. User level thread can run on any operating system.
3. Scheduling can be application specific.
4. User level threads are fast to create and manage.
1. In a typical operating system, most system calls are blocking.
2. Multithreaded application cannot take advantage of multiprocessing.
2. Kernel Level : In Kernel level thread, thread management done by the Kernel.
There is no thread management code in the application area. Kernel threads are supported directly by the Operating system.
The Kernel performs thread creation, scheduling and management in Kernel space. Kernel threads are generally slower to create and manage than the user threads.
Advantages of Kernel level thread :
1. Kernel can simultaneously schedule multiple threads from the same process on multiple process.
2. If one thread in a process is blocked, the Kernel can schedule another thread of the same process.
3. Kernel routines themselves can multithreaded.
1. Kernel threads are generally slower to create and manage than the user threads.
2. Transfer of control from one thread to another within same process requires a mode switch to the Kernel.
Advantages of Thread
1.Thread minimize context switching time (In computing, a context switch is the process of storing and restoring the state (context) of a process or thread so that execution can be resumed from the same point at a later time. This enables multiple processes to share a single CPU and is an essential feature of a multitasking operating system).
2. Use of threads provides concurrency within a process.
3. Efficient communication.
4. It is more economical to create and context switch threads.
5. Utilization of multiprocessor architectures – The benefits of multithreading can be greatly increased in a multiprocessor architecture.
Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process.
Multithreading models are three types:
1. Many to many relationship; many user level threads multiplexes to the Kernel thread of smaller or equal numbers.
2. Many to one relationship; many user level threads to one Kernel level thread.
3. One to one relationship; There is one to one relationship of user level thread to the kernel level thread. This model provides more concurrency than the many to one model.
Difference Between User Level and Kernel Level Threads
User Level Thread
Kernel Level Thread
|User level thread are faster to create and manage.||Kernel level thread are slower to create and manage.|
|Implemented by a thread library at the user level.||Operating system support directly to Kernel threads.|
|User level thread can run on any operating system.||Kernel level threads are specific to the operating system.|
|Support provided at the user level called user level thread.||Support may be provided by kernel is called Kernel level threads.|
|Multithread application cannot take advantage of multiprocessing.||Kernel routines themselves can be multithreaded.|
In a multithreaded program environment, fork and exec system calls is changed.
Unix system have two version of fork system calls. One call duplicates all threads and another that duplicates only the thread that invoke the fork system call.
fork() created a newly independent process that has it’s own space in memory and also has own permission depends upon what you assign.
Using exec() the created process is a sub thread of calling process. They also share area in memory and so that also share all permission and resources.
Thread cancellation : is a process of thread terminates before its completion of task. For example, in multiple thread environment, thread concurrently searching through a database. If any one thread returns the result, the remaining thread might be cancelled.
Thread cancellation is of two types.
1. Asynchronous cancellation
2. Synchronous cancellation
Most of the operating system allow a process or thread to be canceled asynchronously.