0% found this document useful (0 votes)
99 views23 pages

All ProgramsOS (12-23)

The document contains code for implementing deadlock avoidance in an operating system. It defines variables to represent processes, resources, allocation and requests. It takes input for the number of processes, resources, total availability and current allocation. It also takes input to identify a process making a new request. The code checks if granting the new request will cause deadlock by violating the safety algorithm, and denies the request if so to avoid deadlock. Otherwise it updates the allocation to grant the request.

Uploaded by

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

All ProgramsOS (12-23)

The document contains code for implementing deadlock avoidance in an operating system. It defines variables to represent processes, resources, allocation and requests. It takes input for the number of processes, resources, total availability and current allocation. It also takes input to identify a process making a new request. The code checks if granting the new request will cause deadlock by violating the safety algorithm, and denies the request if so to avoid deadlock. Otherwise it updates the allocation to grant the request.

Uploaded by

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

Ex.

No: 2
PROGRAM
echo "enter the first string"
read str1
echo "enter the second string"
read str2
echo "the concatinated string is"$str1$str2

PROGRAM
echo "enter the number"
read n
a=-1
b=1
i=0
while [ $i -le $n ]
do
t=`expr $a + $b`
echo $t
a=$b
b=$t
i=`expr $i+1`
done

PROGRAM
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo enter your choice
read a
echo enter the value of b
read b
echo enter the value of c
read c
echo b is $b c is $c
case $a in
1.d=`expr $b+$c`
echo the sum is $d
;;
2.d=`expr $b-$c`
echo the difference is $d
;;
3.d=`expr $b\*$c`
echo the product is $d
;;
4.d=`expr $b/$c`
echo the quotient is $d
;;Esac
Ex.No: 3A
PROGRAM FCFS
#include<stdio.h>
struct process
{
int id,wait,ser,tottime;
}
p[20];
main()
{
int i,n,j,totalwait=0,totalser=0,avturn,avwait;
printf("enter number of process");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter process id");
scanf("enter process service time");
scanf("%d",&p[i].ser);
}
p[1].wait==0;
p[1].tottime=p[1].ser;
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
p[i].wait=p[i].wait+p[j].ser;
}
totalwait=totalwait+p[i].wait;
p[i].tottime=p[i].wait+p[i].ser;
totalser=totalser+p[i].tottime;
}
avturn=totalser/n;
avwait=totalwait/n;
printf("Id\tservice\twait\ttotal");
for(i=1;i<=n;i++)
{
printf("\n%d\t%d\t%d\t%d\n",p[i].id,p[i].ser,p[i].wait,p[i].tottime);
}
printf("average waiting time %d\n",avwait);
printf("average turnaround time %d\n",avturn);
}
Ex.No: 3B
PROGRAM SJF
#include<stdio.h>
struct ff
{
int pid,ser,wait;
}
p[20];
struct ff tmp;
main()
{
int i,n,j,tot=0,avwait,totwait=0,tturn=0,aturn;
printf("enter the number of process");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("enter process id");
scanf("%d",&p[i]);
printf("enter service time");
scanf("%d",&p[i].ser);
p[i].wait=0;
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(p[i].ser>p[j].ser){
tmp=p[i];
p[i]=p[j];
p[j]=tmp;
}}}
printf("PID\tSER\tWAIT\tTOT\n");
for(i=0;i<n;i++){
tot=tot+p[i].ser;
tturn=tturn+tot;
p[i+1].wait=tot;
totwait=totwait+p[i].wait;
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].ser,p[i].wait,tot);
}
avwait=totwait/n;
aturn=tturn/n;
printf("TOTAL WAITING TIME:%d\n",totwait);
printf("AVERAGE WAITING TIME:%d\n",avwait);
printf("TOTAL TURNAROUND TIME:%d\n",tturn);
printf("AVERAGE TURNAROUND TIME:%d\n",aturn);
}
Ex.No:3C Round Robin
PROGRAM
struct roundRobin
{
int pburst,pburst1,wtime,endtime,arrivt,boolean,flagent1;
char pname[5];
}
a[5];
int n,tq;
void input();
void initialize();
void calculate();
void display_waittime();
int main()
{
input();
initialize();
calculate();
display_waittime();
//getch();
//return();
}
void input()
{
int i;
printf("enter total no of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter process name:");
scanf("%s",&a[i].pname);
printf("enter process burst name:");
scanf("%s",&a[i].pburst);
printf("enter process arrival time:");
scanf("%d",&a[i].arrivt);
}
printf("\n enter the time quantum/Time Slice:");
scanf("%d",&tq);
}
void initialize()
{
int i;
for(i=0;i<n;i++){
a[i].pburst1=a[i].pburst;
a[i].wtime=0;
a[i].endtime=0;
a[i].boolean=0;
a[i].flagent1=0;
}}
void calculate()
{
int i,j=0,k=0,flag=1,count=0;
printf("\n---GANTT CHART---\n");
printf("0|");
while(flag){
for(i=0;i<n;i++){
if((k<n)&&(a[i].arrivt<=count)&&(a[i].flagent1==0)){
a[i].wtime=count-a[i].arrivt;
a[i].endtime=count;
a[i].boolean=1;
a[i].flagent1=1;
k++;
}
if((a[i].pburst1>tq)&&(a[i].arrivt<=count)){
if(a[i].boolean==1)
a[i].boolean=0;
else
a[i].wtime=a[i].wtime+(count-a[i].endtime);
count=count+tq;
a[i].pburst1=a[i].pburst1-tq;
a[i].endtime=count;
printf("%d%s|",count,a[i].pname);
}
else if((a[i].pburst1>0)&&(a[i].pburst1<=tq)&&(a[i].arrivt<=count)){
if(a[i].boolean==1)
a[i].boolean=0;
else
a[i].wtime=a[i].wtime+(count-a[i].endtime);
count=count+a[i].pburst1;
a[i].endtime=count;
printf("%d%s|",count,a[i].pname);
a[i].pburst1=0;
j++;
}else if(j==n)flag=0;
}}}
void display_waittime()
{
int i,tot=0,turn=0;
for(i=0;i<n;i++)
{
printf("\n\nwaiting time for process%s is %d",a[i].pname,a[i].wtime);
tot=tot+a[i].wtime;
turn=turn+a[i].endtime-a[i].arrivt;
}
printf("\n\n\t average waiting time=%f",(float)tot/(float)n);
printf("\n\t average turnaround time=%f\n",(float)turn/(float)n);
}
Ex.No:3d Priority
PROGRAM:
main()
{
int i,j,n,t,turn[20],burst[20],p[20],wt[20],c[20];
float await,aturn,twait=0,tturn=0;
printf(“enter the value of n”);
scanf("%d",&n);
printf(“enter the process no burst and arrival time”);
for(i=0;i<n;i++)
{
scanf("%d",&c[i]);
scanf("%d",&burst[i]);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i]>p[j])
{
t=p[i];
p[i]=p[j];
p[j]=t;
t=burst[i];
burst[i]=burst[j];
burst[j]=t;
t=c[i];
c[i]=c[j];

main()
{
int i,j,n,t,turn[20],burst[20],p[20],wt[20],c[20];
float await,aturn,twait=0,tturn=0;
printf("\n Enter the value of n");
scanf("%d",&n);
printf("\n Enter the process no burst and arrivaltime");
for(i=0;i<n;i++)
{
scanf("%d",&c[i]);
scanf("%d",&burst[i]);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i]>p[j])
{
t=p[i];
p[i]=p[j];
p[j]=t;
t=burst[i];
burst[i]=burst[j];
burst[j]=t;
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
wt[i]=0;
turn[i]=burst[i];
}
else
{
turn[i]=turn[i-1]+burst[i];
wt[i]=turn[i]-burst[i];
twait=twait+wt[i];
tturn=tturn+turn[i];
}}
await=twait/n;
aturn=tturn/n;
printf("pno\tbtime\tatime\twtime\tttime");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d\n",c[i],burst[i],p[i],wt[i],turn[i]);
}
printf("\nThe average waiting time is:%f",await);
printf("\n The average turn around time is:%f",aturn);
}
Ex.No:4A
PROGRAM: Sequential Allocation
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
Ex.No:4B
PROGRAM: Indexed
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
Ex.No:4C
PROGRAM: Iinked
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
Ex.No:5
PROGRAM Semaphore
#define BUFFERSIZE 10
int mutex,n,empty,full=0,item,item1
int buffer[20];
int in=0,out=0,mutex=1;
void wait(int s)
{
while(s<0){
printf("\nCannot add an item\n");
exit(0);
}
s--;}
void signals(int s){
s++;
}
void producer()
{
do{
wait(empty);
wait(mutex);
printf("\nEnter an item:");
scanf("%d",&item);
buffer[in]=item;
in=in+1;
signal(mutex);
signal(full);
}while(in<n);
}
void consumer()
{
do{
wait(full);
wait(mutex);
item1=buffer[out];
printf("\nConsumed item=%d",item1);
out=out+1;
signal(mutex);
signal(empty);
}while(out<n);}
void main(){
printf("Enter the value of n:");
scanf("%d",&n);
empty=n;
while(in<n)
producer();
while(in!=out)
consumer();}
Ex.No: 7 Deadlock Avoidance
PROGRAM
main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;j<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;
break;
}}
if(u=0)
{
for(k=1;k<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;
break;
}}}
if(j=0)
{
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");}}
Ex.No:8 Deadlock Detection
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{ int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0; s
clrscr();
printf("Enter total no of processes");
scanf("%d",&tp);
printf("Enter total no of resources");
scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
+
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++) 56
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i; k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i]) found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
Ex.No:9A FIFO Page Replacement
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[20],p=0,q=0,m=0,h,k,i,ql=1,j,u;
char f='F';
clrscr();
printf("Enter numbers:");
for(i=0;i<12;i++)
scanf("%d",&b[i]);
for(i=0;i<12;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(ql<3)
{
ql=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<ql;h++)
printf("%d",a[h]);
if((p==0)&&(ql==3))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q-1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m);
getch();
}
Ex.No:9b LRU
PROGRAM
main()
{
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u;
char f='F';
printf("Enter no:");
for(i=0;i<12;i++)
scanf("%d",&b[i]);
for(i=0;i<12;i++){
if(p==0){
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3){
q1=q;
g-1;}}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q1==3)&&(g!=1))
{
printf("-->%c",f);
m++;}
p=0;
g=0;
if(q1==3){
for(k=0;k<q-1;k++){
if(b[i+1]==a[k])
p=1;}
for(j=0;j<q1;j++){
u=0;
k=i;
while(k>(i-2)&&(k>=0)){
if(b[k]==a[j])
u++;
k--;
}
if(u==0)
q=j;
}}
else{
for(k=0;k<q;k++){
if(b[i+1]==a[k])
p=1;
}}}printf("\nNo of faults:%d",m);}
Ex.No:9C Optimal
PROGRAM
main()
{
int pn[12],m[3]={0,0,0,},m1[3],i,j,k;
int flag,f,pf=0,z;
printf("Enter pgs:");
for(i=0;i<12;i++)
scanf("%d",&pn[i]);
j=0;
for(i=0;i<3;i++)
{
while(j<12)
{
flag=0;
for(k=0;k<3;k++)
{
if(m[k]==pn[j])
{
flag=1;
j++;
i--;
}
if(flag==1)
break;
}
if(flag==0)
{
m[j]-pn[j];
flag=1;
}
j++;
if(flag==1)
break;
}
}
for(i=j;i<12;i++)
{
flag=0;
for(j=0;j<3;j++)
{
if(pn[i]==m[j])
flag=1;
if(flag==0)
{
m1[0]=0;
m1[1]=m1[2]=0;
for(j=0;j<3;j++)
{
f=0;
for(k=i+1;k<12;k++)
{
if(m[j]==pn[k])
{
m1[j]=k;
f=1;
}
if(f==1)
break;
}
}
z=(m1[0]>m1[1]||(m1[0]>m1[2])?m1[0]:m1[2])&&(m1[1]>m1[2]?m1[1]:m1[2]);
for(j=0;j<3;j++)
{
if(pn[z]==m[j])
{
m[j]=pn[i];
pf++;
}
}
}
}
}
printf("no. of faults:%d",pf);
}
Ex.No:10 Shared memory and IPC
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/uio.h>
#include<sys/types.h>
main()
{
int pid,pfd[2],n,a,b,c;
if(pipe(pfd)==-1)
{
printf("\nError in pipe connection\n");
exit(1);
}
pid=fork();
if(pid>0)
{
printf("\nParent Process");
printf("\n\n\tFibonacci Series");
printf("\nEnter the limit for the series:");
scanf("%d",&n);
close(pfd[0]);
write(pfd[1],&n,sizeof(n));
close(pfd[1]); exit(0);
}
else
{
close(pfd[1]);
read(pfd[0],&n,sizeof(n));
printf("\nChild Process");
a=0;
b=1;
close(pfd[0]);
printf("\nFibonacci Series is:");
printf("\n\n%d\n%d",a,b); while(n>2)
{
c=a+b;
printf("\n%d",c);

a=b;
b=c;
n--; } }}
Ex.No:11 A Paging
PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
int b[20],n,i,pa,p,a,d;
printf("\nProgram for paging");
scanf("%d",&n);
printf("\n Enter the base address:");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("\nEnter the logical address:");
scanf("%d",&d);
printf("\n enter the number of pages");
scanf("%d",&p);
for(i=0;i<n;i++)
{
if(i==p)
{
pa=b[i]+d;
a=b[i];
printf("\n\tPage No.\t Base Add. Physical Add.\n\t%d\t%d\t%d\t",p,a,pa);
}}printf("\n Invalid page");}
Ex.No:11b Segmentation
PROGRAM
#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],l[20],n,i,pa,s,a,d;
printf("\nProgram for segmentation");
printf("\nEnter the number of segments:");
scanf("%d",&n);
printf("\nEnter the base address and limit register:");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
scanf("%d",&l[i]);
}
printf("\nEnter the logical address:");
scanf("%d",&d);
printf("Enter segment number");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(i==s)
{
if(d<l[i])
{
pa=b[i]+d;
a=b[i];
printf("\n\tPageNo.\tBaseAdd.PhysicalAdd.\n\t%d\t%d\t%d\t",
exit(0);
}
else
{
printf("\nPage size exceeds");
exit(0);
}
}
}
printf("\nInvalid segment");
}
Ex.No: 12 Threading
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
void* doSomeThing(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d started\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d finished\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf ("\ncan't create thread :[%s]", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}

You might also like