JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
1
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE NUSTULAPUR, KARIMNAGAR
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
2
LAB MANUAL OF UNIX & OS LAB (IT)
(III B.Tech. II SEM)
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
3
INDEX
SNO PROGRAM NAME PAGE
NO
1 Write a shell script to print the multiplication
table for the given number. 1
2 Write a shell script that copies multiple files
into a directory. 2
3 Write a shell script to find no. of words and
characters in a given file. 3
4 Write a shell script to display all files in a
given directory. 4
5 Write a shell script to perform simple calculator. 5
6 Write a C program to count no.of blanks,characters
a) Using standard i/o function. b) Using system
calls.
6-7
7 Write a C program to illustrate the following
Command using system Calls a) cat b) ls c)
mv
8-10
8 Write a C program to illustrate the stat system
call. 11
9 Write a C program that illustrates the creation of
child process using fork() system call. 12
10 Write a C program that illustrates file locking. 13-14
11 Write a C program that implements producer
consumer problem using semaphore system calls. 15-16
12 Write a C program to illustrate inter process
communication using shared memory system calls. 17-18
13 Write a C program to (a) create message queue
(b) write to message queue (c) read from
message queue
19-20
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
4
O.S. PROGRAMMES
S.NO PROGRAM NAME PAGE
NO
1 Write a C program for ROUND ROBIN CPU scheduling
algorithm 20-21
2 Write a C program for SJF CPU scheduling algorithm 22-23
3 Write a C program for FCFS CPU scheduling
algorithm 24
4 Write a C Program for priority CPU scheduling
algorithm.
25-26
5 Write a C Program for sequential file allocation. 27-28
6 Program for indexed file allocation strategy. 29-30
7 Program for linked file allocation strategy. 31-32
8 Write a C Program for MVT first fit. 33
9 Write a C Program for MFT . 34
10 Write a C Program for MVT best fit. 35-36
11 Program for bankers algorithm 37-39
12 Write a C program for FIFO page allocation
algorithm.
40-41
13 Write a C program for LRU page replacement
algorithm.
42-44
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
5
Program 1:Program 1:Program 1:Program 1: AIM: AIM: AIM: AIM: Write a shell script to print the multiplication table for the Write a shell script to print the multiplication table for the Write a shell script to print the multiplication table for the Write a shell script to print the multiplication table for the given number.given number.given number.given number.
Algorithm:Algorithm:Algorithm: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:Source Code:Source Code: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: Input: Input: Input:
enter the number
6
Output:Output:Output:Output:
6 * 1 = 6
6 * 2 = 12
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
6
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:Program 2:Program 2:Program 2:
AIM: AIM: AIM: AIM: Write a shell script that copies multiple files into a directory.Write a shell script that copies multiple files into a directory.Write a shell script that copies multiple files into a directory.Write a shell script that copies multiple files into a directory.
Algorithm:Algorithm:Algorithm: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:Source Code:Source Code:Source Code: #! /bin/sh
for i in `ls *.c`
do
cp $i /root
done
echo “file are copied into root”
Output:Output:Output:Output: file are copied into root”
Program 3 :Program 3 :Program 3 :Program 3 :
AIM: AIM: AIM: AIM: Write a shell script to find no. of words and characters in a Write a shell script to find no. of words and characters in a Write a shell script to find no. of words and characters in a Write a shell script to find no. of words and characters in a given file.given file.given file.given file. Algorithm:Algorithm:Algorithm:Algorithm:
1. Start.
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
7
2. cc = 0,w=0. 3. give file name as commandline argument (i.e $1). 4. for i in `cat $1`. 5. w = w + 1. 6. c = expr “$i” : “*”. 7. cc = cc + c. 8. Repeat 4,5,6,7 steps until eof. 9. End.
Source Code:Source Code:Source Code: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:Input:Input:Input:. ./a.out sun
Output:Output:Output:Output:
no. of characters 56
no. of words 11
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
8
Program 4:Program 4:Program 4:Program 4: AIM: AIM: AIM: AIM: Write a shell script to display all files in a given directory.Write a shell script to display all files in a given directory.Write a shell script to display all files in a given directory.Write a shell script to display all files in a given directory. Algorithm:Algorithm:Algorithm: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:Source Code:Source Code:Source Code: #! /bin/sh
for i in `ls sss`
do
echo $i
done
Output:Output:Output:Output: abc.txt
ss.c
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
9
Program 5:Program 5:Program 5:Program 5: AIM:AIM:AIM:AIM: Write a shell script to perform simple calculator.Write a shell script to perform simple calculator.Write a shell script to perform simple calculator.Write a shell script to perform simple calculator. Algorithm:Algorithm:Algorithm:Algorithm:
1.Start
2.Read a and b
3.Read op
4.Depending on value of operator perform case
Operation.
5.End
Source Code:Source Code:Source Code:Source Code:
#! /bin/sh
echo ‘enter the value for a’
read a
echo ‘enter the value for b’
read b
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
10
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:Input:Input:Input:
enter the value for a
3
enter the value for b
6
enter the operator
*
Output:Output:Output:Output:
18
Program 6a:Program 6a:Program 6a:Program 6a: AIM: AIM: AIM: AIM: Write a C program to count no. of blanks, characters, lines Write a C program to count no. of blanks, characters, lines Write a C program to count no. of blanks, characters, lines Write a C program to count no. of blanks, characters, lines using standard i/o function.using standard i/o function.using standard i/o function.using standard i/o function. Algorithm:Algorithm:Algorithm:Algorithm:
1. Start. 2. open the file using fopen( ) function in “r” mode 3. ch=fgetc(fp) (to read character by character) 4. if ch = ‘ ‘ or ‘\n’ b=b+1.
5. if ch = ‘\n’ l=l+1.
6. c=c+1. 7. Repeat 3,4,5&6 steps until ch = eof 8. End
Source Code:Source Code:Source Code:Source Code: #include<stdio.h>
int main( )
{
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
11
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:Input:Input:Input: ./a.out sss.txt
Output:Output:Output:Output:
no.of blanks 5
n.of lines 2
no.of characters 36
Program 6b:Program 6b:Program 6b:Program 6b: AIM:AIM:AIM:AIM: Write a C program to count no. of blanks, characters, lines Write a C program to count no. of blanks, characters, lines Write a C program to count no. of blanks, characters, lines Write a C program to count no. of blanks, characters, lines using system calls.using system calls.using system calls.using system calls. Algorithm:Algorithm:Algorithm:Algorithm:
1. Start. 2. open the file using open( ) system call in O_RDONLY 3. rc=read(fd,buf,5) (to read characters ) 4. if ch = ‘ ‘ or ‘\n’ w=w+1.
5. if ch = ‘\n’ l=l+1.
6. c=c+1. 7. Repeat 3,4,5&6 steps until ch = eof 8. End
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
12
Source Code:Source Code:Source Code: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:Input:Input:Input: ./a.out sss.txt
Output:Output:Output:Output: no. of blanks 5
no. of lines 2
no. of characters 36
Program 7a:Program 7a:Program 7a:Program 7a: AIM:AIM:AIM:AIM: Write a C program to illustrate the mv command using Write a C program to illustrate the mv command using Write a C program to illustrate the mv command using Write a C program to illustrate the mv command using system Callssystem Callssystem Callssystem Calls Algorithm:Algorithm:Algorithm: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
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
13
Source Code:Source Code:Source Code: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:Input:Input:Input: hai hello A friend in need is a friend in deed
^z
Output:Output:Output:Output: hai hello A friend in need is a friend in dee
Program 7b :Program 7b :Program 7b :Program 7b : AIM: AIM: AIM: AIM: Write a program to illustrate “ls” command using system callsWrite a program to illustrate “ls” command using system callsWrite a program to illustrate “ls” command using system callsWrite a program to illustrate “ls” command using system calls Algorithm:Algorithm:Algorithm:Algorithm:
1. Start. 2. open directory using opendir( ) system call. 3. read the directory using readdir( ) system call. 4. print dp.name and dp.inode . 5. repeat above step until end of directory. 6. End
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
14
Source Code:Source Code:Source Code: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:output:output:output:
mul.sh 12344
division.c 12345
process.c 12346
Program 7c:Program 7c:Program 7c:Program 7c: AIM:AIM:AIM:AIM: Write a C program to illustrate the mv command using Write a C program to illustrate the mv command using Write a C program to illustrate the mv command using Write a C program to illustrate the mv command using system calls.system calls.system calls.system calls. Algorithm:Algorithm:Algorithm:Algorithm:
1. Start 2. open an existed file and one new open file using open()
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
15
system call
3. read the contents from existed file using read( ) system call
4. write these contents into new file using write system call using write( ) system call
5. repeat above 2 steps until eof 6. close 2 file using fclose( ) system call
7. delete existed file using using unlink( ) system 8. End.
Source Code:Source Code:Source Code: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:Output:Output:Output: file is copied
Program 8:Program 8:Program 8:Program 8: AIM:AIM:AIM:AIM: Write a C program to illustrate the stat system callsWrite a C program to illustrate the stat system callsWrite a C program to illustrate the stat system callsWrite a C program to illustrate the stat system calls Algorithm:Algorithm:Algorithm:Algorithm:
1. Declare s and st of type struct stat. 2. Use stat system call by specifying sss.c file
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
16
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:Source Code:Source Code: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 );
}
OutputOutputOutputOutput:::: 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:Program 9:Program 9:Program 9: AIM: AIM: AIM: AIM: Write a C program thWrite a C program thWrite a C program thWrite a C program that illustrates the creation of child at illustrates the creation of child at illustrates the creation of child at illustrates the creation of child process using fork( ) system callprocess using fork( ) system callprocess using fork( ) system callprocess using fork( ) system call
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
17
Algorithm:Algorithm:Algorithm:Algorithm:
1. Start 2. Declare pid 3. create new process using fork( ) system call 4. If pid!=0 then 5. Display parent process getpid(),getppid(). 6. Else 7. Display child process getpid().getppid(). 8. End
Source code:Source code:Source code: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:Output:Output: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:Program 10:Program 10:Program 10:
AIM:AIM:AIM:AIM: Write a C program that illustrates file locking.Write a C program that illustrates file locking.Write a C program that illustrates file locking.Write a C program that illustrates file locking.
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
18
AlAlAlAlgorithm:gorithm:gorithm: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:Source Code:Source Code: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);
}
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
19
printf(“unlocked\n”);
close(fd);
}
Output:Output:Output:Output:
press<return> to try to get lock
trying to get lock
press <return> to release lock
unlocked
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
20
Program 11:Program 11:Program 11:Program 11: AIM:AIM:AIM:AIM:
Write a C program that implements producer consumerWrite a C program that implements producer consumerWrite a C program that implements producer consumerWrite a C program that implements producer consumer problem using semaphore system callsproblem using semaphore system callsproblem using semaphore system callsproblem using semaphore system calls
Algorithm:Algorithm:Algorithm:Algorithm:
1. Start 2. create semaphore using semget( ) system call 3. if successful it returns positive value 4. create two new processes 5. first process will produce 6. until first process produces second process cannot
consume
7. End.
Source code:Source code:Source code: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);
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
21
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:Output:Output:Output: semaphore set created
semaphore set id ‘327690’
producer: ‘0’
consumer:’0’
producer:’1’
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
22
consumer:’1’
Program 12:Program 12:Program 12:Program 12: AIM: AIM: AIM: AIM: Write a C program to illustrate inter process communication Write a C program to illustrate inter process communication Write a C program to illustrate inter process communication Write a C program to illustrate inter process communication using shared memory system calls.using shared memory system calls.using shared memory system calls.using shared memory system calls. Algorithm:Algorithm:Algorithm:Algorithm:
1. Start 2. create shared memory using shmget( ) system call 3. if success full it returns positive value 4. 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:Source Code:Source Code: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);
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
23
}
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:Input:Input:Input:
#./a.out swarupa
Output:Output:Output:Output:
writing to segment swarupa
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
24
Program 13:Program 13:Program 13:Program 13: AIM: AIM: AIM: AIM:
Write a C program to Write a C program to Write a C program to Write a C program to ( a ) create message queue( a ) create message queue( a ) create message queue( a ) create message queue ( b ) write to message queue( b ) write to message queue( b ) write to message queue( b ) write to message queue ( c ) read from message queue( c ) read from message queue( c ) read from message queue( c ) read from message queue Algorithm:Algorithm:Algorithm: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:Source Code:Source Code: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;
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
25
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:Output:Output:Output:
message queue created 0
message placed on the queue successfully
msgrcv:received message:
mtype: 0
mtext: hello world
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
26
LAB MANUAL OFLAB MANUAL OFLAB MANUAL OFLAB MANUAL OF
OPERATING SYSTEM LABOPERATING SYSTEM LABOPERATING SYSTEM LABOPERATING SYSTEM LAB
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
27
Program 1:Program 1:Program 1:Program 1: AIM: AIM: AIM: AIM: Write a C program for ROUND ROBIN CPU scheduling algorithm.Write a C program for ROUND ROBIN CPU scheduling algorithm.Write a C program for ROUND ROBIN CPU scheduling algorithm.Write a C program for ROUND ROBIN CPU scheduling algorithm. Source Code:Source Code:Source Code: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]);
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
28
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:Input:Input:Input:
enter no of process 3
enter time quantum 2
enter process&service time
1
4
2
6
3
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
29
2
Output:Output:Output:Output: process st wt tt
1 4 4 8
2 6 6 12
3 2 4 6
Awt = 4.000000
att = 8.000000
Program 2:Program 2:Program 2:Program 2: AIM:AIM:AIM:AIM: Write a C program for SJF CPU scheduling algorithmWrite a C program for SJF CPU scheduling algorithmWrite a C program for SJF CPU scheduling algorithmWrite a C program for SJF CPU scheduling algorithm Source Code:Source Code:Source Code: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++)
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
30
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:Input:Input:Input: Enter no of jobs
4
Enter burst time
5
12
8
20
Output:Output:Output:Output: Bt wt tt
5 0 5
12 5 13
8 13 25
20 25 45
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
31
aw=10.75000
at=22.000000
Program 3:Program 3:Program 3:Program 3:
AIM:AIM:AIM:AIM: Write a C program for FCFS CPU scheduling algorithmWrite a C program for FCFS CPU scheduling algorithmWrite a C program for FCFS CPU scheduling algorithmWrite a C program for FCFS CPU scheduling algorithm
Source Code:Source Code:Source Code: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]);
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
32
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:Input:Input:Input: enter no of jobs
3
enter bursttime
12
8
20
output:output:output:output:
bt wt tt
12 0 12
8 12 20
20 20 40
aw=10.666670
at=24.00000
Program 4:Program 4:Program 4:Program 4:
AIM:AIM:AIM:AIM: Write a C PrograWrite a C PrograWrite a C PrograWrite a C Program for priority CPU scheduling algorithm.m for priority CPU scheduling algorithm.m for priority CPU scheduling algorithm.m for priority CPU scheduling algorithm. Source Code:Source Code:Source Code: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”);
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
33
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:Input:Input:Input: Enter no of jobs
4
Enter bursttime
10
2
4
7
Enter priority values
4
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
34
2
1
3
Output:Output:Output:Output: Bt priority wt tt
4 1 0 4
2 2 4 6
7 3 6 13
10 4 13 23
aw=5.750000
at=12.500000
Program 5:Program 5:Program 5:Program 5:
AIM:AIM:AIM:AIM: Write a C Program for sequential file allocation.Write a C Program for sequential file allocation.Write a C Program for sequential file allocation.Write a C Program for sequential file allocation. Source Code:Source Code:Source Code:Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
35
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:Input:Input:Input:
enter starting block & length of files
4
5
OutputOutputOutputOutput:
file not allocated (y-1/n-0)
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
36
4 1
5 1
6 1
7 1
8 1
file is allocated to disk
if u want to enter more files(y-1/n-0)
0
Program 6:Program 6:Program 6:Program 6:
AIM:AIM:AIM:AIM: Write a C Program for indexed file allocation strategy.Write a C Program for indexed file allocation strategy.Write a C Program for indexed file allocation strategy.Write a C Program for indexed file allocation strategy.
Source Code:Source Code:Source Code:Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],i,k,j,index[50],n,c,count=0;
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
37
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:Input:Input:Input: enter index block
3
enter no of files on index
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
38
4
5
6
7
8
Output:Output:Output: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:Program 7:Program 7:Program 7: AIM:AIM:AIM:AIM: Write a C Program for linked file allocation strategy.Write a C Program for linked file allocation strategy.Write a C Program for linked file allocation strategy.Write a C Program for linked file allocation strategy.
Source Code:Source Code:Source Code:Source Code: #include<stdio.h>
#include<conio.h>
main()
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
39
{
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:Input:Input:Input: enter how many blocks already allocated
5
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
40
enter block nos
3
7
9
10
14
enter index starting block & length
4 10
Output:Output:Output: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:Program 8:Program 8:Program 8: AIM:AIM:AIM:AIM: Write a C Program for MVT first fit.Write a C Program for MVT first fit.Write a C Program for MVT first fit.Write a C Program for MVT first fit.
Source Code:Source Code:Source Code:Source Code: #include<stdio.h>
#include<conio.h>
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
41
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();
}
InputInputInputInput:
Enter no of blocks3
Enter block size100
200
300
Enter no of process2
Enter process123
243
OutputOutputOutputOutput:
Total fragmentation=134
Program 9:Program 9:Program 9:Program 9:
AIM:AIM:AIM:AIM: Write a C Program for MFT .Write a C Program for MFT .Write a C Program for MFT .Write a C Program for MFT .
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
42
Source Code:Source Code:Source Code: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();
}
InputInputInputInput: Enter no of process3
Enter size of process100
200
300
Enter size of memory900
Enter no of partitions3
outputoutputoutputoutput:
Process memorysize processize fragmentation
1 300 100 200
2 300 200 100
3 300 300 0
Total fragmentation=300
Program 10:Program 10:Program 10:Program 10:
AIM:AIM:AIM:AIM: Write a C Program for MVT best fit.Write a C Program for MVT best fit.Write a C Program for MVT best fit.Write a C Program for MVT best fit.
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
43
Source Code:Source Code:Source Code: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:Input:Input:Input: enter no of blocks
3
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
44
enter size of block
43
43
21
enter no of process
3
enter size of each process
32
40
20
Output:Output:Output:Output:
total fragmentation 15
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
45
Program 11:Program 11:Program 11:Program 11:
AIM:AIM:AIM:AIM: Program for bankers algorithmProgram for bankers algorithmProgram for bankers algorithmProgram for bankers algorithm
Source Code:Source Code:Source Code: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++)
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
46
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();
}
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
47
Input:Input:Input: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:Output:Output: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
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
48
Program 12:Program 12:Program 12:Program 12: AIM:AIM:AIM:AIM:
Write a C program for FIFO page allocation algorithm.Write a C program for FIFO page allocation algorithm.Write a C program for FIFO page allocation algorithm.Write a C program for FIFO page allocation algorithm.
Source Code:Source Code:Source Code: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)
{
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
49
c++;
b[j]=a[i];
j++;
if(j==t)
j=0;
}
}
printf("no of pagefaults=%d",c);
getch( );
}
Input:Input:Input: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:Output:Output:Output: no of pagefaults
6
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
50
Program 13:Program 13:Program 13:Program 13: AIM:AIM:AIM:AIM: Write a C program for LRU page replacement algorithm.Write a C program for LRU page replacement algorithm.Write a C program for LRU page replacement algorithm.Write a C program for LRU page replacement algorithm. Source Code:Source Code:Source Code: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++)
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
51
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();
}
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
52
Input:Input:Input: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:Output:Output:Output: 3 2 5 3 ------------ 2 5 3
2 2 5 3 ------------ 2 5 3
0 2 5 3 ------------ 2 0 3
1 2 0 3 ------------ 2 0 1
3 2 0 1 ------------ 2 0 1
5 2 0 1 ------------ 2 5 1
4 2 5 1 ------------ 2 5 4
2 2 5 4 ------------ 2 5 4
2 2 5 4 ------------ 2 5 4
3 2 5 4 ------------ 2 3 4
4 2 3 4 ------------ 2 3 4
No of pagefaults=7