Wednesday 7 September 2016

Priority Scheduling | Task Scheduler Linux

Priority Scheduling | Task Scheduler Linux

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>

struct Node{
   int p_id;
   int priority;
   int exe_time;
};

int main(){
   int i,j,no_of_processes;
   struct Node *Queue,temp;

   printf("\n\t---> Priority Scheduling <---\n\n");

   printf("Enter no. of Processes:");
   scanf("%d",&no_of_processes);

   Queue=malloc(no_of_processes);

   for(i=0;i<no_of_processes;i++){

      printf("Give Process#%d id :\n",i+1);
      scanf("%d",&Queue[i].p_id);

      printf("Give its Priority:\n");
      scanf("%d",&Queue[i].priority);

      printf("Give Execution Time (in secs.):\n");
      scanf("%d",&Queue[i].exe_time);
   } 

   for(i=0;i<no_of_processes;i++){
      for(j=0;j<no_of_processes-i-1;j++)
          if(Queue[j].priority>Queue[j+1].priority){
             temp=Queue[j];
             Queue[j]=Queue[j+1];
             Queue[j+1]=temp;
          }         
   }

   for(i=0;i<no_of_processes;i++){
      printf("\tPROCESS with Process ID#%d\n\n",Queue[i].p_id);
      printf("This process has Priority=%d and requires %d seconds for execution\n\n",Queue[i].priority,Queue[i].exe_time);
      printf("Execution in Progress\n\n");

      for(j=0;j<Queue[i].exe_time;j++){
          sleep(1);
          printf("*");         
      }

      printf("\n\nPROCESS FINISHED\n\n");
   }
   return 0;
}


No comments

Post a Comment

Recent Posts