OS Lab Programs
#1]Program to display "welcome to unix world" n times
#include<stdio.h>
int main()
{
int i,n;
printf("Enter number");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Welcome to UNIX WORLD\n");
}
#2]program to display process id,parent id and group id
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("process id %d\n",getpid());
printf("Parent id %d\n",getppid());
printf("group id %d\n",getgid());
}
#3]program to print limit of system configuration using built in
#include<stdio.h>
#include<unistd.h>
#include<sys/sysinfo.h>
int main()
{
long ret;
if((ret=sysconf(_SC_ARG_MAX))!=-1)
printf("argmax=%d",ret);
else
perror("args");
if((ret=sysconf(_SC_CHILD_MAX))!=-1)
printf("\nchildmax=%d",ret);
else
perror("child");
if((ret=sysconf(_SC_CLK_TCK))!=-1)
printf("\nclickmax=%d",ret);
else
perror("clicks");
if((ret=sysconf(_SC_NPROCESSORS_CONF))!=-1)
printf("\nnprocessors=%d",ret);
else
perror("\nnprocessors");
}
#4]program to create a child process using fork in-built function
#include<stdio.h>
int main()
{
int pid;
printf("process id %d\n",getpid());
pid=fork();
if(pid<0)
printf("fork failed child not created\n");
else if(pid==0)
printf("child created %d\n",getpid());
return 0;
}
#5]program to dispaly "UNIX PROGRAMMING LAB" n times using system calls and user defined function display(char*). n is value read from keyboard.
#include<stdio.h>
#include<string.h>
void display(char *);
int main()
{
system("clear");
display("UNIX PROGRAMMING LAB\n");
}
void display(char *s)
{
int l,n;
char ch[10],mes[]="Enter n value:";
write(1,mes,strlen(mes));
read(0,ch,10);
n=atoi(ch);
while(n--,n>-1)
write(1,s,strlen(s));
}
#6]Program to implement First Come First Serve Algorithm
#include<stdio.h>
#include<unistd.h>
int main()
{
int bursttime[4];
int waitingtime[4];
int n,i,j;
printf("enter number of processes:");
scanf("%d",&n);
printf("enter burst times of %d processes:",n);
for(i=0;i<n;i++)
{
printf("\nEnter burst time of process %d:",i+1);
scanf("%d",&bursttime[i]);
}
printf("\ncalculating the waiting time....");
for(i=0;i<n;i++)
{
waitingtime[i]=0;
for(j=i-1;j>=0;j--)
{
waitingtime[i]=waitingtime[i]+bursttime[j];
}
}
printf("\n\n\tPROCESS\tBURST TIME \tWAITING TIME");
for(i=0;i<n;i++)
{
printf("\n\t%d\t%d\t\t%d",i+1,bursttime[i],waitingtime[i]);
}
printf("\n");
}
#7]program to implement shortest job first(non- preemption)
#include<stdio.h>
struct process
{
int pno,bt,wt,ft,tat;
}a[10],temp;
main()
{
int i,n,j;
float twt=0.0,total=0.0;
float avgwt,avgtat;
printf("\nenter no of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter pno,bt:");
scanf("%d%d",&a[i].pno,&a[i].bt);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].bt>a[j].bt)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[0].ft=a[0].bt;
a[0].wt=0;
a[0].tat=a[0].wt+a[0].bt;
a[i].wt=a[i-1].ft;
a[i].tat=a[i].wt+a[i].bt;
}
printf("pno\tbt\tft\twt\ttat\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d",a[i].pno,a[i].bt,a[i].ft,a[i].wt,a[i].tat);
}
for(i=0;i<n;i++)
{
twt=twt+a[i].wt;
total=total=total+a[i].tat;
avgwt=twt/n;
avgtat=total/n;
}
printf("avg waiting time is %f\n",avgwt);
printf("avg turnaround time is %f",avgtat);
}
#8]Program to print n times "UNIX PROGRAMMING LAB" reading from "infile.txt" and print the message in "outfile.txt" using a user defined function writefile(n)
#include<stdio.h>
char st[]="Unix programming lab";
main()
{
FILE *fp;
int n;
fp=fopen("infile.txt","w");
printf("\nhow many times do you want to dispaly");
scanf("%d",&n);
fprintf(fp,"%d",n);
writefile(n);
fclose(fp);
}
writefile(int n)
{
int i=0;
FILE *fp;
fp=fopen("outfile.txt","w");
while(i<n)
{
fprintf(fp,"%s\n",st);
i++;
}
fclose(fp);
}
#9]Program to implement Shortest job first (preemption)
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[10],b[10],w[10],at[10],n,s=0,i,j,k,t,t1,t2;
float avg=0,tt=0;
printf("enter no. of processes\n");
scanf("%d",&n);
printf("enter burst times of proceses\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
{
printf("invalid input");
exit(0);
}
b[i]=0;
tt=tt+a[i];
}
printf("enter arrival times\n");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
k=0;
j=1;
for(i=0;i<n-1;i++)
{
if(a[i]!=0)
{
while(at[i+1]!=s)
{
k=i;
for(j=0;j<i;j++)
{
if(a[i]>a[j])
k=j;
}
a[k]=a[k]-1;
b[k]++;
s=s+1;
}
}
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
t1=at[i];
at[i]=at[j];
at[j]=t1;
t2=b[i];
b[i]=b[j];
b[j]=t2;
}
}
}
for(i=0;i<n;i++)
{
w[i]=s-b[i];
s=s+a[i];
}
for(i=0;i<n;i++)
{
w[i]=w[i]-at[i];
avg=avg+w[i];
}
tt=tt+avg;
tt=tt/n;
avg=avg/n;
printf("turn around time is %f",tt);
printf("Avg waiting time is %f",avg);
}
#10]Program to implement Bankers Algorithm
#include<stdio.h>
#include<stdlib.h>
struct process
{
int pid,max[8],alloc[8],need[8],finish;
}p[8];
main()
{
int total[8],avail[8],count=0,i,j,k=0,n,r;
printf("\n Enter no. of process:");
scanf("%d",&n);
printf("\n Enter the no. of resources");
scanf("%d",&r);
for(j=0;j<r;j++)
{
printf("Enter the total resources of type %d",j);
scanf("%d",&total[j]);
avail[j]=total[j];
}
for(i=0;i<n;i++)
{
printf("Enter the process ID: \n");
scanf("%d",&p[i].pid);
p[i].finish=0;
for(j=0;j<r;j++)
{
printf("Enter maximum resources requried of type %d:",j);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("Enter the resoursces allocated of type %d",j);
scanf("%d",&p[i].alloc[j]);
}
}
printf("The available resources sre:\n");
for(j=0;j<r;j++)
printf("%3d \n",avail[j]);
printf("The need matrix i.e:\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
p[i].need[j]=p[i].max[j]-p[i].alloc[j];
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
printf("%3d",p[i].need[j]);
printf("\n");
}
for(i=0,k=0;k<n;i++)
{
if(i==n)
i=0;
if(p[i].finish==0)
{
for(j=0;j<r && p[i].need[j]<=avail[j];j++)
if(j==r)
{
for(j=0;j<r;j++)
{
avail[j]+=p[i].alloc[i];
}
count++;
printf("\n process %d is finished-avail",p[i].pid);
for(j=0;j<r;j++)
printf("%3d",avail[j]);
if(count==n)
{
printf("\n the system is in safe state");
exit(0);
}
k=0;
}
else
k++;
}
else
k++;
}
printf("the system is in unsafe state \n");
}
#11] FCFS with arrival time
#include<stdio.h>
struct process
{
int no,at,bt,ft,wt,tt;
}a[10],temp;
main()
{
int n,i,j,twt=0,tat=0;
float awt,atat;
printf("Enter no. of processes");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter process no");
scanf("%d",&a[i].no);
printf("Enter arrival time and brust time");
scanf("%d%d",&a[i].at,&a[i].bt);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i].at>a[j].at)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
a[1].ft=a[1].at+a[1].bt;
a[1].wt=0;
a[1].tt=a[1].wt+a[1].bt;
for(i=2;i<=n;i++)
{
if(a[i-1].ft>a[i].at)
{
a[i].ft=a[i-1].ft+a[i].bt;
a[i].wt=a[i-1].ft-a[i].at;
a[i].tt=a[i].wt+a[i].bt;
}
else
{
a[i].ft=a[i].at+a[i].bt;
a[i].wt=0;
a[i].tt=a[i].wt+a[i].bt;
}
}
printf("\n no\tat\tbt\tft\twt\ttt\n");
for(i=1;i<=n;i++)
{
twt=twt+a[i].wt;
tat=tat+a[i].tt;
printf("\n%d\t%d\t%d\t%d\t%d\t%d",a[i].no,a[i].at,a[i].bt,a[i].ft,a[i].wt,a[i].tt);
}
awt=(float)twt/n;
atat=(float)tat/n;
printf("average wating time is %f",awt);
printf("\n average turn around time is %f",atat);
}
FIFO replacement
#include<stdio.h>
int main()
{
int i,n;
printf("Enter number");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Welcome to UNIX WORLD\n");
}
#2]program to display process id,parent id and group id
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("process id %d\n",getpid());
printf("Parent id %d\n",getppid());
printf("group id %d\n",getgid());
}
#3]program to print limit of system configuration using built in
#include<stdio.h>
#include<unistd.h>
#include<sys/sysinfo.h>
int main()
{
long ret;
if((ret=sysconf(_SC_ARG_MAX))!=-1)
printf("argmax=%d",ret);
else
perror("args");
if((ret=sysconf(_SC_CHILD_MAX))!=-1)
printf("\nchildmax=%d",ret);
else
perror("child");
if((ret=sysconf(_SC_CLK_TCK))!=-1)
printf("\nclickmax=%d",ret);
else
perror("clicks");
if((ret=sysconf(_SC_NPROCESSORS_CONF))!=-1)
printf("\nnprocessors=%d",ret);
else
perror("\nnprocessors");
}
#4]program to create a child process using fork in-built function
#include<stdio.h>
int main()
{
int pid;
printf("process id %d\n",getpid());
pid=fork();
if(pid<0)
printf("fork failed child not created\n");
else if(pid==0)
printf("child created %d\n",getpid());
return 0;
}
#5]program to dispaly "UNIX PROGRAMMING LAB" n times using system calls and user defined function display(char*). n is value read from keyboard.
#include<stdio.h>
#include<string.h>
void display(char *);
int main()
{
system("clear");
display("UNIX PROGRAMMING LAB\n");
}
void display(char *s)
{
int l,n;
char ch[10],mes[]="Enter n value:";
write(1,mes,strlen(mes));
read(0,ch,10);
n=atoi(ch);
while(n--,n>-1)
write(1,s,strlen(s));
}
#6]Program to implement First Come First Serve Algorithm
#include<stdio.h>
#include<unistd.h>
int main()
{
int bursttime[4];
int waitingtime[4];
int n,i,j;
printf("enter number of processes:");
scanf("%d",&n);
printf("enter burst times of %d processes:",n);
for(i=0;i<n;i++)
{
printf("\nEnter burst time of process %d:",i+1);
scanf("%d",&bursttime[i]);
}
printf("\ncalculating the waiting time....");
for(i=0;i<n;i++)
{
waitingtime[i]=0;
for(j=i-1;j>=0;j--)
{
waitingtime[i]=waitingtime[i]+bursttime[j];
}
}
printf("\n\n\tPROCESS\tBURST TIME \tWAITING TIME");
for(i=0;i<n;i++)
{
printf("\n\t%d\t%d\t\t%d",i+1,bursttime[i],waitingtime[i]);
}
printf("\n");
}
#7]program to implement shortest job first(non- preemption)
#include<stdio.h>
struct process
{
int pno,bt,wt,ft,tat;
}a[10],temp;
main()
{
int i,n,j;
float twt=0.0,total=0.0;
float avgwt,avgtat;
printf("\nenter no of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter pno,bt:");
scanf("%d%d",&a[i].pno,&a[i].bt);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].bt>a[j].bt)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[0].ft=a[0].bt;
a[0].wt=0;
a[0].tat=a[0].wt+a[0].bt;
a[i].wt=a[i-1].ft;
a[i].tat=a[i].wt+a[i].bt;
}
printf("pno\tbt\tft\twt\ttat\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d",a[i].pno,a[i].bt,a[i].ft,a[i].wt,a[i].tat);
}
for(i=0;i<n;i++)
{
twt=twt+a[i].wt;
total=total=total+a[i].tat;
avgwt=twt/n;
avgtat=total/n;
}
printf("avg waiting time is %f\n",avgwt);
printf("avg turnaround time is %f",avgtat);
}
#8]Program to print n times "UNIX PROGRAMMING LAB" reading from "infile.txt" and print the message in "outfile.txt" using a user defined function writefile(n)
#include<stdio.h>
char st[]="Unix programming lab";
main()
{
FILE *fp;
int n;
fp=fopen("infile.txt","w");
printf("\nhow many times do you want to dispaly");
scanf("%d",&n);
fprintf(fp,"%d",n);
writefile(n);
fclose(fp);
}
writefile(int n)
{
int i=0;
FILE *fp;
fp=fopen("outfile.txt","w");
while(i<n)
{
fprintf(fp,"%s\n",st);
i++;
}
fclose(fp);
}
#9]Program to implement Shortest job first (preemption)
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[10],b[10],w[10],at[10],n,s=0,i,j,k,t,t1,t2;
float avg=0,tt=0;
printf("enter no. of processes\n");
scanf("%d",&n);
printf("enter burst times of proceses\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
{
printf("invalid input");
exit(0);
}
b[i]=0;
tt=tt+a[i];
}
printf("enter arrival times\n");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
k=0;
j=1;
for(i=0;i<n-1;i++)
{
if(a[i]!=0)
{
while(at[i+1]!=s)
{
k=i;
for(j=0;j<i;j++)
{
if(a[i]>a[j])
k=j;
}
a[k]=a[k]-1;
b[k]++;
s=s+1;
}
}
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
t1=at[i];
at[i]=at[j];
at[j]=t1;
t2=b[i];
b[i]=b[j];
b[j]=t2;
}
}
}
for(i=0;i<n;i++)
{
w[i]=s-b[i];
s=s+a[i];
}
for(i=0;i<n;i++)
{
w[i]=w[i]-at[i];
avg=avg+w[i];
}
tt=tt+avg;
tt=tt/n;
avg=avg/n;
printf("turn around time is %f",tt);
printf("Avg waiting time is %f",avg);
}
#10]Program to implement Bankers Algorithm
#include<stdio.h>
#include<stdlib.h>
struct process
{
int pid,max[8],alloc[8],need[8],finish;
}p[8];
main()
{
int total[8],avail[8],count=0,i,j,k=0,n,r;
printf("\n Enter no. of process:");
scanf("%d",&n);
printf("\n Enter the no. of resources");
scanf("%d",&r);
for(j=0;j<r;j++)
{
printf("Enter the total resources of type %d",j);
scanf("%d",&total[j]);
avail[j]=total[j];
}
for(i=0;i<n;i++)
{
printf("Enter the process ID: \n");
scanf("%d",&p[i].pid);
p[i].finish=0;
for(j=0;j<r;j++)
{
printf("Enter maximum resources requried of type %d:",j);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("Enter the resoursces allocated of type %d",j);
scanf("%d",&p[i].alloc[j]);
}
}
printf("The available resources sre:\n");
for(j=0;j<r;j++)
printf("%3d \n",avail[j]);
printf("The need matrix i.e:\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
p[i].need[j]=p[i].max[j]-p[i].alloc[j];
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
printf("%3d",p[i].need[j]);
printf("\n");
}
for(i=0,k=0;k<n;i++)
{
if(i==n)
i=0;
if(p[i].finish==0)
{
for(j=0;j<r && p[i].need[j]<=avail[j];j++)
if(j==r)
{
for(j=0;j<r;j++)
{
avail[j]+=p[i].alloc[i];
}
count++;
printf("\n process %d is finished-avail",p[i].pid);
for(j=0;j<r;j++)
printf("%3d",avail[j]);
if(count==n)
{
printf("\n the system is in safe state");
exit(0);
}
k=0;
}
else
k++;
}
else
k++;
}
printf("the system is in unsafe state \n");
}
#11] FCFS with arrival time
#include<stdio.h>
struct process
{
int no,at,bt,ft,wt,tt;
}a[10],temp;
main()
{
int n,i,j,twt=0,tat=0;
float awt,atat;
printf("Enter no. of processes");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter process no");
scanf("%d",&a[i].no);
printf("Enter arrival time and brust time");
scanf("%d%d",&a[i].at,&a[i].bt);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i].at>a[j].at)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
a[1].ft=a[1].at+a[1].bt;
a[1].wt=0;
a[1].tt=a[1].wt+a[1].bt;
for(i=2;i<=n;i++)
{
if(a[i-1].ft>a[i].at)
{
a[i].ft=a[i-1].ft+a[i].bt;
a[i].wt=a[i-1].ft-a[i].at;
a[i].tt=a[i].wt+a[i].bt;
}
else
{
a[i].ft=a[i].at+a[i].bt;
a[i].wt=0;
a[i].tt=a[i].wt+a[i].bt;
}
}
printf("\n no\tat\tbt\tft\twt\ttt\n");
for(i=1;i<=n;i++)
{
twt=twt+a[i].wt;
tat=tat+a[i].tt;
printf("\n%d\t%d\t%d\t%d\t%d\t%d",a[i].no,a[i].at,a[i].bt,a[i].ft,a[i].wt,a[i].tt);
}
awt=(float)twt/n;
atat=(float)tat/n;
printf("average wating time is %f",awt);
printf("\n average turn around time is %f",atat);
}
FIFO replacement
#include<stdio.h> main() { int n,i,j,k,l,np,nf,pf,pos,flag=0; int p[20],f[20]; printf("\nEnter the number of free frames:"); scanf("%d",&nf); printf("\nEnter the referece string length:"); scanf("%d",&np); printf("\nEnter the reference string:"); for(i=0;i<np;i++) scanf("%d",&p[i]); for(i=0;(i<nf)&&(i<np);i++) { f[i]=-1; } pos=0; pf=0; for(j=0;j<np;j++) { flag=0; for(k=0;k<np;k++) if(f[k]==p[j]) { flag=1; break; } if(flag!=1) { pf++; printf("%d %d\n",p[j],pf); f[pos]=p[j]; pos++; if(pos==nf) pos=0; } } printf("\nNumber of page faults is %d",pf); }
LEAST RECENTLY USED algorithm
#include<stdio.h> int main() { int i,m,n,k,a[10],b[10],pf,flag=0,pos=0,l,j; printf("\nEnter the number of free frames:"); scanf("%d",&m); printf("\nEnter the reference string length:");\ scanf("%d",&n); printf("\nEnter the refernce string:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } pf=0; for(i=0;(i<m)&&(i<n);i++) { b[i]=-1; } for(k=0;k<n;k++) { flag=0; for(j=0;j<m;j++) { if(a[k]==b[j]) { flag=1;pos=j; break; } } if(flag==1) { for(l=pos;l<m-1;l++) b[l]=b[l+1]; b[m-1]=a[k]; } else { for(l=0;l<m-1;l++) b[l]=b[l+1]; b[m-1]=a[k]; pf++; printf("%d %d\n",a[k],pf); } } printf("\nNumber of page faults is %d\n",pf); }
OPTIMAL page replacement
#include<stdio.h> main() { int i,j,m,n,a[20],b[20],flag,c[10]; int pos=0,max,l,p,k,pf; printf("\nEnter the number free frames:"); scanf("%d",&m); printf("\nEnter thr reference string length:"); scanf("%d",&n); printf("\nEnter the reference string:"); for(i=0;i<n;i++) scanf("%d",&a[i]); pf=0; for(i=0;i<m;i++) b[i]=-1; for(i=0;i<m;i++) c[i]=0; for(k=0;k<n;k++) { flag=0; for(j=0;j<m;j++) { if(a[k]==b[j]) { flag=1; break; } } if(flag!=1) { for(p=0;p<m;p++) { for(l=k+1;l<n;l++) { if(a[l]==b[p]) break; else c[p]=c[p]+1; } } max=c[0]; pos=0; for(i=1;i<m;i++) { if(max<c[i]) { max=c[i]; pos=i; } } i=pos; b[i]=a[k]; pf=pf+1; for(i=0;i<m;i++) c[i]=0; } } printf("\nNumber of page faults:%d\n",pf); }
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.