Saturday, July 22, 2017

Operating Systems Lab Exp 1cd

Priority Scheduling Algorithm
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
Round Robin Scheduling Algorithm
#include<stdio.h>
void main()
{
      int i, limit, total = 0, x, counter = 0, time_quantum;
      int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
      float average_wait_time, average_turnaround_time;
      clrscr();
      printf("\nEnter Total Number of Processes:\t");
      scanf("%d", &limit);
      x = limit;
      for(i = 0; i < limit; i++)
      {
              printf("\nEnter Details of Process[%d]\n", i + 1);
              printf("Arrival Time:\t");
              scanf("%d", &arrival_time[i]);
              printf("Burst Time:\t");
              scanf("%d", &burst_time[i]);
              temp[i] = burst_time[i];
      }
      printf("\nEnter Time Quantum:\t");
      scanf("%d", &time_quantum);
      printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
      for(total = 0, i = 0; x != 0;)
      {
              if(temp[i] <= time_quantum && temp[i] > 0)
              {
                     total = total + temp[i];
                     temp[i] = 0;
                     counter = 1;
              }
              else if(temp[i] > 0)
              {
                     temp[i] = temp[i] - time_quantum;
                     total = total + time_quantum;
              }
              if(temp[i] == 0 && counter == 1)
              {
                     x--;
                     printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);
                     wait_time = wait_time + total - arrival_time[i] - burst_time[i];
                     turnaround_time = turnaround_time + total - arrival_time[i];
                     counter = 0;
              }
              if(i == limit - 1)
              {
                     i = 0;
              }
              else if(arrival_time[i + 1] <= total)                            
              {
                     i++;
              }
              else
              {
                     i = 0;
              }
      }
      average_wait_time = wait_time * 1.0 / limit;
      average_turnaround_time = turnaround_time * 1.0 / limit;
      printf("\n\nAverage Waiting Time:\t%f", average_wait_time);
      printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);
      getch();

}

Thursday, July 20, 2017

Operating Systems Lab Programs

1.Write a C program to find total and average of student’s marks by entering name, rollno, branch and six subjects marks.

AIM:


Source Code

#include <stdio.h>
int main()
{
    char name[20],rollno[10],branch[3];
    int sub[6],i,n,s,j,tot=0,avg=0;
    printf("how many students data u need to enter ");
    scanf("%d",&n);
    printf("now enter the students details \n");
    for (i=0;i<n;i++)
    {
        printf("Enter the name of the student");
        scanf("%s",name);
        printf("Enter the rollno of the student");
        scanf("%s",rollno);
        printf("Enter the branch of the student");
        scanf("%s",branch);
        printf(" how many subjects marks u want to enter");
        scanf("%d",&s);
        for(j=0;j<s;j++)
        {
        printf("subject marks of the student%d",j+1);
        scanf("%d",&sub[j]);
        tot=tot+sub[j];
        }
        printf("\n Total marks of the student %d %d\n",i+1,tot);
        avg=tot/s;
        printf("Average of the marks of the student %d %d \n",i+1,avg);
    }
    printf("\n successfully Completed\n");
    return 0;
}
2.First Come First Served Scheduling Algorithm

AIM:


Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],b[10],t[10],w[10],c[10],i,m;
float  att=0,awt=0;
clrscr();
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; c[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the arrival times burst times");
for(i=0;i<n;i++)
scanf("%d %d",&a[i],&b[i]);
c[0]=0;
for(i=0;i<n;i++)
c[i+1]=c[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=c[i+1]-a[i];
w[i]=t[i]-b[i];
awt=awt+w[i];
att=att+t[i];
}
awt =awt/n;
att=att/n;
printf("\n\tprocess\tarrival time \twaiting time\tturn arround time\n");
for(i=0;i<n;i++)
{
printf("\tp%d\t\t%d\t\t%d\t\t%d\n",i,a[i],w[i],t[i]);
}
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
getch();
}
3.Shortest Job First Scheduling Algorithm

AIM:


Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int bt[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & burst time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&bt[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(bt[i]<bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
          st[i]=at[i];
else st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\tbursttime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],bt[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}