OS Programmes
OS Programmes
A ) FCFS
Programme :
#include<stdio.h>
void main()
{
int i,j,bt[10],n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
printf("enter number of processes : ");
scanf("%d",&n);
printf("enter the burst time of processes : ");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=(float)w1/n;
at=(float)t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f at=%f",aw,at);
}
Result :
Programme :
#include<stdio.h>
void main()
{
int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
printf("enter number of processes : ");
scanf("%d",&n);
printf("enter the burst time of processes : ");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
if(bt[i]>bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
}
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=(float)w1/n;
at=(float)t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f at=%f",aw,at);
}
Result :
C ) Round Robin
Programme :
#include<stdio.h>
void main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
printf("Enter number of processes : ");
scanf("%d",&n);
printf("Enter burst time for sequences : ");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum : ");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Pno Burst time Wait time Turn around time");
for(i=0;i<n;i++)
printf("\n%d\t %d\t %d\t %d",i+1,bt[i],wt[i],tat[i]);
printf("\nAvg wait time is %f Avg turn around time is %f",awt,atat);
}
Result :
Programme :
#include<stdio.h>
void main()
{
int i,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],w1=0,t1=0,s;
float aw,at;
printf("enter the number of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("The process %d:\n",i+1);
printf("Enter the burst time of processes : ");
scanf("%d",&bt[i]);
printf("Enter the priority of processes %d : ",i+1);
scanf("%d",&prior[i]);
pno[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(prior[i]<prior[j])
{
s=prior[i];
prior[i]=prior[j];
prior[j]=s;
s=bt[i];
bt[i]=bt[j];
bt[j]=s;
s=pno[i];
pno[i]=pno[j];
pno[j]=s;
}
}
}
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
aw=(float)w1/n;
at=(float)t1/n;
}
printf("\np\t bt\t wt\t tat\t prior\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\t %d\t %d\n",pno[i],bt[i],wt[i],tt[i],prior[i]);
printf("aw=%f at=%f\n",aw,at);
}
Result :
p bt wt tat prior
1 5 0 5 1
2 8 5 13 2
3 12 13 25 3
4 20 25 45 4
aw=10.750000 at=22.000000
2 ) Write Programs Using The I / O System Calls Of Unix / Linux Operating System ?
Programme :
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
int main()
{
int fd[2];
char buf1[25]="just a test\n";
char buf2[100];
fd[0] = open("tfile.txt",O_RDWR);
fd[1] = open("tfile.txt",O_RDWR);
write(fd[0],buf1,strlen(buf1));
printf("\nEnter your text now...");
scanf("%[^\n]s",buf1);
write(fd[0],buf1,strlen(buf1));
write(1, buf2, read(fd[1],buf2,sizeof(buf2)));
close(fd[0]);
close(fd[1]);
printf("\n");
return 0;
}
Result :
Hello
Just a test
Hello
3 ) To Implement Dead Lock Avoidance & Prevention By Using Banker’s Algorithm ?
Program :
#include<stdio.h>
//#include<conio.h>
struct da
{
int
max[10],al[10],need[10],befor
e[10],after[10]; }p[10];
void main() {
int
i,j,k,l,r,n,tot[10],av[10],cn=0,cz=
0,temp=0,c=0;
//clrscr();
printf("\n Enter the
no of processes:");
scanf("%d",&n);
printf("\n Enter the
no of resources:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("process %d \n",i+1);
for(j=0;j<r;j++)
{
printf("maximum value for
resource %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++) {
printf("allocated from
resource %d:",j+1);
scanf("%d",&p[i].al[j]);
p[i].need[j]=p[i].max[j]-p[i].al[j];
}
}
for(i=0;i<r;i++)
{
printf("Enter total value of
resource %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++) {
for(j=0;j<n;j++)
temp=temp+p[j].al[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t max allocated
needed total avail");
for(i=0;i<n;i++) {
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].al[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE \t
AVAIL AFTER");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j]>av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n p%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n the above sequence is
a safe sequence"); else
printf("\n deadlock occured");
//getch();
}
Result :
Programme :
#include<stdio.h>
int max[10][10],alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0;
main( )
{
printf("Enter no. of processes, resources : ");
scanf("%d%d",&p,&r);
printf("Enter allocation matrix : ");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
Aprintf(" enter max matrix : ");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf(" enter available matrix : ");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
àfor(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun();
if(flag==0)
{
if(finish[i]!=1)
{
printf("\n\n Failing : Mutual exclusion");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}fun();
printf("\n By allocating required resources to process %d deadlock is prevented ",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("\n\n deadlock is prevented by allocating needed resources");
printf(" \n \n failing : Hold and Wait condition ");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun( );
printf("\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT DEADLOCK");
}
}
}
fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else
break;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
}
Result :
lack of preemption
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Output :
1.Producer
2.Consumer
3.Exit
Enter your choice : 1
Producer produces the item 1
Enter your choice : 2
Consumer consumes item 1
Enter your choice : 2
Buffer is empty!!
Enter your choice : 1
Producer produces the item 1
Enter your choice : 1
Producer produces the item 2
Enter your choice : 1
Producer produces the item 3
Enter your choice : 1
Buffer is full!!
Enter your choice : 3
A ) Pipes
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#define MSGSIZE 16
char* msg1 = "hello, world #1";
char* msg2 = "hello, world #2";
char* msg3 = "hello, world #3";
int main()
{
char inbuf[MSGSIZE];
int p[2], i;
if (pipe(p) < 0)
exit(1);
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
for (i = 0; i < 3; i++) {
read(p[0], inbuf, MSGSIZE);
printf("% s\n", inbuf);
}
return 0;
}
Output :
hello, world #1
hello, world #2
hello, world #3
B ) FIFO
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char *myfifo="myfifo.txt";
mkfifo(myfifo,0666);
char arr1[80],arr2[80];
while (1)
{
fd=open(myfifo.txt,O_WRONLY);
scanf(“%s”,arr2,80,stdin);
write(fd,arr2,strlen(arr2)+1);
close(fd);
fd=open(myfifo,O_RDONLY);
read(fd,arr1,sizeof(arr1));
printf("User 2 :%s",arr1);
close(fd);
}
return 0;
}
Output :
Hello
User 2 : Hello
C ) Message Queues
Programme :
( Writer Process )
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer
{
long mesg_type;
char mesg_text[100];
}message;
int main()
{
key_t key;
int msgid;
key=ftok("progfile",65);
msgid=msgget(key,0666|IPC_CREAT);
message.mesg_type=1;
printf("Write Data:");
scanf("%s",message.mesg_text);
msgsnd(msgid, &message, sizeof(message), 0);
printf("Data send is : %s \n", message.mesg_text);
return 0;
}
Output :
Programme :
( Reader Process )
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer
{
long mesg_type;
char mesg_text[100];
}message;
int main()
{
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
msgrcv(msgid, &message, sizeof(message), 1, 0);
printf("Data Received is : %s \n", message.mesg_text);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
Output :
D ) Shared Memory
Programme :
( Writer Process )
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
{
key_t key=ftok("shmfile",65);
int shmid=shmget(key,1024,0666|IPC_CREAT);
char *str=(char*) shmat(shmid,(void*)0,0);
printf("Write Data:");
scanf("%s",str);
printf("Data written in memory: %s\n",str);
shmdt(str);
return 0;
}
Output :
Programme :
( Reader Process )
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
{
key_t key=ftok("shmfile",65);
int shmid=shmget(key,1024,0666|IPC_CREAT);
char *str=(char*)shmat(shmid,(void*)0,0);
printf("Data read from memory: %s\n",str);
shmdt(str);
shmctl(shmid,IPC_RMID,NULL);
return 0;
}
Output :
A ) Paging
Programme :
#include<stdio.h>
main()
{
int ms,ps,nop,np,rempages,i,j,x,y,pa,offset;
int s[10], fno[10][20];
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
printf("\nEnter the page size -- ");
scanf("%d",&ps);
nop=ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
printf("\nEnter number of processes -- ");
scanf("%d",&np);
rempages=nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i]>rempages)
{
printf("\nMemory is Full");
break;
}
rempages=rempages-s[i];
printf("\nEnter pagetable for p[%d]",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and page number and offset ");
scanf("%d %d %d",&x,&y, &offset);
if(x>np||y>=s[i]||offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);
}
}
Output :
B ) Segmentation
Programme :
#include <stdio.h>
int main()
{
int a[100][100],b[1000],i,j,n,x,base,size,seg,off;
printf("Enter The Segment Count : “);
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter The %d Size : ",i+1);
scanf("%d",&size);
a[i][0]=size;
printf("Enter The Base Address : ");
scanf("%d",&base);
a[i][i]=base;
for(j=0;j<size;j++)
{
x=0;
scanf("%d",&x);
b[base]=x;
base++;
b[base]=x;
}
}
printf("Enter The Segment No And OffSet Value : ");
scanf("%d %d",&seg,&off);
if(off<a[seg][0])
{
int abs=a[seg][1]+off;
printf("The OffSet Is Less Than : %d",a[seg][0]);
printf("\n %d+%d=%d\n",a[seg][1],off,abs);
printf("The Element %d Is At %d",b[abs+1],abs);
}
else
{
printf("Error in Locating");
}
}
Output :