0% found this document useful (0 votes)
285 views

LAB MANUAL - OS - 2021 Regulation Final-1

Uploaded by

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

LAB MANUAL - OS - 2021 Regulation Final-1

Uploaded by

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

1

General commands

Command Function
Date Used to display the current system date and time.
date +%D Displays date only
date +%T Displays time only
date +%Y Displays the year part of date
date +%H Displays the hour part of time
Cal Calendar of the current month
calyear Displays calendar for all months of the specified year
calmonth year Displays calendar for the specified month of the year
Who Login details of all users such as their IP, Terminal No, User name,
who am i Used to display the login details of the user
Uname Displays the Operating System
uname –r Shows version number of the OS (kernel).
uname –n Displays domain name of the server
echo$HOME Displays the user's home directory
Bc Basic calculator. Press Ctrl+dto quit
lp file Allows the user to spool a job along with others in a print queue.
mancmdname Manual for the given command. Press qto exit
history To display the commands used by the user since log on.
exit Exit from a process. If shell is the only process then logs out

Directory commands

Command Function
Pwd Path of the present working directory
mkdirdir A directory is created in the given name under the current directory
mkdirdir1 dir2 A number of sub-directories can be created under one stroke
cdsubdir Change Directory. If the subdir starts with / then path starts from
root (absolute) otherwise from current working directory.
cd To switch to the home directory.
cd / To switch to the root directory.
cd .. To move back to the parent directory
rmdirsubdir Removes an empty sub-directory.
2

File commands

Command Function
cat >filename To create a file with some contents. To end typing press Ctrl+d. The >
symbol means redirecting output to a file. (< for input)
catfilename Displays the file contents.
cat>>filename Used to append contents to a file
cpsrc des Copy files to given location. If already exists, it will be overwritten
cp –isrc des Warns the user prior to overwriting the destination file
cp –r src des Copies the entire directory, all its sub-directories and files.
mv old new To rename an existing file or directory. –i option can also be used
mv f1 f2 f3 dir To move a group of files to a directory.
mv –v old new Display name of each file as it is moved.
rm file Used to delete a file or group of files. –i option can also be used
rm * To delete all the files in the directory.
rm –r * Deletes all files and sub-directories
rm –f * To forcibly remove even write-protected files
Ls Lists all files and subdirectories (blue colored) in sorted manner.
lsname To check whether a file or directory exists.
lsname* Short-hand notation to list out filenames of a specific pattern.
ls –a Lists all files including hidden files (files beginning with .)
ls –xdirname To have specific listing of a directory.
ls –R Recursive listing of all files in the subdirectories
ls –l Long listing showing file access rights (read/write/execute-rwx for
user/group/others-ugo).
cmp file1 file2 Used to compare two files. Displays nothing if files are identical.
wc file It produces a statistics of lines (l), words(w), and characters(c).
chmod perm file Changes permission for the specified file. (r=4, w=2, x=1)
chmod 740 file sets all rights for user, read only for groups and no rights
for others

The commands can be combined using the pipeline (|) operator. For example, number of users
logged in can be obtained as.

who | wc -l

Finally to terminate the unix session execute the command exit or logout.

Output

$ date
Sat Apr 9 13:03:47 IST 2011

$ date +%D
04/09/11

$ date +%T
13:05:33
3

$ date +%Y
2011

$ date +%H
13

$ cal 08 1998
August 1998
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
30 31

$ who
root :0 Apr 9 08:41
vijai pts/0 Apr 9 13:00 (scl-64)
cse4001 pts/3 Apr 9 13:18 (scl-41.smkfomra.com)

$ uname
Linux

$ uname -r
2.4.20-8smp

$ uname -n
localhost.localdomain

$ echo $HOME
/home/vijai

$ echo $USER
vijai

$ bc
3+5
8

$ pwd
/home/vijai/shellscripts/loops

$ mkdir filter
$ ls
filter list.sh regexpr shellscripts

$ cd shellscripts/loops/
4

$ cd
$

$ cd /
[vijai@localhost /]$

[vijai@localhost /]$ cd /home/vijai/shellscripts/loops/


$ cd ..
[vijai@localhost shellscripts]$

$ rmdir filter
$ ls
list.sh regexpr shellscripts

$ cat > greet


hi cse
wishing u the best

$ cat greet
hi ece-a
wishing u the best

$ cat >> greet


bye
$ cat greet
hi cse
wishing u the best bye

$ ls
greet list.sh regexpr shellscripts

$ ls -a
. .bash_logout .canna .gtkrc regexpr .viminfo.tmp
.. .bash_profile .emacs .kde shellscripts .xemacs
.bash_history .bashrc greet list.sh .viminfo

$ ls -l
-rw-rw-r-- 1 vijai vijai 32 Apr 11 14:52 greet
-rw-rw-r-- 1 vijai vijai 30 Apr 4 13:58 list.sh
drwxrwxr-x 2 vijai vijai 4096 Apr 9 14:30 regexpr

$ cp greet ./regexpr/
$ ls
greet list.sh regexpr shellscripts
$ ls ./regexpr
5

demo greet

$ cp -i greet ./regexpr/
cp: overwrite 'greet'? n

$ mv greet greet.txt
$ ls
greet.txt list.sh regexpr shellscripts

$ mv greet.txt ./regexpr/
$ ls
list.sh regexpr shellscripts

$ rm -i *.sh
rm: remove regular file 'fact.sh'? y rm: remove regular
file 'prime.sh'?y
$ ls
list.sh regexpr shellscripts

$ wc list.sh
4 9 30 list.sh
$ wc -l list.sh
4 list.sh

$ cmp list.sh fact.sh


list.sh fact.sh differ: byte 1, line 1

$ ls -l list.sh
-rw-rw-r-- 1 vijai vijai 30 Apr 4 13:58 list.sh

$ chmod ug+x list.sh

$ ls -l list.sh
-rwxrwxr-- 1 vijai vijai 30 Apr 4 13:58 list.sh
$ chmod 740 list.sh
$ ls -l list.sh
-rwxr----- 1 vijai vijai 30 Apr 4 13:58 list.sh
6

A) Swapping values of two variables


# Swapping values – swap.sh
echo –n "Enter value for A : "
read a
echo –n "Enter value for B : "
read b
t=$a
a=$b
b=$t
echo "Values after Swapping"
echo "A Value is $a and B Value is $b"

Output
$ sh swap.sh
Enter value for A : 12
Enter value for B : 23
Values after Swapping
A Value is 23 and B Value is 12

B) Farenheit to Centigrade Conversion


# Degree conversion – degconv.sh
echo -n "Enter Fahrenheit : "
read f
c=`expr \( $f - 32 \) \* 5 / 9` echo
"Centigrade is : $c"

Output
$ sh degconv.sh
Enter Fahrenheit : 213
Centigrade is : 100

C) Biggest of 3 numbers
# Biggest – big3.sh
echo -n "Give value for A B and C: "
read a b c
if [ $a -gt $b -a $a -gt $c ] then
echo "A is the Biggest number"
elif [ $b -gt $c ]
then
echo "B is the Biggest number"
else
echo "C is the Biggest number"
fi

Output
$ sh big3.sh
Give value for A B and C: 4 3 7
C is the Biggest number
7

D) Grade Determination
# Grade – grade.sh
echo -n "Enter the mark : "
read mark
if [ $mark -gt 90 ]
then
echo "S Grade" elif [ $mark -gt 80 ]
then
echo "A Grade" elif [ $mark -gt 70 ]
then
echo "B Grade" elif [ $mark -gt 60 ]
then
echo "C Grade" elif [ $mark -gt 55 ]
then
echo "D Grade" elif [ $mark -ge 50 ]
then
echo "E Grade" else
echo "U Grade"
fi

Output
$ sh grade.sh Enter the mark :
65 C Grade

E) Vowel or Consonant

# Vowel - vowel.sh
echo -n "Key in a lower case character : "
read choice
case $choice in a|e|i|o|u)
echo "It's a Vowel";;
*) echo "It's a Consonant"
esac

Output
$ sh vowel.
Key in a lower case character : e It's a Vowel
8

F) Simple Calculator

# Arithmetic operations — calc.sh


echo -n "Enter the two numbers : "
read a b
echo " 1. Addition"
echo " 2. Subtraction"
echo " 3. Multiplication"
echo " 4. Division"
echo -n "Enter the option : "
read option
case $option in
1) c=`expr $a + $b` echo "$a + $b =
$c";;
2) c=`expr $a - $b` echo "$a - $b =
$c";;
3) c=`expr $a \* $b` echo "$a * $b =
$c";;
4) c=`expr $a / $b` echo "$a / $b =
$c";;
*) echo "Invalid Option" esac

Output
$ sh calc.sh
Enter the two numbers : 2 4
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter the option : 1 2 + 4 = 6

G) Multiplication Table
# Multiplication table – multable.sh clear
echo -n "Which multiplication table? : "
read n
for x in 1 2 3 4 5 6 7 8 9 10 do
p=`expr $x \* $n`
echo -n "$n X $x = $p" sleep 1
done

Output
$ sh multable.sh
Which multiplication table? : 6 6 X 1 = 6
6 X 2 = 12
.....
9

H) Number Reverse
# To reverse a number – reverse.sh
echo -n "Enter a number : "
read n rd=0
while [ $n -gt 0 ] do
rem=`expr $n % 10` rd=`expr $rd \* 10 +
$rem` n=`expr $n / 10`
done
echo "Reversed number is $rd"

Output
$ sh reverse.sh
Enter a number : 234 Reversed
number is 432

I) Prime Number
# Prime number – prime.sh echo -n "Enter the
number : " read n
i=2
m=`expr $n / 2` until [ $i -gt $m
] do
q=`expr $n % $i` if [ $q -
eq 0 ] then
echo "Not a Prime number" exit
fi
i=`expr $i + 1` done
echo "Prime number"

Output
$ sh prime.sh
Enter the number : 17 Prime number
10

Program

/* Process creation - fork.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

main()
{
pid_t pid; int x
= 5; pid = fork();
x++;

if (pid < 0)
{
printf("Process creation error"); exit(-1);
}
else if (pid == 0)
{
printf("Child process:"); printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:"); printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}
}

Output

$ gcc fork.c

$ ./a.out Child process:


Process id is 19499 Value of x
is 6
Process id of parent is 19498

Parent process: Process id is


19498 Value of x is 6
Process id of shell is 3266
11

Program

/* Wait for child termination - wait.c */ #include <stdio.h>


#include <stdlib.h> #include
<unistd.h> #include <sys/types.h>
#include <sys/wait.h>

main()
{
int i, status; pid_t pid;

pid = fork();

if (pid < 0)
{
printf("\nProcess creation failure\n"); exit(-1);
}
else if(pid > 0)
{
wait(NULL);
printf ("\nParent starts\nEven Nos: "); for (i=2;i<=10;i+=2)
printf ("%3d",i);
printf ("\nParent ends\n");
}
else if (pid == 0)
{
printf ("Child starts\nOdd Nos: "); for (i=1;i<10;i+=2)
printf ("%3d",i); printf ("\nChild
ends\n");
}
}

Output

$ gcc wait.c

$ ./a.out Child starts


Odd Nos: 1 3 5 7 9

Child ends

Parent starts
Even Nos: 2 4 6
Parent ends
12

Program

/* Load a program in child process - exec.c */ #include <stdio.h>


#include <stdlib.h> #include
<unistd.h> #include <sys/types.h>

main()
{
pid_t pid;

switch(pid = fork())
{

case -1:
perror("Fork failed"); exit(-1);

case 0:
printf("Child process\n"); execl("/bin/date",
"date", 0); exit(0);

default:
wait(NULL);
printf("Child Terminated\n"); exit(0);
}
}

Output

$ gcc exec.c

$ ./a.out Child
process
Sat Feb 23 17:46:59 IST 2013
Child Terminated
13

Program

/* File status - stat.c */ #include <stdio.h>


#include <sys/stat.h> #include <stdlib.h>
#include <time.h>

int main(int argc, char*argv[])


{
struct stat file; int n;
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n"); exit(-1);
}
if ((n = stat(argv[1], &file)) == -1)
{
perror(argv[1]);
exit(-1);
}

printf("User id : %d\n", file.st_uid); printf("Group id : %d\n", file.st_gid);


printf("Block size : %d\n", file.st_blksize); printf("Blocks allocated : %d\n",
file.st_blocks); printf("Inode no. : %d\n", file.st_ino);
printf("Last accessed : %s", ctime(&(file.st_atime))); printf("Last modified : %s",
ctime(&(file.st_mtime))); printf("File size : %d bytes\n", file.st_size); printf("No. of links :
%d\n", file.st_nlink);

printf("Permissions : ");
printf( (S_ISDIR(file.st_mode)) ? "d" : "-");
printf( (file.st_mode & S_IRUSR) ? "r" : "-");
printf( (file.st_mode & S_IWUSR) ? "w" : "-");
printf( (file.st_mode & S_IXUSR) ? "x" : "-");
printf( (file.st_mode & S_IRGRP) ? "r" : "-");
printf( (file.st_mode & S_IWGRP) ? "w" : "-");
printf( (file.st_mode & S_IXGRP) ? "x" : "-");
printf( (file.st_mode & S_IROTH) ? "r" : "-");
printf( (file.st_mode & S_IWOTH) ? "w" : "-");
printf( (file.st_mode & S_IXOTH) ? "x" : "-"); printf("\n");

if(file.st_mode & S_IFREG) printf("File type :


Regular\n");
if(file.st_mode & S_IFDIR) printf("File type : Directory\n");
}

Output

$ gcc stat.c

$ ./a.out fork.c User id : 0


Group id : 0 Block size :
4096
Blocks allocated : 8 Inode no. :
16627
Last accessed : Fri Feb 22 21:57:09 2013
Last modified : Fri Feb 22 21:56:13 2013 File size : 591 bytes
No. of links : 1 Permissions : -rw-r--r--
File type : Regular
14

Program

/* Directory content listing - dirlist.c */

#include <stdio.h> #include


<dirent.h> #include <stdlib.h>

main(int argc, char *argv[])


{
struct dirent *dptr; DIR *dname;

if (argc != 2)
{
printf("Usage: ./a.out <dirname>\n"); exit(-1);
}

if((dname = opendir(argv[1])) == NULL)


{
perror(argv[1]);
exit(-1);

while(dptr=readdir(dname)) printf("%s\n", dptr->d_name);

closedir(dname);
}

Output

$ gcc dirlist.c

$ ./a.out vijai wait.c


a.out
..
stat.c dirlist.c fork.c
.
exec.c
15

Program

/* File creation - fcreate.c */ #include <stdio.h>


#include <stdlib.h> #include
<string.h> #include <fcntl.h>

main(int argc, char *argv[])


{
int fd, n, len; char
buf[100];

if (argc != 2)
{
printf("Usage: ./a.out <filename>\n"); exit(-1);
}

fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644); if(fd < 0)


{
printf("File creation problem\n"); exit(-1);
}

printf("Press Ctrl+D at end in a new line:\n"); while((n = read(0, buf, sizeof(buf)))


> 0)
{
len = strlen(buf); write(fd, buf,
len);
}
close(fd);
}

Output

$ gcc fcreate.c

$ ./a.out hello File I/O


Open system call is used to either open or create a file. creat system call is used to create a file. It is
seldom used.
^D
16

Program

/* File Read - fread.c */

#include <stdio.h> #include


<stdlib.h> #include <fcntl.h>

main(int argc, char *argv[])


{
int fd,i;
char buf[100]; if (argc
< 2)
{
printf("Usage: ./a.out <filename>\n"); exit(-1);
}

fd = open(argv[1], O_RDONLY); if(fd == -1)


{
printf("%s file does not exist\n", argv[1]); exit(-1);

printf("Contents of the file %s is : \n", argv[1]); while(read(fd, buf, sizeof(buf)) > 0)


printf("%s", buf);

close(fd);
}

Output

$ gcc fread.c

$ ./a.out hello File I/O


open system call is used to either open or create a file. creat system call is used to create a file. It is
seldom used.
17

1. Program

/* File append - fappend.c */ #include <stdio.h>


#include <string.h> #include
<stdlib.h> #include <fcntl.h>

main(int argc, char *argv[])


{
int fd, n, len; char
buf[100];

if (argc != 2)
{
printf("Usage: ./a.out <filename>\n"); exit(-1);
}

fd = open(argv[1], O_APPEND|O_WRONLY|O_CREAT, 0644); if (fd < 0)


{
perror(argv[1]);
exit(-1);
}

while((n = read(0, buf, sizeof(buf))) > 0)


{
len = strlen(buf); write(fd, buf,
len);
}

close(fd);
}

Output

$ gcc fappend.c

$ ./a.out hello
read system call is used to read from file or console write system call is used to write to
file.
^D
18

Program

/* FCFS Scheduling - fcfs.c */#include

#include<stdio.h>

struct process

{
int pid; int
btime; int
wtime; int ttime;
} p[10];

main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;

printf("\nFCFS Scheduling\n\n");
for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\nP%d\t%4d\t%3d\t%2d", p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");

printf("\n\nAverage waiting time: %5.2fms", awat);


printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n"); printf("-");


19

for(i=0; i<(p[n-1].ttime + 2*n); i++)

printf("-");
printf("\n");
printf("|"); for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("0"); for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}
Output

Enter no. of process : 4


Burst time for process P1 (in ms) : 10 Burst time for process P2
(in ms) : 4 Burst time for process P3 (in ms) : 11 Burst time for
process P4 (in ms) : 6

FCFS Scheduling

Process B-Time T-Time W-Time

P1 10 10 0
P2 4 14 10
P3 11 25 14
P4 6 31 25

Average waiting time : 12.25ms


Average turn around time : 20.00ms

GANTT Chart

| P1 | P2 | P3 | P4 |

0 10 14 25 31
20

Program

/* SJF Scheduling – sjf.c */

#include <stdio.h>

struct process
{
int pid; int
btime; int
wtime; int ttime;
} p[10], temp;

main()
{
int i,j,k,n,ttur,twat; float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}

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


{
for(j=i+1; j<n; j++)
{
if((p[i].btime > p[j].btime) ||
(p[i].btime == p[j].btime && p[i].pid > p[j].pid))
{
temp = p[i]; p[i] =
p[j]; p[j] = temp;
}

}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime; p[i].ttime = p[i].wtime +
p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime; twat +=
p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;

printf("\nSJF Scheduling\n\n");
for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
21

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


printf("-"); for(i=0; i<n; i++)
printf("\nP%-4d\t%4d\t%3d\t%2d", p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n"); for(i=0; i<28; i++)
printf("-");
printf("\n\nAverage waiting time: %5.2fms", awat);
printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|"); for(i=0; i<n; i++)
{
k = p[i].btime/2; for(j=0; j<k;
j++)
printf(" "); printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0"); for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}

Output
Enter no. of process : 5
Burst time for process P1 (in ms) : 10
Burst time for process P2 (in ms) : 6
Burst time for process P3 (in ms) : 5
Burst time for process P4 (in ms) : 6
Burst time for process P5 (in ms) : 9

SJF Scheduling

Process B-Time T-Time W-Time

P3 5 5 0
P2 6 11 5
P4 6 17 11
P5 9 26 17
P1 10 36 26

Average waiting time : 11.80ms


22

Average turn around time : 19.00ms

GANTT Chart

| P3 | P2 | P4 | P5 | P1 |

0 5 11 17 26 36
23

Program

/* Priority Scheduling- pri.c */

#include <stdio.h>

struct process
{
int pid; int
btime; int pri;
int wtime; int
ttime;
} p[10], temp;

main()
{
int i,j,k,n,ttur,twat; float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ", (i+1));
scanf("%d", &p[i].btime);
printf("Priority for process P%d : ", (i+1));
scanf("%d", &p[i].pri);
p[i].pid = i+1;
}

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


{
for(j=i+1; j<n; j++)
{
if((p[i].pri > p[j].pri) ||
(p[i].pri == p[j].pri && p[i].pid > p[j].pid) )
{
temp = p[i]; p[i] =
p[j]; p[j] = temp;
}

}
}
p[0].wtime = 0; for(i=0; i<n;
i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime; p[i].ttime = p[i].wtime +
p[i].btime;
}
ttur = twat = 0; for(i=0; i<n;
i++)
{
ttur += p[i].ttime; twat +=
p[i].wtime;
}
awat = (float)twat / n; atur = (float)ttur
/ n;

printf("\n\t Priority Scheduling\n\n"); for(i=0; i<38; i++)


24

printf("-");
printf("\nProcess B-Time Priority T-Time W-Time\n");
for(i=0; i<38; i++)
printf("-"); for (i=0; i<n;
i++)
printf("\nP%-4d\t%4d\t%3d\t%4d\t%4d", p[i].pid,p[i].btime,p[i].pri,p[i].ttime,p[i].wtime);
printf("\n"); for(i=0; i<38; i++)
printf("-");

printf("\n\nAverage waiting time: %5.2fms", awat); printf("\nAverage turn around time :


%5.2fms\n", atur);

printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|"); for(i=0; i<n; i++)
{
k = p[i].btime/2; for(j=0; j<k;
j++)
printf(" "); printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0"); for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++) printf(" ");
printf("%2d",p[i].ttime);
}
}
25

Output

Enter no. of process : 5


Burst time for process P1 (in ms) : 10
Priority for process P1 : 3
Burst time for process P2 (in ms) : 7
Priority for process P2 : 1
Burst time for process P3 (in ms) : 6
Priority for process P3 : 3
Burst time for process P4 (in ms) : 13
Priority for process P4 : 4
Burst time for process P5 (in ms) : 5
Priority for process P5 : 2

Priority Scheduling

Process B-Time Priority T-Time W-Time

P2 7 1 7 0
P5 5 2 12 7
P1 10 3 22 12
P3 6 3 28 22
P4 13 4 41 28

Average waiting time : 13.80ms


Average turn around time : 22.00ms

GANTT Chart

| P2 | P5 | P1 | P3 | P4 |

0 7 12 22 28 41
26

Program

/* Round robin scheduling- rr.c */

#include <stdio.h>

main()
{
int i,x=-1,k[10],m=0,n,t,s=0;
int a[50],temp,b[50],p[10],bur[10],bur1[10]; int
wat[10],tur[10],ttur=0,twat=0,j=0; float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d : ", (i+1));
scanf("%d", &bur[i]);
bur1[i] = bur[i];
}
printf("Enter the time slice (in ms) : ");
scanf("%d", &t);

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


{
b[i] = bur[i] / t;
if((bur[i]%t) != 0)
b[i] += 1;
m += b[i];
}

printf("\n\t\tRound Robin Scheduling\n");

printf("\nGANTT Chart\n"); for(i=0; i<m; i++)


printf(" ");
printf("\n");

a[0] = 0;
while(j < m)
{
if(x == n-1) x = 0;
else
x++;
if(bur[x] >= t)
{
bur[x] -= t;
a[j+1] = a[j] + t;
27

if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
s++;
}
j++;
b[x] -= 1;
printf("P%d |", x+1);
}
else if(bur[x] != 0)
{
a[j+1] = a[j] + bur[x];
bur[x] = 0;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
s++;
}
j++;
b[x] -= 1;
printf("P%d |",x+1);
}
}

printf("\n");
for(i=0;i<m;i++)
printf(" ");
printf("\n");

for(j=0; j<=m; j++)


printf("%d\t", a[j]);

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


{
for(j=i+1; j<n; j++)
{
if(p[i] > p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;

temp = k[i];
k[i] = k[j];
k[j] = temp;
}
}
}
28

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


{
wat[i] = k[i] - bur1[i]; tur[i] = k[i];
}
for(i=0; i<n; i++)
{
ttur += tur[i];
twat += wat[i];
}

printf("\n\n");
for(i=0; i<30; i++)
printf("-");
printf("\nProcess\tBurst\tTrnd\tWait\n");
for(i=0; i<30; i++)
printf("-");
for (i=0; i<n; i++)
printf("\nP%-4d\t%4d\t%4d\t%4d", p[i]+1, bur1[i], tur[i],wat[i]);
printf("\n");
for(i=0; i<30; i++)
printf("-");

awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nAverage waiting time: %.2f ms", awat);
printf("\nAverage turn around time : %.2f ms\n", atur);
}
29

Output

Enter no. of process : 5


Burst time for process P1 : 10 Burst time for process P2
: 29 Burst time for process P3 : 3 Burst time for process
P4 : 7 Burst time for process P5 : 12 Enter the time
slice (in ms) : 10

Round Robin Scheduling

GANTT Chart

P1 | P2 | P3 | P4 | P5 | P2 | P5 | P2 |

0 10 20 23 30 40 50 52 61

Process Burst Trnd Wait

P1 10 10 0
P2 29 61 32
P3 3 23 20
P4 7 30 23
P5 12 52 40

Average waiting time : 23.00 ms


Average turn around time : 35.20 ms
30

Program :

#include<stdio.h>
main()
{
int p[2],pid,pid1;
char msg[25],msg1[25];
pipe(p);
pid=fork();
if(pid!=0)
{
sleep(2);
read(p[0],msg1,21);
printf(“%s”,msg1);
}
else
{
pid1=fork();
if(pid1!=0);
{
sleep(1);
read(p[0],msg1,21);
write(p[1],”Grand child says hello”,21);
}
else
write(p[1],”Says hello to grandpa”,29);
}
}
Sample Output;
Says hello to grandpaX@
31

/*parent_child.c */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(void)
{
int shmid;
char *shmPtr;
int n;
if (fork( ) == 0)
{
sleep(5); /* UUPS */
if( (shmid = shmget(2041, 32, 0)) == -1 )
{
exit(1);
}
shmPtr = shmat(shmid, 0, 0);
if (shmPtr == (char *) -1)
exit(2);
printf ("\nChild Reading .... \n\n");
for (n = 0; n < 26; n++)
putchar(shmPtr[n]);
putchar('\n'); }
else
{
if( (shmid = shmget(2041, 32, 0666 | IPC_CREAT)) == -1 )
{
exit(1);
}
shmPtr = shmat(shmid, 0, 0);
if (shmPtr == (char *) -1)
exit(2);
for (n = 0; n < 26; n++)
shmPtr[n] = 'a' + n;
printf ("Parent Writing .....\n\n") ;
for (n = 0; n < 26; n++)
putchar(shmPtr[n]);
putchar('\n'); wait(NULL);
shmdt(NULL);
if( shmctl(shmid, IPC_RMID, NULL) == -1 )
{
perror("shmctl");
32

exit(-1);
}
}
exit(0);
}

Sample Output:
Parent Writing ....
abcdefghijklmnopqrstuvwxyz

Parent Reading ....


abcdefghijklmnopqrstuvwxyz
33

Program

/* C program to demonstrate working of Semaphores */

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

sem_t mutex;

void* thread(void* arg)


{
//wait sem_wait(&mutex);
printf("\nEntered..\n");

//critical section sleep(4);


//signal
printf("\nJust Exiting...\n");
sem_post(&mutex);
}

int main()
{
sem_init(&mutex, 0, 1); pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL); sleep(2);
pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL); pthread_join(t2,NULL);
sem_destroy(&mutex);

return 0;
}

Output

$ gcc sem.c -lpthread

$ ./a.out Entered..
Just Exiting... Entered..
Just Exiting...
34

PROGRAM

#include <stdio.h> #include


<stdio.h>

main()
{
int r[1][10], av[1][10];
int all[10][10], max[10][10], ne[10][10], w[10],safe[10]; int i=0, j=0, k=0, l=0, np=0, nr=0,
count=0, cnt=0;

clrscr();
printf("enter the number of processes in a system");
scanf("%d", &np);
printf("enter the number of resources in a system");
scanf("%d",&nr);
for(i=1; i<=nr; i++)
{
printf("Enter no. of instances of resource R%d " ,i);
scanf("%d", &r[0][i]);
av[0][i] = r[0][i];
}

for(i=1; i<=np; i++) for(j=1; j<=nr;


j++)
all[i][j] = ne[i][j] = max[i][j] = w[i]=0;
printf("Enter the allocation matrix"); for(i=1; i<=np; i++)
{
for(j=1; j<=nr; j++)
{
scanf("%d", &all[i][j]);
av[0][j] = av[0][j] - all[i][j];
}
}

printf("Enter the maximum matrix"); for(i=1; i<=np; i++)


35

{
for(j=1; j<=nr; j++)
{
scanf("%d",&max[i][j]);
}
}

for(i=1; i<=np; i++)


{
for(j=1; j<=nr; j++)
{
ne[i][j] = max[i][j] - all[i][j];
}
}

for(i=1; i<=np; i++)


{
printf("pocess P%d", i); for(j=1; j<=nr; j++)
{
printf("\n allocated %d\t",all[i][j]); printf("maximum %d\t",max[i][j]);
printf("need %d\t",ne[i][j]);
}
printf("\n \n");
}

printf("\nAvailability "); for(i=1; i<=nr; i++)


printf("R%d %d\t", i, av[0][i]); printf("\n ");
printf("\n safe sequence");
for(count=1; count<=np; count++)
{
for(i=1; i<=np; i++)
{
Cnt = 0;
for(j=1; j<=nr; j++)
{
if(ne[i][j] <= av[0][j] && w[i]==0) cnt++;
}
if(cnt == nr)
{
k++;
safe[k] = i; for(l=1; l<=nr; l++)
av[0][l] = av[0][l] + all[i][l]; printf("\n P%d
",safe[k]); printf("\t Availability "); for(l=1; l<=nr; l++)
printf("R%d %d\t", l, av[0][l]); w[i]=1;
}
}
}
getch();
}
36

Output

enter the number of processes in a system 3


enter the number of resources in a system 3

enter no. of instances of resource R1 10 enter no. of instances of


resource R2 7 enter no. of instances of resource R3 7

Enter the allocation matrix 3 2 1


112
412

Enter the maximum matrix 4 4 4


345
524

pocess P1
allocated 3 maximum 4 need 1
allocated 2 maximum 4 need 2
allocated 1 maximum 4 need 3

pocess P2
allocated 1 maximum 3 need 2
allocated 1 maximum 4 need 3
allocated 2 maximum 5 need 3

pocess P3
allocated 4 maximum 5 need 1
allocated 1 maximum 2 need 1
allocated 2 maximum 4 need 2

Availability R1 2 R2 3 R3 2

safe sequence
P3 Availability R1 6 R2 4 R3 4
P1 Availability R1 9 R2 6 R3 5
P2 Availability R1 10 R2 7 R3 7
37

program

#include<stdio.h>
void main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
printf("enter total no of processes: \n");
scanf("%d",&tp);
printf("enter clain matrix: \n");
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&c[i][j]);
}
}
printf("enter allocation matrix: \n");
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&p[i][j]);
}
}
printf("enter resource vector: \n");
for(i=0;i<5;i++)
{
scanf("%d",&r[i]);
}
printf("enter availability vector: \n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=0;i<4;i++)
{
sum=0;
for(j=0;j<5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
38

k++;
}
}
for(i=0;i<4;i++)
{
for(l=1;l<k;l++)
{
if(i!=m[l])
{
flag=1;
for(j=0;j<5;j++)
{
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=0;j<5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are: \n");
for(j=0;j<tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
}
39

OUTPUT:
$ vi bankersdetection.c
$ cc bankersdetection.c
$ ./a.out
enter total no of processes:
4
enter clain matrix:
01001
00101
00001
10101
enter allocation matrix:
10110
11000
00010
00000
enter resource vector:
21121
enter availability vector:
00001
deadlock causing processes are:
01
40

Program

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

pthread_t tid[2];
int counter;
pthread_mutex_t lock;

void* trythis(void *arg)


{
pthread_mutex_lock(&lock);

unsigned long i = 0;
counter += 1;
printf("\n Job %d has started\n", counter);

for(i=0; i<(0xFFFFFFFF);i++);
41

printf("\n Job %d has finished\n", counter);

pthread_mutex_unlock(&lock);

return NULL;
}

main()
{
int i = 0;
int error;

if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init has failed\n");
return 1;
}

while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThreadcan'tbecreated:[%s]", strerror(error));
i++;
}

pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);

return 0;
}

Output

$ gcc filename.c -lpthread

$ ./a.out
Job 1 started
Job 1 finished
Job 2 started
Job 2 finished
42

Program

#include <stdio.h> #include


<math.h>

main()
{
int size, m, n, pgno, pagetable[3]={5,6,7}, i, j, frameno; double m1;
int ra=0, ofs;

printf("Enter process size (in KB of max 12KB):"); scanf("%d", &size);


m1 = size / 4; n =
ceil(m1);
printf("Total No. of pages: %d", n); printf("\nEnter relative address (in hexa) \n");
scanf("%d", &ra);

pgno = ra / 1000; ofs = ra %


1000;
printf("page no=%d\n", pgno); printf("page table");
for(i=0;i<n;i++)
printf("\n %d [%d]", i, pagetable[i]); frameno = pagetable[pgno];
printf("\nPhysical address: %d%d", frameno, ofs);

Output

Enter process size (in KB of max 12KB):12


Total No. of pages: 3
Enter relative address (in hexa): 2643
page no=2 page
table
0 [5]
1 [6]
2 [7]
Physical address : 7643
43

Program

/* First fit allocation - ffit.c */ #include <stdio.h>

struct process
{
int size; int flag;
int holeid;
} p[10];
struct hole
{
int size;

int actual;
} h[10];

main()
{
int i, np, nh, j;

printf("Enter the number of Holes : "); scanf("%d", &nh);


for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i); scanf("%d", &h[i].size);
h[i].actual = h[i].size;
}

printf("\nEnter number of process : " );


scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}

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


{
for(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1; p[i].holeid = j;
h[j].size -= p[i].size;
}
}
}
}
44

printf("\n\tFirst fit\n");
printf("\nProcess\tPSize\tHole"); for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable"); for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size); printf("\n");
}
Output
Enter the numb er of Holes : 5
Enter size for hole H0 : 100
Enter size for hole H1 : 500
Enter size for hole H2 : 200
Enter size for Enter hole H3 : 300
size for hole H4 : 600

Enter number o f process : 4


enter the size of process P0 : 212
enter the size of process P1 : 417
enter the size of process P2 : 112
enter the size of process P3 : 426

First fit

Process PSize Hole


P0 212 H1
P1 417 H4
P2 112 H1
P3 426 Not allocated

Hole Actual Available


H0 100 100
H1 500 176
H2 200 200
H3 300 300
H4 600 183
45

Program

#include <stdio.h> struct

process
{
int size; int flag;
int holeid;
} p[10];

struct hole
{
int hid; int size;
int actual;
} h[10];

main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual =h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}
for(i=0; i<np; i++)
{
bsort(h, nh); for(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1;
p[i].holeid = h[j].hid;
h[j].size -= p[i].size;
}
}
}
}
printf("\n\tBest fit\n"); printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
46

else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable"); for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual, h[i].size);

printf("\n");
}

void bsort(struct hole bh[], int n)


{
struct hole temp; int i,j;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(bh[i].size > bh[j].size)
{
temp = bh[i]; bh[i] =
bh[j]; bh[j] = temp;
}
}
}
}

Output:

Enter the number of Holes : 5


Enter size for hole H0 : 100
Enter size for hole H1 : 500
Enter size for hole H2 : 200
Enter size for hole H3 : 300
Enter size for hole H4 : 600
Enter number of process : 4
enter the size of process P0 : 212
enter the size of process P1 : 417
enter the size of process P2 : 112
enter the size of process P3 : 426
Best fit
Process PSize Hole
P0 212 H3
P1 417 H1
P2 112 H2
P3 426 H4

Hole Actual Available


H1 500 83
H3 300 88
H2 200 88
H0 100 100
H4 600 174
47

Program

#include <stdio.h> main()


{
int i,j,l,rs[50],frame[10],nf,k,avail,count=0;

printf("Enter length of ref. string : ");


scanf("%d", &l);
printf("Enter reference string :\n");
for(i=1; i<=l; i++)
scanf("%d", &rs[i]);
printf("Enter number of frames : ");
scanf("%d", &nf);

for(i=0; i<nf; i++) frame[i] = -


1;
j = 0;
printf("\nRef. str Page frames"); for(i=1; i<=l; i++)
{
printf("\n%4d\t", rs[i]); avail = 0;
for(k=0; k<nf; k++) if(frame[k] ==
rs[i])
avail = 1;
if(avail == 0)
{
frame[j] = rs[i]; j = (j+1) %
nf; count++;
for(k=0; k<nf; k++) printf("%4d",
frame[k]);
}
}
printf("\n\nTotal no. of page faults : %d\n",count);
}
48

Output
Enter length of ref. string : 20
Enter reference string :
123421562123763
Enter number of frames : 5

Ref. str Page frames


1 1 -1 -1 -1 -1
2 1 2 -1 -1 -1
3 1 2 3 -1 -1
4 1 2 3 4 -1
2
1
5 1 2 3 4 5
6 6 2 3 4 5
2
1 6 1 3 4 5
2 6 1 2 4 5
3 6 1 2 3 5
7 6 1 2 3 7
6
3
Total no. of page faults : 10
49

Program

/* LRU page replacement - lrupr.c */

#include <stdio.h>

int arrmin(int[], int);

main()
{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;
int access[10], freq=0, dm;

printf("Length of Reference string : ");


scanf("%d", &len);
printf("Enter reference string :\n");
for(i=1; i<=len; i++)
scanf("%d", &rs[i]);
printf("Enter no. of frames : "); scanf("%d", &nf);

for(i=0; i<nf; i++) frame[i] = -


1;
j = 0;

printf("\nRef. str Page frames"); for(i=1;


i<=len; i++)
{
printf("\n%4d\t", rs[i]);
avail = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == rs[i])
{
avail = 1;
access[k] = ++freq; break;
}
}
if(avail == 0)
{
dm = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == -1)
dm = 1;
break;
}
if(dm == 1)
{
frame[k] = rs[i]; access[k] = ++freq;
count++;
}
50

else
{
j = arrmin(access, nf); frame[j] = rs[i];
access[j] = ++freq; count++;
}
for(k=0; k<nf; k++) printf("%4d",
frame[k]);
}
}
printf("\n\nTotal no. of page faults : %d\n", count);
}

int arrmin(int a[], int n)


{
int i, min = a[0]; for(i=1; i<n;
i++) if (min > a[i])
min = a[i]; for(i=0;
i<n; i++)
if (min == a[i]) return i;
}

Output

Length of Reference string : 15


Enter reference string :
12342 156212 37 63
Enter no. of frames : 5
Ref. str Page frames
1 1 -1 -1 -1 -1
2 1 2 -1 -1 -1
3 1 2 3 -1 -1
4 1 2 3 4 -1
2
1
5 1 2 3 4 5
6 1 2 6 4 5
2
1
2
3 1 2 6 3 5
7 1 2 6 3 7
6
3
Total no. of page faults : 8
51

Program

#include <stdio.h> @include


<stdlib.h> #include <conio.h>

struct
{
char dname[10]; char
fname[25][10]; int fcnt;
}dir;

main()
{
int i, ch; char f[30];
clrscr(); dir.fcnt = 0;
printf("\nEnter name of directory -- "); scanf("%s", dir.dname);

while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n4. Display Files\t5. Exit\n
Enter your choice--"); scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\n Enter the name of the file -- "); scanf("%s",
dir.fname[dir.fcnt]); dir.fcnt++;
break;

case 2:
printf("\n Enter the name of the file -- "); scanf("%s", f);
for(i=0; i<dir.fcnt; i++)
{
if(strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is deleted ",f); strcpy(dir.fname[i], dir.fname[dir.fcnt-1]);
break;
}
}
if(I == dir.fcnt)
printf("File %s not found", f);
else
dir.fcnt--;
break;

case 3:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for(i=0; i<dir.fcnt; i++)
{
if(strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is found ", f);
break;
52

}
}
if(I == dir.fcnt)
printf("File %s not found", f);
break;

case 4:
if(dir.fcnt == 0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0; i<dir.fcnt; i++)
printf("\t%s", dir.fname[i]);
}
break;
default:
exit(0);
}
}
getch();
}
53

Output

Enter name of directory -- CSE

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- fcfs

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- sjf

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- lru

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- sjf File
sjf is found

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- bank File
bank is not found

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 4
The Files are -- fcfs sjf lru bank

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 2
Enter the name of the file -- lru File
lru is deleted
54

Program

#include <stdio.h> #include


<conio.h> #include <stdlib.h>

struct
{
char dname[10], fname[10][10];
int fcnt;
}dir[10];

main()
{
int i, ch, dcnt, k; char f[30],
d[30];
clrscr();
dcnt=0; while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3.
Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \n Enter your choice -- ");

scanf("%d", &ch); switch(ch)


{
case 1:
printf("\n Enter name of directory -- "); scanf("%s",
dir[dcnt].dname); dir[dcnt].fcnt = 0;
dcnt++;
printf("Directory created"); break;

case 2:

printf("\n Enter name of the directory -- "); scanf("%s", d);


for(i=0; i<dcnt; i++) if(strcmp(d,dir[i].dname) == 0)
{
printf("Enter name of the file -- "); scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created"); break;
}
if(i == dcnt)
printf("Directory %s not found",d); break;

case 3:
printf("\nEnter name of the directory -- "); scanf("%s", d);
for(i=0; i<dcnt; i++)
{
if(strcmp(d,dir[i].dname) == 0)
{
printf("Enter name of the file -- "); scanf("%s", f);
for(k=0; k<dir[i].fcnt; k++)
{
if(strcmp(f, dir[i].fname[k]) == 0)
{
printf("File %s is deleted ", f); dir[i].fcnt--;
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
55
printf("File %s not found",f); goto jmp;

}
}
printf("Directory %s not found",d); jmp : break;

case 4:
printf("\nEnter name of the directory -- "); scanf("%s", d);
for(i=0; i<dcnt; i++)
{
if(strcmp(d,dir[i].dname) == 0)
{
printf("Enter the name of the file -- "); scanf("%s", f);
for(k=0; k<dir[i].fcnt; k++)
{
if(strcmp(f, dir[i].fname[k]) == 0)
{
printf("File %s is found ", f); goto jmp1;
}
}
printf("File %s not found", f); goto jmp1;
}
}
printf("Directory %s not found", d); jmp1: break;

case 5:
if(dcnt == 0)
printf("\nNo Directory's "); else
{
printf("\nDirectory\tFiles"); for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname); for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}

break;

default:
exit(0);
}
}
getch();
}
56

Output

1. Create Directory
2. Create File
3. Delete File
4. Search File
5. Display
6. Exit Enter your choice -- 1
Enter name of directory -- CSE Directory
created

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 1
Enter name of directory -- ECE Directory
created

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory -- ECE Enter
name of the file -- amruth File created

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory -- CSE Enter
name of the file -- kowshik File created

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory -- CSE Enter
name of the file -- pranesh File created

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory -- ECE Enter
name of the file -- ajith
File created

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE kowshik pranesh
ECE amruth ajith

1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 3
Enter name of the directory -- ECE Enter
name of the file -- ajith
File ajith is deleted

Result
Thus user files have been stored in their respective directories and retrieved easily.
57

Program
/* Contiguous Allocation - cntalloc.c */ #include <stdio.h>
#include <string.h>
int num=0, length[10], start[10]; char fid[20][4],
a[20][4];

void directory()
{
int i;
printf("\nFile Start Length\n");

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


printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);
}
void display()
{
int i;
for(i=0; i<20; i++) printf("%4d",i);
printf("\n"); for(i=0; i<20; i++)
printf("%4s", a[i]);
}
main()
{
int i,n,k,temp,st,nb,ch,flag; char id[4];

for(i=0; i<20; i++) strcpy(a[i], "");


printf("Disk space before allocation:\n"); display();
do
{
printf("\nEnter File name (max 3 char) : "); scanf("%s", id);
printf("Enter start block : "); scanf("%d", &st);
printf("Enter no. of blocks : "); scanf("%d", &nb);
strcpy(fid[num], id);
length[num] = nb; flag = 0;

if((st+nb) > 20)


{
printf("Requirement exceeds range\n"); continue;
}
for(i=st; i<(st+nb); i++) if(strcmp(a[i], "") !=
0)
flag = 1;
if(flag == 1)
{
printf("Contiguous allocation not possible.\n"); continue;
}
start[num] = st; for(i=st; i<(st+nb); i++)
strcpy(a[i], id);; printf("Allocation
done\n"); num++;

printf("\nAny more allocation (1. yes / 2. no)? : "); scanf("%d", &ch);


} while (ch == 1); printf("\n\t\t\tContiguous Allocation\n");
printf("Directory:");
directory();
printf("\nDisk space after allocation:\n"); display();
}
58

Output

Disk space before allocation:


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Enter File name (max 3 char) : cp Enter


start block : 14
Enter no. of blocks : 3
Allocation done
Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : tr Enter start


block : 18
Enter no. of blocks : 3
Requirement exceeds range

Enter File name (max 3 char) : tr Enter start


block : 10
Enter no. of blocks : 3
Allocation done
Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : mv Enter


start block : 0
Enter no. of blocks : 2
Allocation done
Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : ps Enter


start block : 12
Enter no. of blocks : 3
Contiguous allocation not possible.
Any more allocation (1. yes / 2. no)? :2

Contiguous Allocation
Directory:
File Start Length cp
14 3
tr 10 3
mv 0 2

Disk space after allocation:


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
mv m tr tr tr cp cp cp
v
59

Program

#include <stdio.h> #include


<conio.h> #include <string.h>

main()
{
static int b[20], i, j, blocks[20][20]; char F[20][20], S[20], ch;
int sb[20], eb[20], x, n; clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::", i+1);
scanf("%s", &F[i]);
printf("\n Enter No. of blocks::", i+1);
scanf("%d",&b[i]);
}

for(i=0;i<n;i++)
{
printf("\n Enter Starting block of file%d::",i+1);

scanf("%d", &sb[i]);
printf("\nEnter blocks for file%d::\n", i+1);
for(j=0; j<b[i]-1;)
{
printf("\n Enter the %dblock ::", j+2);
scanf("%d", &x);
if(b[i] != 0)
{
blocks[i][j] = x; j++;
}
else
printf("\n Invalid block::");
}
}

printf("\nEnter the Filename :");


scanf("%s", &S);
for(i=0; i<n; i++)
{
if(strcmp(F[i],S) == 0)
{
printf("\nFname\tBsize\tStart\tBlocks\n");
printf("\n \n");
printf("\n%s\t%d\t%d\t", F[i], b[i], sb[i]);
printf("%d->",sb[i]);
for(j=0; j<b[i]; j++)
{
if(b[i] != 0)
printf("%d->", blocks[i][j]);
}
}
}
60

printf("\n \n");
getch();
}
61

Output

Enter no. of Files ::2

Enter file 1 name ::fcfs Enter


No. of blocks::3

Enter file 2 name ::sjf


Enter No. of blocks::2

Enter Starting block of file1::8


Enter blocks for file1::
Enter the 2block ::3 Enter
the 3block ::5

Enter Starting block of file2::2


Enter blocks for file2::
Enter the 2block ::6 Enter the

Filename ::fcfs

Fname Bsize Start Blocks

fcfs 3 8 8->3->5
62

Program:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
63

OUTPUT
Enter the max range of disk
200
Enter the size of queue request
8
Enter the queue of disk positions to be read
90 120 35 122 38 128 65 68
Enter the initial head position
50
Disk head moves from 50 to 90 with seek
40
Disk head moves from 90 to 120 with seek
30
Disk head moves from 120 to 35 with seek
85
Disk head moves from 35 to 122 with seek
87
Disk head moves from 122 to 38 with seek
84
Disk head moves from 38 to 128 with seek
90
Disk head moves from 128 to 65 with seek
63
Disk head moves from 65 to 68 with seek
3
Total seek time is 482
Average seek time is 60.250000
64

15(b) Aim : Write C programs to simulate implementation SSTF Disk Scheduling


Algorithm

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
return 0;
}

OUTPUT:
*** SSTF Disk Scheduling Algorithm ***
Enter the size of Queue 5
Enter the Queue 10 17 2 15 4
Enter the initial head position 3
Total Seek Time is14
Average Seek Time is 2.800000

RESULT:
Thus the program was executed and verified successfully
65

Ex.16:Install any guest operating system like Linux using VMware.

Install Ubuntu Linux On VMWare Workstation:

1. Fier up VMWare Workstation


Download the VMWare Workstation application for your host operating system and install it on
your machine. The installation procedure is pretty simple and straight. Read the documentation for more
details. Open the app after installation. Create a new Virtual Machine.

2. Select Custom Configuration Wizard


You can choose either Typical or Custom Wizard. We recommend selecting Custom if you want to install
with all the configurations. If you are okay with default configurations then go ahead with Typical
configurations.
66

3. Select Virtual Machine Hardware Compatibility


Go with the default option if you don’t have the choice.

4. Select the Operating System Media


Select ‘I will install the operating system later’ for an interactive installation.

5. Select Guest Operating System]


67

6. Name the Virtual Machine Name and location

Type a name and give the location details.

7. Allocate the Processors


8. Allocate the Memory for Virtual Machine
9. Choose the Network Configuration
10. Select the I/O Controller Type
11. Select Disk Type
12. Select Virtual Disk
13. Select Disk Capacity
14. Specify Virtual Disk File
15. Create Virtual Machine
16. Supply Ubuntu ISO Image to Virtual Machine
17. Install Ubuntu Linux on VMWare Workstation
18. Power On the Virtual Machine
19. Welcome Ubuntu Virtual Machine
After powering on the Virtual machine, you will be treated with a welcome screen on which
you will see two options: Try Ubuntu and Install Ubuntu. Select Try Ubuntu if you want to run
Ubuntu in live mode. Select Install to continue the installation process.
20. Select Keyboard Layout
21. Software Update and Package Selection in Virtual Machine
22.Partition the Disk
23.Write Changes to Disk
24.Select Time Zone
25.Create an Admin Account
26.Installation of Ubuntu in Progress
27.Reboot Virtual Machine
68

28. Remove the installation media

29. Boot Ubuntu

You might also like