0% found this document useful (0 votes)
20 views55 pages

Os Print Word File

"OS" typically stands for "Operating System." An operating system is system software that manages computer hardware, software resources, and provides common services for computer programs. The most common operating systems include Microsoft Windows, macOS, Linux, and Unix. Each OS has its own set of features, capabilities, and user interfaces, tailored to meet various user needs and computing environments.Operating systems perform various functions, including:Process Management: Handling process

Uploaded by

Muhammad Ibrahim
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)
20 views55 pages

Os Print Word File

"OS" typically stands for "Operating System." An operating system is system software that manages computer hardware, software resources, and provides common services for computer programs. The most common operating systems include Microsoft Windows, macOS, Linux, and Unix. Each OS has its own set of features, capabilities, and user interfaces, tailored to meet various user needs and computing environments.Operating systems perform various functions, including:Process Management: Handling process

Uploaded by

Muhammad Ibrahim
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/ 55

Ex NO-2(A) ILLUSTRATE UNIX COMMANDS

COMMANDS:

1). [cse053@localhost cse053]$ who

cse053 pts/0 May 2 13:13 (172.16.20.173)

cse044 pts/1 May 2 13:14 (172.16.20.183)

cse039 pts/2 May 2 13:14 (172.16.20.188)

2).[cse053@localhost cse053]$ who am i

cse053 pts/0 May 2 13:13 (172.16.20.173)

[cse053@localhost cse053]$ date

Tue May 2 13:14:47 IST 2023

3).[cse053@localhost cse053]$ cal 02 2004

February 2004

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29

4).[cse053@localhost cse053]$ uname

Linux

5).[cse053@localhost cse053]$ bc

bc 1.06

Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'.

3+7

10

[1]+ Stopped bc

6).[cse053@localhost cse053]$ id
uid=552(cse053) gid=552(cse053) groups=552(cse053)

7).[cse053@localhost cse053]$ pwd

/home/cse053

8).[cse053@localhost cse053]$ echo hii

Hii

9).[cse053@localhost cse053]$ man echo

ECHO(1) FSF ECHO(1)

NAME

echo - display a line of text

SYNOPSIS

echo [OPTION]... [STRING]...

DESCRIPTION

NOTE: your shell may have its own version of echo which will supercede

the version described here. Please refer to your shell Ç s documentation

for details about the options it supports.

Echo the STRING(s) to standard output.

-n do not output the trailing newline

-e enable interpretation of the backslash-escaped characters listed

below

-E disable interpretation of those sequences in STRINGs

--help display this help and exit

--version

output version information and exit

Without -E, the following sequences are recognized and interpolated:

\NNN the character whose ASCII code is NNN (octal)

\\ backslash

\a alert BEL

\b backspace

\c suppress trailing newline


\f form feed

\n new line

\r carriage return

\t horizontal tab

\v vertical tab

AUTHOR

Written by FIXME unknown.

REPORTING BUGS

Report bugs to <[email protected]>.

COPYRIGHT

[2]+ Stopped man echo

10).[cse053@localhost cse053]$ ls

\ 5a.c cse ex14b.c ex2.c ex4b.sh ex5b11.a ex9a.c

221.sh 5.c deadlock.c ex15a.c ex3 ex4c11.c ex5b11.c ex9.c

2a.c a ex ex15b.c ex31.cc ex4c1.c ex5b12.c file.sangi

2b.c aa ex091.c ex15.c ex3a.c ex4c2.c ex5bb1.c lex.yy.c

2c.c aaa ex092.c ex15c.c ex3b1.c ex4c4.c ex5bb.c odd.l

32.c add.l ex09.c ex1aa.c ex3b.c ex4c5.c ex5b.c raj

3a.c anu ex10.c ex1a.c ex3.c ex4c6.c ex5c.c raj.txt

3b1.c a.out ex11a.c ex1d.c ex3c1.c ex4c.c ex5d.c ram.txt

3b2.c a.sh ex11b.c ex21.sh ex3c.c ex4c.sh ex61.c ramu.txt

3bb.c b ex11c.c ex221.sh ex4a1.c ex4d11.c ex6a.c rr.c

3b.c bb ex12.c ex223.sh ex4a1.sh ex4d.sh ex6.c sakthi

4a1.c b.sh ex13b.c ex224.sh ex4a.c ex4e.sh ex7a.c temp

4a.c c ex13.c ex22.sh ex4a.sh ex4.sh ex7b.c vishnu

55.c calc.l ex13c.c ex24.sh ex4b1.c ex5a11.c ex7.c vishnu.txt

5a1.c c.l ex14a.c ex25.sh ex4b.c ex5a.c ex8.c

[cse053@localhost cse053]$ cat>a

hii
hello

welcome

[3]+ Stopped cat >a

[cse053@localhost cse053]$ cat>b

Hii

[4]+ Stopped cat >b

11).[cse053@localhost cse053]$ grep i a

Hii

12).[cse053@localhost cse053]$ cat a

hii

hello

welcome

13).[cse053@localhost cse053]$ wc a

3 3 18 a

14).[cse053@localhost cse053]$ cp a b

[cse053@localhost cse053]$ cat b

hii

hello

welcome

15).[cse053@localhost cse053]$ mv a b

[cse053@localhost cse053]$ cat b

hii

hello

welcome

16). [cse053@localhost cse053]$ diff a b

2,3d1

< hello

< welcome

17).[cse053@localhost cse053]$ paste a b


hii hii

hello

welcome

18).[cse053@localhost cse053]$ head -1 a

Hii

19).[cse053@localhost cse053]$ tail -1 a

welcome
EX NO-2(B) SHELL PROGRAMMING

TO CHECK WHETHER A GIVEN NUM IS ODD OR EVEN

echo Enter Any Number For Check Odd Or Even

read no

test=`expr $no % 2`
if [ $test -eq 0 ]
then
echo "This Number Is Even";
else
echo "This Number Is Odd";
fi

OUTPUT:

[cse053@localhost cse053]$ sh 2a.sh

ENter any number to check odd or even

THIS NUMBER IS EVEN


SUM OF TWO NUMBERS

echo $ enter count

read count

sum=0

echo enter numbers

while [ $count -gt 0 ]

do

read num

sum=`expr $sum + $num `

count=`expr $count - 1 `

done

echo sum is

echo $sum

OUTPUT:

[cse053@localhost cse053]$ sh 2b.sh

$ enter count

enter numbers

sum is

8
BIGGEST AMONG 3 NUMBERS

echo enter three numbers


read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo $a is biggest
elif [ $b -gt $a -a $b -gt $c ]
then
echo $b is biggest
else
echo $c is biggest
fi

OUTPUT:

[cse053@localhost cse053]$ sh 2c.sh

enter three numbers

321

3 is biggest
ARITHMETIC EXPRESSION EVALUATION

echo enter a value

read a

echo enter b value

read b

val=`expr $a + $b`

echo "a+b : $val"

val=`expr $a - $b`

echo "a-b : $val"

val=`expr $a \* $b`

echo "a*b : $val"

val=`expr $a / $b`

echo "a/b : $val"

val=`expr $a % $b`

echo "a%b : $val"

OUTPUT:

[cse053@localhost cse053]$ sh 2d.sh

enter a value

enter b value

a+b : 8

a-b : 4

a*b : 12

a/b : 3a%b :
FACTORIAL OF A GIVEN NUMBER

echo "enter a number"


read num
fact=1
for((i=1;i<=num;i++))
{
fact=`expr $fact \* $i`
}
echo factorial of $num is $fact

OUTPUT:
[cse053@localhost cse053]$ sh 2e.sh

enter a number

factorial of 5 is 120
EXNO-3

EXNO-3(A) FORK SYSTEM CALL:

PROGRAM 1: PROGRAM 2:

#include<stdio.h> #include<stdio.h>

#include<unistd.h> #include<unistd.h>

main() main()

{ {

fork(); fork();

printf("Helloworld"); fork();

} fork();

OUTPUT: printf("Helloworld\n");

[cse053@localhost cse053]$ cc 3a.c }

[cse053@localhost cse053]$ ./a.out OUTPUT:

HelloworldHelloworld [cse053@localhost cse053]$ cc 3b.c

[cse053@localhost cse053]$ ./a.out

Helloworld

Helloworld

[cse053@localhost cse053]$ Helloworld

Helloworld

Helloworld

Helloworld

Helloworld

Helloworld
EXNO-3(B) FORK,EXIT,GETPID,WAIT,CLOSE SYSTEM CALLS

#include<stdio.h> }

#include<unistd.h> printf("\n Child ends");

#include<stdlib.h> }

main() else

{ {

int a=-1,b=1,i,c=a+b,n,pid,cpid; printf("\n Parent is waiting for child to complete");

printf("\n Enter no of terms:"); wait(NULL);

scanf("%d",&n); printf("Parent ends");

pid=getpid(); }

printf("\n Parent process id is %d",pid); }

pid=fork(); OUTPUT:

cpid=getpid(); [cse053@localhost cse053]$ cc 4a.c

printf("\n Child process:%d",cpid); [cse053@localhost cse053]$ ./a.out

if(pid==0) Enter no of terms:2

{ Parent process id is 20288

printf("\n Child is producing fibonacci series:"); Child process:20289

Child is producing fibonacci series:

for(i=0;i<n;i++) 0

{ 1

c=a+b; Child ends Parent process id is 20288

printf("\n %d",c); Child process:20288

a=b; Parent is waiting for child to completeParent ends

b=c;
EXNO-3(C) EXECLP SYSTEM CALL

#include<stdio.h> }

#include<unistd.h> else

#include<sys/wait.h> {

main() wait(NULL);

{ printf("Child complete");

int pid; exit(0);

pid=fork(); }

if(pid<0) }

fprintf(stderr,"fork failed\n"); OUTPUT:

exit(-1);

} [cse053@localhost cse053]$ cc ex3c1.c

else if(pid==0) [cse053@localhost cse053]$ ./a.out

{ Child complete

execlp("/bin/ls","ls",NULL);
EXNO-3(D) CLOSE SYSTEM CALL

#include<stdio.h> close(fd[1]);

int main() read(fd[0],a,5);

{ printf("\n The string retrived from the pipi ie %s\


n",a);
int fd[2],child;
}
char a[20];
return 0;
printf("\n Enter the string to enter into the pipe:");
}
scanf("%s",&a);

pipe(fd);
OUTPUT:
child=fork();

if(!child)
[cse053@localhost cse053]$ cc ex5a11.c
{
[cse053@localhost cse053]$ ./a.out
close(fd[0]);

write(fd[1],a,5);
Enter the string to enter into the pipe:hii
}

else
The string retrived from the pipi is hii
{
EXNO-4 CPU SCHEDULING ALOGORITHMS

EXNO-4(A):FIRST COME FIRST SERVE

#include<stdio.h> for(i=2;i<=n;i++)

struct process {

{ p[i].wtime=p[i-1].wtime+p[i-1].btime;

int btime; p[i].ttime=p[i].wtime+p[i].btime;

int wtime; tot_wait=tot_wait+p[i].wtime;

int ttime; tot_turn=tot_turn+p[i].ttime;

} }

p[50]; avg_wait=tot_wait/n;

main() avg_turn=tot_turn/n;

{ printf("\n\n\t\t GANTT CHART\n" );

int i,j,n; for(i=1;i<=n;i++)

float printf("%d\t\t ",i);


tot_num=0.0,tot_turn=0.0,tot_wait=0.0,avg_turn=0.
0,avg_wait=0.0; printf("\n...............................\n");

printf("\n Enter no of process"); for(i=1;i<=n;i++)

scanf("%d", &n); printf("%d\t\t",p[i].wtime);

for(i=1;i<=n;i++) printf("%d",p[n].wtime+p[n].btime);

{ printf("\n ................................\n");

printf("\n Enter the burst time of each person:"); printf("process burst time turn around:\n");

scanf("%d",&p[i].btime); for(i=1;i<=n;i++)

} {

i=1; printf("%5d%10d%15d
%15d",i,p[i].btime,p[i].wtime,p[i].ttime);
p[i].wtime=0;
printf("\n");
p[i].ttime=p[i].btime;
}
tot_wait=p[i].wtime;
printf("\n average wait time :%f \n", avg_wait);
tot_turn=p[i].ttime;
printf("\n average turn around time %f \n
",avg_turn);

}
GANTT CHART

1 2 3
OUTPUT:
...............................

0 2 5 9
[cse053@localhost cse053]$ cc ex4a1.c
................................
[cse053@localhost cse053]$ ./a.out
process burst time turn around:

1 2 0 2
Enter no of process3
2 3 2 5

3 4 5 9
Enter the burst time of each person:2

average wait time :2.333333


Enter the burst time of each person:3

average turn around time 5.333333


Enter the burst time of each person:4
EXNO-4(B)

SHORTEST JOB SCHEDULING:

#include<stdio.h> pno[j]=t;

main() t=bs[i];

{ bs[i]=bs[j];

int p; bs[j]=t;

int n; }

int i,j,tot_wt=0,tot_tt=0; }

float avg_wt=0.0,avg_tu=0.0; }

int stt; stt=0;

int t,pno[10],bs[10],wt[10],tu[10],ft[10]; for(i=1;i<=n;i++)

printf("\n Enter the noof process:"); {

scanf("%d",&p); ft[i]=stt+bs[i];

n=p; wt[i]=stt;

for(i=1;i<=n;i++) tu[i]=wt[i]+bs[i];

{ tot_wt=tot_wt+wt[i];

pno[i]=i; tot_tt=tot_tt+tu[i];

printf("\n Enter the burst time:"); stt=ft[i];

scanf("%d",&bs[i]); }

} avg_wt=tot_wt/n;

for(i=1;i<=n;i++) avg_tu=tot_tt/n;

{ printf("\n\n\t\t\t GANTT CHART\n");

for(j=i+1;j<=n;j++) printf("\n__________________\n");

{ for(i=1;i<=n;i++)

if(bs[i]>bs[j]) printf("|\t p%d \t",pno[i]);

{ printf("|\t\n");

t=pno[i]; printf("____________________\n");

pno[i]=pno[j]; printf("\n");
for(i=1;i<=n;i++) [cse053@localhost cse053]$ cc ex4b1.c

printf("%d\t\t",wt[i]); [cse053@localhost cse053]$ ./a.out

printf("%d",wt[n]+bs[n]);

printf("____________________\n"); Enter the noof process:3

printf("\n"); Enter the burst time:6

printf("pno bursttime(msec),waiting time(msec) turn Enter the burst time:4


sround time(msec)\n");
Enter the burst time:8
for(i=1;i<=n;i++)
GANTT CHART
{
__________________
printf("%d\t\t%d\t\t%d\t\t
%d",pno[i],bs[i],wt[i],tu[i]); | p2 | p1 | p3 |

printf("\n"); ____________________

printf("avg waiting time in millisec is:"); 0 4 10 18

printf("\t%f",avg_wt);

printf("\n Avg turn around time in millisec"); pno bursttime(msec),waiting time(msec) turn sround
time(msec)
printf("\t%f",avg_tu);
2 4 0 4
}
1 6 4 10

3 8 10 18
OUTPUT:
avg waiting time in millisec is: 4.000000

Avg turn around time in millisec 10.000


EXNO-4(C)

PRIORITY SCHEDULING:

#include<stdio.h> at[i]=at[j];

#include<string.h> at[j]=temp;

int main() temp=et[i];

{ et[i]=et[j];

int et[j]=temp;
et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[1
0]; strcpy(t,pn[i]);

int totwt=0,totta=0; strcpy(pn[i],pn[j]);

float awt,ata; strcpy(pn[j],t);

char pn[10][10],t[10]; }

printf("Enter the number of process:"); }

scanf("%d",&n); for(i=0; i<n; i++)

for(i=0; i<n; i++) {

{ if(i==0)

printf("Enter process name,arrivaltime,execution {


time & priority:");
st[i]=at[i];
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
wt[i]=st[i]-at[i];
}
ft[i]=st[i]+et[i];
for(i=0; i<n; i++)
ta[i]=ft[i]-at[i];
for(j=0; j<n; j++)
}
{
else
if(p[i]<p[j])
{
{
st[i]=ft[i-1];
temp=p[i];
wt[i]=st[i]-at[i];
p[i]=p[j];
ft[i]=st[i]+et[i];
p[j]=temp;
ta[i]=ft[i]-at[i];
temp=at[i];
}
totwt+=wt[i]; 0

totta+=ta[i]; 4

} 2

awt=(float)totwt/n; Enter process name,arrivaltime,execution time &


priority:2
ata=(float)totta/n; printf("\nPname\tarrivaltime\
texecutiontime\tpriority\twaiti 3

ngtime\ttatime"); 7

for(i=0; i<n; i++) printf("\n%s\t%5d\t\t%5d\t\t 1


%5d\t\t%5d\t\t%5d",pn[i],at
Enter process name,arrivaltime,execution time &
[i],et[i],p[i],wt[i],ta[i]); priority:3

printf("\nAverage waiting time is:%f",awt); printf("\ 5


nAverage turnaroundti
9
me is:%f",ata);
5
}

Pname arrivaltime executiontime priority


OUTPUT: waitingtime tatime

2 3 7 1 0 7

[cse053@localhost cse053]$ cc ex4c11.c 1 0 4 2 10 14

[cse053@localhost cse053]$ ./a.out 3 5 9 5 9 18

Enter the number of process:3 Average waiting time is:6.333333

Enter process name,arrivaltime,execution time & Average turnaroundtime is:13.0000000


priority:1
EXNO-4(D)

ROUND ROBIN SCHEDULING:

#include<stdio.h> if(b[i]==0)

main() continue;

{ if(b[i]>t)

int i,n,j=0; {

int b[20],p[20],b1[20],f[20],w[20]; printf("\n process %d",i);

int start,finish,t,total=0; finish=start+t;

float aw=0.0,at=0.0; j=j+t;

printf("\n enter no of process"); start=finish;

scanf("%d",&n); b[i]=b[i]-t;

printf("\n enter the time slice"); printf("\t REMAINING BURST TIME:%dms",b[i]);

scanf("%d",&t); }

for(i=1;i<=n;i++) else

{ {

printf("\n enter the burst time of process%d",i); printf("\nPROCESS%d:",i);

scanf("%d",&b[i]); finish=start+b[i];

b1[i]=b[i]; j=j+b[i];

total=total+b[i]; start=finish;

} f[i]=finish;

printf("\n\t\t round robin scheduling"); b[i]=0;

start=0; w[i]=finish-b1[i];

printf("\n the process scheduling is as follows"); printf("\tREMAINING BURST TIME:%dms",b[i]);

printf("\n time slice%d ms",t); }

while(j<total) }

{ }

for(i=1;i<=n;i++) printf("\n\nPROCESS NO\tBURST TIME\tWAITING


TIME\tTURNAROND TIME");
{
for(i=1;i<=n;i++)
{ enter the burst time of process39

printf("\n\n%d%20d%20d%20d",i,b1[i],w[i],f[i]); round robin scheduling

aw=aw+w[i]; the process scheduling is as follows

at=at+f[i]; time slice4 ms

} PROCESS1: REMAINING BURST TIME:0ms

aw=aw/(float)n; process 2 REMAINING BURST TIME:1ms

at=at/(float)n; process 3 REMAINING BURST TIME:5ms

printf("\nAVG WT TIME:%f",aw); PROCESS2: REMAINING BURST TIME:0ms

printf("\nAVG TAT:%f\n",at); process 3 REMAINING BURST TIME:1ms

} PROCESS3: REMAINING BURST TIME:0ms

OUTPUT: PROCESS NO BURST TIME WAITING TIME


TURNAROND TIME

1 3 0 3
[cse053@localhost cse053]$ cc ex4d11.c

[cse053@localhost cse053]$ ./a.out


2 5 7 12

enter no of process3
3 9 8 17
enter the time slice4
AVG WT TIME:5.000000
enter the burst time of process13
AVG TAT:10.6666666
enter the burst time of process25
EXNO-5 INTERPROCESS COMMUNICATION

EXNO-5(A):INTERPROCESS COMMUNICATION USING PIPES:

PROGRAM:

#include<stdio.h> close(fd[1]);

int main() read(fd[0],a,5);

{ printf("\n The string retrived from the pipi ie %s\


n",a);
int fd[2],child;
}
char a[20];
return 0;
printf("\n Enter the string to enter into the pipe:");
}
scanf("%s",&a);

pipe(fd);
OUTPUT:
child=fork();

if(!child)
[cse053@localhost cse053]$ cc ex5a11.c
{
[cse053@localhost cse053]$ ./a.out
close(fd[0]);

write(fd[1],a,5);
Enter the string to enter into the pipe:hello
}

else
The string retrived from the pipi is hello
{
EXNO-5(B) INTERPROCESS COMMUNICATION USING SHARED MEMORY

PROGRAM 1:

#include<stdio.h> #include<unistd.h>

#include<stdlib.h> #include<sys/shm.h>

#include<unistd.h> #include<string.h>

#include<sys/shm.h> int main()

#include<string.h> {

int main() int i;

{ void *shared_memory;

int i; char buff[100];

void *shared_memory; int shmid;

char buff[100]; shmid=shmget((key_t)2345, 1024, 0666);

int shmid; printf("Key of shared memory is %d\n",shmid);

shmid=shmget((key_t)2345, 1024, 0666| shared_memory=shmat(shmid,NULL,0);


IPC_CREAT);
printf("Process attached at %p\n",shared_memory);
printf("Key of shared memory is %d\n",shmid);
printf("Data read from shared memory is : %s\n",
shared_memory=shmat(shmid,NULL,0); (char *)shared_memory);

printf("Process attached at %p\n",shared_memory); }

printf("Enter some data to write to shared memory\ OUTPUT:


n");
[cse053@localhost cse053]$ cc sha.c
read(0,buff,100); strcpy(shared_memory,buff);
[cse053@localhost cse053]$ cc sha1.c
printf("You wrote : %s\n",(char *)shared_memory);
[cse053@localhost cse053]$ ./a.out
}
Key of shared memory is 131074
PROGRAM 2:
Process attached at 0x40018000
#include<stdio.h>
Data read from shared memory is : hai
#include<stdlib.h>
EXNO-6 MUTUAL EXCLUSION BY SEMAPHORE

PROGRAM:

#include<stdio.h> case 3:exit(0);

#define n 3 break;

int }}}
bu[n],a,in=1,out=1,mutex=1,full=0,empty=n,x=0,nc;
int wait(int s)
main()
{
{
return(--s);
int ch;
}
void producer();
int signal(int s)
void consumer();
{
int wait(int);
return(++s);
int signal(int);
}
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
producer()
while(1)
{
{
printf("\nEnterthe item produced by the producer:");
printf("\nENTER YOUR CHOICE");
scanf("%d",&a);
scanf("%d",&ch);
empty=wait(empty);
switch(ch)
mutex=wait(mutex);
{
bu[in]=a;
case 1:if((mutex==1)&&(empty>0))
printf("\nproducer produces the item%d",bu[in]);
producer();
in++;
else
mutex=signal(mutex);
printf("BUFFER IS FULL");
full=signal(full);
break;
}
case 2:if((mutex==1)&&(full>0))
void consumer()
consumer();
{
else
full=wait(full);
printf("BUFFER IS EMPTY");
mutex=wait(mutex);
break;
nc=bu[out];

printf("\nconsumer consumes item%d",nc); Enterthe item produced by the producer:2

out++;

mutex=signal(mutex); producer produces the item2

empty=signal(empty); ENTER YOUR CHOICE1

Enterthe item produced by the producer:3

OUTPUT:

[cse053@localhost cse053]$ cc ex6.c producer produces the item3

[cse053@localhost cse053]$ ./a.out ENTER YOUR CHOICE1

BUFFER IS FULL

1.PRODUCER ENTER YOUR CHOICE2

2.CONSUMER

3.EXIT consumer consumes item1

ENTER YOUR CHOICE2

ENTER YOUR CHOICE1 consumer consumes item2

ENTER YOUR CHOICE2

Enterthe item produced by the producer:1 consumer consumes item2

ENTER YOUR CHOICE2

producer produces the item1 BUFFER IS EMPTY

ENTER YOUR CHOICE1 ENTER YOUR CHOICE


EXNO-7 DEADLOCK AVOIDANCE USING BANKERS ALGORITHM

PROGRAM:

#include<stdio.h> {
printf("\n\t");
#include<stdlib.h> for(j=0;j<m;j++)
#include<string.h> {
int available[20]; printf("%d",need[i][j]);
int max[20][20]; }
int allocation[20][20]; }
int need[20][20]; printf("\n\t need calculation completed...");
int main() cntr=0;
{ while(1)
int n,m,i,j,cntr=0,allow=0,no,req1,req2,req3; {
int flag[20]={0},f=0; for(i=0;i<n;i++)
printf("\n\t enter the number of processes:"); {
scanf("%d",&n); allow=0;
printf("enter the number of resources:"); for(j=0;j<m;j++)
scanf("%d",&m); {
printf("\n enter the allocation matrix\n\t"); if(need[i][j]<=available[j]&&flag[i]==0)
for(i=0;i<n;i++) allow=1;
{ else
printf("\n\t"); {
for(j=0;j<m;j++) break;
{ }
scanf("%d",&allocation[i][j]); }
} if(allow==1)
} {
printf("\nEnter MAX matrix : \n\t"); printf("\n\t *****process P%d is safe *****",i);
for(i=0;i<n;i++) available[0]+=allocation[i][0];
{ available[1]+=allocation[i][1];
printf("\n\t"); available[2]+=allocation[i][2];
for(j=0;j<m;j++) flag[i]=1;
{ cntr++;
scanf("%d",&max[i][j]); }
} }
} if(cntr==n)
printf("\n\tEnter Available : "); break;
for(i=0;i<m;i++) }
{ while(1)
printf("\n\tEnter available value of resource %d : ",i); {
scanf("%d",&available[i]); printf("enter requested process:");
} scanf("%d",&no);
printf("\n\t Calculating need...."); printf("\n enter the requested resources:");
for(i=0;i<n;i++) scanf("%d%d%d",&req1,&req2,&req3);
{ if(req1<=need[no][0]&&req2<need[no]
for(j=0;j<m;j++) [1]&&req3<need[no][2])
{ if(req1<available[0]&&req2<available[1]&&req3<ava
need[i][j]=max[i][j]-allocation[i][j]; ilable[2])
} {
} printf("\n\t !!!!!!!GRANTED REQUESTED!!!!!!!!!");
for(i=0;i<n;i++) available[0]=available[0]-req1;
available[1]=available[1]-req2; 211
available[2]=available[2]-req3;
allocation[no][0]=allocation[no][0]+req1; 002
allocation[no][1]=allocation[no][1]+req2;
allocation[no][2]=allocation[no][2]+req3; Enter MAX matrix :
need[no][0]=need[no][0]-req1;
need[no][1]=need[no][1]-req2; 753
need[no][2]=need[no][2]-req3;
} 322
else
printf("\n\t ***Process p%d must be waited!!!",no); 902
else
printf("\n\t ***Error....exceding request allocation 222
***");
printf("\n do you want to continue<yes=1/no=0>:"); 433
scanf("%d",&f);
if(f==0) Enter Available :
break; Enter available value of resource 0 : 3 3 2
}
} Enter available value of resource 1 :
Enter available value of resource 2 :
Calculating need....
743
122
OUTPUT : 600
011
431
need calculation completed...
[cse053@localhost cse053]$ cc ex7.c *****process P1 is safe *****
*****process P3 is safe *****
[cse053@localhost cse053]$ ./a.out *****process P4 is safe *****
*****process P0 is safe *****
*****process P2 is safe *****enter requested
process:1
enter the number of processes:5
enter the number of resources:3 enter the requested resources:5 3 2

enter the allocation matrix ***Error....exceding request allocation ***


do you want to continue<yes=1/no=0>:1
010 enter requested process:4 4 4

200 enter the requested resources:1

302 ***Error....exceding request allocation *** do


you want to continue<yes=1/no=0>
EXNO-8 DEADLOCK AVOIDANCE ALGORITHM

PROGRAM:

#include<stdio.h> {
static int mark[20]; int canbeprocessed= 0;
int i, j, np, nr; if(mark[i]!=1)
int main() {
{ for(j=0;j<nr;j++)
int alloc[10][10],request[10] {
[10],avail[10],r[10],w[10]; if(request[i][j]<=w[j])
printf ("\nEnter the no of the process: "); canbeprocessed=1;
scanf("%d",&np); else
printf ("\nEnter the no of resources: "); {
scanf("%d",&nr); canbeprocessed=0;
for(i=0;i<nr; i++) break;
{ }
printf("\nTotal Amount of the Resource R % d: ",i+1); }
scanf("%d", &r[i]); if(canbeprocessed)
} {
printf("\nEnter the request matrix:"); mark[i]=1;
for(i=0;i<np;i++) for(j=0;j<nr;j++)
for(j=0;j<nr;j++) w[j]+=alloc[i][j];
scanf("%d",&request[i][j]); }
printf("\nEnter the allocation matrix:"); }
for(i=0;i<np;i++) }
for(j=0;j<nr;j++) int deadlock=0;
scanf("%d",&alloc[i][j]); for(i=0;i<np;i++)
for(j=0;j<nr;j++) if(mark[i]!=1)
{ deadlock=1;
avail[j]=r[j]; if(deadlock)
for(i=0;i<np;i++) printf("\n Deadlock detected");
{ else
avail[j]-=alloc[i][j]; printf("\n No Deadlock possible");
} }
}
for(i=0;i<np;i++)
{
int count=0; OUTPUT:
for(j=0;j<nr;j++)
{ [cse053@localhost cse053]$ cc ex8.c
if(alloc[i][j]==0)
count++; [cse053@localhost cse053]$ ./a.out
else
break;
}
if(count==nr) Enter the no of the process: 5
mark[i]=1;
} Enter the no of resources: 3
for(j=0;j<nr; j++)
w[j]=avail[j]; Total Amount of the Resource R 1: 7
for(i=0;i<np; i++)
Total Amount of the Resource R 2: 2
Enter the allocation matrix:
Total Amount of the Resource R 3: 6 010
200
Enter the request matrix: 303
000 211
202 002
000
100 Deadlock detected
002
EXNO-9 THREADING

PROGRAM:

#include<stdio.h> {
err=pthread_create(&tid[i],NULL,&createthread,NUL
#include<string.h> L);
#include<pthread.h> if(err!=0)
#include<stdlib.h> printf("\n Cant create thread:[%s]",strerror(err));
#include<unistd.h> else
pthread_t tid[20]; printf("\n Thread created successfully");
void * createthread(void * arg) i++;
{ }
unsigned long i=0; sleep(5);
pthread_t id =pthread_self(); return 0;
if(pthread_equal(id,tid[0])) }
{
printf("\n First thread processing\n");
} OUTPUT:
else
{
printf("\n Second thread processing \n"); [cse053@localhost cse053]$ cc ex9.c
}
for(i=0;i<10000000;i++) [cse053@localhost cse053]$ ./a.out
return NULL;
}
int main(void)
{ Thread created successfully
int i =0; Thread created successfully
int err; First thread processing
while(i<2)
Second thread processing
EXNO-10 PAGING TECHNIQUE

PROGRAM:

#include<stdio.h> printf("\n offset is %d",offset);

main() pa=fno[p]*size +offset;

{ printf("\n the physical address is %d",pa);

int n,size,la,i,pno[10],fno[10],offset,pa,limit,p; }

printf("enter the no of pages:"); OUTPUT:

scanf("%d",&n); [cse053@localhost cse053]$ cc ex10.c

printf("enter the size of pages:"); [cse053@localhost cse053]$ ./a.out

scanf("%d",&size); enter the no of pages:4

limit=n*size-1; enter the size of pages:4

printf("enter logical address within the limit enter logical address within the limit 15:6
%d:",limit);
enter the pages:0
scanf("%d",&la);
enter the frame no:5
for(i=0;i<n;i++)
enter the pages:1
{
enter the frame no:3
printf("enter the pages:");
enter the pages:2
scanf("%d",&pno[i]);
enter the frame no:8
printf("enter the frame no:");
enter the pages:3
scanf("%d",&fno[i]);
enter the frame no:6
}
pageno frame no
printf("pageno \t frame no");
0 5
for(i=0;i<n;i++)
1 3
{
2 8
printf("\n %d \t\t %d",pno[i],fno[i]);
3 6
}
the page number is 1
p=la/size;
offset is 2
printf("\n the page number is %d",p);
the physical address is 14
offset=la % size;
EXNO-11 MEMORY ALLOCATION METHODS

EXNO-11(A) FIRST FIT

PROGRAM:

#include<stdio.h> else
printf("Not allocated");
void main() }
{ }
int bsize[10], psize[10], bno, pno, flags[10],
allocation[10], i, j;
for(i = 0; i < 10; i++) OUTPUT:
{
flags[i] = 0;
allocation[i] = -1; [cse053@localhost cse053]$ cc ex11a.c
}
printf("Enter no. of blocks: "); [cse053@localhost cse053]$ ./a.out
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]); Enter no. of blocks: 5
printf("\nEnter no. of processes: ");
scanf("%d", &pno); Enter size of each block: 100
printf("\nEnter size of each process: "); 500
for(i = 0; i < pno; i++) 200
scanf("%d", &psize[i]); 300
for(i = 0; i < pno; i++) 600
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i]) Enter no. of processes: 4
{
allocation[j] = i; Enter size of each process: 212
flags[j] = 1; 417
break; 112
} 426
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i < bno; i++) Block no. size process no. size
{ 1 100 Not allocated
printf("\n%d\t\t%d\t\t", i+1, bsize[i]); 2 500 1 212
if(flags[i] == 1) 3 200 3 112
printf("%d\t\t\t%d",allocation[i] 4 300 Not allocated
+1,psize[allocation[i]]); 5 600 2 417
EXNO-11(B) BEST FIT

PROGRAM:

#include<stdio.h> lowest=10000;
int main() }
{ printf("\n Process no\t Process size\t Block no\t
int Block size\t Fragment");
fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=999 for(i=1;i<=np&&parray[i]!=0;i++)
9; printf("\n %d \t\t %d\t\t %d\t\t %d\t\t
static int barray[20],parray[20]; %d",i,p[i],parray[i],b[parray[i]],fragment[i]);
printf("\n\t\t MEMORY MANAGEMENT SCHEME- }
BEST FIT\n");
printf("\n Enter the no of blocks:");
scanf("%d",&nb);
printf("Enter the no.of process:"); OUTPUT:
scanf("%d",&np);
printf("Enter the size of blocks:"); [cse053@localhost cse053]$ cc ex11b.c
for(i=1;i<=nb;i++)
{ [cse053@localhost cse053]$ ./a.out
printf("Block no:%d-",i);
scanf("%d",&b[i]);
} MEMORY MANAGEMENT SCHEME-BEST FIT
printf("\n Enter the size of the processes:");
for(i=1;i<=np;i++) Enter the no of blocks:5
{ Enter the no.of process:4
printf("Process no:%d-",i); Enter the size of blocks:Block no:1-100
scanf("%d",&p[i]); Block no:2-500
} Block no:3-200
for(i=1;i<=np;i++) Block no:4-300
{ Block no:5-600
for(j=1;j<=nb;j++)
{ Enter the size of the processes:Process no:1-212
if(barray[j]!=1) Process no:2-417
{ Process no:3-112
temp=b[j]-p[i]; Process no:4-426
if(temp>=0)
if(lowest>temp) Process no Process size Block no Block size
{ Fragment
parray[i]=j; 1 212 4
300 88
lowest=temp;
2 417 2
}
500 83
} 3 112 3
} 200 88
fragment[i]=lowest; 4 426 5
barray[parray[i]]=1; 600 174
EXNO-11(C) WORST FIT

PROGRAM:

#include<stdio.h> }
int main() }
{ fragment[i]=greatest;
int barray[parray[i]]=1;
fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=999 lowest=10000;
9,greatest; }
static int barray[20],parray[20]; printf("\n Process no\t Process size\t Block no\t
printf("\n\t\t MEMORY MANAGEMENT SCHEME- Block size\t Fragment");
WORST FIT\n"); for(i=1;i<=np&&parray[i]!=0;i++)
printf("\n Enter the no of blocks:"); printf("\n %d \t\t %d\t\t %d\t\t %d\t\t
scanf("%d",&nb); %d",i,p[i],parray[i],b[parray[i]],fragment[i]);
printf("Enter the no.of process:"); }
scanf("%d",&np); OUTPUT:
printf("Enter the size of blocks:");
for(i=1;i<=nb;i++)
{ [cse053@localhost cse053]$ cc ex11c.c
printf("Block no:%d-",i);
scanf("%d",&b[i]); [cse053@localhost cse053]$ ./a.out
}
printf("\n Enter the size of the processes:");
for(i=1;i<=np;i++)
{ MEMORY MANAGEMENT SCHEME-WORST FIT
printf("Process no:%d-",i);
scanf("%d",&p[i]); Enter the no of blocks:5
} Enter the no.of process:4
for(i=1;i<np;i++) Enter the size of blocks:Block no:1-100
{ Block no:2-500
for(j=1;j<=nb;j++) Block no:3-200
{ Block no:4-300
if(barray[j]!=1) Block no:5-600
{
temp=b[j]-p[i]; Enter the size of the processes:Process no:1-212
if(temp>=0) Process no:2-417
if(greatest<temp) Process no:3-112
{ Process no:4-426
parray[i]=j;
greatest=temp; Process no Process size Block no Block size
} Fragment
1 212 5 600 388
EXNO-12 PAGE REPLACEMENT ALGORITHMS

EXNO-12(A) FIFO PAGE REPLACEMENT

PROGRAM:

#include<stdio.h>
int main()
{ OUTPUT:
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\nEnter the number of pages:\n"); [cse053@localhost cse053]$ cc ex12a.c
scanf("%d",&n);
printf("\n Enter the page number:"); [cse053@localhost cse053]$ ./a.out
for(i=0;i<n;i++)
scanf("%d",&a[i]); Enter the number of pages:
printf("\nEnter the no of frames:"); 12
scanf("%d",&no);
for(i=0;i<n;i++) Enter the page number:
frame[i]=-1; 1
j=0; 2
printf("Ref string \t page frames"); 3
for(i=1;i<n;i++) 4
{ 5
printf("%d\t\t",a[i]); 6
avail=0; 7
for(k=0;k<no;k++) 8
if(frame[k]==a[i]) 9
avail=1; 10
if(avail==0) 11
{ 12
frame[j]=a[i];
j=(j+1)%no; Enter the no of frames:4
count++; Ref string page frames
for(k=0;k<no;k++) 3 3 -1 -1 -1
printf("%d\t",frame[k]); 4 3 4 -1 -1
} 5 3 4 5 -1
printf("\n"); 6 3 4 5 6
} 7 7 4 5 6
printf("Page fault is %d",count); 8 7 8 5 6
return 0; 9 7 8 9 6
} 10 7 8 9 10
11 11 8 9 10
12 11 12 9 10
Page fault is 10

EXNO-12(B) LRU PAGE REPLACEMENT


PROGRAM:

#include<stdio.h> for(r=0;r<f;r++)
int main() {
{ for(j=r;j<f;j++)
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20]; {
printf("Enter no of pages:"); if(b[r]<b[j])
scanf("%d",&n); {
printf("Enter the reference string:"); t=b[r];
for(i=0;i<n;i++) b[r]=b[j];
scanf("%d",&p[i]); b[j]=t;
printf("Enter no of frames:"); }
scanf("%d",&f); }
q[k]=p[k]; }
printf("\n\t%d\n",q[k]); for(r=0;r<f;r++)
c++; {
k++; if(c2[r]==b[0])
for(i=1;i<n;i++) q[r]=p[i];
{ printf("\t%d",q[r]);
c1=0; }
for(j=0;j<f;j++) printf("\n");
{ }
if(p[i]!=q[j]) }
c1++; }
} printf("\nThe no of page faults is %d",c);
if(c1==f) }
{
c++; OUTPUT:
if(k<f)
{ [cse053@localhost cse053]$ cc ex12b.c
q[k]=p[i];
k++; [cse053@localhost cse053]$ ./a.out
for(j=0;j<k;j++)
printf("\t%d",q[j]); Enter no of pages:12
printf("\n"); Enter the reference string:1 2 3 4 5 6 7 8 9 10 11 12
} Enter no of frames:4
else
{ 1
for(r=0;r<f;r++) 1 2
{ 1 2 3
c2[r]=0; 1 2 3 4
for(j=i-1;j<n;j--) 5 2 3 4
{ 5 6 3 4
if(q[r]!=p[j]) 5 6 7 4
c2[r]++; 5 6 7 8
else 9 6 7 8
break; 9 10 7 8
} 9 10 11 8
} 9 10 11 12
for(r=0;r<f;r++)
b[r]=c2[r]; The no of page faults is 4
EXNO-12(C) LFU PAGE REPLACEMENT

PROGRAM:

#include<stdio.h> int j;
struct frame for(j=0;j<3;j++)
{ {
int val; if(f[j].val==page_seq[i])
int freq; return j;
int pos; }
}f[30]; return -1;
int no_frame,no_page,page_seq[100],page_fault=0; }
void input(){ int position()
int i; {
printf("\n\n least frequently used"); int i,j=0,k;
printf("\n Enter no of frames"); for(i=0;i<3;i++)
scanf("%d",&no_frame); {
printf("\nEnter no of pages"); if(f[i].pos==-1)
scanf("%d",&no_page); return i;
printf("Enter page sequence"); }
for(i=0;i<3;i++) for(i=0;i<3;i++)
scanf("%d",&page_seq[i]); {
printf("\n"); if(f[j].freq>f[i].freq)
for(i=0;i<3;i++){ {
f[i].pos=-1; j=i;
f[i].val=-1; } }
f[i].freq=0; k=j;
} for(i=0;i<3;i++)
} {
void display() if(f[j].freq==f[i].freq&&j!=i)
{ {
static int preev_page_fault=0; if(f[j].pos>f[i].pos)
int i; j=i;
for(i=0;i<3;i++) }
{ k=j;
if(f[i].val==-1) return k;
{ }
printf("0"); return 0;
} }
else void LFU()
{ {
printf("%d",f[i].val); int i,k;
} input();
} for(i=0;i<3;i++)
if(preev_page_fault!=page_fault) {
{ k=search(i);
printf("F"); if(k!=-1)
preev_page_fault=page_fault; {
} f[k].freq++;
printf("\n"); f[k].pos=i;
} }
int search(int i) if(k==-1)
{ {
k=position();
f[k].pos=i;
f[k].val=page_seq[i]; [cse053@localhost cse053]$ cc ex12c.c
f[k].freq=1;
page_fault++; [cse053@localhost cse053]$ ./a.out
}
display();
}
Least frequently used
}
Enter no of frames3
int main()
{
Enter no of pages4
LFU();
Enter page sequence7
printf("\n The no. of page faults %d",page_fault);
8
}
9
}
700F
780F
789F

The no. of page faults 3


OUTPUT:
EXNO-13 FILE ORGANIZATION TECHNIQUES

EXNO-13(A) SINGLE LEVEL DIRECTORY FILE ORGANIZATION

PROGARM:
#include<stdio.h>
int main()
{
int master,s[20]; OUTPUT:
char f[20][20][20];
char d[20][20];
int i,j; [cse053@localhost cse053]$ cc ex13a.c
printf("enter number of directories:");
scanf("%d",&master); [cse053@localhost cse053]$ ./a.out
printf("enter names of directories:");
for(i=0;i<master;i++)
scanf("%s",&d[i]); enter number of directories:2
printf("enter size of directories:"); enter names of directories:abc
for(i=0;i<master;i++) def
scanf("%d",&s[i]); enter size of directories:2
printf("enter the file names:"); 4
for(i=0;i<master;i++) enter the file names:a
for(j=0;j<s[i];j++) b
scanf("%s",&f[i][j]); c
printf("\n"); d
printf("directory\tsize\tfilename\n"); e
printf("************************************* f
*\n");
for(i=0;i<master;i++) directory size filename
{ **************************************
printf("%s\t\t%2d\t",d[i],s[i]); abc 2 a
for(j=0;j<s[i];j++) b
printf("%s\n\t\t\t",f[i][j]);
printf("\n"); def 4 c
} d
printf("\t\n"); e
} f

EXNO-13(B) TWO LEVEL DIRECTORY FILE ORGANIZATION


PROGRAM:

#include<stdio.h> printf("%s\t",dir[i].fname[j][k]);
struct st printf("\n\t\t");
{ }
char dname[10]; printf("\n");
char sdname[10][10]; }
char fname[10][10][10]; }
int ds,sds[10];
}dir[10];
int main()
{
int i,j,k,n; OUTPUT:
printf("enter number of directories");
scanf("%d",&n);
for(i=0;i<n;i++) [cse053@localhost cse053]$ cc ex13b.c
{
printf("enter directory %d [cse053@localhost cse053]$ ./a.out
names:",i+1);scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds); enter number of directories2
for(j=0;j<dir[i].ds;j++) enter directory 1 names:aaa
{ enter size of directories:2
printf("enter subdirectionary name and size:"); enter subdirectionary name and size:ab
scanf("%s",&dir[i].sdname[j]); 2
scanf("%d",&dir[i].sds[j]); enter file name:aa
for(k=0;k<dir[i].sds[j];k++) enter file name:bb
{ enter subdirectionary name and size:bc
printf("enter file name:"); 1
scanf("%s",&dir[i].fname[j][k]); enter file name:cc
} enter directory 2 names:xyz
} enter size of directories:2
} enter subdirectionary name and size:xx
printf("\nDIRNAME\t\tSIZE\tSUBDIRNAME\tSIZE\ 1
tFILES"); enter file name:ss
printf("\ enter subdirectionary name and size:yz
n****************************************** 1
***********\n"); enter file name:as
for(i=0;i<n;i++)
{ DIRNAME SIZE SUBDIRNAME SIZE FILES
printf("%s\t\t%d",dir[i].dname,dir[i].ds); *******************************************
for(j=0;j<dir[i].ds;j++) **********
{ aaa 2 ab 2 aa bb
printf("\t%s\t\t%d\ bc 1 cc
t",dir[i].sdname[j],dir[i].sds[j]);for(k=0;k<dir[i].sds[j];
k++) xyz 2 xx 1 ss
yz 1 as

EXNO-14 FILE ALLOCATION STRATAGIES


EXNO-14(A) SEQUENTIAL FILE ALLOCATION

PROGRAM:

#include<stdio.h> if(i!=0)
#include<fcntl.h> start++;
#include<string.h> printf("\n %s\t\t %d\t %d\t %d\
int main() n",name[i],temp,start,n[i]);
{ }
char buff[10][10],name[10][10]; }
int fp,n[20],start=0,num,i,temp;
printf("\n Enter the number of file:");
scanf("%d",&num); OUTPUT:
for(i=0;i<num;i++)
{
printf("\n enter the name of the file %d:",i+1); [cse053@localhost cse053]$ cc ex14a.c
scanf("%s",&name[i]);
fp=open(name[i],O_RDONLY); [cse053@localhost cse053]$ ./a.out
read(fp,buff[i],100);
n[i]=strlen(buff[i]);
printf("%d",n[i]); Enter the number of file:2
} enter the name of the file 1:f1
printf("\nFile name\t Start\tEnd\tLength\n"); 0
for(i=0;i<num;i++) enter the name of the file 2:f2
{ 0
if(i==0) File name Start End Length
temp=0;
else f1 0 -1 0
temp=start+1;
start=start+n[i]-1; f2 0 -1 0

EXNO-14(B) LINKED FILE ALLOCATION


PROGRAM:

#include<stdio.h> }
struct file
{ OUTPUT:
char fname[10];
int start,size,block[10];
}f[10]; [cse053@localhost cse053]$ cc ex14b.c
int main()
{ [cse053@localhost cse053]$ ./a.out
int i,j,n;
printf("\n enter no of files:");
scanf("%d",&n);
for(i=0;i<n;i++) enter no of files:2
{
printf("\n enter file name:"); enter file name:f1
scanf("%s",&f[i].fname);
printf("\n enter starting block:"); enter starting block:20
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start; enter no of blocks:3
printf("\n enter no of blocks:");
scanf("%d",&f[i].size); enter block numbers:5
printf("\n enter block numbers:"); 10
for(j=1;j<=f[i].size;j++) 20
{
scanf("%d",&f[i].block[j]); enter file name:f2
}
} enter starting block:30
printf("\n file \t start \t size \t block \n");
for(i=0;i<n;i++) enter no of blocks:2
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size); enter block numbers:10
for(j=1;j<=f[i].size-1;j++) 20
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]); file start size block
printf("\n"); f1 20 3 5--->10--->20
} f2 30 2 10--->20

EXNO-14(C) INDEX FILE ALLOCATION


PROGRAM:

#include<stdio.h> printf("\n Allocated");


int main() printf("File Indexed");
{ for(k=0;k<n;k++)
int f[50],i,k,j,inde[50],n,c,count=0,p; printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
for(i=0;i<50;i++) printf("\nenter 1 to enter more files and 0 to exit");
f[i]=0;
x: scanf("%d",&c);
printf("enter the index block:"); if(c==1)
scanf("%d",&p); goto x;
if(f[p]==0) else
{ exit(0);
f[p]=1; }
printf("enter the number of files on index\t");
scanf("%d",&n);
}
else OUTPUT:
{
printf("Block already allocated\n"); [cse053@localhost cse053]$ cc ex14c.c
goto x;
} [cse053@localhost cse053]$ ./a.out
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
enter the index block:10
for(i=0;i<n;i++)
enter the number of files on index 2
if(f[inde[i]]==1)
3
{
5
printf("Block already allocated");
goto x;
AllocatedFile Indexed
}
10->3:1
for(j=0;j<n;j++)
10->5:1
f[inde[j]]=1;
enter 1 to enter more files and 0 to exit 0

EXNO-15 DISK SCHEDULLNG ALGORITHMS


EXNO-15(A) FCFS:

PROGRAM:

#include<stdio.h> }
#include<stdlib.h>
int main()
{ OUTPUT:
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n"); [cse053@localhost cse053]$ cc ex15a.c
scanf("%d",&n);
printf("Enter the Requests sequence\n"); [cse053@localhost cse053]$ ./a.out
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n"); Enter the number of Requests
scanf("%d",&initial); 7
for(i=0;i<n;i++) Enter the Requests sequence
{ TotalHeadMoment=TotalHeadMoment+abs(RQ[i]- 82
initial); 170
initial=RQ[i]; 43
} 140
24
printf("Total head moment is 16
%d",TotalHeadMoment); 190
return 0; Enter initial head position
50
Total head moment is 642

EXNO-15(B) SSTF:
PROGRAM:

#include<stdio.h> count++;
#include<stdlib.h> }
int main()
{ printf("Total head movement is
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0; %d",TotalHeadMoment);
printf("Enter the number of Requests\n"); return 0;
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"); OUTPUT:
scanf("%d",&initial);
[cse053@localhost cse053]$ cc ex15b.c
while(count!=n)
{ [cse053@localhost cse053]$ ./a.out
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial); Enter the number of Requests
if(min>d) 7
{ Enter the Requests sequence
min=d; 82
index=i; 170
} 43
140
} 24
TotalHeadMoment=TotalHeadMoment+min; 16
initial=RQ[index]; 190
Enter initial head position
RQ[index]=1000; 50
Total head movement is 208

EXNO-15(C) SCAN
PROGRAM:

#include<stdio.h> initial=RQ[i];
#include<stdlib.h> }
int main()
{ TotalHeadMoment=TotalHeadMoment+abs(size-
int RQ[i-1]-1);
RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move; initial = size-1;
printf("Enter the number of Requests\n"); for(i=index-1;i>=0;i--)
scanf("%d",&n); {
printf("Enter the Requests sequence\n"); TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
for(i=0;i<n;i++) initial);
scanf("%d",&RQ[i]); initial=RQ[i];
printf("Enter initial head position\n");
scanf("%d",&initial); }
printf("Enter total disk size\n"); }
scanf("%d",&size);
printf("Enter the head movement direction for high else
1 and for low 0\n"); {
scanf("%d",&move); for(i=index-1;i>=0;i--)
{
for(i=0;i<n;i++) TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
{ initial);
for(j=0;j<n-i-1;j++) initial=RQ[i];
{ }
if(RQ[j]>RQ[j+1])
{ TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-
int temp; 0);
temp=RQ[j]; initial =0;
RQ[j]=RQ[j+1]; for(i=index;i<n;i++)
RQ[j+1]=temp; {
} TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
initial);
} initial=RQ[i];
}
}
int index; }
for(i=0;i<n;i++)
{ printf("Total head movement is
if(initial<RQ[i]) %d",TotalHeadMoment);
{ return 0;
index=i; }
break;
}
}

OUTPUT:
if(move==1)
{ [cse053@localhost cse053]$ cc ex15c.c
for(i=index;i<n;i++)
{ [cse053@localhost cse053]$ ./a.out
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
initial);
190
Enter the number of Requests Enter initial head position
7 50
Enter the Requests sequence Enter total disk size
82 200
170 Enter the head movement direction for high 1 and
43 for low 0
140 0
24 Total head movement is 240
16

EXNO-15(D) C- SCAN
PROGRAM:

#include<stdio.h> {
#include<stdlib.h> TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
int main() initial);
{ initial=RQ[i];
int }
RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n"); TotalHeadMoment=TotalHeadMoment+abs(size-
scanf("%d",&n); RQ[i-1]-1);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++) TotalHeadMoment=TotalHeadMoment+abs(size-1-
scanf("%d",&RQ[i]); 0);
printf("Enter initial head position\n"); initial=0;
scanf("%d",&initial); for( i=0;i<index;i++)
printf("Enter total disk size\n"); {
scanf("%d",&size); TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
printf("Enter the head movement direction for high initial);
1 and for low 0\n"); initial=RQ[i];
scanf("%d",&move);
}
}
for(i=0;i<n;i++)
{ else
for( j=0;j<n-i-1;j++) {
{ for(i=index-1;i>=0;i--)
if(RQ[j]>RQ[j+1]) {
{ TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
int temp; initial);
temp=RQ[j]; initial=RQ[i];
RQ[j]=RQ[j+1]; }
RQ[j+1]=temp;
} TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-
0);
}
} TotalHeadMoment=TotalHeadMoment+abs(size-1-
0);
int index; initial =size-1;
for(i=0;i<n;i++) for(i=n-1;i>=index;i--)
{ {
if(initial<RQ[i]) TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
{ initial);
index=i; initial=RQ[i];
break;
} }
} }

printf("Total head movement is


if(move==1) %d",TotalHeadMoment);
{ return 0;
for(i=index;i<n;i++) }
OUTPUT: 43
140
[cse053@localhost cse053]$ cc ex15d.c 24
16
[cse053@localhost cse053]$ ./a.out 190
Enter initial head position
50
Enter total disk size
Enter the number of Requests 200
7 Enter the head movement direction for high 1 and
Enter the Requests sequence for low 0
82 0
170 Total head movement is 366

EXNO-15(E) LOOK
PROGRAM:
#include<stdio.h> }
#include<stdlib.h>
int main() for(i=index-1;i>=0;i--)
{ {
int TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move; initial);
printf("Enter the number of Requests\n"); initial=RQ[i];
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"); else
scanf("%d",&initial); {
printf("Enter total disk size\n"); for(i=index-1;i>=0;i--)
scanf("%d",&size); {
printf("Enter the head movement direction for high TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
1 and for low 0\n"); initial);
scanf("%d",&move); initial=RQ[i];
}
for(i=0;i<n;i++)
{ for(i=index;i<n;i++)
for(j=0;j<n-i-1;j++) {
{ TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
if(RQ[j]>RQ[j+1]) initial);
{ initial=RQ[i];
int temp;
temp=RQ[j]; }
RQ[j]=RQ[j+1]; }
RQ[j+1]=temp;
} printf("Total head movement is
%d",TotalHeadMoment);
} return 0;
} }

int index;
for(i=0;i<n;i++) OUTPUT:
{
if(initial<RQ[i]) [cse053@localhost cse053]$ cc ex15e.c
{
index=i; [cse053@localhost cse053]$ ./a.out
break;
}
}
Enter the number of Requests
if(move==1) 7
{ Enter the Requests sequence
for(i=index;i<n;i++) 82
{ 170
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]- 43
initial); 140
initial=RQ[i]; 24
16 200
190 Enter the head movement direction for high 1 and
Enter initial head position for low 0
50 0
Enter total disk size Total head movement is 208
EXNO-15(F) C - LOOK

PROGRAM:

#include<stdio.h> for(i=index;i<n;i++)
#include<stdlib.h> {
int main() TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
{ initial);
int initial=RQ[i];
RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move; }
printf("Enter the number of Requests\n");
scanf("%d",&n); for( i=0;i<index;i++)
printf("Enter the Requests sequence\n"); {
for(i=0;i<n;i++) TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
scanf("%d",&RQ[i]); initial);
printf("Enter initial head position\n"); initial=RQ[i];
scanf("%d",&initial);
printf("Enter total disk size\n"); }
scanf("%d",&size); }
printf("Enter the head movement direction for high
1 and for low 0\n"); else
scanf("%d",&move); {
for(i=index-1;i>=0;i--)
{
for(i=0;i<n;i++) TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
{ initial);
for( j=0;j<n-i-1;j++) initial=RQ[i];
{ }
if(RQ[j]>RQ[j+1])
{ for(i=n-1;i>=index;i--)
int temp; {
temp=RQ[j]; TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-
RQ[j]=RQ[j+1]; initial);
RQ[j+1]=temp; initial=RQ[i];
}
}
} }
}
printf("Total head movement is
int index; %d",TotalHeadMoment);
for(i=0;i<n;i++) return 0;
{ }
if(initial<RQ[i])
{
index=i; OUTPUT:
break;
} [cse053@localhost cse053]$ cc ex15f.c
}
[cse053@localhost cse053]$ ./a.out

if(move==1)
{ Enter the number of Requests
7 Enter initial head position
Enter the Requests sequence 50
82 Enter total disk size
170 200
43 Enter the head movement direction for high 1 and
140 for low 0
24 0
16 Total head movement is 316
190

You might also like