What Is Process Scheduling?
What Is Process Scheduling?
The act of determining which process is in the ready state, and should be moved to the running state is known
as Process Scheduling.
The prime aim of the process scheduling system is to keep the CPU busy all the time and to deliver minimum
response time for all programs. For achieving this, the scheduler must apply appropriate rules for swapping
processes IN and OUT of CPU.
Scheduling fell into one of the two general categories:
Non Pre-emptive Scheduling: When the currently executing process gives up the CPU voluntarily.
Pre-emptive Scheduling: When the operating system decides to favour another process, pre-empting the
A new process is initially put in the Ready queue. It waits in the ready queue until it is selected for execution(or
dispatched). Once the process is assigned to the CPU and is executing, one of the following several events
can occur:
The process could issue an I/O request, and then be placed in the I/O queue.
The process could create a new subprocess and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the
ready queue.
In the first two cases, the process eventually switches from the waiting state to the ready state, and is then put
back in the ready queue. A process continues this cycle until it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.
Types of Schedulers
There are three types of schedulers available:
Operations on Process
Below we have discussed the two major operation Process Creation and Process Termination.
Process Creation
Through appropriate system calls, such as fork or spawn, processes may create other processes. The process
which creates other process, is termed the parent of the other process, while the created sub-process is
termed its child.
Each process is given an integer identifier, termed as process identifier, or PID. The parent PID (PPID) is also
stored for each process.
On a typical UNIX systems the process scheduler is termed as sched, and is given PID 0. The first thing done
by it at system start-up time is to launch init, which gives that process PID 1. Further Init launches all the
system daemons and user logins, and becomes the ultimate parent of all other processes.
A child process may receive some amount of shared resources with its parent depending on system
implementation. To prevent runaway children from consuming all of a certain system resource, child processes
may or may not be limited to a subset of the resources originally allocated to the parent.
There are two options for the parent process after creating the child :
Wait for the child process to terminate before proceeding. Parent process makes a wait() system call, for
either a specific child process or for any particular child process, which causes the parent process to block
until the wait() returns. UNIX shells normally wait for their children to complete before issuing a new
prompt.
Run concurrently with the child, continuing to process without waiting. When a UNIX shell runs a process
as a background task, this is the operation seen. It is also possible for the parent to run for a while, and
then wait for the child later, which might occur in a sort of a parallel processing operation.
There are also two possibilities in terms of the address space of the new process:
if(pid < 0)
{
//Error occurred
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
//Child process
execlp("/bin/ls","ls",NULL);
}
else
{
//Parent process
//Parent will wait for the child to complete
wait(NULL);
printf("Child complete");
exit(0);
}
}
Process Termination
By making the exit(system call), typically returning an int, processes may request their own termination. This
int is passed along to the parent if it is doing a wait(), and is typically zero on successful completion and some
non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons, including :
When a process ends, all of its system resources are freed up, open files flushed and closed, etc. The process
termination status and execution times are returned to the parent if the parent is waiting for the child to
terminate, or eventually returned to init if the process already became an orphan.
The processes which are trying to terminate but cannot do so because their parent is not waiting for them are
termed zombies. These are eventually inherited by init as orphans and killed off.
1. User Threads
2. Kernel Threads
User threads, are above the kernel and without kernel support. These are the threads that application
programmers use in their programs.
Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel level threads,
allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls
simultaneously.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:
In the many to one model, many user-level threads are all mapped onto a single kernel thread.
Thread management is handled by the thread library in user space, which is efficient in nature.
One to One Model
The one to one model creates a separate kernel thread to handle each and every user thread.
Most implementations of this model place a limit on how many threads can be created.
Linux and Windows from 95 to XP implement the one-to-one model for threads.
The many to many model multiplexes any number of user threads onto an equal or smaller number of
kernel threads, combining the best features of the one-to-one and many-to-one models.
Users can create any number of the threads.
Blocking the kernel system calls does not block the entire process.
Processes can be split across multiple processors.