0% found this document useful (0 votes)
24 views24 pages

OS Programmes

os manual

Uploaded by

Tulasirama M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views24 pages

OS Programmes

os manual

Uploaded by

Tulasirama M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1 ) Write A C Program Simulate The Following CPU Scheduling Algorithms ?

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 :

enter number of processes : 3


enter the burst time of processes : 24 3 3
bt wt tt
24 0 24
3 24 27
3 27 30
aw=17.000000 at=27.000000
B ) SJF

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 :

enter number of processes : 4


enter the burst time of processes : 6 8 7 3
bt wt tt
3 0 3
6 3 9
7 9 16
8 16 24
aw=7.000000 at=13.000000

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 :

Enter number of processes : 4


Enter burst time for sequences : 5 12 8 20
Enter time quantum : k
Pno Burst time Wait time Turn around time
1 5 0 5
2 12 5 17
3 8 17 25
4 20 25 45
Avg wait time is 11.750000 Avg turn around time is 23.000000
D ) Priority

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 :

enter the number of processes : 4


The process 1:
Enter the burst time of processes : 5
Enter the priority of processes 1 : 1
The process 2:
Enter the burst time of processes : 8
Enter the priority of processes 2 : 2
The process 3:
Enter the burst time of processes : 12
Enter the priority of processes 3 : 3
The process 4:
Enter the burst time of processes : 20
Enter the priority of processes 4 : 4

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 ?

A ) Dead Lock Avoidance

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 :

Enter the no of processes : 4


Enter the no of resources : 3
process 1
maximum value for resource 1 : 3
maximum value for resource 2 : 2
maximum value for resource 3 : 2
allocated from resource 1 : 1
allocated from resource 2 : 0
allocated from resource 3 : 0
process 2
maximum value for resource 1 : 6
maximum value for resource 2 : 1
maximum value for resource 3 : 3
allocated from resource 1 : 5
allocated from resource 2 : 1
allocated from resource 3 : 1
process 3
maximum value for resource 1 : 3
maximum value for resource 2 : 1
maximum value for resource 3 : 4
allocated from resource 1 : 2
allocated from resource 2 : 1
allocated from resource 3 : 1
process 4
maximum value for resource 1 : 4
maximum value for resource 2 : 2
maximum value for resource 3 : 2
allocated from resource 1 : 0
allocated from resource 2 : 0
allocated from resource 3 : 2
Enter total value of resource 1 : 9
Enter total value of resource 2 : 3
Enter total value of resource 3 : 6
max allocated needed total avail
P1 322 100 222 936 112
P2 613 511 102
P3 314 211 103
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


p2 010 623
p1 401 723
p3 620 934
p4 514 936
the above sequence is a safe sequence
B ) Dead Lock Prevention

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 :

Enter no. of processes, resources : 3 2


Enter allocation matrix : 2 4 5
345
enter max matrix : 4 3 4
561
enter available matrix : 2 5
Failing : Mutual exclusion
By allocating required resources to process 3 deadlock is prevented

lack of preemption

deadlock is prevented by allocating needed resources

failing : Hold and Wait condition


AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT DEADLOCK

4 ) Write A C Program To Implement The Producer – Consumer Problem Using


Semaphores Using Unix / Linux System Calls ?

#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

5 ) Write a C program to illustrate the following IPC mechanisms ?

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 :

Create File : vi myfifo.txt

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 :

Write Data: Hello


Hello
Data send is : Hello

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 :

Data Received is : Hello

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 :

Write Data: Hello


Data written in memory: Hello

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 :

Data read from memory: Hello


6 Write a C program to simulate the following memory segment techniques ?

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 :

Enter the memory size -- 1000


Enter the page size -- 100
The no. of pages available in memory are -- 10
Enter number of processes -- 3
Enter no. of pages required for p[1]-- 4
Enter pagetable for p[1]8 6 9 5
Enter no. of pages required for p[2]-- 5
Enter pagetable for p[2]1 4 5 7 3
Enter no. of pages required for p[3]-- 5
Memory is Full
Enter Logical Address to find Physical Address
Enter process no. and pagenumber and offset 2 3 60
The Physical Address is -- 760

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 :

Enter The Segment Count : 2


Enter The 1 Size : 4
Enter The Base Address : 1 2 3 4 5
Enter The 2 Size : 3
Enter The Base Address : 10 7 8 9
Enter The Segment No And OffSet Value : 1 2
The OffSet Is Less Than : 3
10+2=12
The Element 9 Is At 12

You might also like