Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

JYOTHISHMATHI INSTITUTE OF
TECHNOLOGY & SCIENCE
NUSTULAPUR, KARIMNAGAR

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

LAB MANUAL OF
UNIX & OS LAB (IT)
(III B.Tech. II SEM)

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

INDEX
SNO

PROGRAM NAME

Write a shell script to print the multiplication


table for the given number.
Write a shell script that copies multiple files
into a directory.
Write a shell script to find no. of words and
characters in a given file.
Write a shell script to display all files in a
given directory.
Write a shell script to perform simple calculator.
Write a C program to count no.of blanks,characters
a) Using standard i/o function. b) Using system
calls.
Write a C program to illustrate the following
Command using system Calls a) cat
b) ls
c)
mv
Write a C program to illustrate the stat system
call.
Write a C program that illustrates the creation of
child process using fork() system call.
Write a C program that illustrates file locking.
Write a C program that implements producer
consumer problem using semaphore system calls.
Write a C program to illustrate inter process
communication using shared memory system calls.
Write a C program to
(a) create message queue
(b) write to message queue
(c) read from
message queue

2
3
4
5
6

8
9
10
11
12
13

PAGE
NO
1
2
3
4
5
6-7

8-10

11
12
13-14
15-16
17-18
19-20

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

O.S. PROGRAMMES
S.NO
1
2
3
4
5
6
7
8
9
10
11
12
13

PROGRAM NAME
Write a C program for ROUND ROBIN CPU scheduling
algorithm
Write a C program for SJF CPU scheduling algorithm
Write a C program for FCFS CPU scheduling
algorithm
Write a C Program for priority CPU scheduling
algorithm.
Write a C Program for sequential file allocation.
Program for indexed file allocation strategy.
Program for linked file allocation strategy.
Write a C Program for MVT first fit.
Write a C Program for MFT .
Write a C Program for MVT best fit.
Program for bankers algorithm
Write a C program for FIFO page allocation
algorithm.
Write a C program for LRU page replacement
algorithm.

PAGE
NO
20-21
22-23
24
25-26
27-28
29-30
31-32
33
34
35-36
37-39
40-41
42-44

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 1:
AIM:
Write a shell script to print the multiplication table for the
given number.
Algorithm:
1.Start.
2.Read n.
3.i=1.
4.f = n * i.
5.i = i + 1.
6.Display f.
7.Repeat 4,5,6 steps until i = 10.
8.End.

Source Code:
#! /bin/sh
echo enter the number
read n
for i in 1 2 3 4 5 6 7 8 9 10
do
x= `expr $n \* $i`
done

Input:
enter the number
6

Output:
6*1=6
6 * 2 = 12

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60

Program 2:
AIM:
Write a shell script that copies multiple files into a directory.
Algorithm:
1.Start.
2.Read *.c files into i.
3.Copy $i to the root directory.
4.Repeat 2,3 steps until all values of $i..
5.End.

Source Code:
#! /bin/sh
for i in `ls *.c`
do
cp $i /root
done
echo file are copied into root

Output:
file are copied into root

Program 3 :
AIM:
Write a shell script to find no. of words and characters in a
given file.
Algorithm:
1. Start.
6

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


2.
3.
4.
5.
6.
7.
8.
9.

cc = 0,w=0.
give file name as commandline argument (i.e $1).
for i in `cat $1`.
w = w + 1.
c = expr $i : *.
cc = cc + c.
Repeat 4,5,6,7 steps until eof.
End.

Source Code:
# /bin/sh
w=0
cc=0
for i in cat $1
do
j= $i
echo $j
w=`expr $w + 1`
c=`expr $j:.*`
cc = `expr $cc + $c`
done
echo no.of characters $cc
echo no.of words $w

Input:.
./a.out sun

Output:
no. of characters 56
no. of words 11

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 4:
AIM:
Write a shell script to display all files in a given directory.
Algorithm:
1.Start.
2.Read i value from ls sss.
3.Display i value .
4.Repeat above 2 steps until end of directory.
5.End.

Source Code:
#! /bin/sh
for i in `ls sss`
do
echo $i
done

Output:
abc.txt
ss.c

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 5:
AIM:
Write a shell script to perform simple calculator.
Algorithm:
1.Start
2.Read a and b
3.Read op
4.Depending on value of operator perform
Operation.
5.End

Source Code:
#! /bin/sh
echo enter the value for a
read a
echo enter the value for b
read b

case

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


echo enter operator
read op
case $op in
+) c=`expr $a + $b`;;
-) c = `expr $a -$b`;;
\*) c = `expr $a \* $b`;;
/) c = `expr $a / $b ;;
esac
echo $c

Input:
enter the value for a
3
enter the value for b
6
enter the operator
*

Output:
18

Program 6a:
AIM:
Write a C program to count no. of blanks, characters, lines
using standard i/o function.
Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.

Start.
open the file using fopen( ) function in r mode
ch=fgetc(fp) (to read character by character)
if ch = or \n
b=b+1.
if ch = \n
l=l+1.
c=c+1.
Repeat 3,4,5&6 steps until ch = eof
End

Source Code:
#include<stdio.h>
int main( )
{
10

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


file *fp:
int b=0,nl=0,c=0;
char ch;
fp=fopen(text.txt,r);
while(ch!=eof)
{
ch=fgetc(fp);
if(ch== )
b++;
if(ch==\n)
nl++;
c++;
}
printf(no.of blanks %d,b);
printf(no.of lines %d,nl);
printf(no.of characters %d,c);
}

Input:
./a.out sss.txt

Output:
no.of blanks 5
n.of lines 2
no.of characters 36

Program 6b:
AIM:
Write a C program to count no. of blanks, characters, lines
using system calls.
Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.

Start.
open the file using open( ) system call in O_RDONLY
rc=read(fd,buf,5) (to read characters )
if ch = or \n
w=w+1.
if ch = \n
l=l+1.
c=c+1.
Repeat 3,4,5&6 steps until ch = eof
End

11

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


Source Code:
#include<stdio.h>
int main( )
{
int fd:
int b=0,nl=0,c=0;
char ch[1];
fd=open(text.txt,O_RDONLY);
while((rc=read(fd,ch,1))>0)
{
if(ch== )
b++;
if(ch==\n)
nl++;
c++;
}
printf(no.of blanks %d,b);
printf(no.of lines %d,nl);
printf(no.of characters %d,c);
}

Input:
./a.out sss.txt

Output:
no. of blanks 5
no. of lines 2
no. of characters 36

Program 7a:
AIM:
Write a C program to illustrate the mv command using
system Calls
Algorithm:
1. open one existed file and one new open file using open( )
system call
2. read the contents from keyboard using read( )
3. write these contents into file using write()
4. repeat 2,3 steps until eof
5. close 2 file using fclose( ) system call
6. delete existed file using using unlink( ) system

12

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


Source Code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int fd,fd1,rc;
char ch[5];
fd1=open(sss,O_WRONLY | O_CREATE);
while((rc=read(stdin,ch,5))>0)
write(fd1,ch,rc);
close fd1;
fd1=open(sss,O_RDONLY);
while((rc=read(fd1,ch,5))>0)
write(stdout,ch,rc);
close fd1;
}

Input:
hai hello
A friend in need is a friend in deed
^z

Output:
hai hello
A friend in need is a friend in dee

Program 7b :
AIM:
Write a program to illustrate ls command using system calls
Algorithm:
1.
2.
3.
4.
5.
6.

Start.
open directory using opendir( ) system call.
read the directory using readdir( ) system call.
print dp.name and dp.inode .
repeat above step until end of directory.
End
13

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


Source Code:
#include<stdio.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
dir *dp;
struct dirent *d;
if(((dp=opendir(sss)))==null)
printf(cannodt open\n);
while((d=readdir(dp)!=null))
{
printf(%s\t,d->d_name);
printf(%d\n,d->d_ino);
}
closedir(dirp);
exit(0);
}

output:
mul.sh
division.c
process.c

12344
12345
12346

Program 7c:
AIM:
Write a C program to illustrate the mv command using
system calls.
Algorithm:
1. Start
2. open an existed file and one new open file using open()
14

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


3.
4.
5.
6.
7.
8.

system call
read the contents from existed file using read( ) system
call
write these contents into new file using write system
call using write( ) system call
repeat above 2 steps until eof
close 2 file using fclose( ) system call
delete existed file using using unlink( ) system
End.

Source Code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int fd,fd1,rc;
char ch[5];
fd=open(sss,O_RDONLY );
fd1= open(rrr,O_WRONLY | O_CREATE);
while((rc=read(fd,ch,5))>0)
write(fd1,ch,rc);
close fd1;
close fd;
unlink(fd);
printf( file is copied );
}

Output:
file is copied

Program 8:
AIM:
Write a C program to illustrate the stat system calls
Algorithm:
1. Declare s and st of type struct stat.
2. Use stat system call by specifying sss.c file
15

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


3. stat returns s (s may be 0 or -1)
4. if s<0 then
display there is no file with the name sss.c.
5. Display information about sss.c file
6. Display inode number of sss.c.
7. Display last access time of file sss.c.
8. Display size of the file sss.c.

Source Code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int s;
struct stat st;
s=stat(sss.c,&st);
if(s<0)
{
printf(use of stat() is unsuccessful );
exit(0);
}
printf(inode number of sss.c file is %d,st.st_ino);
printf(no.of links to sss.c is %d, st.st_nlink );
printf(file permissions sss.c is %d,st.st_mode);
printf(size of sss.c file is ,st.st_size);
printf(last access time of sss.c %d,st.st_atime );
}

Output::
Output
inode number of sss.c file is 12345
no.of links to sss.c file is 2
file permissions of sss.c file is 666
size of sss.c file is 8
last access time of sss.c file is 7:40

Program 9:
AIM:
Write a C program that
that illustrates the creation of child
process using fork( ) system call

16

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.

Start
Declare pid
create new process using fork( ) system call
If pid!=0 then
Display parent process getpid(),getppid().
Else
Display child process getpid().getppid().
End

Source code:
#include<stdio.h>
int main( )
{
printf(original process with pid %d ppid %d\n,
getpid() ,getppid());
pid=fork();
if(pid!=0)
printf(parent process with pid %d ppid %d \n,
getpid(),getppid());
else
{
sleep(5);
printf(child process with pid %d ppid %d\n,
getpid(),getppid());
}
printf( pid %d terminates ,getpid());
}

Output:
original process with pid 3456 and ppid 3525
child process with pid 3457 and ppid 3456
pid 3457 terminates
parent process with pid 3456 and ppid 3525
pid 3456 terminates

Program 10:
AIM:
Write a C program that illustrates file locking.

17

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


Algorithm:
Al
gorithm:
1. Start
2. Flock is predefined data structure for locking
techniques.
3. Opening rr.txt in read and write mode.
4. l_type is read lock
5. By pressing return it reads that character through
getchar() function.
6. It gets the lock.
7. Next we are sending unlock type into l_type.
8. By pressing return it gets unlocked.
9. End

Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char* argv[])
{
struct flock f1={f_wrlck,seek_set,0,0,0);
int fd;
f1.l_type=f_rdlck;
if((fd=open(rr.txt,o_rdwr))==-1)
{
perror(open);
exit(1);
}
printf(press<return> to try to get lock:);
getchar();
printf(trying to get lock):
if(fnctl(fd,f_setlkw,&f1)==-1)
{
perror(fcntl);
exit(1);
}
printf(got lock \n);
printf(press <return> to release lock:);
getchar( );
f1.l_type=f_unlck;
if(fcntl(fd,f_setlk,&f1)==-1)
{
perror(fcntl);
exit(1);
}
18

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


printf(unlocked\n);
close(fd);
}

Output:
press<return> to try to get lock
trying to get lock
press <return> to release lock
unlocked

19

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 11:
AIM:
Write a C program that implements producer consumer
problem using semaphore system calls
Algorithm:
1.
2.
3.
4.
5.
6.

Start
create semaphore using semget( ) system call
if successful it returns positive value
create two new processes
first process will produce
until first process produces second process cannot
consume
7. End.

Source code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define num_loops 2
int main(int argc,char* argv[])
{
int sem_set_id;
int child_pid,i,sem_val;
struct sembuf sem_op;
int rc;
struct timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(main:semget);
exit(1);
}
printf(semaphore set created,semaphore setid%d\n ,
sem_set_id);
20

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


child_pid=fork();
switch(child_pid)
{
case -1:
perror(fork);
exit(1);

case 0:
for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(producer:%d\n,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(consumer:%d\n,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}
}
break;
}
return 0;
}

Output:
semaphore set created
semaphore set id 327690
producer: 0
consumer:0
producer:1
21

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


consumer:1

Program 12:
AIM:
Write a C program to illustrate inter process communication
using shared memory system calls.
Algorithm:
1.
2.
3.
4.

Start
create shared memory using shmget( ) system call
if success full it returns positive value
attach the created shared memory using shmat( ) system
call
5. write to shared memory using shmsnd( ) system call
6. read the contents from shared memory using shmrcv( )
system call
7. End .

Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<sys/shm.h>
#define shm_size 1024
int main(int argc,char * argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if(argc>2)
{
fprintf(stderr,usage:stdemo[data_to_writte]\n);
exit(1);
}
if((shmid=shmget(key,shm_size,0644/ipc_creat))==-1)
{
perror(shmget);
exit(1);
22

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


}
data=shmat(shmid,(void *)0,0);
if(data==(char *)(-1))
{
perror(shmat);
exit(1);
}
if(argc==2)
printf(writing to segment:\%s\\n,data);
if(shmdt(data)==-1)
{
perror(shmdt);
exit(1);
}
return 0;
}

Input:
#./a.out swarupa

Output:
writing to segment swarupa

23

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 13:
AIM:
Write a C program to
( a ) create message queue
( b ) write to message queue
( c ) read from message queue
Algorithm:
1. Start
2. create message queue using msgget( ) system call
3. while creating message queue it returns id into file
descriptor
4. print the message queue id
5. if successful rite into message queue using msgsnd()
system call
6. read the contents from message queue using msgrcv( )
system call
7. End.

Source Code:
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
int main(int argc,char* argv[])
{
int q_id;
if(q_id=msgget(ipc_private,0600)==-1)
perror(msgget);
struct msgbuf
{
long mtype;
char mtext[20];
}
struct msgbuf* msg;

24

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


struct msgbuf* recv_msg;
int rc;
printf(message queue created %d\n,q_id);
msg=(struct msgbuf*)malloc(sizeof(struct msgbuf) +
strlen(hello world));
msg->mtype=1;
strcpy(msg->mtext,helloworld);
free(msg);
printf(message placed on queue successfully\n);
recv_msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+
strlen(hello world));
if(rc==-1)
{
perror(main:msgrcv);
exit(1);
}
printf(msgrcv:received message:\nmtype%d\n; mtext
%s\n,recv_msg->mtype,recv_msg->mtext);
return 0;
}

Output:
message queue created 0
message placed on the queue successfully
msgrcv:received message:
mtype: 0
mtext: hello world

25

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

LAB MANUAL OF

OPERATING SYSTEM LAB

26

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 1:
AIM:
Write a C program for ROUND ROBIN CPU scheduling algorithm.
Source Code:
#include<stdio.h>
main()
{
int s[10],p[10],n,i,j,wi=0,w[10],t[10], st[10],tq,tst=0;
int tt=0,tw=0;
float aw at;
printf("enter no.of process");
scanf("%d",&n);
printf("\n enter time quanum");
scanf("%d",&tq);
printf("\n enter process&service time");
for(i=0;i<n;i++)
scanf("%d%d",&p[i],&s[i]);

27

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


for(i=0;i<n;i++)
st[i]=s[i];
tst=tst+s[i];
for(j=0;j<tst;j++)
for(i=0;i<n;i++)
{
if(s[i]>tq)
{
s[i]=s[i]-tq;
w1=w1+tq;
t[i]=w1;
w[i]=t[i]-st[i];
}
else if(s[i]!=0)
{
w1=w1+tq;
t[i]=w1;
w[i]=t[i]-st[i];
s[i]=s[i]-tq;
}
}
for(i=0;i<n;i++)
{
tw=tw+w[i];
tt=tt+t[i];
}
aw=tw/n;
at=tt/n;

printf("process\tst\twt\ttt");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d",p[i],st[i],w[i],t[i]);
printf("awt=%d",aw);
printf("att=%d",at);
}

Input:
enter no of process 3
enter time quantum 2
enter process&service time
1
4
2
6
3
28

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


2

Output:
process
1
2
3

st

wt

tt

4
6
2

4
6
4

8
12
6

Awt = 4.000000
att = 8.000000

Program 2:
AIM:
Write a C program for SJF CPU scheduling algorithm
Source Code:
#include<stdio.h>
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0;
float at,aw;
printf(enter no of jobs);
scanf(%d,&n);
printf(enter burst time);
for(i=0;i<n;i++)
scanf(%d,&bt[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
29

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


if(bt[i]<bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
for(i=0;i<n;i++)
{
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf(\nbt\twt\ttt);
for(i=0;i<n;i++)
printf(%d\t%d\t%d\n,bt[i],wt[i],tt[i]);
printf(aw=%d\nat=%d,aw,at);
getch();
}

Input:
Enter no of jobs
4
Enter burst time
5
12
8
20

Output:
Bt
5
12
8
20

wt
0
5
13
25

tt
5
13
25
45
30

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


aw=10.75000
at=22.000000

Program 3:
AIM:
Write a C program for FCFS CPU scheduling algorithm
Source Code:
include<stdio.h>
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0;
float at,aw;
printf(enter no of jobs);
scanf(%d,&n);
printf(enter burst time);
for(i=0;i<n;i++)
scanf(%d,&bt[i]);

31

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


for(i=0;i<n;i++)
{
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf(\nbt\twt\ttt);
for(i=0;i<n;i++)
printf(%d\t%d\t%d\n,bt[i],wt[i],tt[i]);
printf(aw=%d\nat=%d,aw,at);
getch();
}
Input:
enter no of jobs
3
enter bursttime
12
8
20

output:
bt
wt
tt
12
0
12
8
12
20
20
20
40
aw=10.666670
at=24.00000

Program 4:
AIM:
Program
Write a C Progra
m for priority CPU scheduling algorithm.
Source Code:
#include<stdio.h>
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0;
float at,aw;
printf(enter no of jobs);
32

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


scanf(%d,&n);
printf(enter burst time);
for(i=0;i<n;i++)
scanf(%d,&bt[i]);
printf(enter priority values);
for(i=0;i<n;i++)
scanf(%d,&pt[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(pt[i]<pt[j])
{
t=pt[i];
pt[i]=pt[j];
pt[j]=t;
k=bt[i];
bt[i]=bt[j];
bt[j]=k;
}
for(i=0;i<n;i++)
{
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf(\nbt\tprority\twt\ttt);
for(i=0;i<n;i++)
printf(%d\t%d\t%d\t%d\n,bt[i],pt[i],wt[i],tt[i]);
printf(aw=%d\nat=%d,aw,at);
getch();
}

Input:
Enter no of jobs
4
Enter bursttime
10
2
4
7
Enter priority values
4

33

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


2
1
3

Output:
Bt

priority

wt

tt

4
2
7
10

1
2
3
4

0
4
6
13

4
6
13
23

aw=5.750000
at=12.500000

Program 5:
AIM:
Write a C Program for sequential file allocation.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
34

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


int f[50],i,st,j,len,c,k,count=0;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n enter starting block & length of files");
scanf("%d%d",&st,&len);
printf("\n file not allocated(yes-1/no-0)");
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[i]==0)
{
f[j]=1;
printf("\n%d\t%d",,j,f[j]);
if(j==(st+len-1))
printf("\n the file is allocated to disk");
}
}
else
printf("file is not allocated");
count=0;
printf("\n if u want to enter more files(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}

Input:
enter starting block & length of files
4
5

Output:
file not allocated (y-1/n-0)
35

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


4
5
6
7
8

1
1
1
1
1

file is allocated to disk


if u want to enter more files(y-1/n-0)
0

Program 6:
AIM:
Write a C Program for indexed file allocation strategy.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],i,k,j,index[50],n,c,count=0;

36

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


for(i=0;i<50;i++)
f[i]=0;
X:
printf("enter index block");
scanf("%d",&i);
if(f[i]!=1)
{
f[i]=1;
printf("enter no of files on index");
scanf("%d",&n);
}
y:
for(i=0;i<n;i++)
scanf("%d",&index[i]);
if(f[index[i]==0)
count++;
if(count==n)
{
for(j=0;j<nj++)
f[index[j]=1;
printf("\nallocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n%d->%d:%d",i,index[k],f[index[k]);
}
else
{
printf("\n file in the index already allocation");
printf("\nenter another file indexed");
goto y;
}
printf("\n index is already allocated");
count=0;
printf("\n if u enter one more block(1/0)");
scanf("%d",&c);
if(c==1)
goto x;
getch( );
}

Input:
enter index block
3
enter no of files on index

37

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


4
5
6
7
8

Output:

Allocated
file indexed
4->5:1
4->6:1
4->7:1
4->8:1
index is already allocated
if u enter one more block(1/0)
0

Program 7:
AIM:
Write a C Program for linked file allocation strategy.
Source Code:
#include<stdio.h>
#include<conio.h>
main()

38

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


{
int f[50],p,i,j,k,a,st,len,n;
for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks already allocated");
scanf("%d",&p);
printf("\nenter the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter index sarting block & length");
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
}
else
printf("\nif u enter one more (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}

Input:
enter how many blocks already allocated
5

39

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


enter block nos
3
7
9
10
14
enter index starting block & length
4
10

Output:
4->1
5->1
6->1
7->file is already allocated
8->1
9->file is already allocated
10->file is already allocated
11->1
12->1
13->1
14->file is already allocated
15->1
16->1
17->1

Program 8:
AIM:
Write a C Program for MVT first fit.
Source Code:
#include<stdio.h>
#include<conio.h>
40

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


main()
{
int i,j,t1=0,k,n;
int p[10],h[10];
static int t[10];
printf(enter no of blocks);
scanf(%d,&n);
printf(enter block sizes);
for(j=0;j<n;j++)
scanf(%d,&h[j]);
printf(enter no of process);
scanf(%d,&k);
printf(enter each process size);
for(j=0;j<k;j++)
scanf(%d,&p[i]);
for(i=0;i<k;i++)
for(j=0;j<n;j++)
if(p[i]<=h[j])
{
t[i]=h[j]=h[j]-p[i];
break;
}
for(i=0;i<k;i++)
t1+=t[i];
printf(total fragmentation=%d,t1);
getch();
}

Input:
Enter no of blocks3
Enter block size100
200
300
Enter no of process2
Enter process123
243
Output:
Total fragmentation=134

Program 9:
AIM:
Write a C Program for MFT .

41

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


Source Code:
#include<stdio.h>
#include<conio.h>
main( )
{
int a[10],b[10],c[10],i,j,p,s,n,t=0;
printf(enter no of process);
scanf(%d,&n);
printf(enter size of each process);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
printf(enter size of memory);
scanf(%d,&s);
printf(enter no of partition);
scanf(%d,&p);
for(i=0;i<p;i++)
b[i]=s/n;
for(i=0;i<p;i++)
c[i]=b[i]-a[i];
for(i=0;i<p;i++)
t=t+c[i];
printf(\nprocess\tmemorysize\tprocessizefragementation);
for(i=0;i<p;i++)
printf(%d\t%d\t%d\t%d\n,t,b[i],a[i],c[i]);
printf(total fragementation=%d,t);
getch();
}
Enter no of process3
Input:
Enter size of process100
200
300
Enter size of memory900
Enter no of partitions3
output:
Process
memorysize processize
fragmentation
1
300
100
200
2
300
200
100
3
300
300
0
Total fragmentation=300

Program 10:
AIM:
Write a C Program for MVT best fit.

42

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,d,p[10],b[10],i,j,ts;
printf(enter no of blocks);
scanf(%d,&d);
printf(enter size of block);
for(i=0;i<d;i++)
scanf(%d,&b[i]);
printf(\n enter no of process);
scanf(%d,&n);
printf(enter size of each process);
for(i=0;i<n;i++)
scanf(%d,&p[i]);
sort(&b,d);
for(i=1;i<=n;i++)
for(j=0;j<=d;j++)
if(p[i]<=b[j])
{
b[j]=b[j]-p[i];
break;
}
for(i=1;i<=d;i++)
ts=ts+b[i];
printf(total fragmentation=%d,ts);
getch();
}
sort(int b[10],int n)
{
int i,j,t;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}

Input:
enter no of blocks
3

43

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


enter size of block
43
43
21
enter no of process
3
enter size of each process
32
40
20

Output:
total fragmentation 15

44

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 11:
AIM:
Program for bankers algorithm
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int
i,j,a=0,b=0,c=0,f[10],t[10][10],al[10][10],ta[10][10];
int a1[10][10], max[10][10], n[10][10], n1,p,k=0;
printf(\n enter no.of resources);
scanf(%d,n1);
printf(\nenter the max no .of resources for each type);
for(i=0;i<n1;i++)
scanf(%d,&t[b][i]);
printf(\nenter no .of process);
scanf(%d,&p);
printf(\nenter allocation resources);
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<n1;j++)
scanf(%d,&a1[i][j]);
}
for(i=0;i<p;i++)
for(j=0;j<n1;j++)
{
if(a1[i][j]<=t[b][j])
{
t[b][j]+=a1[i][j];
continue;
}
else
printf(\n wrong resourcesallocation);
printf(\n chance of deadlock occurrence after
allocation);
for(j=0;j<n1;j++)
printf(%d,a1[b][j]);
printf(\n enter the max resources for every process);
for(i=0;i<p;i++)

45

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


for(j=0;j<n1;j++);
{
scanf(%d,&max[i][j]);
n[i][j]=max[i][j]-a1[i][j];
}
j=0;
printf(\n needed resources for every process to start
execution);
for(i=0;i<p;i++)
printf(\n%d %d%d,n[i][j],n[i][j+1],n[i][j+2]);
printf(\n safe sequence the sequence of process to
compute
their execution);
for(a=0;a<(p-c);)
for(i=0;i<p;i++)
{
j=0;
b=0;
if(f[i]==0)
{
if(n[i][j]<=a1[b][j]&&n[i][j+1]<=a1[b][j+1]&&
n[i][j+2]<=a1[b][j+2])
{
printf(\n process %d execution started and
completed,i+1);
for(k=0;k<n-1;k++)
a1[b][k]+=a1[i][k];
f[i]=1;
c++;
}
else
f[i]=0;
}
}
getch();
}

46

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Input:

enter no.of resources


3
enter the max no .of resources for each type
10 5 7
enter the no .of process
5
enter allocation of resources
0 1 0 2 0 0 3 0 2 2 1 1 0 0 2

Output:
total available resources after allocation
3 3 2
enter the max resources for every process
7 5 3 3 2 2 9 0 2 2 2 2 4 3 3
needed resources for every process to start execution
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
Safe sequence ,the sequence of process to complete their
execution
Procee 2 execution started
& completed
Procee 4 execution started
& completed
Procee 5 execution started
& completed
Procee 1 execution started
& completed
Procee 3 execution started
& completed

47

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 12:
AIM:
Write a C program for FIFO page allocation algorithm.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[5],n,t,i,c=0,k=0,j=0,m,x;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&t);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<t)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==3)
{
48

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


c++;
b[j]=a[i];
j++;
if(j==t)
j=0;
}
}
printf("no of pagefaults=%d",c);
getch( );
}

Input:
enter no of requests
13
enter no of frames
3
enter requests one by one
2
5
3
2
0
1
3
5
4
2
2
3
4

Output:
no of pagefaults
6

49

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 13:
AIM:
Write a C program for LRU page replacement algorithm.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a[50],b[40],c[15],n,f,i,y,s,p=0,k=0,l=0,m,x,g;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&f);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<f)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
c[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
printf("%d",a[i]);
for(x=0;x<f;x++)
50

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE


printf("%4d",b[x]);
printf("---------");
for(x=0;x<f;x++)
if(b[x]==a[i])
break;
if(x==f)
{
p++;
l=0;
for(g=i-1;g>0;g--)

for(x=0;x<f&&l<=f;x++)
if(a[g]==b[x]&&c[x]==b[x])
{
y=c[x];
c[x]=-1;
l++;
}
if(l==f)
{
for(s=0;s<f;s++)
if(y==b[s])
b[s]=a[i];
}
if(l<f)
for(s=0;s<f;s++)
if(c[s]!=-1)
{
b[s]=a[i];
break;
}
for(s=0;s<f;s++)
c[s]=b[s];
}
for(s=0;s<f;s++)
printf("%4d",b[s]);
printf("\n");
}
printf("\n no of pagefaults=%d",p);
getch();
}

51

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Input:
enter no of requests
13
enter no of frames
3
enter requests one by one
2 5 3 2 0 1 3 5 4 2 2 3 4

Output:
3 2 5
2 2 5
0 2 5
1 2 0
3 2 0
5 2 0
4 2 5
2 2 5
2 2 5
3 2 5
4 2 3
No of

3 -----------3 -----------3 -----------3 -----------1 -----------1 -----------1 -----------4 -----------4 -----------4 -----------4 -----------pagefaults=7

2
2
2
2
2
2
2
2
2
2
2

5
5
0
0
0
5
5
5
5
3
3

3
3
3
1
1
1
4
4
4
4
4

52

You might also like