Showing posts with label operating system. Show all posts
Showing posts with label operating system. Show all posts

Thursday, April 26, 2012

First Fit program in C language | Memory Management | Operating System

Source code for First Fit Algorithm in C 


int main(){
   
   int p,m;  
   printf("Enter number of processes:");
   scanf("%d",&p);
   printf("Enter number of Memory blocks:");
   scanf("%d",&m);
   
   int parr[p],marr[m],i;
   for(i=0;i<p;i++)
   {
     printf("Enter size of process %d:",i+1);
     scanf("%d",&parr[i]);      
   }
   for(i=0;i<m;i++)
   {
     printf("Enter size of memory %d:",i+1);
     scanf("%d",&marr[i]);      
   }
   int j;
   for(i=0;i<p;i++){
       for(j=0;j<m;j++){
         if(marr[j]>=parr[i]){
              marr[j]-=parr[i];
              printf("Allocating process %d to memory %d\n Size remaining in it after allocation %d\n\n",i+1,j+1,marr[j]);   
              break;            
         }  
         
        
     }    
      if(j==m)
         {printf("Not enough memory for process %d",i);break;}        
   }
  getch();  
}

C Program to implement Worst fit memory management Algorithm

Worst Fit Memory management Algorithm Source code in C Language



int main(){
   
   int p,m;  
   printf("Enter number of processes:");
   scanf("%d",&p);
   printf("Enter number of Memory blocks:");
   scanf("%d",&m);
   
   int parr[p];
   struct mem{
          int id;
          int size;
   }marr[m];
   int i;
   for(i=0;i<p;i++)
   {
     printf("Enter size of process %d:",i+1);
     scanf("%d",&parr[i]);      
   }
   for(i=0;i<m;i++)
   {
     printf("Enter size of memory %d:",i+1);
     scanf("%d",&marr[i].size);   
     marr[i].id=i+1;   
   }
   int j;
   for(i=0;i<m;i++)
   for(j=i+1;j<m;j++)
   if(marr[i].size<marr[j].size)
   {
    struct mem t=marr[i];
    marr[i]=marr[j];
    marr[j]=t;                               
   }
   for(i=0;i<p;i++){
       for(j=0;j<m;j++){
         if(marr[j].size>=parr[i]){
              marr[j].size-=parr[i];
              printf("Allocating process %d to memory %d\n Size remaining in it after allocation %d\n\n",i+1,j+1,marr[j].size);   
              break;              
         }  
         
        
     }    
      if(j==m)
         {printf("Not enough memory for process %d",i);break;}        
   }
  getch();  
}

Wednesday, April 25, 2012

Best Fit Source code in C | Memory management Algorithm | Operation System

C Program to implement Best Fit Memory management algorithm


int main(){
   
   int p,m;  
   printf("Enter number of processes:");
   scanf("%d",&p);
   printf("Enter number of Memory blocks:");
   scanf("%d",&m);
   
   int parr[p];
   struct mem{
          int id;
          int size;
   }marr[m];
   int i;
   for(i=0;i<p;i++)
   {
     printf("Enter size of process %d:",i+1);
     scanf("%d",&parr[i]);      
   }
   for(i=0;i<m;i++)
   {
     printf("Enter size of memory %d:",i+1);
     scanf("%d",&marr[i].size);   
     marr[i].id=i+1;   
   }
   int j;
   for(i=0;i<m;i++)
   for(j=i+1;j<m;j++)
   if(marr[i].size>marr[j].size)
   {
    struct mem t=marr[i];
    marr[i]=marr[j];
    marr[j]=t;                               
   }
   for(i=0;i<p;i++){
       for(j=0;j<m;j++){
         if(marr[j].size>=parr[i]){
              marr[j].size-=parr[i];
              printf("Allocating process %d to memory %d\n Size remaining in it after allocation %d\n\n",i+1,j+1,marr[j].size);   
              break;            
         }  
         
        
     }    
      if(j==m)
         {printf("Not enough memory for process %d",i);break;}        
   }
  getch();  
}

Next fit Program in C Language Memory management Algorithm

Next fit Program in C Language Memory management Algorithm Operating System programs in C language

int main(){
   
   int p,m;  
   printf("Enter number of processes:");
   scanf("%d",&p);
   printf("Enter number of Memory blocks:");
   scanf("%d",&m);
   
   int parr[p],marr[m],i;
   for(i=0;i<p;i++)
   {
     printf("Enter size of process %d:",i+1);
     scanf("%d",&parr[i]);      
   }
   for(i=0;i<m;i++)
   {
     printf("Enter size of memory %d:",i+1);
     scanf("%d",&marr[i]);      
   }
   int j=0;
   for(i=0;i<p;i++){
      printf("search %d %d",i,j);
       for(;;j=(j+1)%m){
         if(marr[j]>=parr[i]){
              marr[j]-=parr[i];
              printf("Allocating process %d to memory %d\n Size remaining in it after allocation %d\n\n",i+1,j+1,marr[j]);   
              printf("%d %d\n\n",j,(j+1)%m);
              
              break;            
         }  
         
        
     }    
      if(j==m)
         {printf("Not enough memory for process %d",i);break;}        
   }
  getch();  
}

Wednesday, March 14, 2012

FCFS First come first serve with arrival time CPU Scheduling Program in c dev cpp

 #include <stdio.h>  
 #include <conio.h>  
 /* First Come First Serve CPU Scheduling Algorithm  
   By Suraj Jha & Abhas Tandon  
 */  
 int wt[10],bt[10],at[10],tat[10],n;  
 float awt,atat;  
 void input(){  
        printf("Enter Number of processes:");  
        scanf("%d",&n);  
        int i;  
        for(i=0;i<n;i++)  
        {  
          printf("Enter Burst Time of process %d:",i+1);  
          scanf("%d",&bt[i]);         
          printf("Enter Arrival Time of process %d:",i+1);  
          scanf("%d",&at[i]);         
        }  
 }  
 void calculate(){  
    wt[0]=0;  
    atat=tat[0]=bt[0];  
    int btt=bt[0];//to store total burst time sum  
    int i;  
    for(i=1;i<n;i++){  
      wt[i]=btt-at[i];  
      btt+=bt[i];        
      awt+=wt[i];     
      tat[i]= wt[i]+bt[i];   
      atat+=tat[i];    
    }  
    atat/=n;  
    awt/=n;       
 }  
 void display(){  
    int i;  
    printf("SR.\tA.T.\tB.T.\tW.T.\tT.A.T.\n");  
    for(i=0;i<n;i++)  
    {  
    printf("%3d\t%3d\t%3d\t%3d\t%4d\n",i+1,at[i],bt[i],wt[i],tat[i]);          
    }    
    printf("Average Waiting Time: %f\nAverage Turn Around Time:%f",awt,atat);  
 }    
 int main(){  
       printf("FCFS CPU Scheduling Algorithm\n");  
       input();  
       calculate();  
       display();   
       getch();   
 }  

Short Job First Non PREEMPTIVE with arrival time CPU Scheduling Algorithm in C Source code program

 #include <stdio.h>  
#include <conio.h>
/* Short Job First NON PREEMPTIVE CPU Scheduling Algorithm
By Suraj Jha & Abhas Tandon
Please note that program has some extra printf statements that were used for debugging
They can be removed if required
*/
int n;
struct sjf{
int sr;
int wt,bt,at,tat;
int flag;
}arr[10];
struct sjf queue[15];
int f=-1,r=-1;
void enqueue(struct sjf x){
if(f==-1 && r==-1)
{
queue[++f]=x;
r++;
}
else{
queue[++r]=x;
}
printf("Added %d to queue\n",queue[r].sr);
}
int dequeue(){
int x=f;
if(f==r)
f=r=-1;
else
f++;
printf("Dequeue %d\n",queue[x].sr);
return queue[x].sr;
}
void sortQueue(){
int i,j;
for(i=f;i<=r;i++)
{
for(j=i+1;j<=r;j++) {
if(queue[i].bt>queue[j].bt)
{
struct sjf temp= queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
}
void dispq(){
int i;
if(f==-1)
printf("Empty");
for(i=f;i<=r;i++)
printf("%d ",queue[i].sr);
printf("\n%d %d\n",f,r);
}
float awt,atat;
void input(){
printf("Enter Number of processes:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("Enter Burst Time of process %d:",i+1);
scanf("%d",&arr[i].bt);
printf("Enter Arrival Time of process %d:",i+1);
scanf("%d",&arr[i].at);
arr[i].sr=i;
arr[i].flag=0;
}
}
void calculate(){
int i,j,btto;
arr[0].wt=0;
//arr[0].flag=1;
int btt=arr[0].bt;
arr[0].tat=arr[0].bt;
atat=arr[0].bt;
i=0;
do{
printf("Start of Do While\n");
for(j=1;j<n;j++){
printf("cmp arr[j] %d btt %d jFlag %d\n",arr[j].at,btt,arr[j].flag);
if(arr[j].at<btt )
enqueue(arr[j]);
}
sortQueue();
printf("Sorted Queue\n");
btto=btt;//Starting time for this process
printf("Btto=%d\n",btto);
int pid=dequeue();
while(arr[pid].flag==1){
pid=dequeue();
}
btt+=arr[pid].bt;
printf("After Adding %d btt =%d\n",queue[f].bt,btt);
arr[pid].flag=1;//to show that process is executed
printf("%d Process %d executed",i,pid);
arr[pid].wt=btto - arr[pid].at;
arr[pid].tat= arr[pid].bt+ arr[pid].wt;
printf("A %d %d %d %d\n",btto,arr[pid].at,arr[pid].bt,arr[pid].wt);
awt+=arr[pid].wt;
atat+= arr[pid].tat;
//printf("%d %d\n",f,r);
i++;
printf("I incremented\n");
dispq();
while(f!=-1)
dequeue();
printf("Queue refresed Ready for next run\n");
}while(i<n-1);
awt/=n;
atat/=n;
}
void display(){
int i;
printf("SR.\tA.T.\tB.T.\tW.T.\tT.A.T.\n");
for(i=0;i<n;i++)
{
printf("%3d\t%3d\t%3d\t%3d\t%4d\n",i+1,arr[i].at,arr[i].bt,arr[i].wt,arr[i].tat);
}
printf("Average Waiting Time: %f\nAverage Turn Around Time:%f",awt,atat);
}
int main(){
printf("SJF CPU Scheduling Algorithm\n");
input();
calculate();
display();
getch();
}

Sunday, March 11, 2012

What is 'Turn around time' in operating system (OS) | cpu scheduling algorithms

Turnaround is total time between submission of a process and its completion.

It is also required as input for scheduling scheduling algorithms.

What is 'burst time' in operating system (OS) | cpu scheduling algorithms

Burst Time is actually time that is required to complete execution of particular task or process.

CPU Scheduling algorithms require Burst time as input.

Different CPU Scheduling algorithms are :
a) FCFS.
b) SJF.
c) PRIORITY.
d) ROUND ROBIN.