Friday, 30 August 2013

Linux Process Scheduler

Linux Process Scheduler

I am trying to write a process manager that schedules different processes
using the real-time scheduling policy SCHED_FIFO. What I want to do is set
the priorities of processes and have them execute according to the
priority.
I have test processes that are paused, and wait to be resumed by the
process manager so that they execute their tasks.
Here is part of the code for the test process:
while(1) {
kill(myPid, SIGTSTP); // pause process until resumed by scheduler
printf("Process %s with PID %d and priority %d\n",
argv[0], myPid, param.sched_priority);
printf("Process %s processing...\n", argv[0]);
k = 0;
for (i = 0; i < 10000; i++) // do random task
{
for (j = 0; j < 10000; j++)
{
k++;
}
}
printf("Process %s done. Going to sleep.\n", argv[0]);
sched_yield(); // yield the processor
}
The following is a sample code for the process manager:
pid_t child[3]; // holds child processes
while(1)
{
for (i = 0; i < num_child; i++)
{
kill(child[i], SIGCONT); // resume process
child_param.sched_priority = BASE_CHILD_PRIORITY + i * 10; // set
priority
sched_setscheduler(child[i], SCHED_FIFO, &child_param); // set policy
}
}
Although I am able to get the highest priority to run first, the processes
do not complete their task fully before yield the processor. The output of
my problem can be seen below.
Process 1 with PID 5975 and priority 79
Process 1 processing...
Process 2 with PID 5974 and priority 69
Process 3 with PID 5973 and priority 59
Process 2 processing...
Process 3 processing...
Process 1 done. Going to sleep.
Process 2 done. Going to sleep.
Process 3 done. Going to sleep.
Why don't the processes with SCHED_FIFO policy complete their full task
before the next process starts? Thanks!

No comments:

Post a Comment