0% found this document useful (0 votes)
12 views39 pages

OS Lab Programs For Print

The document contains a series of shell scripts and C programs that demonstrate various programming concepts such as finding the greatest of two numbers, calculating the area of a circle, determining odd or even numbers, and implementing various scheduling algorithms like FCFS, SJF, and Round Robin. Additionally, it covers file allocation methods and deadlock avoidance using the Banker's algorithm. Each section includes code snippets and user prompts for input.

Uploaded by

Ezhil Arasi
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)
12 views39 pages

OS Lab Programs For Print

The document contains a series of shell scripts and C programs that demonstrate various programming concepts such as finding the greatest of two numbers, calculating the area of a circle, determining odd or even numbers, and implementing various scheduling algorithms like FCFS, SJF, and Round Robin. Additionally, it covers file allocation methods and deadlock avoidance using the Banker's algorithm. Each section includes code snippets and user prompts for input.

Uploaded by

Ezhil Arasi
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/ 39

EX.NO:2.

A /*GREATEST OF TWO NUMBERS*/

echo "Enter the 2 values"

read a b

if [ $a -gt $b ]

then

echo "$a is greater"

else

echo "$b is greater"

fi

"greatestof2.sh" 8L, 107C


EX.NO:2.B /*AREA OF CIRCLE*/

echo " Enter the radious value"

read r

d=`expr 22 / 7`

a=`expr $d \* $r \* $r`

echo "The area of the circle with radious $r is $a"

"circle.sh" 6L, 132C


EX.NO:2.C /*ODD OR EVEN*/

echo " Enter a number"

read a

b=`expr $a % 2`

if [ $b -eq 0 ]

then

echo "The given number is even"

else

echo " The given number is odd"

fi

"odd-even.sh" 9L, 141C


EX.NO:2.D BIGGEST OF THREE NUMBERS

echo "Enter the three values"

read a b c

if [ $a -gt $b ]

then

if [ $a -gt $c ]

then

echo "$a is greatest number"

fi

elif [ $b -gt $c ]

then

echo "$b is greatest number"

else

echo "$c is greatest number"

fi

"biggestof3.sh" 15L, 208C


EX.NO:2.E /*SERIES OF NUMBERS*/

echo "The series is :"

for i in 1 2 3 4 5 6 7 8 9

do

echo $i

done

"series.sh" 5L, 67C


EX.NO: 2.F /*SUM AND AVERAGE OF N NUMBERS*/

echo "Enter the limit"

read n

i=1

s=0

while [ $i -le $n ]

do

s=`expr $s + $i`

i=`expr $i + 1`

done

avg=`expr $s / $n`

echo " The sum of $n number is $s"

echo "The average of $n is $avg"

"sum&avgofNnumbers.sh" 10L, 134C


EX.NO:2.G /*AVERAGE OF GIVEN NUMBERS*/

echo "Enter the limit"

read n

echo "Enter $n numbers"

i=0

sum=0

while [ $i -lt $n ]

do

read a

sum=`expr $sum + $a`

i=`expr $i + 1`

done

avg=`expr $sum / $n`

echo " The sum of entered $n numbers is $sum"

echo "The average of entered $n numbers is $avg"

"avgofgivennumbers.sh" 15L, 254C


EX.NO:2.H /*FACTORIAL OF A NUMBER*/

echo "Enter a number"

read n

f=1

i=1

while [ $i -le $n ]

do

f=`expr $f \* $i`

i=`expr $i + 1`

done

echo "The factorial of $n is $f"

"factorial.sh" 10L, 131C


EX.NO:2.I /*FIBONACCI SERIES*/

echo "Enter a number"

read n

f=0

a=0

b=1

i=0

echo "The series is:"

while [ $i -lt $n ]

do

f=`expr $f + $a`

a=`expr $b`

b=`expr $f`

echo $f

i=`expr $i + 1`

done

"fibonacci.sh" 16L, 161C


EX.NO:2.J /*ARITHMETIC OPERATIONS*/

echo "Enter the two numbers"

read a b

echo "Enter the option"

read opt

case $opt in

1)c=`expr $a + $b`

echo " The sum is : $c";;

2)c=`expr $a - $b`

echo " The difference is : $c";;

3)c=`expr $a \* $b`

echo "The product is : $c";;

4)c=`expr $a / $b`

echo " The quotient is : $c";;

5)c=`expr $a % $b`

echo "The remainder is : $c";;

esac

"arithmetic.sh" 16L, 335C


********* 4.A) PROGRAM FOR FCFS SCHEDULING *********

#include<stdio.h>
main()
{
int i,j,n,t,turn[20],burst[20],a[20],wt[20],e[20],c[20],et[20];
float await,aturn,twait=0,tturn=0;
printf("\n Enter Value of N:");
scanf("%d",&n);
printf("\n Enter the Process No,Burst and Arrival Time:");
for(i=0;i<n;i++)
{
scanf("%d",&c[i]);
scanf("%d",&burst[i]);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[i]=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;
et[i]=a[i]+burst[i];
}
else{
wt[i]=et[i-1]-a[i];
et[i]=burst[i]+et[i-1];
}
twait=twait+wt[i];
turn[i]=burst[i]+wt[i];
tturn=tturn+turn[i];
}
await=twait/n;
aturn=tturn;
printf("PROCESS NO\t BURSTTIME \tARRIVAL TIME \t WAIT TIME \t TURN AROUND
Time");
for(i=0;i<n;i++)
{
printf("\n %d\t%d\t%d\t%d\t%d\t\n",c[i],burst[i],a[i],wt[i],turn[i]);
}
printf("\n The Average Waiting Time is: %f", await);
printf("\n the Average Turn Arround Time is: %f", aturn);}
*********** 4.B) PROGRAM FOR SJF SCHEDULING*********

#include<stdio.h>
main()
{
int i,j,n,t,turn[20],burst[20],a[20],wt[20],e[20],c[20],et[20];
float await,aturn,twait=0,tturn=0;
printf("\n ENTER THE VALUE OF N :/n");
scanf("%d",&n);
printf("\n Enter the Processno,Bursttime, Arrival time:");
for(i=0;i<n;i++){
scanf("%d",&c[i]);
scanf("%d",&burst[i]);
scanf("%d",&a[i]);}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++){
if(burst[i]>burst[j]){
t=burst[i];
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;
et[i]=burst[i];
}
else
{
wt[i]=et[i-1];
et[i]=burst[i]+et[i-1];
}
twait=twait+wt[i];
turn[i]=burst[i]+wt[i];
tturn=tturn+turn[i];
}
await=twait/n;
aturn=tturn/n;
printf("Pno \t Btime \t Atime \t Wtime \t TTime");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d\t\n",c[i],burst[i],a[i],wt[i],turn[i]);
}
printf("\n The Average Waiting Time is %f",await);
printf("\n The Average Turn Arround Time is %f",aturn);
}
***************4.C) PRIORITY SCHEDULING *********

#include<stdio.h>
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 Processno,Burst and Priority : ");
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=0;j<n;j++){
if(p[i]>p[j]){
t=p[i];
p[i]=p[j];
p[j]=t;
t=c[i];
c[i]=c[j];
c[j]=t;
t=burst[i];
burst[i]=burst[j];
burst[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 \t Btime \t Priority \t Wtime \t TTime\t");
for(i=0;i<n;i++)
{
printf("\n %d\t%d\t%d\t%d\t%d\t\n",c[i],burst[i],p[i],wt[i],turn[i]);
}
printf("\n The Average Waiting Time is %f",await);
printf("\n The Average Turn Arround Time is %f",aturn);
}
********** 4.D) PROGRAM FOR ROUNDROBIN SCHEDULING *********

#include<stdio.h>
main()
{
int i,t[20],b[20],wt[20],x[20],tq,et=0,tet=0,c[20],n,e;
float at,awt,tt=0,tw=0;
printf("\n Enter the Value of N : ");
scanf("%d",&n);
printf("\n Enter the Time quantum Value");
scanf("%d",&tq);
printf("\n Enter the Value of Processno and Burst time");
for(i=0;i<n;i++)
{
scanf("%d",&c[i]);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
x[i]=b[i];
tet=tet+b[i];}
while(et!=tet){
for(i=0;i<n;i++){
if(x[i]<=0)
{
et=e;}
else
if(x[i]>=tq)
{
et=et+tq;
t[i]=et;}
else{
et=et+x[i];
t[i]=et;}
x[i]=x[i]-tq;
e=et;
}}
for(i=0;i<n;i++){
wt[i]=t[i]-b[i];
tt=tt+t[i];
tw=tw+wt[i];
}
at=tt/n;
awt=tw/n;
printf("\n\t Pno \tBtime \t Wtime \t TTime");
for(i=0;i<n;i++){
printf("\n\t%d\t%d\t%d\t%d\t%d\n",c[i],b[i],wt[i],t[i]);
}
printf("\n Average Waiting Time %f",awt);
printf("\n Average Turn arround Time %f",at);
}
*********** 6) PRODUCER CONSUMER PROBLEM USING SEMAPHORE *********

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("\n Cannot add an Item.\n");
exit(0);}
s--;
}
void signal(int s)
{
s++;}
void producer()
{
do{
wait(empty); // Initialized to the value n.
wait(mutex);
printf("\n Enter an Item:");
scanf("%d",&item);
buffer[in]=item;
in=in+1;
signal(mutex);
signal(full);}
while(in<n);}
void consumer()
{
do{
wait(full); // Initialized to 0 (Zero)
wait(mutex);
item1=buffer[out];
printf("\n Consumed Item=%d",item1);
out=out+1;
signal(mutex);
signal(empty);
}
while(out<n);
}

main()
{
printf(Enter the value of N":);
scanf("%d",&n);
empty=n;
while(in<n)
producer();
while(in!=out)
consumer();
}
*************** 14.A) SEQUENTIAL FILE 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 no 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();
}
********** 14.B) INDEXED FILE ALLOCATION *********

#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();}
*********** 14.C)LINKED FILE ALLOCATION *********

#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();
}
**********7) DEAD LOCK AVOIDANCE USING BANKERS ALGORITHM *********

#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
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];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
*********** 8) DEADLOCK DETECTION*********

#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;
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++)
{
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();
}
************ 12.A) FIFO REPLACEMENT ALGORITHM *********

#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
************** 12.B) LRU REPLACEMENT ALGORITHM *********

#include<stdio.h>
#include<conio.h>
void main()
{
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u;
char f='F';
clrscr();
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);
getch();

}
************** 12.C) LFU REPLACEMENT ALGORITHM *********

#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;

printf("Enter no of frames : ");


scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);

for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}
************** 5) SHARED MEMORY IPC *********

#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
int main()
{
key_t key=15;
int shmid_1,shmid_2;
if((shmid_1=shmget(key,1000,0640|IPC_CREAT))==-1)
{
perror("Shmget shmid_1");
return 1;
}
printf("\n First Shared Memory Identifier is %d",shmid_1);
if((shmid_2=shmget(IPC_PRIVATE,20,0640))==-1)
{
perror("Shmget shmid_2");
return 2;
}
printf("\n Second Shared Memory Identifier is %d",shmid_2);
return 0;
}
************* 10) MEMORY MANAGEMENT SCHEME[PAGING] *********

#include<stdio.h>
main()
{
int b[20],n,i,pa,p,a,d;
printf("Program for Paging");
printf("Enter the No.of pages");
scanf("%d",&n);
printf("Enter the Base Address");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("Enter the Logical Address");
scanf("%d",&d);
printf("Enter the Pageno");
scanf("%d",&p);
for(i=0;i<n;i++)
{
if(i==p)
{
pa=b[i]+d;
a=b[i];
printf("\n\t Pageno\t Baseaddress\t Physical address\n%d\t%d\t%d\t",p,a,pa);
exit(0);
}
}
printf("\n Invalid page");
}
*************** 9) THREAD & SYNCHRONIZATION*********

#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;
}
*************** 15.A) SSTF DISK SCHEDULING ALGORITHM *********
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for sstf disk scheduling
/* loop will execute until all process is completed*/
while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

Output:
Enter the number of Request
8
Enter Request Sequence
95 180 34 119 11 123 62 64
Enter initial head Position
50
Total head movement is 236
********** 15.B) FCFS DISK SCHEDULING *********

#include <stdio.h>
#include <math.h>
int size = 8;
void FCFS(int arr[],int head)
{
int seek_count = 0;
int cur_track, distance;
for(int i=0;i<size;i++)
{
cur_track = arr[i];
// calculate absolute distance
distance = fabs(head - cur_track);
// increase the total count
seek_count += distance;
// accessed track is now new head
head = cur_track;
}
printf("Total number of seek operations: %d\n",seek_count);
// Seek sequence would be the same
// as request array sequence
printf("Seek Sequence is\n");
for (int i = 0; i < size; i++) {
printf("%d\n",arr[i]);
}
}
//Driver code
int main()
{
// request array
int arr[8] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
FCFS(arr,head);
return 0;
}

Output:
Total number of seek operations = 510
Seek Sequence is
176
79
34
60
92
11
41
114
************** 11.A) FIRST FIT *********
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme -First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);getch();
}
************** 11.C) BEST-FIT *********
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
*********** 11.B ) WORST-FIT *********

#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme -Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
************** 13.A) SINGLE LEVEL DIRECTORY ORGANIZATION*********

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
*********** 13.B) TWO LEVEL DIRECTORY ORGANIZATION**********

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\t
Enter your choice --
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}}

You might also like