Thursday, 7 April 2016

Character Stuffing

 // PROGRAM FOR CHARACTER STUFFING  
 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 void main()  
 {  
 int i,j=1;  
 char str1[100], str2[100], str3[100];  
 //clrscr();  
 printf("Enter a string: ");  
 scanf("%s",&str1);  
 //GENERATING ENCODED MESSAGE  
 str2[0] = '@';  
 for(i=0; i< strlen(str1); i++)  
 {  
 if( str1[i]=='@' || str1[i] == '#')  
 {  
 str2[j] = '#'; j++;  
 }  
 str2[j] = str1[i]; j++;  
 }  
 str2[j] = '@'; j++;  
 str2[j] = '\0';  
 printf("Encoded message is: %s", str2);  
 // DECODED MESSAGE or ACTUAL MESSAGE  
 j=0;  
 for(i=1; i<(strlen(str2)-1);i++)  
 {  
 if(str2[i]=='#')  
 i++;  
 str3[j]=str2[i];  
 j++;  
 }  
 str3[j] ='\0';  
 printf("\nDecoded or actual message is: %s", str3);  
 getch();  
 } 

Bit Stuffing

  //PROGRAM FOR BIT-STUFFING   
 #include<stdio.h>  
 #include <string.h>  
 int main()  
 {  
   char ed[100]; //ed - Entered Data  
   char ta[100]; //ta - Temporary Array  
   char dd[100]; //dd - Decoded Data  
   char ecd[100]={'0','1','1','1','1','1','1','0'}; //ecd - EnCoded Data  
   int n, i, j=0, count=0;  
   printf("Enter the string:");  
   scanf("%s",&ed);  
   printf("\nEntered bit pattern: %s",ed);  
 //ENCODING  
   for(i=0; i<strlen(ed); i++)  
   {  
     if(ed[i]=='1')  
       count++;  
     else  
       count = 0;  
     ta[j]=ed[i]; j++;  
     if(count==5)  
     {  
       ta[j] = '0';  
       j++;  
       count = 0;  
     }  
   }  
   ta[j] = '\0';  
   strcat(ta,ecd);  
   strcat(ecd,ta);  
   printf("\nEncoded Data: %s",ecd);  
 //DECODING  
   j=0; count =0;  
   for(i=8; i<strlen(ecd)-8; i++)  
   {  
     if(ecd[i]=='1')  
       count ++;  
     else  
       count = 0;  
     dd[j] = ecd[i]; j++;  
     if(count==5)  
     {  
       i++;  
       count=0;  
     }  
   }  
   dd[j] ='\0';  
   printf("\nDecoded Data: %s", dd);  
   return 0;  
 }  

Optimal Page Replacement Algorithm

 //OPTIMAL PAGE REPLACEMENT ALGORITHM  
 #include <stdio.h>  
 #include <conio.h>  
 int main()  
 {  
   int n, rss, fa[20], rsa[50], ta[20]; //n-No_of_Frames  
   //rss->Reference_String_Size::fa->Frame_Array  
   //rsa->Reference_String_Array::ta->Temporary_Array  
   int i,j,k, d,pfc=0,npf, cp,ff=0;  
   //d-distance[How soon a page will be used again?]  
   //cp->Current_Position :: ff->Frames_Filled ::pfc->Page_Fault_Count  
   //npf:NO_Page_Faults [0-False, 1-True]  
   printf("Enter number of frames: ");  
   scanf("%d", &n);  
   printf("Enter number of pages in reference string: ");  
   scanf("%d", &rss);  
   printf("Enter Reference string:\n");  
   for(i=0; i<rss; i++)  
     scanf("%d",&rsa[i]);  
   for(i=0;i<n;i++)  
   {  
     fa[i]=-1;  
     ta[i]=999;  
   }  
   printf("\nCURRENT_PAGE\t\tPAGES_IN_FRAME\t\tPAGE_FAULT_OCCURED?\n");  
   for(i=0; i<rss; i++)  
   {  
     printf("\n\t%d\t\t",rsa[i]);  
     npf=0;  
     for(j=0;j<n;j++)  //Checking for the page in FRAME ARRAY  
     {  
       if(fa[j]==rsa[i])  
       {  
         npf = 1;  
         printf("\t\t\t\tNO");  
         break;  
       }  
     }  
     if(npf==0)   // if page fault occurs  
     {  
       pfc++;  
       if(ff<n)  
       {  
         fa[ff]=rsa[i];  
         ff++;  
       }  
       else  
       {  
         for(k=0;k<n;k++)  
           ta[k]=999;  
         for(k=0; k<n; k++) //finding how near a page is  
         {  
           d = 0; // d-> distance  
           for(j=i+1;j<rss;j++)  
           {  
             if(fa[k]==rsa[j])  
             {  
               ta[k]=d;  
               break;  
             }  
             else  
               d++;  
           }  
         }  
         cp=0;  
         for(j=1;j<n;j++)  
         {  
           if(ta[cp]<ta[j])  
             cp=j; //cp->current position  
         }  
         fa[cp] = rsa[i];  
       }  
       for(j=0;j<n;j++)  
         printf("%d\t",fa[j]);  
       printf("\tYES");  
     }  
   }  
   printf("\nTotal no of pagefaults: %d",pfc);  
   return 0;  
 }  

LRU Page Replacement Algorithm

 //LRU PAGE REPLACEMENT ALGORITHM  
 #include <stdio.h>  
 #include <conio.h>  
 int main()  
 {  
   int n, rss, fa[20], rsa[50], ta[20]; //n-No_of_Frames  
   //rss->Reference_String_Size::fa->Frame_Array  
   //rsa->Reference_String_Array::ta->Temporary_Array  
   int i,j,k,pfc=0,npf, cp;  
   //cp->Current_Position ::pfc->Page_Fault_Count  
   //npf:NO_Page_Faults [0-False, 1-True]  
   printf("Enter number of frames: ");  
   scanf("%d", &n);  
   printf("Enter number of pages in reference string: ");  
   scanf("%d", &rss);  
   printf("Enter Reference string:\n");  
   for(i=0; i<rss; i++)  
     scanf("%d",&rsa[i]);  
   for(i=0;i<n;i++)  
     fa[i]=-1;  
   for(i=0;i<n;i++)  
     ta[i]=n-(i+1);  
   printf("\nCURRENT_PAGE\t\tPAGES_IN_FRAME\t\tPAGE_FAULT_OCCURED?\n");  
   for(i=0; i<rss; i++)  
   {  
     printf("\n\t%d\t\t",rsa[i]);  
     npf=0;  
     for(j=0;j<n;j++)  //Checking for the page in FRAME ARRAY  
     {  
       if(fa[j]==rsa[i])  
       {  
         npf = 1;  
         for(k=0;k<n;k++)  
           ta[k]++;  
         ta[j]=0;  
         printf("\t\t\t\tNO");  
         break;  
       }  
     }  
     if(npf==0)   // if page fault occurs  
     {  
       pfc++;  
       cp=0;  
       for(j=1;j<n;j++)  
         if(ta[cp]<ta[j])  
           cp=j; //cp->current position  
       fa[cp] = rsa[i];  
       for(k=0;k<n;k++)  
         ta[k]++;  
       ta[cp]=0;  
       for(j=0;j<n;j++)  
         printf("%d\t",fa[j]);  
       printf("\tYES");  
     }  
   }  
   printf("\nTotal no of pagefaults: %d",pfc);  
   return 0;  
 }  

FIFO Page Replacement Algorithm

 //FIFO PAGE REPLACEMENT ALGORITHM  
 #include <stdio.h>  
 #include <conio.h>  
 int main()  
 {  
   int n, rss, fa[20], rsa[50]; //n-No_of_Frames  
   //rss->Reference_String_Size::fa->Frame_Array  
   //rsa->Reference_String_Array::ta->Temporary_Array  
   int i,j,k,pfc=0,npf, cp=0;  
   //cp->Current_Position :: ff->Frames_Filled ::pfc->Page_Fault_Count  
   //npf:NO_Page_Faults [0-False, 1-True]  
   printf("Enter number of frames: ");  
   scanf("%d", &n);  
   printf("Enter number of pages in reference string: ");  
   scanf("%d", &rss);  
   printf("Enter Reference string:\n");  
   for(i=0; i<rss; i++)  
     scanf("%d",&rsa[i]);  
   for(i=0;i<n;i++)  
     fa[i]=-1;  
   printf("\nCURRENT_PAGE\t\tPAGES_IN_FRAME\t\tPAGE_FAULT_OCCURED?\n");  
   for(i=0; i<rss; i++)  
   {  
     printf("\n\t%d\t\t",rsa[i]);  
     npf=0;  
     for(j=0;j<n;j++)  //Checking for the page in FRAME ARRAY  
     {  
       if(fa[j]==rsa[i])  
       {  
         npf = 1;  
         printf("\t\t\t\tNO");  
         break;  
       }  
     }  
     if(npf==0)   // if page fault occurs  
     {  
       pfc++;  
       fa[cp] = rsa[i];  
       cp=(cp+1)%n;  
       for(j=0;j<n;j++)  
         printf("%d\t",fa[j]);  
       printf("\tYES");  
     }  
   }  
   printf("\nTotal no of pagefaults: %d",pfc);  
   return 0;  
 }  

Priority Scheduling

 //PROGRAM FOR PRIORITY CPU SCHEDULING ALGORITHM" WITH NO-PRE_EMPTION & ARRIVAL TIMES     
 #include<stdio.h>  
 #include<string.h>  
 int main(void)  
 {  
 //VARIABLE DECLARATION  
   char pn[20][20], c[20][20]; //PN-PROGRAM NAMES C-A TEMPORARY ARRAY  
      int n,i,j,at[20], bt[20], pt[20], wt[20],ct[20],tat[20]; 
//bt-BURST TIME ;pt-PRIORITY;wt-WAITING TIME; tat-TURN AROUND TIME  
      int temp1, temp2, temp3, count=0,twt=0;//,tat=0;  
      printf("Enter number of processes:");  
      scanf("%d", &n);  
      printf("Enter <ProcessName> <ArrivalTime> <BurstTime> <Priority> :\n");  
 //TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES  
      for(i=0; i<n; i++)  
           scanf("%s%d%d%d",&pn[i],&at[i],&bt[i],&pt[i]);  
 //SCHEDULING THE PROCESSES ACCORDING TO SJF  
   for(i=0 ; i<n; i++)  
   {  
           for(j=i+1; j<n; j++)  
       if (  
         //IF THERE IS NO PROCESS IN MAIN MEMORY,  
           //SORT PROCESSES ACCORDING TO ARRIVAL TIMES &  
           //IF WE GOT PROCESSES WITH SAME ARRIVAL TIME SORT THEM BY PRIORITY  
                     ( (i==0||count<1)&&(at[i]>at[j]||(at[i]==at[j]&&pt[i]>pt[j])) )  
//IF WE GOT ONLY ONE PROCESS IN MAIN MEMORY AFTER COMPLETION OF THE CURRENT PROCESS  
                ||      (count == 1 && ct[i-1]>=at[j])  
         //IF WE GOT MORE THAN ONE PROCESS IN MAIN MEMORY, SORT THEM BY PRIORITY  
                ||  ((ct[i-1]>=at[j]&&pt[i]>pt[j]))// || (ct[i-1]<at[i]&&ct[i-1]>=at[j]))  
                  )  
                     //SWAPPING PROCESSES  
                     {  
           temp1 = bt[i];  
           bt[i] = bt[j];  
           bt[j] = temp1;  
           temp2 = at[i];  
           at[i] = at[j];  
           at[j] = temp2;  
           temp3 = pt[i];  
           pt[i] = pt[j];  
           pt[j] = temp3;  
           strcpy(c[i],pn[i]);  
           strcpy(pn[i],pn[j]);  
           strcpy(pn[j],c[i]);  
         }  
     if(i==0 || count<1)  
       ct[i] = at[i] + bt[i];  
     else  
       ct[i] = ct[i-1] + bt[i];  
     wt[i] = ct[i] - (at[i] + bt[i]);  
     tat[i] = ct[i] - at[i];  
     count = 0 ;  
     for(j=i+1; j<n; j++)  
       if(ct[i]>=at[j])  
         count++;  
   }  
 //PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED  
      printf("S.N.\tPN\tAT\tBT\tPri\tCT\tWT\tTAT\n");  
      for(i=0; i<n; i++)  
printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],pt[i],ct[i],wt[i],tat[i]);  
 } //END OF MAIN  
 /* EXAMPLE OUTPUT  
 Enter number of processes:4  
 Enter <ProcessName> <ArrivalTime> <BurstTime> <Priority> :  
 p1 3  2 4  
 p2 2  1 6  
 p3 10 2 3  
 p4 2  3 2  
 S.N.  PN   AT   BT  Pri   CT   WT   TAT  
 1     p4   2    3    2    5    0    3  
 2     p1   3    2    4    7    2    4  
 3     p2   2    1    6    8    5    6  
 4     p3   10   2    3    12   0    2  
 */  

Round Robin CPU Scheduling Algorithm

 //PROGRAM FOR ROUND ROBIN "CPU SCHEDULING ALGORITHM" WITH ARRIVAL TIMES  
 #include<stdio.h>  
 #include<string.h>  
 int main(void)  
 {  
 //VARIABLE DECLARATION  
   char pn[20][20], c[20][20]; //PN-PROGRAM NAMES  
      int n,i,j,k,l, tq, at[20], bt[20], rbt[20], wt[20],tt[20],ct[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME  
      int temp1, temp2, temp3, count=0,twt=0, tn;//,tat=0;  
      printf("Enter <Number_of_Processes & Time_Quantum:\n");  
      scanf("%d%d", &n, &tq);  
      printf("Enter PN, AT, BT:\n");  
 //TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES, BURST-TIMES  
      for(i=0; i<n; i++)  
           scanf("%s%d%d",&pn[i],&at[i],&bt[i]);  
   for(i=0; i<n; i++)  
     rbt[i]=bt[i];  
 //SCHEDULING THE PROCESSES ACCORDING TO SJF  
      for(i=0;i<n;i++)  
   {  
                for(j=i+1; j<n;j++)  
                {  
 //SORTING BASED ON ARRIVAL TIMES  
         if(at[i]>at[j])  
         {  
           temp1 = bt[i];  
           bt[i] = bt[j];  
           bt[j] = temp1;  
           temp2 = at[i];  
           at[i] = at[j];  
           at[j] = temp2;  
           temp3 = rbt[i];  
           rbt[i] = rbt[j];  
           rbt[j] = temp3;  
           strcpy(c[i],pn[i]);  
           strcpy(pn[i],pn[j]);  
           strcpy(pn[j],c[i]);  
         }  
       }      //END OF J FOR-LOOP  
      }//END OF I FOR-LOOP  
 tn = at[0];  
  label:  
   for(i=0; i<n; i++)  
   {  
     if(at[i]>tn)  i--;  
       if(rbt[i]>0)  
       {  
         if(rbt[i]>tq)  
         {  
           tn += tq;  
           rbt[i] -= tq;  
         }  
         else  
         {  
           tn += rbt[i];  
           rbt[i] = 0;  
           ct[i] = tn;  
           count++;  
         }  
       }  
   }  
   if(count<n) goto label;  
 //CALCULATING WAITING TIME & TAT  
 for(i=0;i<n;i++)  
 {  
      wt[i] = ct[i]-at[i]-bt[i];  
      twt += wt[i];  
 }  
 //PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED  
      printf("S.N.\tPN\tAT\tBT\tCT\tWT\n");  
      for(i=0; i<n; i++)  
           printf("%d\t%s\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i]);  
      printf("Total waiting time:%d", twt);  
 } //END OF MAIN  

SJF CPU Scheduling Algorithm (Preemptive)

//PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITH PRE_EMPTION
 #include<stdio.h>  
 #include<conio.h>  
 int main()  
 {  
   int n,at[10], bt[10], ct[10], wt[10], tn =0, c, ta[10],tat[10];  
   //at-ArritvalTime::br-BurstTime::ct-CompletionTime::ta-TemporaryArray  
   //wt-WaitingTime::tat-TurnAroundTime::tn-CurrentTime(TimeNow)  
  // int i, j, k, tot, pc=0, pointer = 0, lp=-1;//lp-Last-executedProcess  
   int i, k, tot, pc=0, pointer = 0, lp=-1;//lp-Last-executedProcess  
   char pn[10][10];  
   printf("Enter the number of processes: ");  
   scanf("%d",&n);  
   printf("Enter <ProcessName> <ArrivalTime> <BurstTime>\n");  
   for(i=0;i<n;i++)  
     scanf("%s%d%d",&pn[i],&at[i],&bt[i]);  
   for(i=0; i<n; i++)  
   {  
       ct[i] = -1;  
       ta[i] = bt[i];  
   }  
   while(pc!=n)  
   {  
     k=0;  
     c = 0;  
     for(i=0; i<n; i++)  
     {  
       if(ct[i]<0 && at[i]<=tn)  
         c++;  
     }  
     if(c==0)  
       tn++;  
     else  
     {  
       pointer = 0;  
       while(at[pointer]>tn || ct[pointer]>0)  
         pointer++;  
       for(k=pointer+1; k<n; k++)  
         if( (at[k]<=tn && ct[k]<0) &&  
           ( (bt[pointer]==bt[k] && k==lp) || bt[pointer]>bt[k] ) )  
             pointer = k;  
       if(ct[pointer]<0)  
       {  
         bt[pointer]--;  
         tn++;  
         if(bt[pointer]==0)  
         {  
           ct[pointer] = tn;  
           wt[pointer] = ct[pointer] - ( at[pointer]+ ta[pointer] );  
           tat[pointer] = ct[pointer] - at[pointer];  
           pc++;  
         }  
       }  
       lp = pointer;  
     }  
   }  
   printf("\nPN\tAT\tBT\tCT\tWT\tTAT\n");  
   for(i=0;i<n;i++)  
     printf("%s\t%d\t%d\t%d\t%d\t%d\n",pn[i],at[i],ta[i],ct[i],wt[i],tat[i]);  
   return 0;  
 }  
 /* SAMPLE OUTPUT  
 Enter the number of processes: 6  
 Enter <ProcessName> <ArrivalTime> <BurstTime>  
 p1 3 1  
 p2 1 4  
 p3 10 6  
 p4 25 10  
 p5 1 3  
 p6 12 3  
 PN   AT    BT   CT   WT   TAT  
 p1   3     1     5    1     2  
 p2   1     4     9    4     8  
 p3   10    6    19    3     9  
 p4   25   10    35    0    10  
 p5   1     3    4     0     3  
 p6   12    3    15    0     3  
 */  

SJF CPU Scheduling Algorithm (Non-Preemptive)

 //PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITHOUT PRE_EMPTION  
 #include<stdio.h>  
 #include<conio.h>  
 int main()  
 {  
   int at[10], bt[10], ct[10], wt[10], ta[10], tat[10];  
   //at-ArritvalTime::br-BurstTime::ct-CompletionTime::ta-TemporaryArray  
   //wt-WaitingTime::tat-TurnAroundTime::tn-CurrentTime(TimeNow)  
   int n, i, k, pc=0, pointer = 0, tn =0, c;//pc-ProcessesCompleted  
   char pn[10][10]; //pn-ProcessName  
   printf("Enter the number of processes: ");  
   scanf("%d",&n);  
   printf("Enter <ProcessName> <ArrivalTime> <BurstTime>\n");  
   for(i=0;i<n;i++)  
     scanf("%s%d%d",&pn[i],&at[i],&bt[i]);  
   for(i=0; i<n; i++)  
   {  
     ct[i] = -1;  
     ta[i] = bt[i];  
   }  
   while(pc!=n)  
   {  
     c = 0;  
     for(i=0; i<n; i++)  
       if(ct[i]<0 && at[i]<=tn)  
         c++;  
     if(c==0)  
       tn++;  
     else  
     {  
       pointer = 0;  
       while(at[pointer]>tn || ct[pointer]>0)  
         pointer++;  
       for(k=pointer+1; k<n; k++)  
         if(at[k]<=tn && ct[k]<0 && bt[pointer]>bt[k])  
           pointer = k;  
       if(ct[pointer]<0)  
       {  
         tn=tn+bt[pointer];  
         bt[pointer] = 0;  
         ct[pointer] = tn;  
         wt[pointer] = ct[pointer] - ( at[pointer]+ ta[pointer] );  
         tat[pointer] = ct[pointer] - at[pointer];  
         pc++;  
       }  
     }  
   }  
   printf("\nPN\tAT\tBT\tCT\tWT\tTAT\n");  
   for(i=0;i<n;i++)  
     printf("%s\t%d\t%d\t%d\t%d\t%d\n",pn[i],at[i],ta[i],ct[i],wt[i],tat[i]);  
   return 0;  
 }  
 /* EXAMPLE OUTPUT  
 Enter the number of processes: 6  
 Enter <ProcessName> <ArrivalTime> <BurstTime>  
 p1 3 2  
 p2 6 5  
 p3 1 1  
 p4 6 2  
 p5 15 3  
 p6 20 10  
 PN   AT   BT    CT    WT   TAT  
 p1   3     2     5    0     2  
 p2   6     5     13   2     7  
 p3   1     1     2    0     1  
 p4   6     2     8    0     2  
 p5   15    3    18    0     3  
 p6   20   10    30    0    10  
 */   
//ANOTHER PROGRAM for the same problem
 //PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITH NO-PRE_EMPTION & ARRIVAL TIMES  
 #include<stdio.h>  
 #include<string.h>  
 int main(void)  
 {  
 //VARIABLE DECLARATION  
   char pn[20][20], c[20][20]; //PN-PROGRAM NAMES C-A TEMPORARY ARRAY  
      int n,i,j,at[20], bt[20], wt[20],ct[20],tat[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME  
      int temp1, temp2, count=0,twt=0;//,tat=0;  
      printf("Enter number of processes:");  
      scanf("%d", &n);  
      printf("Enter PN, AT, BT:\n");  
 //TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES  
      for(i=0; i<n; i++)  
           scanf("%s%d%d",&pn[i],&at[i],&bt[i]);  
 //SCHEDULING THE PROCESSES ACCORDING TO SJF  
   for(i=0 ; i<n; i++)  
   {  
           for(j=i+1; j<n; j++)  
       if (  
         //IF THERE IS NO PROCESS IN MAIN MEMORY,  
           //SORT PROCESSES ACCORDING TO ARRIVAL TIMES &  
           //IF WE GOT PROCESSES WITH SAME ARRIVAL TIME SORT THEM BY BURST TIMES  
                     ( (i==0||count<1)&&(at[i]>at[j]||(at[i]==at[j]&&bt[i]>bt[j])) )  
         //IF WE GOT ONLY ONE PROCESS IN MAIN MEMORY AFTER COMPLETION OF THE CURRENT PROCESS  
                ||      (count == 1 && ct[i-1]>=at[j])  
         //IF WE GOT MORE THAN ONE PROCESS IN MAIN MEMORY, SORT THEM BY BURST TIMES  
                ||      ((ct[i-1]>=at[j]&&bt[i]>bt[j]))// || (ct[i-1]<at[i]&&ct[i-1]>=at[j]))  
                  )  
                     //SWAPPING PROCESSES  
                     {  
           temp1 = bt[i];  
           temp2 = at[i];  
           bt[i] = bt[j];  
           at[i] = at[j];  
           bt[j] = temp1;  
           at[j] = temp2;  
           strcpy(c[i],pn[i]);  
           strcpy(pn[i],pn[j]);  
           strcpy(pn[j],c[i]);  
         }  
     if(i==0 || count<1)  
       ct[i] = at[i] + bt[i];  
     else  
       ct[i] = ct[i-1] + bt[i];  
     wt[i] = ct[i] - (at[i] + bt[i]);  
     tat[i] = ct[i] - at[i];  
     count = 0 ;  
     for(j=i+1; j<n; j++)  
       if(ct[i]>=at[j])  
         count++;  
   }  
 //PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED  
      printf("S.N.\tProcess-Name\tArrival-Time\tBurst-Time\tCT\tWT\tTAT\n");  
      for(i=0; i<n; i++)  
           printf("%d\t\t%s\t\t%d\t\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i],tat[i]);  
 } //END OF MAIN  
 /* EXAMPLE OUTPUT  
 Enter number of processes:6  
 Enter PN, AT, BT:  
 p1 3 2  
 p2 6 5  
 p3 1 1  
 p4 6 2  
 p5 15 3  
 p6 20 10  
 S.N.  Process-Name  Arrival-Time  Burst-Time   CT   WT   TAT  
 1        p3       1        1    2    0    1  
 2        p1       3        2    5    0    2  
 3        p4       6        2    8    0    2  
 4        p2       6        5    13   2    7  
 5        p5       15       3    18   0    3  
 6        p6       20       10   30   0    10  
 */  

FCFS

FCFS CPU Scheduling Algorithm

 //PROGRAM FOR FIRST-COME-FIRST-SERVE "CPU SCHEDULING ALGORITHM"  
 #include<stdio.h>  
 #include<string.h>  
 int main(void)  
 {  
   //VARIABLE DECLARATION  
   char pn[20][20], c[20][20]; //PN-PROGRAM NAMES  
   int n,i,j,at[20], bt[20], wt[20],tat[20], ct[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME  
   int twt=0, ttat=0, temp1, temp2;  
   printf("Enter number of processes:");  
   scanf("%d", &n);  
 //TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES  
 printf("Enter <Process-name> <Arrival-time> <Burst-time> for processes:\n", (i+1));  
   for(i=0; i<n; i++)  
       scanf("%s%d%d",&pn[i],&at[i],&bt[i]);  
 //SORT THE PROCESSES ACCORDING TO ARRIVAL TIMES  
  for(i=0;i<n;i++)  
  {  
      for(j=i+1; j<n;j++)  
      {  
           if(at[i]>at[j])  
           {  
                temp1 = bt[i];  
                temp2 = at[i];  
                bt[i] = bt[j];  
                at[i] = at[j];  
                bt[j] = temp1;  
                at[j] = temp2;  
                strcpy(c[i],pn[i]);  
                strcpy(pn[i],pn[j]);  
                strcpy(pn[j],c[i]);  
           }  
      }  
      if(i==0) ct[0]=at[0]+bt[0];  
      if(i>0)  
      {  
     if(at[i]>ct[i-1])  
                ct[i]=at[i]+bt[i];  
           else  
                ct[i]=ct[i-1]+bt[i];  
      }  
  }  
 //CALCULATING WAITING TIME & TAT  
 wt[0]=0;  
 tat[0]=ct[0]-at[0];  
 for(i=1;i<n;i++)  
 {  
      tat[i] = ct[i]-at[i];  
      wt[i] = tat[i]-bt[i];  
      twt += wt[i];  
      ttat += tat[i];  
 }  
 //PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED  
      printf("S.N.\tPN\tAT\tBT\tCT\tWT\tTAT\n");  
      for(i=0; i<n; i++)  
           printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i],tat[i]);  
      printf("Total waiting time:%d\n", twt);  
      printf("Total Turn Around Time:%d", ttat);  
 }