OS Lab Manual
OS Lab Manual
OS Lab Manual
AIM:
UNIX:
LINUX:
STRUCTURE OF A LINUXSYSTEM:
a) UNIX kernel
b) Shells
c) Tools andApplications
UNIX KERNEL:
Kernel is the core of the UNIX OS. It controls all tasks, schedule all Processes
and carries out all the functions of OS. Decides when one programs tops and
another starts.
SHELL:
Shell is the command interpreter in the UNIX OS. It accepts command from
the user and analyses and interprets them
Commands
a) date
–used to check the
date and time Syn:$date
Format Purpose Example Result
b) cal
–used to display the calendar Syn:$cal 2 2009
c) echo
Syn:$echo “text”
d) ls
Syn:$ls –s
All files (include files with prefix) ls–l Lodetai (provide file statistics)
ls– u Sort by access time (or show when last accessed together with–l)
ls–r Reverseorder
ls–f Mark directories with /,executable with* , symbolic links with @, local sockets with =,
named pipes(FIFOs)with
ls– h“ Human Readable”, show file size in Kilo Bytes & Mega Bytes (h can be used
together with –l or)
ls[a-m]*List all the files whose names begin with alphabetsFrom„a‟to„m‟ls[a]*List All the
files whose name begins with „a‟or„A‟
e) lp
–used to take printouts
Syn:$lp filename
f)man
$man cat
g) who &whoami
–it displays data about all users who have logged into the system currently.
The next command displays about current user only.
Syn:$who$whoami
h) uptime
–tells you how long the computer has been running since its last reboot or power-off.
Syn:$uptime
i)uname
–it displays the system information such as hardware platform, system name and
processor, OS type.
Syn:$uname–a
j) hostname
k) bc
Creation:
Syn:$cat>filename
Viewing:
Syn:$cat filename
Syn:$cat>>filename
Concatenate:
Syn:$catfile1file2>file3
b) grep–used to search a particular word or pattern related to that word from the file.
Syn:$grep search wordfilename
$cut–c1-10emp
$cut–f 3,6emp
-c cutting columns
-f cutting fields
Eg:$head student
Syn:$head-2student
Eg:$tail student
Examples:
$chmodu-wx student
Removes write and execute permission for users
$chmodu+rw,g+rwstudent
$chmodg=rwx student
Assigns absolute permission for groups of all read, write and execute permissions
k) wc–it counts the number of lines, words, character in a specified file(s) with the
options as–l,-w,-c
u– +assign r–
user read
s -remove
g–gr w–
oup write
=assign
o– x-ex
othe absolutely
ecut
rs
e
$wc –w filename
$wc–c filename
VIVA
The redirection operator is used for redirecting the output of a specific command as an
input to another command. The following are the two ways of using this:
$ ls <options> <directory>
For example, if you want to list all the files in the Example directory, then the command
will be as follows:
$ ls Example/
There are three options for permission groups available to you in Linux. These are
● owners: these permissions will only apply to owners and will not affect other
groups.
● groups: you can assign a group of users specific permissions, which will only
impact users within the group.
● all users: these permissions will apply to all users, and as a result, they present
the greatest security risk and should be assigned with caution.
Question 4.3: What are the three kinds of file permissions in Linux?
There are three kinds of file permissions in Linux:
Read (r): Allows a user or group to view a file.
Write (w): Permits the user to write or modify a file or directory.
Execute (x): A user or grup with execute permissions can execute a file or view a
directory.
Question 4.5: How to change Linux permissions in numeric code
Use numbers instead of “r”, “w”, or “x”.
Permission numbers are:
0 = ---
1 = --x
2 = -w-
3 = -wx
4 = r-
5 = r-x
6 = rw-
7 = rwx
For example:
chmod 777 foldername
will give read, write, and execute permissions for everyone.
chmod 700 foldername
will give read, write, and execute permissions for the user only.
chmod 327 foldername
will give write and execute (3) permission for the user, w (2) for the group, and read,
write, and execute for the users
RESULT
The study about basics of UNIX and Basic UNIX Commands is done successfully.
EXPERIMENT 2
System calls
System calls of Linux operating system:* fork, exec, getpid, exit, wait, close, stat, opendir,
readdir
2.1
PROGRAM
#include<stdio.h>
#include<dirent.h>
#include<stdlib.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf("enter the directory name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("the given directory does not exist");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}
OUTPUT
2.2
PROGRAM
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
void main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf("error in process creation");
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf("the parent process id is %d",pid1);
}
else
{
pid2=getpid();
printf(" the child process id is %d",pid2);
}
}
OUTPUT
2.3
PROGRAM
#include <unistd.h>
int main(void) {
char *binaryPath = "/bin/ls";
char *arg1 = "-l";
char *arg2 = "/home/simat";
return 0;
}
OUTPUT
simat@simat-Veriton-Series:~$ ./a.out
total 11484
drwxr-xr-x 3 simat simat 4096 Dec 16 12:05 006
-rw-rw-r-- 1 simat simat 360 Feb 13 10:09 1.c
drwxr-xr-x 2 simat simat 4096 Dec 20 15:07 21
-rw-rw-r-- 1 simat simat 307 Feb 13 10:17 2.c
-rwxrwxr-x 1 simat simat 8296 Feb 13 11:35 a.out
-rw-r--r-- 1 simat simat 173 Nov 16 14:43 arth.l
-rw-r--r-- 1 simat simat 2070 Nov 16 14:53 arth.tab.h
-rw-r--r-- 1 simat simat 507 Nov 16 14:53 arth.y
-rw-r--r-- 1 simat simat 642 Jan 3 10:48 bc.c
-rw-rw-r-- 1 simat simat 184 Feb 2 15:41 calc.l
-rw-r--r-- 1 simat simat 44730 Feb 2 15:45 calc.tab.c
-rw-r--r-- 1 simat simat 2050 Feb 2 15:45 calc.tab.h
2.4
PROGRAM
#include<stdio.h>
#include<sys/stat.h>
int main()
{
//pointer to stat struct
struct stat sfile;
printf("Bye\n");
return 0;
}
OUTPUT
simat@simat-Veriton-Series:~$ gcc wait.c
simat@simat-Veriton-Series:~$ ./a.out
HP: hello from parent
HC: hello from child
Bye
CT: child has terminated
Bye
VIVA
Question 1: Explain Process Management System Calls in Linux
The System Calls to manage the process are:
Question 2
How to terminate a running process in Linux?
Every process has a unique process id. To terminate the process, we first need to find the
process id. The ps command will list all the running processes along with the process id. And then
we use the kill command to terminate the process.
System call fork() is used to create processes. It takes no arguments and returns a process ID. The
purpose of fork() is to create a new process, which becomes the child process of the caller. After a new
child process is created, both processes will execute the next instruction following the fork() system call.
Therefore, we have to distinguish the parent from the child. This can be done by testing the returned
value of fork():
● If fork() returns a negative value, the creation of a child process was unsuccessful.
● fork() returns a zero to the newly created child process.
● fork() returns a positive value, the process ID of the child process, to the parent.
● The exec() also refers to a system call that helps in creating processes.
● There is a primary difference between fork and exec. The fork, on the one hand, generates new
processes while simultaneously preserving its parent process.
● The exec, on the other hand, creates new processes but doesn’t preserve the parent process
simultaneously.
The system call wait() function blocks the calling process until one of its child processes exits or a signal
is received.
RESULT
System calls of Linux operating system is familiarized
EXP 3
Memory Allocation Methods
AIM
To Implement Memory Allocation Methods a) First Fit b) Worst Fit c) Best Fit
a) First Fit
ALGORITHM
—
PROGRAM : First Fit
#include <stdio.h>
#define max 25
int i, j, k = 0, nb, nf, temp = 0, high = 0, flag = 0;
int main()
{
int b[max], f[max];
printf("\nMemory 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]);
}
firstfit(b, f);
return 0;
}
b) Worst Fit
ALGORITHM
—
PROGRAM : Worst Fit
#include <stdio.h>
#define max 25
int i, j, k = 0, nb, nf, temp = 0, highest = 0, flag = 0;
int main()
{
int b[max], f[max];
printf("\nMemory Management Scheme -Worst Fit");
printf("\nEnter the number of blocks : ");
scanf("%d", &nb);
printf("Enter the number of files : ");
scanf("%d", &nf);
printf("\nEnter the size of the blocks : \n");
for (i = 1; i <= nb; i++)
{
printf("Block %d : ", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files :-\n");
for (i = 1; i <= nf; i++)
{
printf("File %d: ", i);
scanf("%d", &f[i]);
}
worstfit(b, f);
return 0;
}
c) Bestfit
ALGORITHM
—
PROGRAM : Best Fit
#include <stdio.h>
#define max 25
int i, j, k = 0, nb, nf, temp = 0, lowest = 999, flag = 0;
b[k] = lowest;
lowest = 999;
}
}
int main()
{
int b[max], f[max];
printf("\nMemory Management Scheme -Best Fit");
printf("\nEnter the number of blocks : ");
scanf("%d", &nb);
printf("Enter the number of files : ");
scanf("%d", &nf);
printf("\nEnter the size of the blocks : \n");
for (i = 1; i <= nb; i++)
{
printf("Block %d : ", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files :-\n");
for (i = 1; i <= nf; i++)
{
printf("File %d: ", i);
scanf("%d", &f[i]);
}
bestfit(b, f);
return 0;
}
VIVA
What do you mean by an operating system? What are its basic functions?
Operating System (OS) is basically a software program that manages and
handles all resources of a computer such as hardware and software. An OS is
responsible for managing, handling, and coordinating overall activities and
sharing of computer resources. It acts as an intermediary among users of
computer and computer hardware.
RESULT
Implementation of the Memory Allocation Methods for fixed partition is done successfully
Experiment 4
CPU scheduling algorithms
AIM
To implement the CPU scheduling algorithms. a) FCFS b) SJF c) Priority d) Round Robin
4.1 FCFS
ALGORITHM
1. Begin
2. Input the processes along with their burst time (bt).
3. Find turnaround time for all processes
4. Find waiting time (wt) for all processes.
a. wt[0] = 0 ( Process 1 need not to wait)
b. Find waiting time for all other processes
wt= tat-bt
5. average waiting time = total_waiting_time / no_of_processes.
6. average turnaround time = total_turn_around_time/no_of_processes.
7. Display waiting time, turnaround time of each process
8. Display average waiting time,average turnaround time
9. End
PROGRAM : FCFS
#include <stdio.h>
struct process
{
int pid, bt, ct, wt, tat;
} p[20];
int main()
{
int n, i;
printf("\nEnter the Number of Processes : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
p[i].pid = i;
printf("\nEnter the Burst time for Process %d : ", i);
scanf("%d", &p[i].bt);
}
p[0].ct = 0;
for (i = 0; i < n; i++)
{
p[i].ct = p[i - 1].ct + p[i].bt;
p[i].tat = p[i].ct;
}
p[0].wt = 0;
for (i = 0; i < n; i++)
p[i].wt = p[i].tat - p[i].bt;
return 0;
}
OUTPUT : FCFS
4.2 SJF
ALGORITHM
1. Begin
2. Input the processes along with their burst time (bt).
3. Sort the processes according to burst time, lowest to highest
4. Find turnaround time for all processes
5. Find waiting time (wt) for all processes.
a. wt[0] = 0 ( Process 1 in the sorted list need not to wait)
b. Find waiting time for all other processes wt= tat-bt
6. average waiting time = total_waiting_time / no_of_processes.
7. average turnaround time = total_turn_around_time/no_of_processes.
8. Sort the processes according to the process id
9. Display waiting time, turnaround time of each process
10. Display average waiting time,average turnaround time
11. End
PROGRAM : SJF
#include <stdio.h>
struct process
{
int pid, bt, ct, wt, tat;
} p[20], s;
int main()
{
int n, i, j;
printf("\n Enter the no. of processes : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
p[i].pid = i;
printf("\n Enter the burst time for Process %d : ", i);
scanf("%d", &p[i].bt);
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (p[j].bt > p[j + 1].bt)
{
s = p[j];
p[j] = p[j + 1];
p[j + 1] = s;
}
p[-1].ct = 0;
for (i = 0; i < n; i++)
{
p[i].ct = p[i - 1].ct + p[i].bt;
p[i].tat = p[i].ct;
}
p[0].wt = 0;
for (i = 1; i < n; i++)
p[i].wt = p[i].tat - p[i].bt;
printf("\nProcess execution order:\n");
for (i = 0; i < n; i++)
printf("P%d->", p[i].pid);
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (p[j].pid > p[j + 1].pid)
{
s = p[j];
p[j] = p[j + 1];
p[j + 1] = s;
}
printf("\n\tPID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for (i = 0; i < n; i++)
printf("\n\t%d\t\t%d\t\t\t%d\t\t\t%d", p[i].pid, p[i].bt, p[i].tat, p[i].wt);
printf("\n");
return 0;
}
Output : SJF
0 21 32 11
1 3 5 2
2 6 11 5
3 2 2 0
4.3 Priority
Algorithm:
PROGRAM : Priority
#include <stdio.h>
struct process
{
int pid, bt, ct, wt, tat, priority;
} p[20], s;
int main()
{
int n, i, j;
printf("\n Enter the no. of processes : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
p[i].pid = i;
printf("\n Enter the Priority for Process %d : ", i);
scanf("%d", &p[i].priority);
printf("\n Enter the burst time for Process %d : ", i);
scanf("%d", &p[i].bt);
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (p[j].priority > p[j + 1].priority)
{
s = p[j];
p[j] = p[j + 1];
p[j + 1] = s;
}
p[-1].ct = 0;
for (i = 0; i < n; i++)
{
p[i].ct = p[i - 1].ct + p[i].bt;
p[i].tat = p[i].ct;
}
p[0].wt = 0;
for (i = 1; i < n; i++)
p[i].wt = p[i].tat - p[i].bt;
printf("\nProcess execution order:\n");
for (i = 0; i < n; i++)
printf("P%d->", p[i].pid);
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (p[j].pid > p[j + 1].pid)
{
s = p[j];
p[j] = p[j + 1];
p[j + 1] = s;
}
printf("\n\tPID\tPriority\tBurst Time\t Turnaround Time\t Waiting Time\n");
for (i = 0; i < n; i++)
printf("\n\t%d\t%d\t\t%d\t\t\t%d\t\t\t%d", p[i].pid, p[i].priority, p[i].bt, p[i].tat, p[i].wt);
printf("\n");
return 0;
}
Output : Priority
0 3 21 30 9
1 1 6 6 0
2 2 3 9 6
3 4 2 32 30
int dequeue()
{
int ele, i;
if (rear == -1)
return -1;
else
{
ele = rq[0];
for (i = 0; i <= rear; i++)
rq[i] = rq[i + 1];
rear--;
return ele;
}
}
int main()
{
int n, i, j, sum = 0, ts, id, ct = 0;
printf("\nEnter the no. of processes : ");
scanf("%d", &n);
printf("\nEnter the Time Slice : ");
scanf("%d", &ts);
for (i = 0; i < n; i++)
{
p[i].pid = i;
printf("\nEnter the burst time for Process %d : ", i);
scanf("%d", &p[i].bt);
p[i].btcopy = p[i].bt;
}
for (i = 0; i < n; i++)
{
enqueue(i);
}
id = dequeue();
printf("\nExecution order: ");
printf(" P%d->", id);
while (id != -1)
{
if (p[id].bt > ts)
{
ct = ct + ts;
p[id].tat = ct;
p[id].bt = p[id].bt - ts;
if (p[id].bt != 0)
enqueue(p[id].pid);
}
else
{
ct = ct + p[id].bt;
p[id].tat = ct;
p[id].bt = 0;
}
id = dequeue();
if (id != -1)
printf(" P%d->", id);
}
for (i = 0; i < n; i++)
p[i].wt = p[i].tat - p[i].btcopy;
printf("\n\n\n\tPID\t\tBurst Time\t\t Turnaround Time\t Waiting Time\n");
for (i = 0; i < n; i++)
printf("\n\t%d\t\t\t%d\t\t\t%d\t\t\t%d", p[i].pid, p[i].btcopy, p[i].tat, p[i].wt);
printf("\n");
return 0;
}
Execution order: P0-> P1-> P2-> P3-> P0-> P2-> P0-> P0-> P0->
0 21 32 11
1 3 8 5
2 6 21 15
3 2 15 13
VIVA
1. What is a process?
● Switching the CPU to another process requires performing a state save of the current process
and a state restore of a different process. This task is known as a context switch.
● Context-switch time is pure overhead, because the system does no useful work while
switching.
Non-Preemptive Scheduling
● In this scheduling, once the resources (CPU cycles) are allocated to a process, the process holds
the CPU till it gets terminated or reaches a waiting state.
Preemptive Scheduling:
● Preemptive scheduling is used when a process switches from running state to ready state or from
the waiting state to ready state.
5. What are the objectives of Process Scheduling Algorithms
Arrival Time : Time at which the process arrives in the ready queue.
Completion Time : Time at which process completes its execution.
Burst Time : Time required by a process for CPU execution.
Turn Around Time : Time Difference between completion time and arrival time.
Waiting Time : Time Difference between turn around time and burst time.
Experiment 5
IPC using Shared Memory
Aim: To Implement programs for Inter Process Communication using Shared Memory
ALGORITHM
Writer Process
1. Begin
2. Generate a unique key using function ftok()
3. Create a shared memory segment using shmget and get an identifier for the
shared memory segment.
4. Attach to the shared memory segment created using shmat
5. Write the data to the reader to the shared memory segment created
6. Detach from the shared memory segment using shmdt()
7. End
Reader Process
1. Begin
2. Generate a unique key using function ftok()
3. Create a shared memory segment using shmget and get an identifier for the
shared memory segment.
4. Attach to the shared memory segment created using shmat
5. Enter the data to the reader to the shared memory segment created
6. Detach from the shared memory segment using shmdt()
7. To destroy the shared memory call shmctl()
8. End
Program:
Code : Writer
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
int main()
{
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char *str = (char *)shmat(shmid, (void *)0, 0);
printf("Write Data : ");
gets(str);
printf("Data written in memory : %s\n", str);
shmdt(str);
return 0;
}
Code : Reader
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
{
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char *str = (char *)shmat(shmid, (void *)0, 0);
printf("Data read from memory: %s\n", str);
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Output :
simat@simat-desktop:~$ gcc writer.c
simat@simat-desktop:~$ ./a.out
Write Data : hai
Data written in memory: hai
VIVA
1. What is independent and cooperating processes?
Independent Process
A process is independent if it cannot affect or be affected by the other processes
executing in the system.
Any process that does not share data with any other process is independent
Cooperating process can affect or be affected by other processes, including sharing data
2. What is IPC?
1. Shared Memory
2. Message passing
.N
Shared Memory Model Message Passing Model
o
The shared memory region is A message passing facility is used for
1.
used for communication. communication.
The problem is defined as follows: there is a fixed-size buffer and a Producer process, and a
Consumer process.
The Producer process creates an item and adds it to the shared buffer. The Consumer process
takes items out of the shared buffer and “consumes” them.
1. The Producer process must not produce an item if the shared buffer is full.
2. The Consumer process must not consume an item if the shared buffer is empty.
3. At any given instance, only one process should be able to access the shared buffer and
make changes to it.
● A shared-memory region resides in the address space of the process creating the
shared-memory segment.
● Other processes that wish to communicate using this shared-memory segment must
attach it to their address space.
1. Request a memory segment that can be shared between processes to the operating
system.
2. Associate a part of that memory or the whole memory with the address space of the
calling process.
EXPERIMENT 6
AIM
#include <stdio.h>
#include <stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int signal(int s)
return (++s);
int wait(int s)
return (--s);
void producer()
empty = wait(empty);
mutex = wait(mutex);
x++;
mutex = signal(mutex);
full = signal(full);
void consumer()
full = wait(full);
mutex = wait(mutex);
x--;
mutex = signal(mutex);
empty = signal(empty);
}
void main()
int n;
while (1)
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
scanf("%d", &n);
switch (n)
case 1:
producer();
else
printf("\nBUFFER IS FULL\n");
break;
case 2:
consumer();
else
printf("\nBUFFER IS EMPTY\n");
break;
case 3:
exit(0);
break;
}
VIVA
Binary Semaphore: Initial value of the semaphore flag is 1. This ensures that only
one thread can enter the critical section at a time. The value of the flag would
either be 1 or 0.
Counting Semaphore: Initial value of the semaphore flag can be any positive
number. The value of the flag denotes the maximum number of threads that can
be in the critical section at any point in time.
6. What are the different kinds of operations that are possible on semaphore?
Wait()
Signal()
Implementation
wait () {
s--;}
signal () { s++;}
EXPERIMENT 7
BANKERS ALGORITHM
AIM
To Implement the banker’s algorithm for deadlock avoidance
ALGORITHM
Safety Algorithm -the algorithm for finding out whether or not a system is in a safe state.
Let Work and Finish be vectors of length m and n, respectively.
1. Begin
2. Initialize Work = Available and Finish[i] = false for i = 0, 1, ..., n − 1.
3. Find an index i such that both
1. Finish[i] == false
2. Needi ≤ Work
If no such i exists, go to step 4.
4. Work =Work + Allocationi
Finish[i] = true
Go to step 2.
5.. If Finish[i] == true for all i, then the system is in a safe state.
6. End
PROGRAM
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int avail[100];
int total_res[100];
int total_alloc[100];
int req[100];
struct process
{
int pid;
int max[100];
int alloc[100];
int need[100];
int finish;
} p[20], temp;
void main()
{
int i, j, m, n, flag = 0, x = 0, k = 0, sequence[20], availtemp[100], a = 0, f = 0, work[50];
printf("ENTER THE NUMBER OF RESOURCES : ");
scanf("%d", &m);
printf("MAXIMUM RESOURCE COUNT FOR : \n");
for (j = 0; j < m; j++)
{
printf("\tRESOURCE %d : ", j);
scanf("%d", &total_res[j]);
}
printf("\nENTER THE NUMBER OF PROCESSES : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
p[i].pid = i;
printf("\nMAXIMUM ALLOCATION FOR PROCESS %d : ", p[i].pid);
for (j = 0; j < m; j++)
scanf("%d", &p[i].alloc[j]);
printf("\nMAXIMUM REQUIREMENT FOR PROCESS %d : ", p[i].pid);
for (j = 0; j < m; j++)
scanf("%d", &p[i].max[j]);
for (j = 0; j < m; j++)
total_alloc[j] = total_alloc[j] + p[i].alloc[j];
for (j = 0; j < m; j++)
p[i].need[j] = p[i].max[j] - p[i].alloc[j];
p[i].finish = 0;
}
printf("\n Matrix of Total Allocation\n");
for (i = 0; i < m; i++)
printf("%d", total_alloc[i]);
printf("\n AVAILABLE MATRIX\n");
for (i = 0; i < m; i++)
avail[i] = total_res[i] - total_alloc[i];
for (i = 0; i < m; i++)
{
work[i] = avail[i];
printf("%d", avail[i]);
}
while (a < m)
{
for (i = 0; i < n; i++)
{
if (p[i].finish != 1)
{
for (j = 0; j < m; j++)
{
if (work[j] >= p[i].need[j])
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag != 0)
{
sequence[x] = p[i].pid;
x++;
}
f = i * m;
for (j = 0; j < m; j++)
{
if (flag != 0)
{
work[j] += p[i].alloc[j];
availtemp[f] = work[j];
f++;
p[i].finish = 1;
}
}
}
}
a++;
}
Output :
Sequence of Execution
P1->P3->P4->P0->P2->
System is Safe
EXPERIMENT 8
Disk Scheduling Algorithms
AIM
PROGRAM 1 : FCFS
for(int i=0;i<size;i++)
{
cur_track = arr[i];
int main()
{
int arr[MAX],head,n,i,t;
FCFS(arr,head,n);
return 0;
}
OUTPUT
Enter the total no. of tracks200
Enter the no. of requests9
Enter the track requests 60 143 15 185 85 120 33 28 146
Enter the position of read write head70
Total number of seek operations: 736
Seek Sequence is
60->143->15->185->85->120->33->28->146->
PROGRAM 2 : SCAN
#include <stdio.h>
int main()
{
int head, req[20], n, i, j, seektime = 0, max, flag = 0, seq[20], c = 0, temp;
printf("Enter the Max number of Cylinders : ");
scanf("%d", &max);
printf("Enter the Number of Requests : ");
scanf("%d", &n);
printf("Enter the requests : ");
for (i = 0; i < n; i++)
{
scanf("%d", &req[i]);
if (req[i] > max - 1)
flag = 1;
}
if (flag == 1)
printf("Process cannot complete");
else
{
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (req[j] > req[j + 1])
{
temp = req[j];
req[j] = req[j + 1];
req[j + 1] = temp;
}
}
}
printf("Enter the current position of the head : ");
scanf("%d", &head);
j = 0;
while (req[j] <= head)
{
j++;
}
for (i = j; i < n; i++)
{
seektime += req[i] - head;
head = req[i];
seq[c] = req[i];
c++;
}
seektime += (max - 1) - req[n - 1];
head = max - 1;
for (i = j - 1; i >= 0; i--)
{
seektime += head - req[i];
head = req[i];
seq[c] = req[i];
c++;
}
printf("Seektime =%d\n", seektime);
printf("Seek Sequence : ");
for (i = 0; i < c; i++)
printf("%d->", seq[i]);
}
}
Output : SCAN
Enter the Max number of Cylinders : 200
Enter the Number of Requests : 7
Enter the requests : 82 170 43 140 24 16 190
Enter the current position of the head : 50
Seektime =332
Seek Sequence : 82->140->170->190->43->24->16->
PROGRAM 2 : C-SCAN
#include <stdio.h>
int main()
{
int head, current, req[20], n, i, j, seektime = 0, max, flag = 0, seq[20], c = 0, temp;
printf("Enter the Max number of Cylinders : ");
scanf("%d", &max);
printf("Enter the Number of Requests : ");
scanf("%d", &n);
printf("Enter the requests : ");
for (i = 0; i < n; i++)
{
scanf("%d", &req[i]);
if (req[i] > max - 1)
flag = 1;
}
if (flag == 1)
printf("Process cannot complete");
else
{
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (req[j] > req[j + 1])
{
temp = req[j];
req[j] = req[j + 1];
req[j + 1] = temp;
}
}
}
printf("Enter the current position of the head : ");
scanf("%d", &head);
j = 0;
while (req[j] <= head)
{
j++;
}
for (i = j; i < n; i++)
{
seektime += req[i] - head;
head = req[i];
seq[c] = req[i];
c++;
}
seektime += (max - 1) - req[n - 1];
seektime += max - 1;
head = 0;
for (i = 0; i < j; i++)
{
seektime += req[i] - head;
head = req[i];
seq[c] = req[i];
c++;
}
printf("Seektime =%d\n", seektime);
printf("Seek Sequence : ");
for (i = 0; i < c; i++)
printf("%d->", seq[i]);
}
}
Output : C-SCAN
Enter the Max number of Cylinders : 200
Enter the Number of Requests : 7
Enter the requests : 82 170 43 140 24 16 190
Enter the current position of the head : 50
Seektime =391
Seek Sequence : 82->140->170->190->16->24->43->
VIVA
1. What is disk scheduling
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.
3. Explain the terms: seek time, rotational latency,disk access time, disk
response time
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.
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.
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 Response Time: Response Time is the average of time spent by a request
waiting to perform its I/O operation.
EXPERIMENT 9
PAGE REPLACEMENT
AIM
a)FIFO
ALGORITHM
return 0;
}
Output : FIFO
Enter the number of reference : 13
Enter the references : 5 7 6 0 7 1 7 2 0 1 7 1 0
Enter the frame size : 4
Total number of faults = 7
Total number of references = 13
Miss ratio = 53.85%
Hit ratio = 46.15%
b)LRU
ALGORITHM
return 0;
}
Output : LRU
Enter the number of reference : 13
Enter the references : 5 7 6 0 7 1 7 2 0 1 7 1 0
Enter the frame size : 3
Total number of faults = 9
Total number of references = 13
Miss ratio = 69.23%
Hit ratio = 30.77%
VIVA
2. Optimal page replacement algorithm i replaces the page whose demand in the future is
least as compared to other pages from frames (secondary memory).
The purpose of this algorithm is to minimize the number of page faults. Also, one of the most
famous abnormalities in the paging technique is "Belady's Anomaly", which is least seen in this
algorithm.
3. Page fault: A page fault occurs when a program attempts to access data or code that is in its
address space, but is not currently located in the system RAM.
4. Virtual memory is a memory management technique where secondary memory can be
used as if it were a part of the main memory. Virtual memory is a common technique
used in a computer's operating system (OS).
In paging, the program is divided into fixed or In segmentation, the program is divided into variable
1.
mounted size pages. size sections.
3. Page size is determined by hardware. Here, the section size is given by the user.
In paging, the logical address is split into a Here, the logical address is split into section number
6.
page number and page offset. and section offset.
14. This protection is hard to apply. Easy to apply for protection in segmentation.
17. Paging results in a less efficient system. Segmentation results in a more efficient system.
SHELL PROGRAMMING
AIM
To write simple functions with basic tests, loops, patterns in shell
ALGORITHM
PROGRAM
Steps:
gedit filename.sh
bash filename.sh
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i))
}
ALGORITHM
—
PROGRAM : Best Fit
#include <stdio.h>
#define max 25
int i, j, k = 0, nb, nf, temp = 0, lowest = 999, flag = 0;
b[k] = lowest;
lowest = 999;
}
}
int main()
{
int b[max], f[max];
printf("\nMemory Management Scheme -Best Fit");
printf("\nEnter the number of blocks : ");
scanf("%d", &nb);
printf("Enter the number of files : ");
scanf("%d", &nf);
printf("\nEnter the size of the blocks : \n");
for (i = 1; i <= nb; i++)
{
printf("Block %d : ", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files :-\n");
for (i = 1; i <= nf; i++)
{
printf("File %d: ", i);
scanf("%d", &f[i]);
}
bestfit(b, f);
return 0;
}