Multilevel Queue Scheduling
In Multilevel queue scheduling, the ready queue is divided into sub-queues and each process is assigned permanently to one of these sub-queue based on some process property like process priority, user, process size or process type etc. Each queue has its own scheduling algorithm and the queues themselves are also scheduled. For example, suppose there are two queues one for foreground processes and other for background processes. In foreground queue, the processes are scheduled using FCFS and in background queue processes are scheduled using SJF. The two queues themselves are scheduled using RR.
Example 1:
Process P1, P4 and P5 belong to queue Q1 and P2 and P3 belong to queue Q2. Processes in Q1 are scheduled using RR with time quantum 3 while processes in Q2 are scheduled using FCFS. Out of the two queue Q1 has higher priority over Q2 i.e. if there is any process in Q1 it will be scheduled first.
Solution:
Waiting Time:
P1 : (0-0)+2=2 P2 : 6-1=5 P3 : 13-2=11
P4 : 3-2=1 P5 : 8-8=0
Av. Waiting Time = 19/5 = 3.8 (Ans)
Example 2:
Queues Q1 and Q2 themselves are scheduled using RR (TQ=5)
Solution:
Waiting Time:
P1 : 0 P2 : (3-2)=1 P3 : (13-2)+1=12
P4 : 8-4=4 P5 : (10-5)+5=5
Av. Waiting Time = 27/5 = 5.4 (Ans)
Replacing a process image - exec( )
Their is a family of function which can be used for replacing the current process with a new process. They differ on the number of arguments and the way they start a new process. The various functions are execl, execlp, execle, execv and execvp.
We will discuss the use of execl and the others can be used on similar lines. The syntax for execl is
int execl(const char *path, const char *arg0, ..., (char *)0);
The 1st argument is the PATH for the program while the last argument will be NULL.
Program:
#include
#include
int main()
{
printf("Before execl\n");
execl("/bin/ps","ps","-a",NULL);//
printf("After execlp\n");
}
Output:
$gcc -o exec execl.c
$./exec
Explanation: The program prints the first message and then calls execl. execl searches for the program ps in the PATH variable and executes it replacing the original program. Hence the second message doesn't get printed. It can be seen in the output also that the name of the process running is ps and not the user process exec.
system( )
system( ) - function can be used to make a program run from within another program thus creating a new process. The system( ) function runs the command passed to it and then waits for it to complete.
Example: system.c
#include
#include
int main()
{
printf("Before system function\n");
system("ps");
printf("After system function\n");
}
Output:
$gcc -o system system.c
$./system
The program runs and executes the first printf statement and then calls system with string system("ps") which executes the ps program. The program resumes when the ps command has finished and prints After system function.
The call to the system function will create a whole new process in the system. This can be verified from the output screen which shows 2 different processes system with PID 54 and ps with PID 56.