Os Lab Manual Aiml
Os Lab Manual Aiml
Os Lab Manual Aiml
LABORATORY MANUAL
Objectives
To develop academic, professional and financial alliances with the industry as well
as the academia at national and transnational levels
To develop academic, professional and financial alliances with the industry as well
as the academia at national and transnational levels.
To cultivate strong community relationships and involve the students and the staff in
local community service.
To constantly enhance the value of the educational inputs with the participation of
students, faculty, parents and industry.
Vision
Mission
To keep pace with advancements in knowledge and make the students competitive and
capable at the global level.
To create an environment for the students to acquire the right physical,
intellectual, emotional and moral foundations and shine as torch bearers of
tomorrow’s society.
To strive to attain ever-higher benchmarks of educational excellence.
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING AND
• To impart technical education in the field of data science of excellent quality with a high
level of professional competence, social responsibility, and global awareness among the
students
Mission
• To impart technical education that is up to date, relevant and makes students competitive
and employable at global level
• To provide technical education with a high sense of discipline, social relevance in an
intellectually, ethically and socially challenging environment for better tomorrow
• Educate to the global standards with a benchmark of excellence and to kindle the spirit of
innovation.
Program Outcomes(PO)
Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
Design/development of solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions.
Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice
Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PSO2: Apply data science concepts and algorithms to solve real world problems of the
society
PSO3: Apply data science techniques in the various domains like agriculture, education
healthcare for better society
PEO1: Develop cutting-edge skills in data science and its related technologies, such as machine
learning, predictive analytic, and data engineering.
PEO2: Design and develop data-driven solutions to real-world problems in a business, research,
or social environment.
PEO3: Apply data engineering and data visualization techniques to discover, investigate, and
interpret data.
PEO5: Integrate fields within computer science, optimization, and statistics to develop better
solutions
Sl.NO Experiments
1 Develop a c program to implement the Process system calls (fork (), exec(), wait(),
create process(), terminate process())
2 Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.
Perform a case study by installing and exploring various types of operating systems on a physical
or logical (virtual) machine. (Linux Installation). Instructions to Install Ubuntu Linux 18.04
(LTS) along with Windows Back Up Your Existing Data!
This is highly recommended that you should take backup of your entire data before start with
the installation process. Obtaining System Installation Media
Before booting the installation system, one need to change the boot order and set CD-ROM
as first boot device.
As your computer starts, press the DEL-F10, HP-F9 during the initial startup screen.
Depending on the BIOS manufacturer, a menu may appear. However, consult the hardware
documentation for the exact key strokes.
Beneath the installation-type question are two checkboxes; one to enable updates while installing
and another to enable third-party software.
Stay connected to the internet so you can get the latest updates while you install Ubuntu.
DEPT OF CSE-DS,ATME,MYSURU 1
OPERATING SYSTEMS LABORATORY BCS303
If you are not connected to the internet, you will be asked to select a wireless network,
if available. We advise you to connect during the installation so we can ensure your
machine is up to date
Use the checkboxes to choose whether you’d like to install Ubuntu alongside another operating
system, delete your existing operating system and replace it with Ubuntu, or — if you’re an
advanced user — choose the ’Something else’ option.
DEPT OF CSE-DS,ATME,MYSURU 2
OPERATING SYSTEMS LABORATORY BCS303
Begin installation
After configuring storage, click on the ‘Install Now’ button. A small pane will appear with an
overview of the storage options you’ve chosen, with the chance to go back if the details are
incorrect.
Click Continue to fix those changes in place and start the installation process.
DEPT OF CSE-DS,ATME,MYSURU 3
OPERATING SYSTEMS LABORATORY BCS303
If you are connected to the internet, your location will be detected automatically. Check
your location is correct and click ’Forward’ to proceed.
If you’re unsure of your time zone, type the name of a local town or city or use the map to
select your location.
DEPT OF CSE-DS,ATME,MYSURU 4
OPERATING SYSTEMS LABORATORY BCS303
Positive: If you’re having problems connecting to the Internet, use the menu in the top-right-
hand corner to select a network.
Login details
Enter your name and the installer will automatically suggest a computer name and username.
These can easily be changed if you prefer. The computer name is how your computer will appear
on the network, while your username will be your login and account name.
Next, enter a strong password. The installer will let you know if it’s too weak.
You can also choose to enable automatic login and home folder encryption. If your machine is
portable, we recommend keeping automatic login disabled and enabling encryption. This should
stop people accessing your personal files if the machine is lost or stolen.
DEPT OF CSE-DS,ATME,MYSURU 5
OPERATING SYSTEMS LABORATORY BCS303
If you enable home folder encryption and you forget your password, you won’t be able
to retrieve any personal data stored in your home folder.
Background installation
The installer will now complete in the background while the installation window teaches you a
little about how awesome Ubuntu is. Depending on the speed of your machine and network
connection, installation should only take a few minutes.
DEPT OF CSE-DS,ATME,MYSURU 6
OPERATING SYSTEMS LABORATORY BCS303
Installation complete
After everything has been installed and configured, a small window will appear asking you to
restart your machine. Click on Restart Now and remove either the DVD or USB flash drive when
prompted. If you initiated the installation while testing the desktop, you also get the option to
continue testing.
DEPT OF CSE-DS,ATME,MYSURU 7
OPERATING SYSTEMS LABORATORY BCS303
Congratulations! You have successfully installed the world’s most popular Linux operating
system!
DEPT OF CSE-DS,ATME,MYSURU 8
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 1:
Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process, terminate process)
System Calls:
The interface between a process and an operating system is provided by system calls. In general,
system calls are available as assembly language instructions. They are also included in the
manuals used by the assembly level programmers. System calls are usually made when a process
in user mode requires access to a resource. Then it requests the kernel to provide the resource via
a system call.
If a file system requires the creation or deletion of files. Reading and writing from files
also require a system call.
Network connections also require system calls. This includes sending and receiving packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call
DEPT OF CSE-DS,ATME,MYSURU 9
OPERATING SYSTEMS LABORATORY BCS303
There are mainly five types of system calls. These are explained in detail as follows −
Process Control
These system calls deal with processes such as process creation, process termination etc.
File Management
These system calls are responsible for file manipulation such as creating a file, reading a file,
writing into a file etc.
Device Management
These system calls are responsible for device manipulation such as reading from device buffers,
writing into device buffers etc.
Information Maintenance
These system calls handle information and its transfer between the operating system and the user
program.
Communication
These system calls are useful for interprocess communication. They also deal with creating
and deleting a communication connection.
fork ( )
Used to create new process. The new process consists of a copy of the address space of the
original process. The value of process id for the child process is zero, whereas the value of
process id for the parent is an integer value greater than zero.
Syntax: fork ( );
DEPT OF CSE-DS,ATME,MYSURU 10
OPERATING SYSTEMS LABORATORY BCS303
wait ( )
The parent waits for the child process to complete using the wait system call. The wait
system call returns the process identifier of a terminated child, so that the parent can tell
which of its possibly many children has terminated.
exit ( )
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit system call. At that point, the process may return data
(output) to its parent process (via the wait system call).
DESCRIPTION:
Used to create new processes. The new process consists of a copy of the address space of the
original process. The value of process id for the child process is zero, whereas the value of
process id for the parent is an integer value greater than zero.
Syntax: Fork ( );
DEPT OF CSE-DS,ATME,MYSURU 11
OPERATING SYSTEMS LABORATORY BCS303
ALGORITHM:
Step 4: If child id value is greater than zero then print as “i am in the parent process”.
Step 5: If child id! = 0 then using getpid() system call get the process id.
Step 6: Print “i am in the parent process” and print the process id.
Step 7: If child id! = 0 then using getppid() system call get the parent process id.
Step 8: Print “i am in the parent process” and print the parent process id.
Step 9: Else If child id value is less than zero then print as “i am in the child
process”. Step 10: If child id! = 0 then using getpid() system call get the process id.
Step 11: Print “i am in the child process” and print the process id.
Step 12: If child id! = 0 then using getppid() system call get the parent process id.
Step 13: Print “i am in the child process” and print the parent process id.
DEPT OF CSE-DS,ATME,MYSURU 12
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM :
SOURCE CODE:
#include<stdio.h>
#include <unistd.h>
#include<sys/types.h>
int main()
int id,childid;
id=getpid();
if((childid=fork())>0)
else
DEPT OF CSE-DS,ATME,MYSURU 13
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
$ vi fork.c
$ cc fork.c
$ ./a.out
RESULT:
ALGORITHM:
Step 3: Get the child id value using the system call fork ().
Step 4: If child id value is less than zero then print “fork failed”.
Step 5: Else if child id value is equal to zero, it is the id value of the child and then start the child
process to execute and perform Steps 7 & 8.
Step 7: Use a for loop for almost five child processes to be called.
Step 8: After execution of the for loop then print “child process
DEPT OF CSE-DS,ATME,MYSURU 14
OPERATING SYSTEMS LABORATORY BCS303
ends”.
DEPT OF CSE-DS,ATME,MYSURU 15
OPERATING SYSTEMS LABORATORY BCS303
Step 9: Execute the system call wait ( ) to make the parent to wait for the child process to
get over.
Step 10: Once the child processes are terminated, the parent terminates and hence prints “Parent
process ends”.
Step 11: After both the parent and the child processes get terminated it execute the wait ( )
system call to permanently get deleted from the OS.
SOURCE CODE:
/* wait system call */
#include <stdlib.h>
#include <errno.h>
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
pid_t pid;
int rv;
switch(pid=fork())
case -1:
perror("fork");
DEPT OF CSE-DS,ATME,MYSURU 16
OPERATING SYSTEMS LABORATORY BCS303
exit(1);
case 0:
fflush(stdout);
exit(rv);
default:
fflush(stdout);
wait(&rv);
fflush(stdout);
fflush(stdout);
%d\n",WEXITSTATUS(rv));
DEPT OF CSE-DS,ATME,MYSURU 17
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
$ vi wait.c
$ cc wait.c
$ ./a.out
10
Exec()
In execl() function, the parameters of the executable file is passed to the function as different
arguments. With execv(), you can pass all the parameters in a NULL terminated array argv. The
first element of the array should be the path of the executable file. Otherwise, execv() function
works just as execl() function.
DEPT OF CSE-DS,ATME,MYSURU 18
OPERATING SYSTEMS LABORATORY BCS303
ALGORITHM:
Step 3: Print execution of exec system call for the ls Unix command.
Step 4: Execute the execv function using the appropriate syntax for the Unix command ls.
Step 5: The list of all files and directories of the system is displayed.
PROGRAM :
SOURCE CODE:
#include<stdio.h>
#include<sys/types.h>
printf("before execv\n");
execv("/bin/ls",argv);
printf("after execv\n");
OUTPUT:
$ vi execv.c
$ cc execv.c
$ ./a.out
before execv
DEPT OF CSE-DS,ATME,MYSURU 19
OPERATING SYSTEMS LABORATORY BCS303
a1 aaa aaa.txt abc a.out b1 b2 comm.c db db1 demo2 dir1 direc.c execl.c execv.c f1.txt
RESULT:
DEPT OF CSE-DS,ATME,MYSURU 20
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 2:
Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.
Arrival Time : Time at which the process arrives in the ready queue.
Turn Around Time : Time Difference between completion time and arrival time.
Waiting Time(W.T) : Time Difference between turn around time and burst time.
A typical process involves both I/O time and CPU time. In a uniprogramming system like MS
DOS, time spent waiting for I/O is wasted and CPU is free during this time. In
multiprogramming systems, one process can use CPU while another is waiting for I/O. This is
possible only with process scheduling.
DEPT OF CSE-DS,ATME,MYSURU 21
OPERATING SYSTEMS LABORATORY BCS303
Max throughput [Number of processes that complete their execution per time unit]
First Come First Serve (FCFS): Simplest scheduling algorithm that schedules according to arrival
times of processes. First come first serve scheduling algorithm process that requests the CPU
first is allocated the CPU first. It is implemented by using the FIFO queue. When a process
enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is
allocated to the process at the head of the queue. The running process is then removed from the
queue. FCFS is a non – preemptive scheduling algorithm.
Shortest Job First (SJF): Process which has the shortest burst time is scheduled first. If two
processes have the same bust time then FCFS is used to break the tie. It is a non preemptive
scheduling algorithm
DESCRIPTION:
DEPT OF CSE-DS,ATME,MYSURU 22
OPERATING SYSTEMS LABORATORY BCS303
ALGORITHM:
Step 4: Initially, Waiting time of first process is zero and Total time for the first process is
Step 5: Calculate the Total time and Processing time for the remaining processes.
Step 6: Waiting time of one process is the Total time of the previous process.
DEPT OF CSE-DS,ATME,MYSURU 23
OPERATING SYSTEMS LABORATORY BCS303
Step 7: Total time of process is calculated by adding Waiting time and Service time.
Step 8: Total waiting time is calculated by adding the waiting time for lack process.
Step 9: Total turn around time is calculated by adding all total time of each process.
Step 10: Calculate Average waiting time by dividing the total waiting time by total number of
process.
Step 11: Calculate Average turn around time by dividing the total time by the number of process.
PROGRAM:
SOURCE CODE:
#include<stdio.h>
int main()
char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
scanf("%d",&n);
for(i=0;i<n;i++)
DEPT OF CSE-DS,ATME,MYSURU 24
OPERATING SYSTEMS LABORATORY BCS303
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
for(i=0;i<n;i++)
if(i==0)
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
printf("\nPName
for(i=0;i<n;i++)
DEPT OF CSE-DS,ATME,MYSURU 25
OPERATING SYSTEMS LABORATORY BCS303
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
OUTPUT:
$ cc fcfs.c
$ ./a.out
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 2 3
Enter the Process Name, Arrival Time & Burst Time: 2 5 6
Enter the Process Name, Arrival Time & Burst Time: 3 6 7
1 2 3 2 3 5
2 5 6 5 6 11
3 6 7 11 12 18
DEPT OF CSE-DS,ATME,MYSURU 26
OPERATING SYSTEMS LABORATORY BCS303
DESCRIPTION:
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
P0 0-0=0
P1 5-1=4
P2 14 - 2 = 12
P3 8-3=5
DEPT OF CSE-DS,ATME,MYSURU 27
OPERATING SYSTEMS LABORATORY BCS303
ALGORITHM:
Step 4: Initially the waiting time of first short process as 0 and total time of first short is process
the service time of that process.
Step 5: Calculate the total time and waiting time of remaining process.
Step 6: Waiting time of one process is the total time of the previous process.
Step 7: Total time of process is calculated by adding the waiting time and service time of
each process.
Step 8: Total waiting time calculated by adding the waiting time of each
process. Step 9: Total turn around time calculated by adding all total time of
each process.
Step 10: Calculate average waiting time by dividing the total waiting time by total number
of process.
Step 11: Calculate average turn around time by dividing the total waiting time by total number
of process.
DEPT OF CSE-DS,ATME,MYSURU 28
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM:
*/ #include<stdio.h>
#include<string.h>
Void main()
int i=0,pno[10],bt[10],n,wt[10],temp=0,j,tt[10];
float sum,at;
scanf("%d",&bt[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(bt[i]>bt[j])
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
DEPT OF CSE-DS,ATME,MYSURU 29
OPERATING SYSTEMS LABORATORY BCS303
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
}}}
wt[0]=0;
for(i=1;i<n;i++)
wt[i]=bt[i-1]+wt[i-1];
sum=sum+wt[i];
n"); for(i=0;i<n;i++)
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n p%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tt[i]);
printf("\n\n\t Average waiting time%f\n\t Average turn around time%f", sum, at);
DEPT OF CSE-DS,ATME,MYSURU 30
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
$ cc sjf.c
$ ./a.out
p0 1
p1 5
p2 2
p3 3
p4 4
p0 1 0 1
p1 2 1 3
p2 3 3 6
p3 4 6 10
p4 5 10 15
DEPT OF CSE-DS,ATME,MYSURU 31
OPERATING SYSTEMS LABORATORY BCS303
DESCRIPTION
ALGORITHM:
DEPT OF CSE-DS,ATME,MYSURU 32
OPERATING SYSTEMS LABORATORY BCS303
a) If the time quantum is greater than the remaining burst time then waiting time is
calculated as: a[i].waittime=count + tq.
b) Else if the time quantum is greater than the remaining burst time then waiting time is
calculated as: a[i].waittime=count - remaining burst time
Step 5: Calculate the average waiting time and average turnaround
time Step 6: Print the results of the step 4.
Step 7: Stop the program.
PROGRAM :
SOURCE CODE:
/* A program to simulate the Round Robin CPU scheduling algorithm */
#include<stdio.h>
struct process
{
int burst,wait,comp,f;
}p[20]={0,0};
int main()
{
int n,i,j,totalwait=0,totalturn=0,quantum,flag=1,time=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
printf("\nEnter The Quantum time (in ms)
:"); scanf("%d",&quantum);
for(i=0;i<n;i++)
{
printf("Enter The Burst Time (in ms) For Process #%2d
:",i+1); scanf("%d",&p[i].burst);
p[i].f=1;
}
printf("\nOrder Of Execution \n");
DEPT OF CSE-DS,ATME,MYSURU 33
OPERATING SYSTEMS LABORATORY BCS303
p[i].comp+=quantum;
}
else
{ p[i].wait=time-p[i].comp;
j=p[i].burst-p[i].comp;
p[i].comp=p[i].burst;
p[i].f=0;
}
printf("\nprocess # %-3d %-10d %-10d %-10d", i+1, time, time+j, p[i].burst-p[i].comp);
time+=j;
}
}
}
printf("\n\n ");
printf("\nProcess \t Waiting Time TurnAround Time
"); for(i=0;i<n;i++)
{
printf("\nProces%-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
totalwait=totalwait+p[i].wait;
DEPT OF CSE-DS,ATME,MYSURU 34
OPERATING SYSTEMS LABORATORY BCS303
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n-------------------");
printf("\nWaiting Time: %fms",totalwait/(float)n); p
rintf("\nTurnAround Time :
%fms\n\n",totalturn/(float)n); return 0;
}
OUTPUT:
$ cc rr.c
$ ./a.out
DEPT OF CSE-DS,ATME,MYSURU 35
OPERATING SYSTEMS LABORATORY BCS303
process # 3 55 60 34
process # 1 60 65 0
process # 2 65 70 5
process # 3 70 75 29
process # 2 75 80 0
process # 3 80 85 24
process # 3 85 90 19
process # 3 90 95 14
process # 3 95 100 9
Process # 3 100 105 4
process # 3 105 109 0
DEPT OF CSE-DS,ATME,MYSURU 36
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 3:
Develop a C program to simulate producer-consumer problem using semaphores.
Priority scheduling is a non-preemptive algorithm and one of the most common scheduling
algorithms in batch systems. Each process is assigned first arrival time (less arrival time process
first) if two processes have same arrival time, then compare to priorities (highest process first).
Also, if two processes have same priority then compare to process number (less process number
first). This process is repeated while all process get executed.
Implementation –
First input the processes with their arrival time, burst time and priority.
First process will schedule, which have the lowest arrival time, if two or more processes
will have lowest arrival time, then whoever has higher priority will schedule first.
Now further processes will be schedule according to the arrival time and priority of the
process. (Here we are assuming that lower the priority number having higher priority). If
two process priority are same then sort according to process number.
Note: In the question, They will clearly mention, which number will have higher priority
and which number will have lower priority.
Once all the processes have been arrived, we can schedule them based on their priority.
Consider the below table fo processes with their respective CPU burst times and the priorities.
DEPT OF CSE-DS,ATME,MYSURU 37
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM
SOURCE CODE
#include<stdio.h>
void main()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
DEPT OF CSE-DS,ATME,MYSURU 38
OPERATING SYSTEMS LABORATORY BCS303
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
OUTPUT:
$ cc rr.c
$ ./a.out
DEPT OF CSE-DS,ATME,MYSURU 39
OPERATING SYSTEMS LABORATORY BCS303
Process no 1 : 3 1
Process no 2 : 4 2
Process no 3 : 5 3
Process no 4 : 6 4
4 6 0 6 4
3 5 6 11 3
2 4 11 15 2
1 3 15 18 1
PROGRAM 3:
Develop a C program to simulate producer-consumer problem using semaphores.
DEPT OF CSE-DS,ATME,MYSURU 40
OPERATING SYSTEMS LABORATORY BCS303
Mutex
Mutex is used to achieve mutual exclusion. mutex ensures that at any particular time only the
producer or the consumer is accessing the buffer.
Mutex - mutex is a binary semaphore variable that has a value of 0 or 1.
We will use the Signal() and wait() operation in the above-mentioned semaphores to arrive at a
solution to the Producer-Consumer problem.
Signal() - The signal function increases the semaphore value by 1.
Wait() - The wait operation decreases the semaphore value by 1.
PROGRAM
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1; // Initializing the mutex variable with the value 1.
int full = 0; // Initializing the full variable with the value 0.
Int empty = 10, data = 0; // empty variable will store the number of empty slots in the buffer
{
--mutex; // decrementing the value of mutex
++full; // Increase the number of full slots
--empty; // decrementing the number of slots available
data++;// incrementing data which means that the data is produced
printf("\nProducer produces item number: %d\n", data);
++mutex; // incrementing the value of mutex
void consumer()// A function that will resemble the consumer's consumption of data
DEPT OF CSE-DS,ATME,MYSURU 41
OPERATING SYSTEMS LABORATORY BCS303
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item number: %d.\n", data);
data--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Enter 1 for Producer"
"\n2. Enter 2 for Consumer"
"\n3. Enter 3 to Exit");
{
case 1: if ((mutex == 1) && (empty != 0))
{
producer();
}
else
{
printf("The Buffer is full. New data cannot be produced!");
}
DEPT OF CSE-DS,ATME,MYSURU 42
OPERATING SYSTEMS LABORATORY BCS303
break;
case 2:
if ((mutex == 1) && (full != 0))
{
consumer();
}
else
{
printf("The Buffer is empty! New data cannot be consumed!");
}
break;
case 3:
exit(0);
break;
}
}
}
OUTPUT:
$ cc rr.c
$ ./a.out
DEPT OF CSE-DS,ATME,MYSURU 43
OPERATING SYSTEMS LABORATORY BCS303
DEPT OF CSE-DS,ATME,MYSURU 44
OPERATING SYSTEMS LABORATORY BCS303
DEPT OF CSE-DS,ATME,MYSURU 45
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 5:
Develop a C program which demonstrates interprocess communication between a reader
process and a writer process. Use mkfifo, open, read, write and close APIs in your
program
In computing, a named pipe (also known as a FIFO) is one of the methods for inter-process
communication.
DEPT OF CSE-DS,ATME,MYSURU 46
OPERATING SYSTEMS LABORATORY BCS303
this file.
DEPT OF CSE-DS,ATME,MYSURU 47
OPERATING SYSTEMS LABORATORY BCS303
FIFO special file is entered into the filesystem by calling mkfifo() in C. Once we have
created a FIFO special file in this way, any process can open it for reading or writing, in
the same way as an ordinary file. However, it has to be open at both ends simultaneously
before you can proceed to do any input or output operations on it.
Creating a FIFO file: In order to create a FIFO file, a function calls i.e. mkfifo is used.
mkfifo() makes a FIFO special file with name pathname. Here mode specifies the FIFO’s
permissions. It is modified by the process’s umask in the usual way: the permissions of the
created file are (mode & ~umask).
Using FIFO: As named pipe(FIFO) is a kind of file, we can use all the system calls associated
with it i.e. open, read, write, close.
Example Programs to illustrate the named pipe: There are two programs that use the same
FIFO. Program 1 writes first, then reads. The program 2 reads first, then writes. They both keep
doing it until terminated.
P1:Write First
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];
DEPT OF CSE-DS,ATME,MYSURU 48
OPERATING SYSTEMS LABORATORY BCS303
while (1)
{
fd = open(myfifo, O_WRONLY);
fgets(arr2, 80, stdin);
write(fd, arr2, strlen(arr2)+1);
close(fd);
fd = open(myfifo, O_RDONLY);
read(fd, arr1, sizeof(arr1));
P2:Read first
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd1;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
DEPT OF CSE-DS,ATME,MYSURU 49
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
$ cc fifo1.c
$ ./a.out
DEPT OF CSE-DS,ATME,MYSURU 50
OPERATING SYSTEMS LABORATORY BCS303
DEPT OF CSE-DS,ATME,MYSURU 51
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 5:
Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
AIM: Simulate bankers algorithm for Dead Lock Avoidance (Banker‘s Algorithm)
DESCRIPTION:
Deadlock is a situation where in two or more competing actions are waiting f or the other to
finish, and thus neither ever does. When a new process enters a system, it must declare the
maximum number of instances of each resource type it needed. This number may exceed the
total number of resources in the system.
When the user request a set of resources, the system must determine whether the allocation of
each resources will leave the system in safe state. If it will the resources are allocation; otherwise
the process must wait until some other process release the resources.
Data structures Allocation:
If Allocation [i, j]=k, Pi allocated to k instances of resource Rj Need: If
Need[I, j]=k, Pi may need k more instances of resource type Rj, Need[I, j]=Max[I, j]-
Allocation[I, j];
Safety Algorithm
DEPT OF CSE-DS,ATME,MYSURU 52
OPERATING SYSTEMS LABORATORY BCS303
3. Have the system pretend to have allocated the requested resources to process Pi by modifying
the
state as follows;
Available=Available-Request I; Allocation I=Allocation +Request I; Need i=Need i- Request I;
If the resulting resource allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However if the state is unsafe, the Pi must wait for Request i and the old
resource-allocation state is restored.
ALGORITHM:
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
void main()
{
int work[5],avl[5],alloc[10][10],l;
int need[10][10],n,m,I,j,avail[10],max[10][10],k,count,i,fcount=0,pr[10];
DEPT OF CSE-DS,ATME,MYSURU 53
OPERATING SYSTEMS LABORATORY BCS303
DEPT OF CSE-DS,ATME,MYSURU 54
OPERATING SYSTEMS LABORATORY BCS303
{
count=0;
for(j=1;j<=m;j++)
{
if((finish[i]=='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]='t';
pr[k]=i;
break;
}
}
for(i=1;i<=n;i++)
if(finish[i]=='t')
fcount++;
if(fcount==n)
{
}
else
printf("\n the system is in safe state");
for(i=1;i<=n;i++)
printf("\n %d",pr[i]);
printf("\n the system is not in safe state");
//getch();
}
Expected Output:
Enter the no of process 5
DEPT OF CSE-DS,ATME,MYSURU 55
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 6
Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.
DESCRIPTION
One of the simplest methods for memory allocation is to divide memory into several fixed-sized
partitions. Each partition may contain exactly one process. In this multiple-partition method, when a
partition is free, a process is selected from the input queue and is loaded into the free partition. When
the
DEPT OF CSE-DS,ATME,MYSURU 56
OPERATING SYSTEMS LABORATORY BCS303
process terminates, the partition becomes available for another process. The operating system keeps a
table indicating which parts of memory are available and which are occupied. Finally, when a process
arrives and needs memory, a memory section large enough for this process is provided. When it is time to
load or swap a process into main memory, and if there is more than one free block of memory of
sufficient size, then the operating system must decide which free block to allocate. Best-fit strategy
chooses the block that is closest in size to the request.
First-Fit
This is a very basic strategy in which we start from the beginning and allot the first hole, which is big
enough as per the requirements of the process. The first-fit strategy can also be implemented in a
way where we can start our search for the first-fit hole from the place we left off last time.
Best-Fit
This is a greedy strategy that aims to reduce any memory wasted because of internal fragmentation in the
case of static partitioning, and hence we allot that hole to the process, which is the smallest hole that fits
the requirements of the process. Hence, we need to first sort the holes according to their sizes and pick
the best fit for the process without wasting memory.
Worst-Fit
This strategy is the opposite of the Best-Fit strategy. We sort the holes according to their sizes and choose
the largest hole to be allotted to the incoming process. The idea behind this allocation is that as the
process is allotted a large hole, it will have a lot of space left behind as internal fragmentation. Hence,
this will create a hole that will be large enough to accommodate a few other processes.
PROGRAM
WORST-FIT
#include<stdio.h>
#define max 25
void main()
{
DEPT OF CSE-DS,ATME,MYSURU 57
OPERATING SYSTEMS LABORATORY BCS303
int
frag[max],b[max],f[max],i,j,nb,nf,t
emp; static int bf[max],ff[max];
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];
DEPT OF CSE-DS,ATME,MYSURU 58
OPERATING SYSTEMS LABORATORY BCS303
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
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]);
}
OUT PUT
$ cc mm.c
$ ./a.out
DEPT OF CSE-DS,ATME,MYSURU 59
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
Program :BEST-
FIT #include<stdio.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];
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++)
DEPT OF CSE-DS,ATME,MYSURU 60
OPERATING SYSTEMS LABORATORY BCS303
{
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\tBlockSize\
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]);
}
OUTPUT
$ cc best.c
$./a.out
Enter the number of blocks:
3 Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
DEPT OF CSE-DS,ATME,MYSURU 61
OPERATING SYSTEMS LABORATORY BCS303
Block 2: 2
Block 3: 7
Enter the size of the
files:- File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
Program :FIRST-FIT
#include<stdio.h>
#define max 25
void main()
{
int
frag[max],b[max],f[max],i,j,nb,nf,temp,highes
t=0; static int bf[max],ff[max];
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++)
{
DEPT OF CSE-DS,ATME,MYSURU 62
OPERATING SYSTEMS LABORATORY BCS303
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)
{
}
}
frag[i]=highest; bf[ff[i]]=1; highest=0;
}
ff[i]=j; highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
DEPT OF CSE-DS,ATME,MYSURU 63
OPERATING SYSTEMS LABORATORY BCS303
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]);
}
OUTPUT
$ CC first.c
$./a.out
Enter the number of blocks:
3 Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the
files:- File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
DEPT OF CSE-DS,ATME,MYSURU 64
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 7:
Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU
DESCRIPTION:
Page replacement algorithms are an important part of virtual memory management and it helps the
OS to decide which memory page can be moved out making space for the currently needed page.
However, the ultimate objective of all page replacement algorithms is to reduce the number of
page faults.
FIFO-This is the simplest page replacement algorithm. In this algorithm, the operating system
keeps track of all pages in the memory in a queue, the oldest page is in the front of the queue.
When a page needs to be replaced page in the front of the queue is selected for removal.
LRU-In this algorithm page will be replaced which is least recently used
ALGORITHM:
1. Start the process
2. Read number of pages n
3. Read number of pages no
4. Read page numbers into an array a[i]
5. Initialize avail[i]=0 .to check page hit
6. Replace the page with circular queue, while re-placing check page availability in the
frame Place avail[i]=1 if page is placed in theframe Count page faults
7. Print the results.
8. Stop the process.
DEPT OF CSE-DS,ATME,MYSURU 65
OPERATING SYSTEMS LABORATORY BCS303
A) FIRST IN FIRST
#include<stdio.h>
int fr[3];
void main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int
flag1=0,flag2=0,pf=0,frsize=3,top=0;
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0; flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])
{
flag1=1; flag2=1; break;
}}
if(flag1==0)
{
for(i=0;i<frsize;i++)
DEPT OF CSE-DS,ATME,MYSURU 66
OPERATING SYSTEMS LABORATORY BCS303
{
if(fr[i]==-1)
{
fr[i]=page[j]; flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults : %d ",pf+frsize);
}
void display()
{
int i; printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}
DEPT OF CSE-DS,ATME,MYSURU 67
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
$cc a2.c
$ ./a.out
2 -1 -1
2 3 -1
2 3 -1
2 3 1
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2
Number of page faults: 9
DEPT OF CSE-DS,ATME,MYSURU 68
OPERATING SYSTEMS LABORATORY BCS303
ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
SOURCE CODE :
#include<stdio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
for(i=0;i<3;i++)
{
fr[i]=-1;
DEPT OF CSE-DS,ATME,MYSURU 69
OPERATING SYSTEMS LABORATORY BCS303
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1; break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j]; flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
DEPT OF CSE-DS,ATME,MYSURU 70
OPERATING SYSTEMS LABORATORY BCS303
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf+frsize);
}
void display()
{
int i; printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}
DEPT OF CSE-DS,ATME,MYSURU 71
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 1
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
352
No of page faults: 7
LRU
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of
frames. Step 3: Read the number
of pages. Step 4: Read the page
numbers.
Step 5: Initialize the values in frames to -1.
Step 6: Allocate the pages in to frames by selecting the page that has not been used for the
longest period of time.
DEPT OF CSE-DS,ATME,MYSURU 72
OPERATING SYSTEMS LABORATORY BCS303
Step 7: Display the number of page faults. Step 8: Stop the program.
PROGRAM :
SOURCE CODE: /* A program to simulate LRU Page Replacement Algorithm */
#include<stdio.h>
Int main()
{
int a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n;
char f='F';
printf("Enter the number of
pages:"); scanf("%d",&n);
printf("Enter %d Page Numbers:",n); for(i=0;i<n;i+
+) scanf("%d",&b[i]); for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
if(q1<3) { q1=q; }
}
printf("\n%d",b[i]); printf("\
t"); for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
DEPT OF CSE-DS,ATME,MYSURU 73
OPERATING SYSTEMS LABORATORY BCS303
DEPT OF CSE-DS,ATME,MYSURU 74
OPERATING SYSTEMS LABORATORY BCS303
}
printf("\nNo of faults:%d",m);
}
OUTPUT:
$ cc lru.c
$ ./a.out
Enter the number of pages:
12 Enter 12 Page Numbers:
2 2-->F
3 23-->F
2 23
1 231-->F
5 531-->F
2 521-->F
4 524-->F
5 524
3 324-->F
DEPT OF CSE-DS,ATME,MYSURU 75
OPERATING SYSTEMS LABORATORY BCS303
2 324
5 354-->F
2 352-->F
No of faults: 7
RESULT: Thus the program was executed and verified successfully.
PROGRAM 8
Simulate following File Organization Techniques
a) Single level directory b) Two level directory
DESCRIPTION: The directory structure is the organization of files into a hierarchy of folders. In a
single-level directory system, all the files are placed in one directory. There is a root directory
which has all files. It has a simple architecture and there are no sub directories. Advantage of
single level directory system is that it is easy to find a file in the directory.
SOURCE CODE :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct
char dname[10],f
name[10][10];
void main()
int i,ch;
char f[30];
dir.fcnt = 0;
scanf("%s", dir.dname);
while(1)
DEPT OF CSE-DS,ATME,MYSURU 77
OPERATING SYSTEMS LABORATORY BCS303
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
scanf("%s",dir.fname[dir.fnt]);
dir.fcnt++;
break;
scanf("%s",f);
for(i=0;i<dir.fname[i]==0)
if(strcmp(f, dir.fname[i])==0)
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
if(i==dir.fcnt)
else
DEPT OF CSE-DS,ATME,MYSURU 78
OPERATING SYSTEMS LABORATORY BCS303
dir.fcnt--;
break;
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
if(strcmp(f, dir.fname[i])==0)
break;
if(i==dir.fcnt)
printf("File %s not
found",f); break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
DEPT OF CSE-DS,ATME,MYSURU 79
OPERATING SYSTEMS LABORATORY BCS303
break;
default: exit(0);
OUTPUT:
$cc f1.c
$./a.out
SOURCE CODE :
#include<stdio.h>
#include<string.h>
DEPT OF CSE-DS,ATME,MYSURU 81
OPERATING SYSTEMS LABORATORY BCS303
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
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 -- ");
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++)
DEPT OF CSE-DS,ATME,MYSURU 82
OPERATING SYSTEMS LABORATORY BCS303
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");
}
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++)
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;
DEPT OF CSE-DS,ATME,MYSURU 83
OPERATING SYSTEMS LABORATORY BCS303
}
}
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)
DEPT OF CSE-DS,ATME,MYSURU 84
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT
$ cc f2.c
$./a.out
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice -- 1
Enter name of directory -- DIR1 Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
DEPT OF CSE-DS,ATME,MYSURU 85
OPERATING SYSTEMS LABORATORY BCS303
PROGRAM 9
Develop a C program to simulate the Linked file allocation strategies.
DEPT OF CSE-DS,ATME,MYSURU 86
OPERATING SYSTEMS LABORATORY BCS303
A.SEQUENTIAL:
AIM:
C program for implementing sequential file allocation method.
DESCRIPTION:
The most common form of file structure is the sequential file in this type of file, a fixed format
is used for records. All records (of the system) have the same length, consisting of the same
number of fixed length fields in a particular order because the length and position of each field are
known, only the values of fields need to be stored, the field name and length for each field are attributes
ALGORITHM:
a) Check whether the required locations are free from the selected location.
if(b[s1].flag==0)
for (j=s1;j<s1+p[i];j++)
if((b[j].flag)==0)
count++;
DEPT OF CSE-DS,ATME,MYSURU 87
OPERATING SYSTEMS LABORATORY BCS303
if(count==p[i])
break;
locations. for(s=s1;s<(s1+p[i]);s++)
k[i][j]=s;
j=j+1;
b[s].bno=s;
b[s].flag=1;
Step 5: Print the results file no, length, Blocks allocated. Step
SOURCE CODE :
#include<stdio.h>
Int main()
int f[50],i,st,j,len,c,k;
for(i=0;i<50;i++)
f[i]=0;
X:
DEPT OF CSE-DS,ATME,MYSURU 88
OPERATING SYSTEMS LABORATORY BCS303
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;
else
exit();
DEPT OF CSE-DS,ATME,MYSURU 89
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
B. INDEXED
DESCRIPTION:
In the chained method file allocation table contains a field which points to starting block of
memory. From it for each bloc a pointer is kept to next successive block. Hence,there is no
external fragmentation.
ALGORITHM:
DEPT OF CSE-DS,ATME,MYSURU 90
OPERATING SYSTEMS LABORATORY BCS303
locations. q=random(100);
if(b[q].flag==0)
b[q].flag=1;
b[q].fno=j;
r[i][j]=q;
SOURCE CODE :
#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
for(i=0;i<50;i++)
f[i]=0;
DEPT OF CSE-DS,ATME,MYSURU 91
OPERATING SYSTEMS LABORATORY BCS303
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("\
n allocated");
DEPT OF CSE-DS,ATME,MYSURU 92
OPERATING SYSTEMS LABORATORY BCS303
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;
else
exit();
OUTPUT:
$ CC index.c
$ ./a.out
enter index block 9
Enter no of files on index 3 1
23
Allocated
File indexed
9->1:1
9->2;1
9->3:1 enter 1 to enter more files and 0 to exit
C) LINKED:
DEPT OF CSE-DS,ATME,MYSURU 93
OPERATING SYSTEMS LABORATORY BCS303
DESCRIPTION: In the chained method file allocation table contains a field which points to
starting block of memory. From it for each bloc a pointer is kept to next successive block. Hence,
there is no external fragmentation
ALGORTHIM:
b) If the location is free allocate and set flag=1 to the allocated locations.
for(i=0;i<n;i++)
for(j=0;j<s[i];j++)
{
q=random(100); if(b[q].flag==0)
b[q].flag=1;
b[q].fno=j;
r[i][j]=q;
if(j>0)
DEPT OF CSE-DS,ATME,MYSURU 94
OPERATING SYSTEMS LABORATORY BCS303
p=r[i][j-1]; b[p].next=q;}
,Blocks allocated.
SOURCE CODE :
#include<stdio.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:
DEPT OF CSE-DS,ATME,MYSURU 95
OPERATING SYSTEMS LABORATORY BCS303
length"); 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
allocated",j);
k++;
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
DEPT OF CSE-DS,ATME,MYSURU 96
OPERATING SYSTEMS LABORATORY BCS303
OUTPUT:
Enter how many blocks that are already allocated 3 Enter the blocks no.s
that are already allocated 4 7 Enter the starting index block & length 3 7 9
3->1
4->1 file is already allocated
5->1
6->1
7->1 file is already allocated
8->1
9->1file is already allocated
10->1
11->1
12->1
VIVA QUESTIONS
1) List the various types of files
2) What are the various file allocation strategies?
3) What is linked allocation?
4) What are the advantages of linked allocation?
5) What are the disadvantages of sequential allocation methods?
PROGRAM 10:
DEPT OF CSE-DS,ATME,MYSURU 97
OPERATING SYSTEMS LABORATORY BCS303
DESCRIPTION
One of the responsibilities of the operating system is to use the hardware efficiently. For the disk drives,
meeting this responsibility entails having fast access time and large disk bandwidth. Both the access time
and the bandwidth can be improved by managing the order in which disk I/O requests are serviced which
is called as disk scheduling. The simplest form of disk scheduling is, of course, the first-come, first-served
(FCFS) algorithm. This algorithm is intrinsically fair, but it generally does not provide the fastest service.
In the SCAN algorithm, The disk arm starts at one end, and moves towards the other end, servicing
requests as it reaches each cylinder, until it gets to the other end of the disk. At the other end, the direction
of head movement is reversed, and servicing continues. The head continuously scans back and forth across
the disk. C-SCAN is a variant of SCAN designed to provide a more uniform wait time. Like SCAN, C-
SCAN moves the head from one end of the disk to the other, servicing requests along the way. When the
head reaches the other end, however, it immediately returns to the beginning of the disk without servicing
any requests on the return trip
SOURCE CODE
#include<stdio.h>
main()
scanf("%d'",&n);
DEPT OF CSE-DS,ATME,MYSURU 98
OPERATING SYSTEMS LABORATORY BCS303
scanf("%d",&h);
t[0]=0;t[1]=h;
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;
}}}
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++;
DEPT OF CSE-DS,ATME,MYSURU 99
OPERATING SYSTEMS LABORATORY BCS303
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];
INPUT
OUTPUT
tracks 150 50
160 10
DEPT OF CSE-DS,ATME,MYSURU 10
0
OPERATING SYSTEMS LABORATORY BCS303
184 24
90 94
70 20
60 10
58 2
55 3
18 37
DEPT OF CSE-DS,ATME,MYSURU 10
1