Saturday, August 9, 2014

FCFS and SJF program

 This is a program for the implementation of First Come First Serve and Shortest Job First Scheduling. If you have any doubts, please let me know.

#include<stdio.h>
#include<string.h>
struct process
{
   int arr,bur,wait,turn,t;
   char pid[5];
}p[100],temp;

int n;
void chart()
{
            int i,j,k;
            printf("0");
            for(j=0;j<n;j++)
            {
                        i=0;
                        while(i!=p[j].bur)
                        {
                                    printf("=");
                                    i++;
                        }
            printf("%d",p[j].t);
            }
            printf("\n");
            i=0;
            printf("p%s",p[0].pid);
           
            for(j=0,k=1;j<n-1;j++,k++)
            {
                        while(i!=p[j].t)
                        {
                                    printf(" ");
                                    i++;
                        }
            printf("p%s",p[k].pid);
            }
            printf("\n");

}

void avg()
    {
        int i;
        float avgw=0,avgt=0;
        for(i=0;i<n;i++)
       {
        avgw=avgw+p[i].wait;
        avgt=avgt+p[i].turn;
      }
    avgw=avgw/n;
    avgt=avgt/n;

    printf("\nAverage waiting time :%d\nAverage turnaround time :%d\n",avgw,avgt);
 }


void disp()
{
         printf("\npid\tburst\tarrival\twait\tturnaround\n");
         int i;
         for(i=0;i<n;i++)
            {
            printf("%s\t%d\t%d\t%d\t%d\n",p[i].pid,p[i].bur,p[i].
arr,p[i].wait,p[i].turn);
        }
    avg();
    chart();

}

void fcfs()
{
        int i,j;
         for(i=0;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
               {
                if(p[i].arr>p[j].arr)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
             }
        }
        p[0].wait=0;
        p[0].turn=p[0].wait+p[0].bur;
                        p[0].t=p[0].bur;

        for(i=1;i<n;i++)
       {
            p[i].t=p[i-1].t+p[i].bur;
            p[i].wait=p[i-1].t-p[i].arr;
            p[i].turn=p[i].wait+p[i].bur;
      }
                        disp();
}


void sjf()
{
            int i,j;
            for(i=0;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
                {
                if(p[i].arr>p[j].arr)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
                }
            }
            for(i=1;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
                {
                if(p[i].bur>p[j].bur)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
                }
          }

            p[0].wait=0;
            p[0].turn=p[0].wait+p[0].bur;
            p[0].t=p[0].bur;
            for(i=1;i<n;i++)
            {
               p[i].t=p[i-1].t+p[i].bur;
               p[i].wait=p[i-1].t-p[i].arr;
               p[i].turn=p[i].wait+p[i].bur;
            }
                disp();
}
void main()
{
    int j,i,ch;
    printf("Enter number of processess : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
            printf("Enter pid : ");
            scanf("%s",p[i].pid);
            printf("enter arrival time : ");
            scanf("%d",&p[i].arr);
            printf("Enter burst time : ");
            scanf("%d",&p[i].bur);
        }


    printf("\nMENU\n1.FCFS\n2.SJF\n3.exit\nEnter your choice : ");
    scanf("%d",&j);
    switch(j)
    {
    case 1:fcfs();
              break;
    case 2:  sjf();
              break;
    case 3:
          break;
    default : printf("Wrong Choice");
    }
}





Program for Best Fit, Worst Fit and First Fit

This is a program for the implementation of Best fit, Worst Fit, and First Fit for the operating systems lab. If you have any doubts please let me know.

#include<stdio.h>
#include<stdlib.h>
int i,j,n,x,ch,size,ch1,ch2,y,pid;
struct alloc1
{
  int id,sa,ea,size;
  struct alloc1 *next;
}*start=NULL,*ptr=NULL,*newn=NULL;

struct free1
{
  int stadd,eadd,size1;
  struct free1 *next;
}*start1=NULL,*newn1=NULL,*ptr2=NULL,*preptr2=NULL;

void displayfree(struct free1 *temp)
{
printf("\n\nFree Space\n");
temp=start1;
while(temp!=NULL)
    {
    printf("%d",temp->stadd);
    printf(" - %d\n",temp->eadd);
    temp=temp->next;
    }}

void display(struct alloc1 *temp1)
{
printf("\n\nAllocated\n");
temp1=start;
while(temp1!=NULL)
    {
                        printf("p%d\t",temp1->id);
    printf("%d",temp1->sa);
    printf(" - %d\n",temp1->ea);
    temp1=temp1->next;
    }
}
void init()
{
   newn->sa=ptr2->stadd;
   newn->ea=newn->sa+size-1;
   ptr2->stadd=ptr2->stadd+size;
   ptr2->size1=ptr2->size1-size;
}
void main()
{
    printf("\nEnter the number of processes:\n");
  scanf("%d",&n);
   for(i=0;i<n;i++)
  {
         newn=(struct alloc1 *) malloc(sizeof(struct alloc1));
         printf("\nEnter process id: ");
         scanf("%d",&newn->id);
         printf("\nEnter the start address: ");
         scanf("%d",&newn->sa);
         printf("\nEnter the end address: ");
         scanf("%d",&newn->ea);
         newn->size=(newn->ea-newn->sa)+1;
         if(start==NULL)
        {
            start=newn;
            start->next=NULL;
            ptr=start;
        }
       else
       {
            while(ptr->next!=NULL)
            {
            ptr=ptr->next;
            }
            ptr->next=newn;
            ptr=ptr->next;
            ptr->next=NULL;
       }
}
 printf("\nEnter the details of free space\n");
 do
 {
    newn1=(struct free1 *) malloc(sizeof(struct free1));
   printf("\nEnter the starting address: ");
   scanf("%d",&newn1->stadd);
   printf("\nEnter the ending address: ");
   scanf("%d",&newn1->eadd);
   newn1->size1=(newn1->eadd-newn1->stadd)+1;
    if(start1==NULL)
  {
    start1=newn1;
    start1->next=NULL;
  ptr2=start1;
   }
   else
   {
     while(ptr2->next!=NULL)
       ptr2=ptr2->next;
     ptr2->next=newn1;
     ptr2=ptr2->next;
     ptr2->next=NULL;
  }
  ptr2=start1;
  printf("\nPress 1 to add more...");
  scanf("%d",&ch);
 }while(ch==1);
printf("\n BEFORE FITTING\n");
displayfree(ptr2);
 ptr=start;
display(ptr);
do
{
  printf("\nEnter the size of new process: ");
  scanf("%d",&size);
  printf("\nEnter the process id:");
  scanf("%d",&pid);
 newn=((struct alloc1 *)malloc(sizeof(struct alloc1)));
   newn->next=NULL;  
   newn->size=size;
               newn->id=pid;
     printf("\nMENU:\n1.First fit\n2.Best fit\n3.Worst fit\n4.Exit\n");
     printf("\nEnter your choice:\n");
     scanf("%d",&ch1);
     switch(ch1)
     {
       case 1:
   ptr2=start1;
   if(ptr2->size1==size)
   {
   start1=ptr2->next;
   }
   else
   {
   while(ptr2->size1<size)
   {
    if(ptr2->next==NULL)
     {
        printf("\nInsufficient memory");
  goto lab;
      }
   preptr2=ptr2;
   ptr2=ptr2->next;
   if(ptr->size==size)
   {
   preptr2->next=ptr2->next;
   }}}
   init(); ptr=start;
while(ptr->next!=NULL)
   ptr=ptr->next;
      ptr->next=newn;
   printf("\nFIRST FIT\n");
   display(ptr);
   displayfree(ptr2);
          lab: break;
case 2:
   ptr2=start1;
   x=1000;
   while(ptr2!=NULL)
   {
   if((ptr2->size1>=size)&&(ptr2->size1<=x))
   {
   x=ptr2->size1;
   }
   ptr2=ptr2->next;
  }
  if(x==1000)
  {
   printf("\nInsufficient memory");
  break;
  }
   ptr2=start1;
   while(ptr2->size1!=x)
   {
   preptr2=ptr2;
   ptr2=ptr2->next;
   }
init();
if(x==size)
   preptr2->next=ptr2->next;
   ptr=start;
   while(ptr->next!=NULL)
   ptr=ptr->next;
   ptr->next=newn;
   printf("\nBEST FIT\n");
   display(ptr);
   displayfree(ptr2);
break;
case 3:   
   ptr2=start1;
       y=0;
   while(ptr2!=NULL)
   {
   if((ptr2->size1>=size)&&(ptr2->size1>=y))
   {
   y=ptr2->size1;
   }
   ptr2=ptr2->next;
  }
if(y==0)
   {
   printf("\nInsufficient memory");
  break;
   }
   ptr2=start1;
   while(ptr2->size1!=y)
   {
   preptr2=ptr2;
   ptr2=ptr2->next;
   }
   if(y==size)
   {
   preptr2->next=ptr2->next;
   }
   init(); ptr=start;
   while(ptr->next!=NULL)
   {
   ptr=ptr->next;
   }
   ptr->next=newn;
  printf("\nWORST FIT\n");
   display(ptr);
   displayfree(ptr2);
   break;
case 4:break;
default:break;
}
printf("\npress 1 to add more..");
scanf("%d",&ch2);
}while(ch2==1);
}