Os Lab Manual
Os Lab Manual
Os Lab Manual
TECHNOLOGY
( Approved by AICTE, Affiliated to JNTU, Hyd) Vyasapuri,
Bandlaguda, Post : Keshavgiri, Hyderabad - 500 005.
Course Objectives:
simulation
● Introduce basic Unix commands, system call interface for process management, interprocess
communication and I/O in Unix
Course Outcomes:
● Simulate and implement operating system concepts such as scheduling,
deadlock management, file management and memory management.
● Able to implement C programs using Unix system calls
LIST OF EXPERIMENTS:
3. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention.
4. Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls.
5. Write C programs to illustrate the following IPC mechanisms
TEXT BOOKS:
1. Operating System Principles- Abraham Silberchatz, Peter B. Galvin, Greg Gagne 7th
REFERENCES BOOKS:
1. Operating Systems – Internals and Design Principles, William Stallings, Fifth
Edition–2005,Pearson Education/PHI
5. 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. [PEO’s:
1,2 and 3] [PSO’s: 1,2 and 3]
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. [PEO’s: 1,2 and 3] [PSO’s: 1,2 and 3]
Use mathematical abstractions and Algorithmic design along with open source programming
tools to solve complexities involved in efficient programming.[PO:1,2,3,4 and 5] & [PEO:1
and 2]
Ensure programming & documentation skills for each individual student in relevant su
bjects i.e., C, C++, Java, DBMS, Web Technologies (Development), Linux, Data
Warehousing & Data Mining and on Testing Tools.[PO:1,2,3,4,5,10 and 11] &
[PEO:1,2 and 3]
Ensure employability and career development skills through Industry oriented mini &
major projects, internship, industry visits, seminars and workshops. [PO:6,7,8,9,10,11
and 12] & [PEO:1,2 and 3]
EXPERIMENT 1
1.1 OBJECTIVE
Write a C program to simulate the following non-preemptive CPU scheduling algorithms to find turnaround
time
and waiting time for the above problem.
a) FCFS b) SJF c) Round Robin d) Priority
DESCRIPTION
Assume all the processes arrive at the same time.
FCFS CPU SCHEDULING ALGORITHM
For FCFS scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times. The
scheduling is performed on the basis of arrival time of the processes irrespective of their other parameters. Each
process will be executed according to its arrival time. Calculate the waiting time and turnaround time of each of
the processes accordingly.
SJF CPU SCHEDULING ALGORITHM
For SJF scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times. Arrange
all the jobs in order with respect to their burst times. There may be two jobs in queue with the same execution
time, and then FCFS approach is to be performed. Each process will be executed according to the length of its
burst time. Then calculate the waiting time and turnaround time of each of the processes accordingly.
ROUND ROBIN CPU SCHEDULING ALGORITHM
For round robin scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times,
and the size of the time slice. Time slices are assigned to each process in equal portions and in circular order,
handling all processes execution. This allows every process to get an equal chance. Calculate the waiting time
and turnaround time of each of the processes accordingly.
PRIORITY CPU SCHEDULING ALGORITHM
For priority scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times, and
the priorities. Arrange all the jobs in order with respect to their priorities. There may be two jobs in queue with
the same priority, and then FCFS approach is to be performed. Each process will be executed according to its
priority. Calculate the waiting time and turnaround time of each of the processes accordingly.
PROGRAM
FCFS CPU SCHEDULING ALGORITHM
#include<stdio.h>
int main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
output
#include<stdio.h>
int main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
printf("\n /*****SJF****/");
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f\n", tatavg/n);
OUTPUT
OUTPUT:
d) Priority
#include<stdio.h>
int main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("\n priority scheduling \n");
printf("\nEnter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("\n Enter the Burst Time & Priority of Process %d",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i];
}
output
EXPERIMENT 2
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main()
{
int fd[2];
char buf1[25]=”just a test\n”;
char buf2[50];
fd[0]=open(“file1”,O_RDWR);
fd[1]=open(“file2”,O_RDWR);
write(fd[0],buf1,strlen(buf1);
printf(“\n enter the text now…”);
scanf(“\n %s”,buf1);
printf(“\n cat file1 is \n hai”);
write(fd[0],buf2,sizeof(buf1));
write(fd[1],buf2,sizeof(buf2));
close(fd[0]);
}
EXPERIMENT 3
Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention
3(B). Write a C program to simulate Bankers Algorithm for Dead Lock Prevention
#include<stdio.h>
void main()
{
char job[10][10];
int time[10],avail,tem[10],temp[10];
int safe[10];
int ind=1,i,j,q,n,t;
}
Output:
Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls
DESCRIPTION
#include<stdio.h>
void main()
{
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
scanf(“%d”, &choice);
switch(choice){
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
Break;
else
{
consume = buffer[out];
out = (out+1)%bufsize;
}
br
ea
k;
} } }
OUTPUT
3.
2. Ex
1. Produce Consume it
Enter your choice: 2
Buffer is
Empty
3.
2. Ex
1. Produce Consume it
Enter your choice: 1
EXPERIMENT 5
intmain()
{
// ftok to generate unique key
key_t key = ftok("shmfile",65);
return0;
}
SHARED MEMORY FOR READER PROCESS
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
usingnamespacestd;
int main()
{
// ftok to generate unique key
key_t key = ftok("shmfile",65);
return0;
}
Output:
Output:
Conceptually, a pipe is a connection between two processes, such that the standard output from one process
becomes the standard input of the other process. In UNIX Operating System, Pipes are useful for communication
between related processes(inter-process communication).
● Pipe is one-way communication only i.e we can use a pipe such that One process write to the pipe, and the
other process reads from the pipe. It opens a pipe, which is an area of main memory that is treated as
a “virtual file”.
● The pipe can be used by the creating process, as well as all its child processes, for reading and writing. One
process can write to this “virtual file” or pipe and another related process can read from it.
● If a process tries to read before something is written to the pipe, the process is suspended until something is
written.
● The pipe system call finds the first two available positions in the process’s open file table and allocates them
for the read and write ends of the pipe.
Syntax in C language:
Parameters :
fd[0] will be the fd(file descriptor) for the
read end of pipe.
fd[1] will be the fd for the write end of pipe.
Returns : 0 on Success.
-1 on error.
intmain()
{
charinbuf[MSGSIZE];
intp[2], i;
if(pipe(p) < 0)
exit(1);
/* continued */
/* write pipe */
Output:
hello, world #1
hello, world #2
hello, world #3
IPC using Message Queues
OPERATING SYSTEMS Page
MAHAVEER INSTITUTE OF SCIENCE &
TECHNOLOGY
( Approved by AICTE, Affiliated to JNTU, Hyd) Vyasapuri,
Bandlaguda, Post : Keshavgiri, Hyderabad - 500 005.
A message queue is a linked list of messages stored within the kernel and identified by a message queue identifier.
A new queue is created or an existing queue opened by msgget().
New messages are added to the end of a queue by msgsnd(). Every message has a positive long integer type field, a
non-negative length, and the actual data bytes (corresponding to the length), all of which are specified to msgsnd()
when the message is added to a queue. Messages are fetched from a queue by msgrcv(). We don’t have to fetch the
messages in a first-in, first-out order. Instead, we can fetch messages based on their type field.
All processes can exchange information through access to a common system message queue. The sending process
places a message (via some (OS) message-passing module) onto a queue which can be read by another process.
Each message is given an identification or type so that processes can select the appropriate message. Process must
share a common key in order to gain access to the queue in the first place.
msgget(): either returns the message queue identifier for a newly created message
queue or returns the identifiers for a queue which exists with the same key value.
intmain()
{
key_t key;
intmsgid;
intmain()
{
key_t key;
intmsgid;
return0;
}
Output:
EXPERIMENT 6
output
#include <stdio.h>
//#include <conio.h>
#include <math.h>
#include<stdlib.h>