os_exs
os_exs
List of Experiments
Course Name : Operating System
Datta Meghe College of Engineering
Course Code : CSL403/CSC404
Airoli, Navi Mumbai
placementalgorithms i.e
Best Fit, First
–Fit and Worst –Fit.
9 Write a program to CSC404.4
implement various page
replacement policies.
Practical Incharge
DATTA MEGHE COLLEGE OF ENGINEERING, AIROLI, NAVI
MUMBAI
Institute Vision : To create value - based technocrats to fit in the world of work and research
Institute Mission : To adapt the best practices for creating competent human beings
to work in the world of technology and research.
Department Mission :
M1: To promote an educational environment that combines academics with intellectual curiosity.
M2: To develop human resource with sound knowledge of theory and practical in the discipline of
Computer Engineering and the ability to apply the knowledge to the benefit of society at large.
M3: To assimilate creative research and new technologies in order to facilitate students to be a lifelong
learner who will contribute positively to the economic well-being of the nation.
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and
an engineering specialization to the solution of complex engineering problems.
2. 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.
3. 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.
4. 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.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern engineering
and IT tools including prediction and modelling to complex engineering activities with an understanding
of the limitations.
6. 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.
7. 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.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
Datta Meghe College of Engineering, AiroliDepartment of Computer Engineering
Course Name: Operating System Lab (R-19)
Course Code: CSC403
Year of Study: S.E., Semester: IV
Course Outcomes
CSC404.1 Understand the objectives, functions and structure of OS
CSC404.6 Apply concepts of I/O management and analyze techniques of disk scheduling.
DATTA MEGHE COLLEGE OF ENGINEERING
DEPARTMENT OF COMPUTER ENGINEERING
ACADEMIC YEAR 2022-23 (TERM II)
SUBJECT: OERATING SYSTEM
SEM: IV
RUBRICS FOR GRADING EXPERIMENTS
Rubric Rubric Title Criteria Marks
Number
(out of 15)
R1 Punctuality, On-time 3
Completion Time
Delayed by not more than a Week 2
/ Timeline
Delayed more than a Week 1
Weak understanding 1
Partial Implementation 2
Partial Results 2
Moderate documented 2
ENGINEERING DEPARTMENT OF
OPERATING SYSTEM
SEM: IV
R1 Punctuality, On-time 2
Completion Time
Delayed by not more than a Week 1
/ Timeline
Delayed more than a Week 0
Weak understanding 0
AIM:-To study and implement the internal commands of Linux like ls, chdir, mkdir,
chown , chmod , chgrp, ps , etc
THEORY:-
WHAT IS LINUX?
Linux is an Operating System’s Kernel. You might have heard of UNIX. Well, Linux is a
UNIX clone. But it was actually created by Linus Torvalds from Scratch. Linux is free and
open-source, that means that you can simply change anything in Linux and redistribute it
in your own name! There are several Linux Distributions, commonly called “distros”. A
few of them are:
● Ubuntu Linux
● Red Hat Enterprise Linux
● Linux Mint
● Debian
● Fedora
1. Command - ls
OUTPUT:
2. Command – chmod
OUTPUT:
3. Command - Sort
4. Command - chdir
Example chdir /
OUTPUT:
5. Command - mkdir
OUTPUT:
6. Command - chown
Name chown
Purpose To change owner, change the user and/or group ownership of each given
File to a new Owner.
Syntax chown [options] new_owner object(s)
Example The following would transfer the ownership of a file named file1 and a
directory named dir1 to a new owner named alice:
chown root test.php
OUTPUT:
7. Command - chgrp
Name chgrp
Purpose 'chgrp' command changes the group ownership of each given File to Group
(which can be either a group name or a numeric group id) or to match the
same group as an existing reference file.
Syntax chgrp [OPTION]… GROUP FILE…
Example To Make oracleadmin the owner of the database directory
$ chgrp wheel .
OUTPUT:
8. Command - ps
Name ps
Purpose displays information about a selection of the active processes.
Syntax ps aux
Example $ ps aux
OUTPUT:
9. Command – man
Name man
Purpose It is used to show the manual of the inputted command.
Syntax $ man <command_name>
Example The inputting command will show the manual or all relevant information
for the change directory command.
$ man cd
OUTPUT:
10. Command – rm
OUTPUT:
R1 R2 R3 R4 R5 Total Signature
THEORY: -
SHELL SCRIPTS:
Shell scripts are short programs that are written in a shell programming
language and interpreted by a shell process. They are extremely useful for automating
tasks on Linux and other Unix-like operating systems.
A shell is a program that provides the traditional, text-only user interface for Unix-
like operating systems. Its primary function is to read commands (i.e., instructions) that are
typed into a console (i.e., an all-text display mode) or terminal window (i.e., all-text
mode window) and then execute (i.e., run) them. The default shell on Linux is the very
commonly used and highly versatile bash.
#!/bin/bash
echo “HELLO WORLD”
The process list shows all the processes with various process specific details in separate
columns.Some of the column names are pretty self explanatory.
PID –Process ID
USER - The system user account running the process.
%CPU - CPU usage by the process.
%MEM - Memory usage by the process
COMMAND - The command (executable file) of the process
OUTPUT:
OUTPUT:
R1 R2 R3 R4 R5 Total Signature
AIM:- To study and implement the following system calls: open, read, write, close,
getpid, setpid, getuid, getgid, getegid, geteuid.
THEORY: -
SYSTEM CALL:
When a program in user mode requires access to RAM or a hardware resource, it
must ask the kernel to provide access to that resource. This is done via something called
a system call.
When a program makes a system call, the mode is switched from user mode to kernel mode.
This is called a context switch.
Then the kernel provides the resource which the program requested. After that, another
context switch happens which results in change of mode from kernel mode back to user
mode.
Generally, system calls are made by the user level programs in the following situations:
OUTPUT:
2. Reading From a File : read()
Description: To read bytes from a file, it uses the “read()” system call.
OUTPUT:
3. Writing to a File: write()
Description: To write bytes to a file, it uses the “write()” system call,
OUTPUT:
4. Closing a File: “close()”
Description: uses the “close()” system call to free the file descriptor of the input.
OUTPUT:
5. Process management - getpid() & getppid()
Description: A process may obtain its own process ID and parent process ID numbers
by using the “getpid()” and “getppid()” system calls, respectively.
Here “getpid()” and “getppid()” return a process’ID number and parent process’ ID
number, respectively.
The parent process ID number of PID 1 (i.e., “init”) is 1.
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main() {
printf("Process ID = %d\n",getpid());
printf("Parent Process ID = %d\n",getpid());
return 0;
}
OUTPUT:
6. Accessing User and Group IDs
Description: The system calls that allow you to read a process real and effective IDs
OUTPUT:
CONCLUSION:- Thus we have studied and explored the commands of system calls.
R1 R2 R3 R4 R5 Total Signature
AIM: - To Implement basic commands of Linux like ls, cp, mv and others using kernel
APIs.
THEORY:-
1. Command – stat
Name stat
Purpose To check the status of a file. This provides more detailed information about
a file than ‘ls -l’ output.
Syntax $ stat usrcopy
Example stat data.txt
2. Command – cal
Name cal
Purpose Displays the calendar of the current month.
Syntax $ cal
Example $ cal
July 2012
Su Mo Tu We Th Fr Sa
1234567
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
OUTPUT:
3. Command – VI editor
Name VI editor
Purpose VI stands for Visual editor; another text editor in Linux. This is a standard
editor in many Linux/Unix environments.
Syntax $ vi filename
Example $ vi hello.txt
4. Command – mv
Name mv - move
Purpose Move files or directories. The 'mv' command works like 'cp' command,
except that the original file is removed. But, the mv command can be used
to rename the files (or directories).
Syntax $ mv source destination
Example mv myfile.txt myfiles
Move the file myfile.txt into the directory myfiles. If myfiles is a file, it will
be overwritten. If the file is marked as read-only, but you own the file, you
will be prompted before overwriting it.
OUTPUT:
5. Command copy
Name cp - copy
Purpose Copy files and directories. If the source is a file, and the destination (file)
name does not exit, then source is copied with new name i.e. with the name
provided as the destination.
Syntax $ cp source destination
Example $ cp usrlisting listing_copy.txt
OUTPUT:
6. Command – date
Name date
Purpose Displays current time and date.
If you are interested only in time, you can use 'date +%T' (in hh:mm:ss):
Syntax $ date
Example $ date
Fri Jul 6 01:07:09 IST 2012
$ date +%T
01:13:14
OUTPUT:
7. Command – whoami
Name whoami
Purpose This command reveals the user who is currently logged in.
Syntax $ whoami
Example $ whoami
raghu
OUTPUT:
8. Command – pwd
Name pwd
Purpose ‘pwd’ command prints the absolute path to current working directory.
Syntax $ pwd
Example $ pwd
/home/raghu
OUTPUT:
9. Command – touch
Name touch
Purpose For creating an empty file, use the touch command.
Syntax $ touch filename
Example $ touch file1 file2 file3
$ ls –l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
OUTPUT:
10. Command – wc
OUTPUT:
R1 R2 R3 R4 R5 Total Signature
AIM: - Write a program to implement CPU Scheduling algorithms like FCFS & SJF.
THEORY:-
1. FIRST-COME, FIRST-SERVE SCHEDULING (FCFS):
In this, which process enter the ready queue first is served first. The OS maintains DS that
is ready queue. It is the simplest CPU scheduling algorithm. If a process request the CPU
then it is loaded into the ready queue, which process is the head of the ready queue, connect
the CPU to that process.
OUTPUT:
2. SHORTEST JOB FIRST:
The criteria of this algorithm are which process having the smallest CPU burst, CPU is
assigned to that next process. If two process having the same CPU burst time FCFS is used
to break the tie.
R1 R2 R3 R4 R5 Total Signature
THEORY:-
The producer consumer problem is a synchronization problem. We have a buffer of fixed size. A
producer can produce an item and can place in the buffer. A consumer can pick items and can
consume them. We need to ensure that when a producer is placing an item in the buffer, then at
the same time consumer should not consume any item. In this problem, buffer is the critical section.
To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track of
number of items in the buffer at any given time and “Empty” keeps track of number of unoccupied
slots.
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
return 0;
int wait(int s)
{
return (--s);
int signal(int s)
{
return(++s);
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Output
R1 R2 R3 R4 R5 Total Signature
AIM:- Write a program to demonstrate the concept of deadlock avoidance through Banker’s
Algorithm
THEORY:-
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all resources,
then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.
Why Banker’s algorithm is named so?
Banker’s algorithm is named so because it is used in banking system to check whether loan can be
sanctioned to a person or not. Suppose there are n number of account holders in a bank and the
total sum of their money is S. If a person applies for a loan then the bank first subtracts the loan
amount from the total money that bank has and if the remaining amount is greater than S then
only the loan is sanctioned. It is done because if all the account holders come to withdraw
their money then the bank can easily do it.
In other words, the bank would never allocate its money in such a way that it can no longer satisfy
the needs of all its customers. The bank would try to be in safe state always.
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of
processes m = 3; // Number
of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
R1 R2 R3 R4 R5 Total Signature
AIM: - Write a program to implement dynamic partitioning placement algorithms i.e Best Fit,
First –Fit and Worst –Fit.
THEORY:-
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 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 chooses the first available block that is large enough. Worst-fit chooses the
largest available block.
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block
%d:",i);scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
if(bf[j]!=1)
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
ff[i]=j;
lowest=temp;
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
OUTPUT:
/*Program to implement FIRST-FIT dynamic partitioning placement algorithm*/
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
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++)
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
ff[i]=j;
highest=temp;
}}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
OUTPUT:
/*Program to implement Worst-Fit dynamic partitioning placement algorithm*/
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
if(bf[j]!=1)
temp=b[j]-f[i];
if(temp>=0)
ff[i]=j;
break;
frag[i]=temp;
bf[ff[i]]=1;
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
OUTPUT:
R1 R2 R3 R4 R5 Total Signature
THEORY:-
In a operating systems that use paging for memory management, page replacement algorithm are
needed to decide which page needed to be replaced when new page comes in. Whenever a new
page is referred and not present in memory, page fault occurs and Operating System replaces
one of the existing pages with newly needed page. Different page replacement algorithms
suggest different ways to decide which page to replace. The target for all algorithms is to reduce
number of page faults.
Page Fault – A page fault is a type of interrupt, raised by the hardware when a running program
accesses a memory page that is mapped into the virtual address space, but not loaded in physical
memory.
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
for(i=0;i<n;i++)
for(k=0;k<f;k++)
if(m[k]==rs[i])
break;
}
if(k==f)
m[count++]=rs[i];
pf++;
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\n");
if(count==f)
count=0;
getch();
OUTPUT:
2. Least Recently Used (LRU)
In Least Recently Used (LRU) algorithm is a Greedy algorithm where the page to be replaced is
least recently used. The idea is based on locality of reference, the least recently used page is not
likely.
#include<stdio.h>
#include<conio.h>
main()
next=1; clrscr();
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
flag[i]=0;
scanf("%d",&f);
for(i=0;i<f;i++)
count[i]=0;
m[i]=-1;
for(i=0;i<n;i++)
for(j=0;j<f;j++)
if(m[j]==rs[i])
flag[i]=1;
count[j]=next;
next++;
}
if(flag[i]==0)
{
if(i<f)
m[i]=rs[i];
count[i]=next;
next++;
else
min=0;
for(j=1;j<f;j++)
min=j;
m[min]=rs[i];
count[min]=next;
next++;
pf++;
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
OUTPUT:
R1 R2 R3 R4 R5 Total Signature
AIM: - Write a program to implement Disk Scheduling algorithms like FCFS, SCAN and C-
SCAN.
THEORY:-
Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk.
Disk scheduling is also known as I/O scheduling.
● Seek Time:Seek time is the time taken to locate the disk arm to a specified track where the
data is to be read or write. So the disk scheduling algorithm that gives minimum average
seek time is better.
● Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to
rotate into a position so that it can access the read/write heads. So the disk scheduling
algorithm that gives minimum rotational latency is better.
● Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating
speed of the disk and number of bytes to be transferred.
● Disk Access Time:
FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are
addressed in the order they arrive in the disk queue. Let us understand this with the help of an
example.
C program for FCFS disk scheduling:
#include<stdio.h>
int main()
{
int
queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
OUTPUT :
SCAN disk scheduling :
In the SCAN algorithm, the disk arm starts at one end of the disk and moves toward
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. The SCAN algorithm is sometimes
called the elevator algorithm, since the disk arm behaves just like an elevator in a building, first
servicing all the requests going up and then reversing to service requests the other way.
#include<stdio.h>
int main()
{
int
queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2
[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
OUTPUT :
C-SCAN disk scheduling :
queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2
[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
OUTPUT :
CONCLUSION: -Thus we have studied and implemented Disk Scheduling algorithms like
FCFS, SCAN and C-SCAN.
THEORY:-
The dining-philosophers problem is considered a classic synchronization problem because
it is an example of a large class of concurrency-control problems. It is a simple
representation of the need to allocate several resources among several processes in a
deadlock-free and starvation-free manner. Consider five philosophers who spend their
lives thinking and eating. The philosophers share a circular table surrounded by five
chairs, each belonging to one philosopher. In the center of the table is a bowl of rice, and
the table is laid with five single chopsticks. When a philosopher thinks, she does not
interact with her colleagues. From time to time, a philosopher gets hungry and tries to
pick up the two chopsticks that are closest to her (the chopsticks that are between her and
her left and right neighbors). A philosopher may pick up only one chopstick at a time.
Obviously, she cam1ot pick up a chopstick that is already in the hand of a neighbor. When
a hungry philosopher has both her chopsticks at the same time, she eats without releasing
her chopsticks. When she is finished eating, she puts down both of her chopsticks and
starts thinking again. The dining-philosophers problem may lead to a deadlock situation
and hence some rules have to be framed to avoid the occurrence of deadlock.
PROGRAM
int tph, philname[20], status[20], howhung, hu[20], cho; main()
{
int i; clrscr();
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i]=(i+1); status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf(“\n All are hungry..\nDead lock stage will occur”);
printf(\n”Exiting\n”);
else{
for(i=0;i<howhung;i++){
printf(“Enterphilosopher%dposition:”,(i+1));
scanf(“%d”,&hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time
\t3.Exit\nEnter your choice:");
scanf("%d", &cho);
switch(cho)
{
case 1: one();
break;
case 2: two();
break;
case 3: exit(0);
default: printf("\nInvalid option..");
}
}while(1);
}
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same
time\n"); for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j]; s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],
philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
}
INPUT
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 3
Enter philosopher 1 position: 2
Enter philosopher 2 position: 4
Enter philosopher 3 position: 5
OUTPUT
1.One can eat at a time
2.Two can eat at a time
3.Exit Enter your choice: 1
Allow one philosopher to eat at any time
P 3 is granted to eat
P 3 is waiting
P 5 is waiting
P 0 is waiting
P 5 is granted to eat
P 5 is waiting
P 0 is waiting
P 0 is granted to eat
P 0 is waiting
THEORY:-
A. SEQUENTIAL:
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 of the file structure.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations to each in sequential order a).
Randomly select a location from availablelocation s1= random(100);
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++;
}
if(count==p[i]) break;
}
b) Allocate and set flag=1 to the allocated 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 6: Stop the program
SOURCE CODE :
#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
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
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}
OUTPUT:
Enter the starting block & length of file 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk.
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:
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q= random(100);
a) Check whether the selected location is free .
b) If the location is free allocate and set flag=1 to the allocated locations.
q=random(100);
{
if(b[q].flag==0)
b[q].flag=1;
b[q].fno=j;
r[i][j]=q;
Step 5: Print the results file no, length ,Blocks
allocated.
Step 6: Stop the program
SOURCE CODE :
#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
main() {
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x: printf("enter index block
\t");
scanf("%d",&p);
if(f[p]==0)
{ f[p]=1;
printf("enter no of files on index \t");
scanf("%d",&n); }
else {
printf("Block already allocated \n");
goto x; }
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1) {
printf("Block already allocated");
goto x; }
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d ->%d:%d",p,inde[k],f[inde[k]]);
printf(" Enter 1 to enter more files and 0 to exit \t");
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch(); }
OUTPUT:
Enter the starting block & length of file 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk
OUTPUT:
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:
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.
D. ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of
files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q=
random(100);
a) Check whether the selected location is free .
b) If the location is free allocate and set flag=1 to the allocated locations.
While allocating next location address to attach it to previous location
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)
{
}
}
p=r[i][j-1]; b[p].next=q;}
Step 5: Print the results file no, length ,Blocks
allocated.
Step 6: Stop the program
SOURCE CODE :
#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++) f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & 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
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto
X;
else
exit();
getch( );}
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
CONCLUSION: In conclusion, this C program illustrates the sequential file allocation
method, showcasing how data blocks are allocated in a sequential manner in a file system.
R1 R2 R3 R4 R5 Total Signature