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, the task with a higher priority is placed at the front, and the array element is-1, which indicates the end.
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 [])
1 #include<stdio.h> 2 void scheduler(int task[], int n, int system_task[], int user_task[]) 3 { 4 int *x = new int[n]; 5 int i,j,k; 6 for (i=0; i < n; i++) 7 x[i] = i; 8 for (i=0; i < n-1; i++) 9 for (j=1; j < n-i; j++)10 if (task[j-1] > task[j])11 {12 int t = task[j-1];13 task[j-1] = task[j];14 task[j] = t;15 t = x[j-1];16 x[j-1] = x[j];17 x[j] = t;18 }19 for (int i=0; i < n; i++)20 printf("%d ",task[i]);21 printf("\n");22 j = 0;23 k = 0;24 for (i=0; i < n; i++)25 {26 if(task[i] < 50)27 system_task[j++] = x[i];28 else if (task[i] <= 255)29 user_task[k++] = x[i];30 else ;31 }32 system_task[j] = -1;33 user_task[k] = -1;34 }35 int main()36 {37 int task[100],sys[100],user[100],n;38 scanf("%d",&n);39 for (int i=0; i < n; i++)40 scanf("%d",&task[i]);41 scheduler(task,n,sys,user);42 for (int i=0; sys[i] != -1; i++)43 printf("%d ",sys[i]);44 printf("\n");45 for (int i=0; user[i] != -1; i++)46 printf("%d ",user[i]);47 printf("\n");48 49 }
Operating System Task Scheduling