Task Scheduling in the operating system. Operating system tasks are classified into system tasks and user tasks. The system task priority is <50, the user task priority is> = 50, and <= 255. An invalid task with a priority greater than 255 should be excluded. The existing task queue task [] has a length of n. The element value in the task indicates the priority of the task. The smaller the value, the higher the priority. The scheduler function implements the following functions, store the tasks in task [] to the system_task [] array and the user_task [] array in sequence by system tasks and user tasks (the element value in the array is the subscript of the task in the task [] array ). ), in addition, tasks with a higher priority are listed first, and tasks with the same priority are arranged in the queue Order (that is, tasks with the same priority are listed first). The array element is-1, indicating the end of the task.
Example: task [] = {0, 30,155, 1, 80,300,170, 40, 99} system_task [] = {0, 3, 1, 7, -1} user_task [] = {4, 8, 2, 6,-1}
Function interface void scheduler (int task [], int n, int system_task [], int user_task [])
There are multiple solutions to this problem. One is to find the subscript of the smallest number of tasks in the task array and put it into the system_task and user_task arrays at a time. However, there are a few more control points in this solution. If you are not careful, an error occurs in a logic. Another solution is to take the number from the task array sequentially and insert the number to the system_task and user_task arrays by the insert sort method. This solution is very clear and logically easy to control. The last solution is to copy the number of task arrays to a struct array and sort the struct array, then, the minimum subscript is obtained from the sorted struct array and placed in the corresponding system_task and user_task arrays.
The insert Code is as follows:
View plaincopy to clipboardprint? Void main ()
{
Int task [] = {0,300,400, 1, 80,300,170, 40, 99 };
Int n = sizeof (task)/sizeof (int );
Int * system_task = new int [n + 1];
Int * user_task = new int [n + 1];
Scheduler (task, n, system_task, user_task );
Taskprint (system_task, user_task, n + 1 );
}
Void scheduler (int task [], int n, int system_task [], int user_task [])
{
Int syscount = 0;
Int usercount = 0;
For (int I = 0; I <n; I ++)
{
Int current = task [I];
If (current> = 0 & current <50)
{
System_task [syscount ++] = I;
For (int j = syscount-2; j> = 0 & syscount <= n + 1; j --)
{
If (task [system_task [j]> current)
{
System_task [j + 1] = system_task [j];
}
Else
Break;
}
System_task [j + 1] = I;
}
Else if (current> = 50 & current <= 255)
{
User_task [usercount ++] = I;
For (int j = usercount-2; j> = 0 & usercount <= n + 1; j --)
{
If (task [user_task [j]> current)
{
User_task [j + 1] = user_task [j];
}
Else
Break;
}
User_task [j + 1] = I;
}
}
System_task [syscount] =-1;
User_task [usercount] =-1;
}
Void taskprint (int system_task [], int user_task [], int n)
{
For (int I = 0; I <n; I ++)
{
If (system_task [I]! =-1)
Cout <system_task [I] <endl;
Else
Break;
}
For (I = 0; I <n; I ++)
{
If (user_task [I]! =-1)
Cout <user_task [I] <endl;
Else
Break;
}
}
Author's "rein07 column"