Os Lab Record Final
Os Lab Record Final
MANIKAVALLI REGNO:411621104029
EX NO:1
BASIC UNIX COMMANDS
DATE:
INTRODUCTION TO UNIX
WHAT IS UNIX?
UNIX is an operating system which was first developed in the 1960s, and
has been under constant development ever since. By operating system, we
mean the suite of programs which make the computer work. It is a stable,
multi-user, multi-tasking system for servers, desktops and laptops.
TYPES OF UNIX
There are many different versions of UNIX, although they share common
similarities. The most popular varieties of UNIX are Sun Solaris,
GNU/Linux, and MacOS X.
Here in the School, we use Solaris on our servers and workstations, and
Fedora Linux on the servers and desktop PCs.
The UNIX operating system is made up of three parts; the kernel, the shell
and the programs.
THE KERNEL
The kernel of UNIX is the hub of the operating system: it allocates time and
memory to programs and handles the filestore and communications in
response to system calls.
As an illustration of the way that the shell and the kernel work together,
suppose a user types rm myfile (which has the effect of removing the
filemyfile). The shell searches the filestore for the file containing the
program rm, and then requests the kernel, through system calls, to execute
the program rm on myfile. When the process rm myfile has finished
running, the shell then returns the UNIX prompt % to the user, indicating
that it is waiting for further commands.
THE SHELL
The shell acts as an interface between the user and the kernel. When a user
logs in, the login program checks the username and password, and then
starts another program called the shell. The shell is a command line
interpreter (CLI). It interprets the commands the user types in and arranges
for them to be carried out. The commands are themselves programs: when
they terminate, the shell gives the user another prompt (% on our systems).
The adept user can customize his/her own shell, and users can use different
shells on the same machine. Staff and students in the school have the tcsh
shell by default.
The tcsh shell has certain features to help the user inputting commands.
History - The shell keeps a list of the commands you have typed in. If you
need to repeat a command, use the cursor keys to scroll up and down the list
or type history for a list of previous commands.
A file is a collection of data. They are created by users using text editors,
running compilers etc.
Examples of files:
UNIX ARCHITECTURE
The main concept that unites all versions of UNIX is the following four basics
−
Kernel: The kernel is the heart of the operating system. It interacts with
hardware and most of the tasks like memory management, tash schedul-
ing and file management.
Shell: The shell is the utility that processes your requests. When you
type in a command at your terminal, the shell interprets the command
and calls the program that you want. The shell uses standard syntax for
all commands. C Shell, Bourne Shell and Korn Shell are most famous
shells which are available with most of the Unix variants.
Files and Directories: All data in UNIX is organized into files. All files
are organized into directories. These directories are organized into a tree-
like structure called the file system.
All the files are grouped together in the directory structure. The file-system
is arranged in a hierarchical structure, like an inverted tree. The top of the
hierarchy is traditionally called root (written as a slash / )
In the diagram above, we see that the home directory of the undergraduate
student "ee51vn" contains two sub-directories (docs and pics) and a file
called report.doc.
UNIX PROCESS
One further consideration on this topic is the fact that a running UNIX process
can spawn “child” processes. For example, any program you run from inside a
UNIX Shell will be a child process of that shell. Conversely, the shell is the
parent process of this child. Child processes have associated with them both
their own process id (PID) as well as their parent’s process id (PPID).
Normally this concept of parent and child processes is not something you need
to be bothered with as a user. However, it can be useful to understand how
UNIX organizes processes if you are trying to keep track of certain system
resources (e.g. memory and CPU), if you are working with environment
variables, or if you need to track down a rogue program or script.
OUTPUT:
1.Date:
[oslab@localhost~]$date
2.Cal:
[oslab@localhost~]$cal
January 2020
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
3.Echo:
GOOD MORNING
4.Who:
[oslab@localhost~]$ who
5.Who am i:
[oslab@localhost~]$ who am i
6.TTY:
[oslab@localhost~]$tty
/dev/pts/2
7.Man:
[oslab@localhost~]$ man date
NAME
SYNOPSIS
DESCRIPTION
Display the current time in the given FORMAT, or set the system date.
-d, --date=STRING
-f, --file=DATEFILE
-r, --reference=FILE
-R, --rfc-2822
--version
...skipping...
-f, --file=DATEFILE
-r, --reference=FILE
-R, --rfc-2822
TIMESPEC
-s, --set=STRING
--version
2.Cd:
[oslab@localhost~]$cd int
3.Pwd:
[oslab@localhost~]$pwd
/home/oslab/int
4.Rmdir:
[oslab@localhost~]$rmdir int
5.ls:
[oslab@localhost~]$ls
Fruits
2.cp:
[oslab@localhost~]$ cp fruits mango
3.Move:
[oslab@localhost~]$ mv fruits fruit
4.Remove:
[oslab@localhost~]$ rm fruits
5.Word count:
[oslab@localhost~]$wc -c mango
46 mango
[oslab@localhost~]$wc mango
1 9 46 mango
6.Sort:
[oslab@localhost~]$ sort demo
2.Difffer:
3.Filter:
(a) Head:
[oslab@localhost~]$head -3 example
A
C
(b)Tail:
[oslab@localhost~]$tail -2 example
FB
4.Numbering:
[oslab@localhost~]$nl mango
RESULT:
EX NO:2a
FORK SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
STEP 1: Start .
STEP 2: Fork is used to create a child process.
STEP 3: Fork () - a child process is created.
STEP 4: Prints“ Hello World”.
STEP 5: Stop.
PROGRAM
#include<stdio.h>
#include<unistd.h>
main()
fork();
printf("Hello World\n");
OUTPUT
Hello World
Hello World
PROGRAM
#include<stdio.h>
#include<unistd.h>
main()
fork();
fork();
fork();
printf("Hello World\n");
OUTPUT
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
RESULT:
AIM:
To write a c program in linear to implement fork (), exec (),
wait () a system.
ALGORITHM:
STEP 1: Get the number of terms for Fibonacci series.
PROGRAM
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<conio.h>
main()
int a=-1,b=1,i,c=a+b,n,pid,cpid;
scanf("%d",&n);
pid=getpid();
pid=fork();
cpid=getpid();
printf("\nchild process:%d",cpid);
if(pid==0)
for(i=0;i<n;i++)
c=a+b;
printf("\n%d",c);
a=b;
b=c;
printf("\nchild ends");
else
wait(NULL);
printf("\nparent ends");
OUTPUT
[it27@mm4 ~]$a.out
child process:8723
child process:8732
RESULT:
Thus, the program in linux to implement fork (), exec (), wait ()
a system is executed successfully and the output is verified.
DATE:
AIM:
To write a c program in linear linux to implement execlp system
call.
ALGORITHM:
STEP 1: Start.
STEP 2: Create a child process using fork () system call.
STEP 3: If pid is less than zero then print error message “fork
failed”.
STEP 4: Else if pid is equal to zero execute execlp ()
system call to list the file using ls command.
STEP 5: Child process completes execution.
STEP 6: Stop
PROGRAM
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>main()
int pid;
pid=fork();
if(pid<0){
fprintf(stderr,"fork failed\n");
exit(-1);
else if(pid==0){
execlp("/bin/ls","ls",NULL);
else{
wait(NULL);
printf("Child Complete");
exit(0);
OUTPUT
RESULT:
Thus, the c program in linear linux to implement execlp system call is executed successfully and the
output is verified.
DATE:
AIM:
To write a unix c program to stimulate opendir and readdir sys-
tem calls.
ALGORITHM:
PROGRAM
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>
DIR *dirname;
dirname=opendir(argv[1]);
dir=readdir(dirname);
while(dir!=NULL)
getchar();
OUTPUT
RESULT:
AIM:
To write a UNIX C- program to stimulate LS system call.
ALGORITHM:
STEP 1: Get the directory to be listed through command line
arguments.
STEP 2: Open the directory using open dir () function.
STEP 3: While there are files to be processed then read the file from
the directory using readdir ().
STEP 4: Print the file name.
STEP 5: Stop.
PROGRAM
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
#include<dirent.h>
DIR *dp;
dp=opendir(argv[1]);
while((link=readdir(dp))!=0)
printf("%s",link->d_name);
closedir(dp);
OUTPUT
system[it27@mm4 aaa]$ cd ..
.bb..aa
RESULT:
DATE:
AIM:
To write a c program for grep system calls in c program .
ALGORITHM:
STEP 1: Declare a file pointers.
STEP 2: Get the file name.
STEP 3: Get the pattern.
STEP 4: Open the file using fopen () system call.
STEP 5: Find the occurrence of the pattern using strstr () function
in the file.
STEP 6: Print the pattern containing statements.
STEP 7: Stop.
PROGRAM
#include<unistd.h>
#include<string.h>
main()
char fn[10],pat[10],temp[1000];
FILE *fp;
scanf("%s",fn);
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp)){
fgets(temp,1000,fp);
if(strstr(temp,pat)){
printf("%s",temp);
}}
fclose(fp);
OUTPUT
[it27@mm4 ~]$a.out
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
RESULT:
DATE:
AIM:
To write a c program for system call for I/O system using linux.
ALGORITHM:
STEP 1: Get the file name
STEP 2: Open the file using open () system call in read only mode.
STEP 3: Read the content of the file using read() system call.
STEP 4: Read from the file descriptor in to the buffer using
read() system call.
STEP 5: Print the content in the buffer / file.
STEP 6: Stop
PROGRAM
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
int fd;
char buf1[100],fname[30];
scanf("%s",fname);
fd=open(fname,O_RDONLY);
read(fd,buf1,30);
close(fd);
OUTPUT
The content is : hi
Hello
RESULT:
Thus the c program for system call for I/O system using
Linux is executed successfully and the output is verified.
AIM:
To write a program to find largest of three numbers.
ALGORITHM:
STEP1: Get the value of a , b and c.
STEP 2: Compare if a is greater than both b and c.
STEP 3: If true then print a is big.
STEP 4: Otherwise compare b is greater than c.
STEP 5: If condition is true then print b is big else print c is big.
STEP 6: Stop.
PROGRAM:
echo "Enter three numbers"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
elif [ $b -gt $c ]
then
else
fi
OUTPUT:
[oslab@localhost~]$sh large3n.sh
873
A is big
RESULT:
AIM:
To write a program to perform arithmetic operations.
ALGORITHM:
STEP1: Get the values of a and b.
STEP 2: Get the option.
STEP 3: If the option is add then perform addition operation.
STEP 4: If option is subtract then do a-b.
STEP 5: If option is multiplication then do a*b.
STEP 6: If option is division then do a /b.
STEP 7: Otherwise print invalid choice.
STEP 8: Stop.
PROGRAM:
echo Enter two numbers
read a b
read op
case $op in
5) break;;
esac
OUTPUT:
[oslab@localhost~]$sh airthmetic.sh
addition=30
[oslab@localhost~]$sh airthmetic.sh
subtration=10
[oslab@localhost~]$sh airthmetic.sh
multiplication=10
[oslab@localhost~]$sh airthmetic.sh
division=8
RESULT:
Thus the program to perform shell program add, sub, mul, div
using case is executed successfully and the output is verified.
EX NO:4c SHELL PROGRAM- FACTORIAL OF A NUMBER
DATE:
AIM:
To write a c program for factorial of the number.
ALGORITHM:
PROGRAM:
echo "Enter a number"
read num
i=1
fact=1
do
i=` expr $i + 1 `
done
OUTPUT:
[oslab@localhost~]$sh fact.sh
Enter a number
RESULT:
Thus the c program for factorial of a number is executed successfully and the output is verified.
AIM:
ALGORITHM:
STEP 1: Get the input basic salary.
STEP 2: Calculate DA and HRA.
STEP 3: Calculate gross salary.
STEP 4: Print the output.
PROGRAM:
echo enter the basic salary
read basic
OUTPUT:
[oslab@localhost~]$sh gross.sh
5000
RESULT:
Thus the program for the gross salary is executed successfully and the output is verifie.
AIM:
To write a program to find sum of the digits.
ALGORITHM:
STEP 1: Get the input number.
STEP 2: Initialise the variable sum to zero.
STEP 3: Check the number is greater than zero.
STEP 4: Take the individual digits and add it
STEP 5: Print the output.
PROGRAM:
echo "Enter the number"
read num
sum=0
do
done
OUTPUT:
[oslab@localhost~]$sh sum.sh
143
Sum of Digits is 8
RESULT:
EX NO:5a
FIRST COME FIRSTSERVESCHEDULING
ALGORITHM
DATE:
AIM:
To implement the first come first serve scheduling algorithm us-
ing c program in LINUX.
ALGORITHM:
STEP 1: Get the number of process, along with their arrival time
and
burst time.
STEP 2: Sort the process in the increasing order of the arrival time.
STEP 3: For all the process calculate the waiting time and turn
around time.
STEP 4: Calculate the average of both waiting time and turn around
time.
STEP 5: Display the process number , arrival time , burst time, turn
around time, waiting time , average turn around time and
average waiting time.
STEP 6: Display the gantt chart.
STEP 7: Stop.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main()
int n,a[10],b[10],t[10],w[10],g[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
a[i]=0;
b[i]=0;
w[i]=0;
g[i]=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<n;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
att=att+t[i];
awt=awt/n;
att=att/n;
for(i=0;i<n;i++)
printf("\n\t\tp%d\t\t%d\t\t%d",i+1,w[i],t[i]);
printf("\n_____________________________________________________\n");
for(i=0;i<n;i++)
printf("\tp%d\t",i+1);
printf("\t\n");
printf("\n_____________________________________________________\n");
printf("\n");
for(i=0;i<=n;i++)
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d\t",g[i]);
printf("\n_____________________________________________________\n");
printf("\n");
OUTPUT:
[oslab@localhost~]$ cc fcfs.c
[oslab@localhost~]$ ./a.out
p1 0 3
p2 3 5
p3 5 9
p4 9 10
GANTT CHART:
_____________________________
p1 | p2 | p3 | p4 |
_____________________________
0 3 5 9 10
RESULT:
Thus the program to implement the first come first serve sched-
uling algorithm is executed successfully and the output is verified.
AIM:
To implement the shortest job first scheduling algorithm using c -
program.
ALGORITHM:
STEP 1: Get the number of process with their arrival time and burst
time.
STEP 2: Check if all the process arrival time is equal, if true then
sort the process in the increasing order of the burst time
STEP 3: Calculate waiting time and turnaround time for each
process.
STEP 4: Otherwise schedule the process which arrived first.
STEP 5: Check every millisecond whether there is any new
process with shorter burst time.
STEP 6: Then schedule the new process and reduce the burst time
of previous by its executed milliseconds.
STEP 7: Calculate waiting and turnaround time.
STEP 8: Calculate average waiting time and average turnaround
time.
PROGRAM:
#include<stdio.h>
main()
int n,i,j,temp,p[10],b[10],bu[10],wt[10],tt[10];
float sum=0.0,twt=0.0;
for(i=0;i<10;i++)
b[i]=p[i]=bu[i]=wt[i]=tt[i]=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("\n%d",&bu[i]);
for(i=1;i<=n;i++)
b[i]=bu[i];
p[i]=i;
for(i=n;i>=1;i--)
for(j=2;j<=n;j++)
if(b[j-1]>b[j])
temp=b[j-1];
b[j-1]=b[j];
b[j]=temp;
temp=p[j-1];
p[j-1]=p[j];
p[j]=temp;
wt[1]=0;
for(i=2;i<=n;i++)
wt[i]=b[i-1]+wt[i-1];
for(i=1;i<=n;i++)
twt=twt+wt[i];
tt[i]=b[i]+wt[i];
sum=sum+tt[i];
twt=twt/n;
sum=sum/n;
printf("\nProcess\tBtime\tWT\tTT\n");
for(i=1;i<=n;i++)
printf("\nP%d\t%d\t%d\t%d\n",p[i],b[i],wt[i],tt[i]);
printf("\n___________________________________________________\n");
for(i=1;i<=n;i++)
printf("\tP%d\t|",p[i]);
printf("\n");
printf("_____________________________________________________\n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t\t",wt[i]);
printf("%d",tt[n]);
printf("\n");
OUTPUT:
[oslab@localhost~]$ cc sjfs.c
[oslab@localhost~]$ ./a.out
Process Btime WT TT
P3 1 0 1
P1 4 1 5
P4 5 5 10
P2 6 10 16
GANTT CHART:
____________________________________
P3 | P1 | P4 | P2
____________________________________
0 1 5 10 16
RESULT:
AIM:
To implement the priority scheduling and dislay the gantt
chart the priority used also completes average waiting time and
average turn around time.
ALGORITHM:
STEP 1: Get the no of processes with their arrival time , burst time
and priority.
STEP 2: If all the process arrival time is equal then sort the
process in increasing order of priority.
STEP 3: For each process calculate waiting time and turn around
time.
STEP 4: If process have different arrival time the schedule the
process which arrived first.
STEP 5: Check every milliseconds whether there is any new
process with higher priority.
STEP 6: Schedule the new process in the CPU and reduce the
burst time of previous process by its executed
milliseconds .
PROGRAM:
#include<stdio.h>
int main()
int n,i,j,temp,temp1,temp2,b[10],t[10],w[10],p[10],pr[10];
float att=0.0,awt=0.0;
for(i=0;i<10;i++)
b[i]=0;
w[i]=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
p[i]=i+1;
for(i=0;i<n;i++)
scanf("%d",&pr[i]);
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(pr[i]>pr[j])
temp=b[i];
temp1=p[i];
temp2=pr[i];
b[i]=b[j];
p[i]=p[j];
pr[i]=pr[j];
b[j]=temp;
p[j]=temp1;
pr[j]=temp2;
}}}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
t[i]=w[i]+b[i];
awt=awt+w[i];
att=att+t[i];
awt=awt/n;
att=att/n;
for(i=0;i<n;i++)
printf("\n\tP%d\t\t%d\t\t%d\n",p[i],w[i],t[i]);
printf("AVG WT is %f",awt);
printf("\nAVGTTis %f",att);
printf("\n\nGANTT Chart");
printf("\n_________________________________________\n");
for(i=0;i<n;i++)
printf("\tP%d\t",p[i]);
printf("\t\n");
printf("\n___________________________________________\n");
for(i=0;i<n;i++)
printf("%d\t\t",w[i]);
printf("%d\t\t",t[n-1]);
printf("\n___________________________________________\n");
printf("\n");
OUTPUT:
[cse023@localhost ~]$ cc priority.c
P4 0 2
P2 2 6
P1 6 9
P3 9 10
AVG WT is 4.250000
GANTT Chart
____________________________________
P4 | P2 | P1 | P3
____________________________________
0 2 6 9 10
RESULT:
Thus the program to implement the priority scheduling and display the
Gantt chart the priority used also completes average waiting time and av-
erage turnaround time is executed successfully and the output is verified.
EX NO:5d
ROUND ROBIN SCHEDULING
ALGORITHM
DATE:
AIM:
To implement round robin scheduling to display the Gantt using
CPU burst time and arrival time.
ALGORITHM:
STEP 1: Get the no. of processes, with their time slice , arrival time
and burst time.
STEP 2: Sort the process in the increasing order of arrival time.
STEP 3: For each process check if the burst time of process is lesser
than time slice, then alter completion schedule the next process.
STEP 4: Else alter time slice expires , schedule the next process.
STEP 5: Calculate waiting time and turn around time of each
process.
STEP 6: Calculate average waiting time and turn around time.
STEP 7: Display the output.
PROGRAM:
#include<stdio.h>
main()
struct
int p,bt,rm,flag;
rr[10];
int slice,i,k,n,g[100],cnt=0,pn[100],to,from,sum=0;
float await=0.0,aturn=0.0;
scanf("%d",&n);
scanf("%d",&slice);
for(i=0;i<n;i++)
scanf("%d",&rr[i].bt);
wait[i]=turn[i]=occ[i]=0;
rr[i].flag=0;
rr[i].rm=rr[i].bt;
sum=sum+rr[i].bt;
k=i=0;
printf("\nFrom\tTO\tProcess\n");
while(k<sum)
from=k;
if(rr[i].rm>0)
if(rr[i].rm<slice)
to=from+rr[i].rm;
else
to=from+slice;
rr[i].rm=rr[i].rm-(to-from);
k=to;
printf("\n%d\t%d\t%d\n",from,to,i+1);
if((rr[i].rm==0)&&(rr[i].flag==0))
rr[i].flag=1;
wait[i]=from-occ[i];
turn[i]=to;
else
occ[i]=occ[i]+(to-from);
i++;
if(i==n)
i=0;
printf("\n\tProcess\tBurst_time\tWaiting_time\tTurn_around_time");
for(i=0;i<n;i++)
printf("\n\tP%d\t\t%d\t\t%d\t\t%d",i+1,rr[i].bt,wait[i],turn[i]);
twait=twait+wait[i];
tturn=tturn+turn[i];
printf("\n");
printf("\n_____________________________________________________\n");
await=(float)twait/n;
aturn=(float)tturn/n;
OUTPUT:
[oslab@localhost~]$ cc roundrobin.c
[oslab@localhost~]$ ./a.out
From TO Process
0 4 1
4 6 2
6 10 3
10 14 4
14 16 1
16 17 3
17 21 4
Process Burst_timeWaiting_timeTurn_around_time
P1 6 10 16
P2 2 4 6
P3 5 12 17
P4 8 13 21
RESULT:
EX NO:6
PRODUCER CONSUMER PROBLEM USING
SEMAPHORE
DATE:
AIM:
ALGORITHM:
STEP 1: Initialize the semaphore mutex, buffer full
and buffer empty.
STEP 2: In producer process, it produces an item in to the buffer.
STEP 3: If buffer is empty, then check the mutex value to enter the
critical section.
STEP 4: If the mutex value is 0, allow the producer to add value in
the temporary variable to buffer.
STEP 5: In consumer process, it should wait if buffer is empty.
STEP 6: If the buffer has item then check the mutex value, if it is
zero remove item from buffer.
STEP 7: Consume the item.
STEP 8: Print the item.
STEP 9: Stop.
PROGRAM:
#include<stdio.h>
#defineBsize 10
void produce();
void consumer();
void producer();
inta,b,c,d;
inti,j,k;
intbuf[Bsize],consumed[10];
struct pro
intitem_no;
item[10];
int main()
do
if(j==Bsize-i)
i=0;
j=0;
produce();
consumer();
else
producer();
consumer();
scanf("%d",&a);
}while(a==1);
void producer()
for(i=0;i<Bsize;i++)
if(buf[i]!='\0')
continue;
else
b=Bsize-i;
scanf("%d",&c);
if(c==1)
produce();
else
break;
void produce()
scanf("%d",&d);
while(j<i+d)
printf("\nItem.no %d",j+1);
scanf("%d",&item[j].item_no);
j++;
for(k=0;k<Bsize;k++)
buf[k]=item[k].item_no;
void consumer()
for(j=0;j<i;j++)
consumed[j]=buf[j];
buf[j]=buf[j-1];
item[j].item_no=0;
j=0;
OUTPUT:
[oslab@localhost~]$ cc semaphore.c
[oslab@localhost~]$ ./a.out
Item.no 1 7
Item.no 2 8
Item.no 3 17
Buffer content is 7
Buffer content is 8
Buffer content is 17
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Item.no 4 12
Item.no 5 3
Buffer content is 7
Buffer content is 8
Buffer content is 17
Buffer content is 12
Buffer content is 3
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Consumed item is 7
Consumed item is 8
Consumed item is 17
Consumed item is 12
Consumed item is 3
Item.no 1 7
Item.no 2 8
Buffer content is 7
Buffer content is 8
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Consumed item is 7
Consumed item is 8
RESULT:
AIM:
To write a c program for implementing a shared memory IPC.
ALGORITHM:
STEP 1: Declare an array to store the string.
STEP 2: Declare an array with two element for pipe descriptors.
STEP 3: Enter the string to store in the pipe.
STEP 4: Create pipe on file descriptor using pipe function call.
STEP 5: Using fork system call , create a child process.
STEP 6: Parent process writes the string from array on to the pipe
using write system call.
STEP 7: The child process retrieve the content from the pipe to the
array using read system call.
PROGRAM:
#include<stdio.h>
int main(){
int fd[2],child;
char buf[30];
scanf("%s",buf);
pipe(fd);
child=fork();
if(!child) {
close(fd[0]);
write(fd[1],buf,5);
wait(0);}
else{
close(fd[1]);
read(fd[0],buf,5);
return (0);
OUTPUT:
RESULT:
EX NO:8
BANKERS ALGORITHM
DATE:
AIM:
ALGORITHM:
STEP 1: Declare an element to store the resources and processes.
STEP 2: Get the total number of resources.
STEP 3: Get the maximum resources to be allocated.
STEP 4: Check the condition for each process.
STEP 5: Print the result.
STEP 6: Stop.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
int work[5],av[5],alloc[10][10],l,n,m,i,j,k;
int need[10][10],avail[10],max[10][10],count,fcount=0,pr[10];
char finish[10]={'f','f','f','f','f','f','f','f','f','f'};
scanf("%d",&n);
scanf("%d",&m);
for(i=1;i<=m;i++)
scanf("%d",&avail[i]);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&max[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&alloc[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
need[i][j]=max[i][j]-alloc[i][j];
for(i=1;i<=n;i++)
k=0;
for(j=1;j<=m;j++)
k=k+alloc[i][j];
av[i]=av[i]-k;
work[i]=av[i];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
count=0;
for(j=1;j<=m;j++)
if((finish[j]=='f')&&(need[i][j]<=work[i]))
count++;
if(count==m)
for(l=1;l<=m;l++)
work[l]=work[l]+alloc[i][l];
finish[i]='f';
pr[i]=i;
break;
for(i=1;i<=n;i++)
if(finish[i]=='f')
fcount++;
if(fcount==n)
for(i=1;i<=n;i++)
printf("\n%d\n",pr[i]);
else
OUTPUT:
[oslab@localhost~]$ cc dlavoid.c
[oslab@localhost~]$ ./a.out
AIM:
To determine whether the process and their request for resources are
in a deadlocked state.
ALGORITHM:
STEP 1: Declare an element to store the resources and processes.
STEP 2: Get the total number of resources.
STEP 3: Get the maximum resources to be allocated.
STEP 4: Check the condition for each process.
STEP 5: Print the result.
STEP 6: Stop.
PROGRAM:
#include<stdio.h>
int main()
int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1;
int m[5],r[5],a[5],temp[5],sum=0;
scanf("%d",&tp);
scanf("%d",&tr);
printf("\nEnter chain(Max.Need)matrix");
for(i=0;i<tp;i++)
printf("\nProcess %d",i+1);
for(j=0;j<tr;j++)
scanf("%d",&c[i][j]);
for(i=0;i<tp;i++)
printf("\nProcess %d",i+1);
for(j=0;j<tr;j++)
scanf("%d",&p[i][j]);
for(i=0;i<tr;i++)
scanf("%d",&r[i]);
for(i=0;i<tr;i++)
scanf("%d",&a[i]);
temp[i]=a[i];
for(i=0;i<tp;i++)
sum=0;
for(j=0;j<tr;j++)
sum+=p[i][j];
if(sum==0)
m[k]=i;
k++;
for(i=0;i<tp;i++)
for(l=0;l<k;l++)
if(i!=m[l])
flag=1;
for(j=0;j<tr;j++)
if(c[i][j]<temp[j])
flag=0;
break;
if(flag==1)
m[k]=i;
k++;
for(j=0;j<tr;j++)
temp[j]+=p[i][j];
for(j=0;j<tp;j++)
found=0;
for(i=0;i<k;i++)
if(j==m[i])
found=1;
if(found==0)
printf("\n%d\t",j+1);
OUTPUT:
[oslab@localhost~]$ cc dldetect.c
[oslab@localhost~]$ ./a.out
Enter chain(Max.Need)matrix
Process 1
0 1 0 0 1
Process 2
0 0 1 0 1
Process 3
0 0 0 0 1
Process 4
1 0 1 0 1
Process 1
1 0 1 1 0
Process 2
1 1 0 0 0
Process 3
0 0 0 1 0
Process 4
0 0 0 0 0
2 1 1 2 1
0 0 0 0 1
2 3
RESULT:
EX NO:10
IMPLEMENTATION OF DINNING
PHILOSOPHER PROBLEM
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
sem_t mutex;
sem_t S[N];
void take_fork(int);
void put_fork(int);
void test(int);
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
int i;
pthread_tthread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
while(1)
int *i=num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
sem_wait(&mutex);
state[ph_num]=HUNGRY;
printf("philospher %d is hungry\n",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
if(state[ph_num]==HUNGRY&&state[LEFT]!=EATING&&state[RIGHT]!=
EATING)
state[ph_num]=EATING;
sleep(2);
printf("philospher %d is eating\n",ph_num+1);
sem_post(&S[ph_num]);}}
sem_wait(&mutex);
state[ph_num]=THINKING;
printf("philospher %d is thinking\n",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
OUTPUT:
[cse023@localhost ~]$gcc -o c ts.c –pthread
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 is hungry
Philosopher 1 is Eating
Philosopher 4 is hungry
Philosopher 4 is Eating
Philosopher 2 is hungry
Philosopher 3 is hungry
Philosopher 5 is hungry
Philosopher 1 is thinking
Philosopher 2 is Eating
Philosopher 4 is thinking
Philosopher 5 is Eating
RESULT:
AIM:
To implement the first fit, worst fit , best fit algorithm
using c program.
ALGORITHM
STEP 1: Start.
STEP 2: Read the number of partitions and the partition size.
STEP 3: Read the number of processes and their sizes.
STEP 4: Check if the strategy is first fit.
STEP 5: Check if the strategy is best fit;
STEP 6: Check if the strategy is worst fit.
STEP 7: Stop
PROGRAM
#include<stdio.h>
void main()
int i,j,temp,b[10],c[10],arr,n,ch,a;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
c[i]=b[i];
scanf("%d",&arr);
scanf("%d",&ch);
switch(ch)
case 1:
for(i=1;i<=n;i++)
if(b[i]>=arr)
printf("\t\t%d",arr);
break;
else
printf("%d",b[i]);
continue;
break;
case 2:
for(i=1;i<=n;i++)
if(b[i]>=b[i+1])
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
for(i=1;i<=n;i++)
if(b[i]>=arr)
a=b[i];
break;
else
printf("%d",b[i]);
continue;
for(i=1;i<=n;i++)
if(c[i]==a)
printf("\t\t%d",arr);
break;
case 3:
for(i=1;i<=n;i++)
if(b[i]>=b[i+1])
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
for(i=1;i<n;i++)
printf(" %d",b[i]);
printf("\t%d",arr);
break;
default:
OUTPUT:
RESULT:
Thus the program to implement the first fit ,worst fit,best fit algo-
rithm using c program is executed successfully and the output is
verified.
EX NO:12
MEMORY MANAGEMENT-
PAGING TECHINIQUE
DATE:
AIM:
ALGORITHM
STEP 1: Start.
STEP 2: Read the logical memory address
STEP 3: Read the page table along with the offset and page frame
STEP 4: Calculate the physical address
STEP 5: Print the physical address for the corresponding logical
Address
STEP 6: Stop
PROGRAM:
#include<stdio.h>
int main(){
int i,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf;
scanf("%d",&phymem);
for(i=20,j=0;i<phymem+20,j<phymem;i++,j++)
arr[i]=j;
scanf("%d",&fs);
nf=phymem/fs;
for(i=0;i<nf;i++)
scanf("%d",&pt[i]);
val=(fs*pt[pgno])+offset;
OUTPUT:
10
RESULT:
Thus the program for memory management of paging techniques is executed successfully and the
output is verified.
EX NO:13a
AIM:
To write a c program to implementation of FIFO page re-
placement algorithm using LINUX.
ALGORITHM:
STEP 1: Start.
STEP 2: Declare the frame size
STEP 3: Make the frames to be empty
STEP 4: The reference is bought into the frames in FIFO manner.
STEP 5: Select the page which the frame are occurs in the first
STEP 6: Repeat the above process until all reference is allocated
STEP 7: Stop
PROGRAM:
#include <stdio.h>
int main ()
int i,f,r,s,count,flag,num,psize;
f=0;
r=0;
s=0;
flag=0;
count=0;
scanf ("%d",&n);
for (i=0;i<n;i++)
scanf ("%d",&psize);
for (i=0;i<psize;i++)
fr[i]=-1;
while(s <n)
flag=0;
num=pg[s];
for (i=0;i<psize;i++)
if (num==fr [i])
s++;
flag=1;
break;
if (flag==0)
if (r <psize)
fr [r]=pg [s];
r++;
s++;
count++;
else
if (f <psize)
fr [f]=pg [s];
r++;
f++;
count++;
else
f=0;
printf ("\n");
for (i=0;i<psize;i++)
printf("%d\t",fr [i]);
getchar ();
OUTPUT:
[cse023@localhost~] $ cc fifopg.c
[cse023@localhost~] $ ./a.out
Enter sequence:
1
3
2
4
2
3
5
4
1
3
Enter size of page frame: 3
1 -1 -1
1 3 -1
1 3 2
4 3 2
4 3 2
4 3 2
4 5 2
4 5 2
4 5 1
4 5 1
4 5 1
page faults= 7
RESULT:
AIM:
To write a program to implementation LRU page replace-
ment algorithm using LINUX.
ALGORITHM:
STEP 1: Start.
STEP 2: Declare the frame size
STEP 3: All the frames is brought into the frames one by one
STEP 4: Select the page which is not recently used
STEP 5: Repeat the above process until all reference is allocated
STEP 6: Stop
PROGRAM:
#include<stdio.h>
main()
int q[20],p[50],c=0,cl,d,f,i,j,k=0,n,r,t,b[20],c2[20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
cl=0;
for(j=0;j<f;j++)
if(p[i]!=q[j])
cl++;
if(cl==f)
c++;
if(k<f)
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
else
for(r=0;r<f;r++)
c2[r]=0;
for(j=i-1;j<n;j--)
if(q[r]!=p[j])
c2[r]++;
else
break;
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
for(j=r;j<f;j++)
if(b[r]<b[j])
t=b[r];
b[r]=b[j];
b[j]=t;
}}}
for(r=0;r<f;r++)
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
printf("\n");
OUTPUT:
RESULT:
EX NO:13c
OPTIMAL PAGE REPLACEMENT
ALGORITHM
DATE:
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Take the input of pages as an array.
STEP 3: Look for the page allocated is present in near future, if no
then replace the page in the memory with new page .
STEP 4: If page already present increment hit, else increment miss.
STEP 5: Repeat till we reach the last element of the array.
STEP 6: Print the number of hits and misses.
STEP 7: Stop
PROGRAM:
#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
int main()
printf("\n................");
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n...........");
for(i=0;i<nor;i++)
printf("\n%4d",ref[i]);
for(i=0;i<nof;i++)
frm[i]=-1;
optcal[i]=0;
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
flag=0;
printf("\n\tref no %d->\t",ref[i]);
for(j=0;j<nof;j++)
if(frm[j]==ref[i])
flag=1;
break;
if(flag==0)
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
int i,j,temp,notfound;
for(i=0;i<nof;i++)
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
notfound=0;
optcal[i]=j;
break;
if(notfound==1)
return i;
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp==frm[i]])
return i;
return 0;
OUTPUT:
.....................................................................................
3
OPTIMAL PAGE REPLACEMENT ALGORITHM
......................................................................................
The given string
...........................
1 3 2 4 2 3 5 4 1 3
ref no 1->1 -1 -1
ref no 3-> 1 3 -1
ref no 2-> 1 3 2
ref no 4-> 4 3 2
ref no 2->
ref no 3->
ref no 5-> 4 3 5
ref no 4->
ref no 1-> 1 3 5
ref no 3->
Number of page faults:6
RESULT:
EX NO:14a
SINGLE LEVEL DIRECTORY
DATE:
AIM:
To write a c program for single level directory.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#definesx 250
#definesy 200
#define bl 30
#definebw 15
#define sy1 250
int sx1=100;
int n,m[10];
char a[10][10];
int cols[]={2,5,1,3,4,6,8,7,9};
typedef struct
{
char name[10];
struct
{
char file[10];
}k[10];
}file_info;
file_infof[10];
void readfiles()
{
int i,j;
printf("enter the no of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the level %d directory:",i+1);
scanf("%s",&f[i].name);
printf("\nenter the no of subdiretories:");
scanf("%d",&m[i]);
for(j=0;j<m[i];j++)
{
printf("\nenter level %d filename:",j+1);
scanf("%s",&f[i].k[j].file);
}
}
}
void fillspace()
{
int x1,y1,i,j;
outtextxy(70,205,"master directory");
outtextxy(10,255,"userfiles");
for(i=0;i<n;i++)
{
setfillstyle(1,cols[i]);
x1=sx+(i%10)*50;
y1=sy+(i/10)*40;
bar(x1,y1,x1+bl+20,y1+bw);
outtextxy(x1+bl/4,y1+bw/4,f[i].name);
line(x1+bl/2,y1+bw,sx1+50,sy1);
sx1=sx1+50;
for(j=0;j<m[i];j++)
{
setfillstyle(1,cols[i]);
x1=sx1+(j%10)*40;
y1=sy1+(j/10)*40;
bar(x1,y1,x1+bl+10,y1+bw);
outtextxy(x1+bl/4,y1+bw/4,f[i].k[j].file);
line(x1+bl/2,y1+bw,x1+bl/2,y1+50);
circle(x1+bl/2,y1+60,7);
}
if(i==0)
{
line(x1+bl,y1+bw,sx1+160,sy1+60);
}
sx1=sx1+100;
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
readfiles();
clrscr();
fillspace();
closegraph();}
OUTPUT:
RESULT:
EX NO:14b
DATE:
AIM:
To write a c program for two level directory.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define SX 250
#define SY 200
#define BL 30
#define BW 15
#define SY1 250
int SX1=100;
int SW1=100;
int n,m[10];
char a[10][10];
int cols[]={2,5,1,3,4,6,7,8,9};
typedef struct
{
char name[10];
struct
{
char file[10];
}k[10];
}file_info;
file_infof[10];
void readfiles()
{
int i,j;
printf("\nenter the no of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the level %d directory:",i+1);
scanf("%s",&f[i].name);
printf("\n enter the no of subdirectories:");
scanf("%d",&m[i]);
for(j=0;j<m[i];j++)
{
printf("\n enter level %d filename:",j+1);
scanf("%s",&f[i].k[j].file);
}
}
}
void fillspace()
{
int X1,Y1,i,j;
outtextxy(70,205,"master directory");
outtextxy(10,255,"userfiles");
for(i=0;i<n;i++)
{
setfillstyle(1,cols[i]);
X1=SX+(i%10)*50;
Y1=SY+(i/10)*40;
bar(X1,Y1,X1+BL+20,Y1+BW);
outtextxy(X1+BL/4,Y1+BW/4,f[i].name);
line(X1+BL/2,Y1+BL,SW1+50,SY1);
SX1=SX1+50;
for(j=0;j<m[i];j++)
{
setfillstyle(1,cols[i]);
X1=SX1+(j%10)*40;
Y1=SY1+(j/10)*40;
bar(X1,Y1,X1+BL+10,Y1+BW);
outtextxy(X1+BL/4,Y1+BW/4,f[i].k[j].file);
line(X1+BL/2,Y1+BW,X1+BL/2,Y1+50);
circle(X1+BL/2,Y1+60,7);
}
if(i==0)
{
line(X1+BL,Y1+BW,SX1+160,SY1+60);
}
SX1=SX1+100;
}}
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
readfiles();
clrscr();
fillspace();
getch();closegraph();}
OUTPUT:
RESULT:
EX NO:14c
HIERARCHICAL DIRECTORY
DATE:
AIM:
To write a c program for hierarchical directory.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
struct tree_ele
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_ele *link[5];
};
typedef struct tree_ele node;
void main()
{
int gm,root,gd=DETECT;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);
getch();
closegraph();
}
create(node **root,intlev,char *dname,intlx,intrx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("\n enter name of dir(file(under%s):",dname);
fflush(stdin);
gets((*root)->name);
printf("\n enter 1 for dir/2 for file:");
scanf("%ds",&(*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("\n no. ofsubdir/file(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;
}
return;
}
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]);
}}
return;}
OUTPUT:
RESULT:
EX NO:14d
DIRECTED ACYCLIC GRAPH
DATE:
AIM:
To write a c program to implement directed acyclic directory in LINUX.
ALGORITHM:
STEP1: Read the name of the root directory.
STEP2: Create user define file
STEP3: Link to be created
STEP 4: Print
STEP 5: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
struct tree_ele
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_ele *link[5];
};
typedef struct tree_ele node;
typedef struct
{
char from[20];
char to[20];
}link;
link L[10];
int nof1;
node *root;
void main()
{
int gm,gd=DETECT;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("\n how many links?");
scanf("%d",&nof1);
for(i=0;i<nof1;i++)
{
printf("file/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name");
fflush(stdin);
gets(L[i].to);
}
return;
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nof1;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
return;
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmp(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
return;
}
create(node **root,intlev,char *dname,intlx,intrx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("\n enter name of dir(file(under%s):",dname);
fflush(stdin);
gets((*root)->name);
printf("\n enter 1 for dir/2 for file:");
scanf("%ds",&(*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("\n no. ofsubdir/file(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;
}
return;
}
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]);
}
}
return;
}
OUTPUT:
file/dir:USER1
RESULT:
EX NO:15a
SEQUENTIAL FILE ALLOCATION
STRATERGY
DATE:
AIM:
ALGORITHM:
STEP2: Get the input for the starting block and length of the file
STEP3: If the block is not used, then it is allocated for the new one
STEP5: Stop
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main()
int f[50],i,st,j,len,c,k;
for(i=0;i<50;i++)
f[i]=0;
X:
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
f[j]=1;
printf("\n%d->%d",j,f[j]);
else
break;
if(j==(st+len))
scanf("%d",&c);
if(c==1)
goto X;
OUTPUT:
[oslab@localhost~]$ cc seqfilealloc.c
[oslab@localhost~]$ ./a.out
5->1
6->1
7->1
8->1
9->1
10->1
Thus the program to implement the sequential file allocation using LINUX
file is executed successfully and the output is verified.
EX NO:15b
LINKED FILE ALLOCATION
STRATERGY
DATE:
AIM:
To implement the index file allocation using c - program on LINUX.
ALGORITHM:
STEP3: If entry any file in the block, if the file is exits, print the file in
already allocated
STEP 4: Stop
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main()
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++)
f[i]=0;
scanf("%d",&p);
for(i=0;i<p;i++)
scanf("%d",&a);
f[a]=1;
X:
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
if(f[j]==0)
f[j]=1;
printf("\n%d->%d",j,f[j]);
else
k++;
scanf("%d",&c);
if(c==1)
goto X;
OUTPUT:
[oslab@localhost~]$ cc linkedfilealloc.c
[oslab@localhost~]$ ./a.out
Enter how many blocks that are already alloated
3
Enter the block no. that are already allocated
467
Enter the starting index block & length
2 4
2->1
3->1
4->file is already allocated
5->1
6->file is already allocated
7->file is already allocated
8->1
If you want to enter one or more file?(yes-1/no-0)1
Enter the starting index block & length
8 2
8->file is already allocated
9->1
10->1
If you want to enter one or more file?(yes-1/no-0)0
RESULT:
EX NO:15c
INDEXED FILE ALLOCATION STRATERGY
DATE:
AIM:
To implement the linked file allocation using c - program.
ALGORITHM:
STEP2: Read the number of files on index that are to be allocated of the
index is empty , the file is allocated the file is allocated to the index is
already allocated
STEP4: Stop
PROGRAM:
#include<stdio.h>
int f[50],i,j,k,inde[50],n,c,count=0,p;
main()
for(i=0;i<50;i++)
f[i]=0;
X:
scanf("%d",&p);
if(f[p]==0)
f[p]=1;
scanf("%d",&n);
else
goto X;
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]==1])
goto X;
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\nAllocated");
printf("\nFile indexed");
for(k=0;k<n;k++)
printf("\n%d->%d=%d",p,inde[k],f[inde[k]]);
scanf("%d",&c);
if(c==1)
goto X;
OUTPUT:
[oslab@localhost~]$ cc indexfilealloc.c
[oslab@localhost~]$ ./a.out
Allocated
File indexed
7->6=1
7->7=1
7->8=1
7->17=1
7->2=1
RESULT: