0% found this document useful (0 votes)
11 views51 pages

UNIX Commands and Shell Programming Guide

The document is a lab manual for Operating Systems, detailing UNIX commands, shell scripting, and C/C++ programming on Ubuntu. It includes examples of process creation using system calls like fork(), exec(), and wait(), as well as implementation of pipes and FIFOs. The manual serves as a comprehensive guide for students in the Department of Information Technology at Kakatiya University.

Uploaded by

mohammahmahfooz
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)
11 views51 pages

UNIX Commands and Shell Programming Guide

The document is a lab manual for Operating Systems, detailing UNIX commands, shell scripting, and C/C++ programming on Ubuntu. It includes examples of process creation using system calls like fork(), exec(), and wait(), as well as implementation of pipes and FIFOs. The manual serves as a comprehensive guide for students in the Department of Information Technology at Kakatiya University.

Uploaded by

mohammahmahfooz
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

OPERATING SYSTEMS

LAB MANUAL

R LAKSHMAN NAIK
Assistant Professor©

Department of Information Technology


KUCE&T, Kakatiya University
Warangal
R LAKSHMAN NAIK Dept. of IT, KUCE&T |2

1. UNIX COMMANDS, FILE PERMISSIONS, VI EDITOR, UNIX SHELL


PROGRAMMING FUNDAMENTALS.
a. User account command
passwd : Change your password.
su : Become another user
exit: Logout
whoami : Check the current being user. It returns username that you are using now.
b. Manual and Process command
man : Show any UNIX command usages
ps : Show process status
c. Directory Command
ls : List the contents of a directory
cd : Change to a directory.
pwd : Show current working directory. It prints out current working directory.
mkdir : Create a new directory.
rmdir : Remove a directory.
d. File Copy/Move/Remove Command
cp : Copy a file.
mv : Move (rename) a file.
rm : Remove (delete) a file.
e. File Display Command
cat : Display the content of a file all at once.
more : Display the contents of a file one display screen at a time.
less: Display the contents of a file a screen at a time. It is enhanced version of more,
has more options and functions. If operator has time, check man page of less.
grep : Search file and print lines that match pattern.
|: Pipe sign for output redirection
sort: The sort command arranges lines of text alphabetically or numerically.
f. Text Editor
1. The popular editor is the vi editor.
2. Another popular text editor is gedit
3. Some other test editors are as follows
1. Some of the console-based text editors are
 emacs—The comprehensive GNU emacs editing environment, which is
much more than an editor; see the section “Working with emacs” later in
this chapter
 nano—A simple text editor similar to the pico text editor included with
the pine email program
 vim—An improved, compatible version of the vi text editor
2. Some of the text editors for the X Window System, which provide a
graphical interface
 kate—A simple KDE text editor
 kedit—Another simple KDE text editor
g. How to write and run shell script programs on ubuntu?
Shell Prompt:
The prompt, $, which is called command prompt, is issued by the shell. While the
prompt is displayed, you can type a command.
Following is a simple example of date command which displays current date and
time:
$date
R LAKSHMAN NAIK Dept. of IT, KUCE&T |3

You can customize your command prompt using environment variable PS1
explained in Environment tutorial.
Shell Types:
In UNIX there are two major types of shells:
1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $
character.
2. The C shell. If you are using a C-type shell, the default prompt is the % character.
There are again various subcategories for Bourne Shell which is listed as follows:
• Bourne shell (sh)
• Korn shell (ksh)
• Bourne again shell (bash)
• POSIX shell (sh)
The different C-type shells follow:
• C shell (csh)
• TENEX/TOPS C shell (tcsh)
Example Script:
Assume we create a [Link] script. Note all the scripts would have .sh extension.
Before you add anything else to your script, you need to alert the system that a shell
script is being started. This is done using the shebang construct. For example:
#!/bin/sh
This tells the system that the commands that follow are to be executed by the Bourne
shell. It's called a shebang because the # symbol is called a hash, and the! Symbol is
called a bang.
To create a script containing these commands, you put the shebang line first and then
add the commands:
#!/bin/bash pwd
ls
Shell Comments:
You can put your comments in your script as follows:
#!/bin/bash
# Author : laxman
# Copyright (c) KU
# Script follows here:
pwd ls
Now you save the above content and make this script executable as follows:
$chmod +x [Link]
Now you have your shell script ready to be executed as follows:
$./[Link]
Example2:
To create a script containing athematic operation, create file
Vi [Link]
$ c=`expr $a + $b`
$ echo “the value of addition=$c”
$ d=`expr $a - $b`
$ echo “the value of subtraction=$d”
$ e= expr $a \* $b`
$ echo “the value of multiplication=$e”
$ f=`expr $a / $b`
$ echo “the value of division=$f”
$ g= echo `expr $a % $b`
R LAKSHMAN NAIK Dept. of IT, KUCE&T |4

$ echo “the value of modulus=$c”


Now you save the above content and make this script executable as follows:
$chmod +x [Link]
Now you have your shell script ready to be executed as follows:
$./[Link]
h. How to write and run c/c++ programs on ubuntu?
Step by step instructions.
1. Install the 'build-essential' package.
$ sudo apt-get install build-essential
2. Write your first C program.
$gedit myhello.c
write code into it, and save
#include <stdio.h>
main()
{
printf("Hello Ubuntu Lover!\n");
}
3. Compile your first C program
$ gcc myhello.c -o myhello
4. Run your first C program
$ ./myhello
Hello Ubuntu Lover!
5. Write your first C++ program
$gedit [Link]
write this code into it, and save

#include <iostream>
using namespace std;
int main()
{
cout << "Hello Ubuntu Lover in C++" << endl;
return 0;
}
6. Compile your first C++ program
$g++ [Link] -o myhellocpp
7. Run your first C++ program
$ ./myhellocpp
Hello Ubuntu Lover in C++
Note: - If you should happen to get permission errors, you need to make the file executable.
You can do this by issuing the following commands below
#chmod +x HelloWorld.C
or
#chmod +x [Link]
R LAKSHMAN NAIK Dept. of IT, KUCE&T |5

2. PROGRAMS ON PROCESS CREATION USING FORK( ), EXEC( ) AND WAIT( )


SYSTEM CALLS
a. Write a program to display processid and groupid.

#include<stdio.h>
main()
{
printf(“pid=%d pgrp=%d”,getpid(),getpgrp(0));
}

Output:-
pid=4267 pgrp=4267

b. Write program to display userid, groupid, processid and parent processid.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
printf(“processid: pid=%d”,getpid());
printf(“parent processid: ppid=%d”,getppid());
printf(“userid : uid=%d”,getuid());
printf(“user groupid=%d”,getgid());
}

Output:-
processid: pid=4574
parent processid: ppid=3263
processid: pid=500
user groupid=500

c. Write program to display process status before and after forking.

#include<stdio.h>
main()
{
int fpid;
printf(“before forking fpid=%d”,fpid);
system(“ps”);
fpid=fork();
system(“ps”);
printf(“after forking fpid=%d”,fpid);
}

Output:-
Pid tty time cmd
3263 pts/11 00:00:00 bash
4865 pts/11 00:00:00 [Link]
R LAKSHMAN NAIK Dept. of IT, KUCE&T |6

d. Write a program to create processid, parent processid and child processid


before and after forking.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int fpid;
printf(“before forking fpid=%d pid=%d ppid=%d\n”,fpid,getpid(),getppid());
fpid=fork();
if(fpid==0)
{
printf(“child process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
else
{
printf(“parent process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
printf(“after forking fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}

Output:-
before forking fpid=13725684 pid=3753 ppid=3062
child process fpid=0 pid=3754 ppid=3753
after forking fpid=0 pid=3754 ppid=3753
parent process fpid=3754 pid=3753 ppid=3062
after forking fpid=3754 pid=3753 ppid=3062

e. Write program to demonstrate wait() system call.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int fpid,status;
printf(“before forking fpid=%d pid=%d ppid=%d\n”,fpid,getpid(),getppid());
fpid=fork();
if(fpid==0)
{
printf(“child process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
else
{
printf(“parent process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
Wait(status);
printf(“after forking fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
R LAKSHMAN NAIK Dept. of IT, KUCE&T |7

Output:-
before forking fpid=-1081197880 pid=4004 ppid=3062
child process fpid=0 pid=4005 ppid=4004
after forking fpid=0 pid=4005 ppid=4004
parent process fpid=4005 pid=4004 ppid=3062
after forking fpid=4005 pid=4004 ppid=3062

3. PROGRAMS ON IMPLEMENTATION OF PIPES AND FIFOS

a. Write a program to explain waiting chain of processes.

#include<stdio.h>
#include<unistd.h>
main()
{
If(fork()==0)
{
printf(“pid=%d\t ppid=%d”,getpid(),getppid());
If(fork()==0)
{
printf(“pid=%d\t ppid=%d”,getpid(),getppid());
If(fork()==0)
printf(“pid=%d\t ppid=%d”,getpid(),getppid());
}}}

Output:-
pid=4311 ppid=4310
pid=4311 ppid=4312
pid=4312 ppid=4313
pid=4311 ppid=4314

b. Write a program to implement pipe() system call.

#include<stdio.h>
#include<unistd.h>
main()
{
int pipefd[2],n;
char buff[100];
if(pipe(pipefd)<0)
perror(“pipe error”);
printf(“read fd=%d,write fd=%d\n”,pipefd[1],pipefd[2]);
if(write(pipefd[1],”hello world\n”,12)!=12)
perror(“write error”);
if(n=read(pipefd[0],buff,sizeof(buff)<=0))
perror(“read error”);
write(1,buff,n);
}
R LAKSHMAN NAIK Dept. of IT, KUCE&T |8

Output:-
readfd=3
writefd=4

c. Write a program to create related processes (parent/child) through pipes.

#include<stdio.h>
#include<unistd.h>
main()
{
int pid,fd[2];
char buff[10]={};
pipe(fd);
pid=fork();
if(pid==0)
{
close(fd[0]);
printf(“is the child\n”);
write(fd[1],”hello”,5);
}
else
{
close(fd[1]);
printf(“is the parent”);
read(fd[0],buff,5);
printf(“%s”,buff);
}}

Output:-
is the child
is the paent hello

d. Write a program to demonstrate the open() system call.

#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1,fd2,fd3;
printf(“before open”);
fd1=open(“/etc/passwd”,0_RDONLY);
fd2=open(“/etc/passwd”,0_WRONLY);
fd3=open(“/etc/passwd”,0_RDONLY);
printf(“fd1=%d fd2=%d fd3=%d” fd1,fd2,fd3”);
}

Output:-
before open fd1=3 fd2=-1 fd3=4
R LAKSHMAN NAIK Dept. of IT, KUCE&T |9

e. Write a program to demonstrate the read() system call.

#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1;
char buf1[20],buf2[20];
buf1[19]=’\0’;
buf2[19]=’\0’;
printf(“****************\n”);
fd1=open(“/etc/passwd”,O_RDONLY);
read(fd1,buf1,19);
printf(“fd1=%d buf1=%s\n”,fd1,buf1);
read(fd1,buf2,19);
printf(“fd1=%d buf2=%s\n”,fd1,buf2);
printf(“*************\n”);
}

Output:-
***************************
fd1=3 buf1=root:*:0:0:root:/00
fd1=4 buf2=root:*:0:0:root:/00
***************************

f. Write a program to demonstrate the read() system call reading two files.

#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1,fd2,fd3;
char buf1[20],buf2[20];
buf1[19]=’\0’;
buf2[19]=’\0’;
printf(“****************\n”);
fd1=open(“/etc/passwd”,O_RDONLY);
fd2=open(“/etc/passwd”,O_RDONLY);
read(fd1,buf1,19);
printf(“fd1=%d buf1=%s\n”,fd1,buf1);
read(fd2,buf2,19);
printf(“fd2=%d buf2=%s\n”,fd2,buf2);
printf(“*************\n”);
}
Output:-
***************
fd1=-1 buf1=8t
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 10

fd2=-1 buf2=8t
putty
***************

g. Write a program to demonstrate the creat(), write() and close() system calls.

#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1;
char *buf1=”I am a string”;
char *buf1=”I am second string”;
printf(“****************\n”);
fd1=creat(“./[Link]”,O_WRONLY);
write(fd1,buf1,20);
write(fd1,buf2,30);
printf(“fd1=%d buf1=%s\n”,fd1,buf1);
close(fd1);
chmod(“./[Link]”,0666);
printf(“****************\n”);
}

Output:-
********************
fd1=3 buf1=i am a string
********************

4. PROGRAMS ON CPU SCHEDULING ALGORITHMS LIKE FCFS, SJF, ETC…


a) FCFS b) SJF c) Round Robin d) Priority e) Multi-level queue

a. FCFS CPU Scheduling Algorithm

#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{

printf("\nEnter Burst Time for Process %d -- ", i);


scanf("%d", &bt[i]);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 11

}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
return(0);
}

b. SJF CPU Scheduling Algorithm

#include<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 12

for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage
Turnaround Time -- %f", tatavg/n);
getch();
return(0);
}

c. Round Robin CPU Scheduling Algorithm

#include<stdio.h>
main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 13

printf("\nEnter Burst Time for process %d -- ", i+1);


scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);

printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND


TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();
return(0);
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 14

d. Priority CPU Scheduling Algorithm

#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)

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

temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;

temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;

}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];


tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 15

printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverage Turnaround


Time is --- %f",tatavg/n);
getch();
return(0);
}

e. Multi-level queue scheduling algorithm

#include<stdio.h>
main()
{
int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time of Process %d --- ", i);
scanf("%d",&bt[i]);
printf("System/User Process (0/1) ? --- ");
scanf("%d", &su[i]);

}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[k])
{
temp=p[i];

p[i]=p[k];
p[k]=temp;

temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;

temp=su[i];
su[i]=su[k];
su[k]=temp;
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 16

wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST
TIME\tWAITING TIME\tTURNAROUND TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);

printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverage


Turnaround Time is --- %f",tatavg/n);
getch();
return(0);
}

5. WRITE A C PROGRAM TO SIMULATE PRODUCER-CONSUMER PROBLEM


USING SEMAPHORES.

#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 17

printf(“\nEnter your choice: ”);


scanf(“%d”, &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);

else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
} Break;

case 2: if(in == out)


printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;

}
}
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 18

6. WRITE A C PROGRAM TO SIMULATE BANKERS ALGORITHM FOR THE


PURPOSE OF DEADLOCK AVOIDANCE.

#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i); printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);

printf("\nEnter New Request Details -- ");


printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}

for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 19

{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;

}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j; f[j].flag=1; for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1; printf("("); for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 20

{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
getch();
}

7. WRITE A C PROGRAM TO SIMULATE PAGING TECHNIQUE OF MEMORY


MANAGEMENT.

#include<stdio.h>
#include<conio.h>
main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 21

clrscr();
printf("\nEnter the memory size -- ");
scanf("%d",&ms);

printf("\nEnter the page size -- ");


scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
printf("\nEnter number of processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];
printf("\nEnter pagetable for p[%d] --- ",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and pagenumber and offset -- ");
scanf("%d %d %d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);
}
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 22

8. WRITE A C PROGRAM TO SIMULATE PAGE REPLACEMENT ALGORITHMS


a) FIFO b) LRU c) LFU d) optimal

a. FIFO Page Replacement Algorithm

#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
clrscr();
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");

for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\n Enter no. of frames -- ");
scanf("%d",&f);

for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is -- \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
getch();

}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 23

b. LRU Page Replacement Algorithm

#include<stdio.h>
#include<conio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
clrscr();
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 24

}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;

m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 25

c. LFU Page Replacement Algorithm

#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
Printf(“\nThe Page Replacement Process is – \n“);
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf(“\tPF No. %d”,pf);
}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 26

d. Optimal page replacement algorithms

#include<stdio.h>
int n; main() { int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s;
int count=1,pf=0,p=0; float pfr;
clrscr();
printf("Enter maximum limit of the sequence: ");
scanf("%d",&max);
printf("\nEnter the sequence: ");
for(i=0;i<max;i++) scanf("%d",&seq[i]);
printf("\nEnter no. of frames: ");
scanf("%d",&n); fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j]) flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count]);
count++; pf++;
}
i++;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 27

flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==1)
{
s=k; break;
}
}
}
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
pf++; printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\nThe no. of page faults are %d",pf);
printf("\nPage fault rate %f",pfr);
getch(); }
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 28

{
if(max<a[i])
{
max=a[i]; k=i;
}
}
return k;
}

9. WRITE A C PROGRAM TO SIMULATE THE FOLLOWING FILE


ALLOCATION STRATEGIES.
a) Sequential b) Linked c) Indexed

a. Sequential File Allocation

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

struct fileTable
{
char name[20];
int sb, nob;
}

ft[30];

void main()
{
int i, j, n; char s[20]; clrscr();
printf("Enter no of files :");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 29

scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched -- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");

else
{

printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS


OCCUPIED\n"); printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);

}
getch();
return (0);
}

b. Liked File Allocation

#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob; struct block *sb;
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 30

ft[30];
struct block
{
int bno;
struct block *next;
};
void main()
{
int i, j, n;
char s[20];
struct block *temp;
clrscr();
printf("Enter no of files :"); scanf("%d",&n); for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp = ft[i].sb;
printf("Enter the blocks of the file :");
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++)
{
temp->next = (struct block*)malloc(sizeof(struct block));
temp = temp->next;
scanf("%d",&temp->bno);
}
temp->next = NULL;
}
printf("\nEnter the file name to be searched -- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
{
printf("%d -> ",temp->bno);
temp = temp->next;
}
}
getch();
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 31

return(0);
}

c. Indexed File Allocation

#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob, blocks[30];
}ft[30];
void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :");
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
}
printf("\nEnter the file name to be searched -- ");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 32

scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED"); printf("\n
%s\t\t%d\t",ft[i].name,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].blocks[j]);
}
getch();
return(0);
}

10. WRITE A C PROGRAM TO SIMULATE THE MVT AND MFT MEMORY


MANAGEMENT TECHNIQUES

a. MFT Memory Management Technique

#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 33

printf("\nEnter the number of processes -- ");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d",nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL
FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif); printf("\nTotal External
Fragmentation is %d",ef);
getch();
}

b. MVT Memory Management Technique

#include<stdio.h>
#include<conio.h>
main()
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 34

int ms,mp[10],i, temp,n=0;


char ch = 'y';
clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms); temp=ms; for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 35

11. WRITE A C PROGRAM TO SIMULATE THE FOLLOWING CONTIGUOUS


MEMORY ALLOCATION TECHNIQUES
a) Worst-fit b) Best-fit c) First-fit

a. First-FIT

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 36

bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

b. BEST-FIT

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 37

{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

c. WORST-FIT
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 38

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb; j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 39

for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

12. WRITE A C PROGRAM TO SIMULATE THE FOLLOWING FILE


ORGANIZATION TECHNIQUES
a) Single level directory b) Two level directory c) Hierarchical

a. Single Level Directory Organization

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;

}dir;

void main()
{
int i,ch;
char f[30];
clrscr();
[Link] = 0;
printf("\nEnter name of directory -- ");
scanf("%s", [Link]);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n4. Display Files\t5. Exit\nEnter
your choice -- ");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 40

scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",[Link][[Link]]);
[Link]++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<[Link];i++)
{
if(strcmp(f, [Link][i])==0)
{
printf("File %s is deleted ",f); strcpy([Link][i],[Link][[Link]-1]);
break;
}
}
if(i==[Link])
printf("File %s not found",f);

else
[Link]--;
break;

case 3: printf("\nEnter the name of the file -- ");


scanf("%s",f);
for(i=0;i<[Link];i++)
{
if(strcmp(f, [Link][i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==[Link])
printf("File %s not found",f);
break;
case 4: if([Link]==0)
printf("\nDirectory Empty");

else
{
printf("\nThe Files are -- ");
for(i=0;i<[Link];i++)
printf("\t%s",[Link][i]);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 41

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

b. Two Level Directory Organization

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];

void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;

while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\t Enter your choice -- ");

scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 42

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

case 4: printf("\nEnter name of the directory -- ");


scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 43

{
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();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 44

c. Hierarchical Directory Organization

#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 45

int i, gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype); (*root)->level=lev;
(*root)->y=50+lev*50; (*root)->x=x;
(*root)->lx=lx; (*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)>nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,
lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i; settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 46

}
}

INPUT
Enter Name of dir/file(under root): ROOT
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for ROOT): 2
Enter Name of dir/file(under ROOT): USER1
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER1): 1
Enter Name of dir/file(under USER1): SUBDIR1
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR1): 2
Enter Name of dir/file(under USER1):
JAVA Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for JAVA): 0
Enter Name of dir/file(under SUBDIR1): VB
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for VB): 0
Enter Name of dir/file(under ROOT): USER2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER2): 2
Enter Name of dir/file(under ROOT): A
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under USER2): SUBDIR2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR2): 2
Enter Name of dir/file(under SUBDIR2):
PPL Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for PPL): 2
Enter Name of dir/file(under PPL): B
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under PPL): C
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under SUBDIR): AI
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for AI): 2
Enter Name of dir/file(under AI): D
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under AI): E
Enter 1 for Dir/2 for File: 2
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 47

OUTPUT

13. WRITE A C PROGRAM TO SIMULATE DISK SCHEDULING ALGORITHMS


a) FCFS b) SCAN c) C-SCAN

a. FCFS Disk Scheduling Algorithm

#include<stdio.h>
main()
{
int t[20], n, i, j, tohm[20], tot=0;
float avhm;
clrscr();
printf("enter the [Link] tracks: ");
scanf("%d",&n);
printf("enter the tracks to be traversed: ");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf("Tracks traversed\tDifference between tracks\n");
for(i=1;i<n+1;i++)
printf("%d\t\t\t%d\n",t[i],tohm[i]);
printf("\nAverage header movements:%f",avhm);
getch();
return 0;
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 48

INPUT
Enter [Link] tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed Difference between tracks
55 45
58 3
60 2
70 10
18 52
90 72
150 60
160 10
184 24

Average header movements:30.888889

b. SCAN Disk Scheduling Algorithm

#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;

clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h); t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 49

for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j];
j--;
p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch();
}
INPUT
Enter [Link] tracks:9
Enter track position:55 58 60 70 18 90 150 160 184

OUTPUT
Tracks traversed Difference between tracks
150 50
33

160 10
184 24
90 94
70 20
60 10
58 2
55 3
18 37

Average header movements: 27.77

c. C-SCAN Disk Scheduling Algorithm


R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 50

#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;
printf("enter the tracks");
for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
for(i=0;i<=n+2;i++)
if(t[i]==h)
j=i;
break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i];
i++;
p++;
}
for(j=0;j<n+2;j++)
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 51

if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];

else
d[j]=atr[j+1]-atr[j];

sum+=d[j];
}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n);
getch();
}

INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
Enter starting position : 100
OUTPUT
Tracks traversed Difference Between tracks
150 50
160 10
184 24
18 240
55 37
58 3
60 2
70 10
90 20

Average seek time : 35.7777779

You might also like