Saturday, August 20, 2011

Round Robin Scheduling Algorithm


              Round Robin


 #include <stdio.h>
 #include <stdlib.h>
 /* Process Data Structure */
 struct process {   
        int pid;                /* Process ID */   
        int burst;              /* CPU Burst Time */   
        int priority;           /* Priority */   
        int working;            /* Working time */   
        int waiting;            /* Waiting time */   
        struct process *next;
        }; /* Function Prototype Declarations */
        struct process *init_process (int pid, int burst, int priority);
        void listprocs (struct process *proc);
        void rr (struct process *proc, int quantum);
       
        int main (void)
       
        {    /* Initialize process list */   
        struct process *plist, *ptmp;   
        plist       = init_process(1, 10, 3);   
        plist->next = init_process(2,  1, 1);
        ptmp = plist->next;   
        ptmp->next  = init_process(3,  2, 3);
        ptmp = ptmp->next;   
        ptmp->next  = init_process(4,  1, 4);
        ptmp = ptmp->next;   
        ptmp->next  = init_process(5,  5, 2);     /* Perform simulations */   
        listprocs(plist);   
          
        rr(plist, 1);     /* Terminate cleanly */   
        while (plist != NULL) {       
              ptmp = plist;       
              plist = plist->next;       
              free(ptmp);   
              };   

              return(0);
           
              };  /* Process list entry initialization routine */
              struct process *init_process (int pid, int burst, int priority) {   
                     struct process *proc;   
                     proc = malloc(sizeof(struct process));   
                     if (proc == NULL) {       
                              printf("Fatal error: memory allocation failure.\nTerminating.\n");       
                              exit(1);   
                              };   
                              proc->pid = pid;   
                              proc->burst = burst;   
                              proc->priority = priority;   
                              proc->working = 0;   
                              proc->waiting = 0;   
                              proc->next = NULL;   
                              return(proc);
                              };

      void rr (struct process *proc, int quantum) {   
       int jobsremain, passes;    
     struct process *copy, *tmpsrc, *tmp, *slot;    
     printf("BEGIN:\tRound-Robin scheduling simulation (Quantum: %d)\n", quantum);    /* Duplicate process list */   
     tmpsrc = proc;   
     copy = tmp = NULL;   
      while (tmpsrc != NULL) {       
      if (copy == NULL) {           
     copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);           
     tmp = copy;       
     }
      else {           
     tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);           
      tmp = tmp->next;       
       };       
      tmpsrc = tmpsrc->next;   
       };     /* Main routine */   
       jobsremain = 1;   
      slot = NULL;   
        while (jobsremain) {       
      jobsremain = 0;         /* Pick next working slot */       
      if (slot == NULL) {           
      slot = copy;           
     jobsremain = 1;       
      }
      else {           
     passes = 0;           
       do {               
     if (slot->next == NULL) {                   
      passes++;                   
       slot = copy;               
               }
       else {                   
       slot = slot->next;               
       };           
      } while (passes <= 2 && slot->burst == slot->working);           
     if (passes <= 2) {               
     jobsremain = 1;           
       };       
      };         /* Perform a cycle */       
     tmp = copy;       
     while (tmp != NULL) {           
     if (tmp->burst > tmp->working) {               
     if (tmp == slot) {                   
    tmp->working += quantum;          
     }
    else {                   
    tmp->waiting += quantum;               
      };           
    };           
   tmp = tmp->next;       
    };   
   };     /* Display statistics and clean up copy */   
  tmp = copy;   
  while (tmp != NULL) {       
 printf("Process: %d\tWorking: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, tmp->working, tmp->waiting, tmp->working + tmp->waiting);       
  tmpsrc = tmp;       
  tmp = tmp->next;       
  free(tmpsrc);   
  };    
                                                                                                                                                                                                                                             printf("END:\tRR scheduling simulation\n\n");
  };

Tuesday, December 14, 2010

Layers of Computer System

SJF

#include
#include
#include
#include
void main()
{
char p[10][5],temp[5];
int tot=0,wt[10],pt[10],i,j,n,temp1,et[10];
float avg=0;
clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;ipt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=0;
for(i=1;i {
wt[i]=wt[i-1]+et[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
}

FCFS

#include
#include
#include

class process
{
float burst_time,wait_time;
class process *next,*ptr;
int max;
public:
void create();
void fcfs();
}
*front=NULL,*rear=NULL;

void process::create()
{
cout<<"ENTER THE MAXIMUM NUMBER OF PROCESSES THAT CAN BE IN THE QUEUE ==> ";
cin>>max;
class process *new_process;
for(int i=1;i<=max;i++) { new_process=new process; cout<<"ENTER THE BURST TIME OF THE PROCESS "< ";
cin>>new_process->burst_time;
if(front==NULL)
{
front=new_process;
ptr=front;
}
else
rear->next=new_process;
rear=new_process;
}
}

void process::fcfs()
{
float avg_waiting_time,total_waiting_time;
cout<<"PROCESS\tBURST TIME"<next;
}
ptr=front;
ptr->wait_time=0;
total_waiting_time=0;
for(i=2;i<=max;i++) { total_waiting_time=total_waiting_time+(ptr->burst_time);
ptr=ptr->next;
ptr->wait_time=total_waiting_time;
}
ptr=front;
total_waiting_time=0;
for(i=1;inext;
total_waiting_time=total_waiting_time+(ptr->wait_time);
}
avg_waiting_time=total_waiting_time/max;
cout<<"AVERAGE WAITING TIME IS ==> "< }

void main()
{
process p;
p.create();
p.fcfs();
}