Operating System Lab Manual(CS)
Operating System Lab Manual(CS)
Prepared By
Name:D.THIRUMAL REDDY& K.TEJASRI
Verified By
Head of the Department:
2
DEPARTMENT OF CS
LAB MANUAL- INDEX
S. No Contents
1 Vision, Mission, PEOs, POs, PSOs & COs
2 Institution Academic Calendar
3 Department Calendar
4 Syllabus
5 GENERAL LABORATORY INSTRUCTIONS
6 Lab schedule
7 Core lab manual
8 Viva questions
9 Additional programs
3
Sri Indu College of Engineering & Technology
Sheriguda (V), R.R.Dist
INSTITUTION VISION
To be a premier institution in engineering & technology and management for competency, values and social
consciousness.
INSTITUTION MISSION
IM1: Provide high quality academic programs, training activities and research facilities.
IM2: Promote continuous industry-institute interaction aimed at promoting employability,
entrepreneurship, leadership and research aptitude among stakeholders.
IM3: Contribute to the economic and technological development of the region, state and nation.
PRINCIPAL
4
Sri Indu College of Engineering & Technology
Sheriguda (V), R.R.Dist
DEPARTMENT VISION
To be excel in areas of cyber security at the regional and global level and to effectively cater to the
needs of the society.
DEPARTMENT MISSION
DM1: Provide high quality academic programmers, training activities and research facilities in cyber security.
DM2: Cultivate continuous industry-institute interaction for employability, entrepreneurship, leadership and
research aptitude among the learners.
DM3: Contribute to the economic, ethical and technological development of the society.
HOD
5
Sri Indu College of Engineering & Technology
Sheriguda (V), R.R.Dist
PO Description
Engineering Knowledge: To be able to apply knowledge of computing, mathematics,
PO 1 Science and Engineering appropriate to the discipline
Problem Analysis: To be able identify, formulate & analyze a problem, and ascertain
PO 2 and define the computing requirements appropriate to its solution.
Design & Development Solutions: To be able to design, implement, and evaluate a
PO 3 computer‐based system, process, component, or program to meet desired needs.
Investigation of complex problems: To be able to identify and analyze user needs and
PO 4 consider them in the selection, creation, evaluation and administration of
Computer‐based systems for providing valid solutions to complex problems.
Modern Tool Usage: To possess skills for creating and in using
PO 5 contemporarytechniques, skills, and tools necessary for computing practice.
Engineering & Society: To apply conceptual knowledge relevant to professional
PO 6 engineering practices in societal, health, safety, legal and cultural issues and their
consequences
Environment & Sustainability: To be able to analyze the local and global impact of
PO 7 computing on individuals, organizations, and society and work towards sustainable
development.
Ethics: To understand contemporary professional, ethical, legal, security and social
PO 8
issues and responsibilities.
Individual & Team work: To Be able to function effectively as an individual and on
PO 9 teams to accomplish a common goal.
Communication: To communicate precisely and effectively both in oral and written
PO 10 form with a range of audiences.
Project management & finance: To apply engineering and management principles for
PO 11 managing and leading economically feasible projects in multi-disciplinary environments
with an effective project plan.
Life Long Learning: To recognize the need for and an ability to engage in independent
PO 12 & lifelong learning for continuing professional development.
6
PROGRAM SPECIFIC OUTCOMES (PSOs)
PSO 2 Design and enforce computing based solutions according to the cyber security requirements.
PSO 3 Identify professional responsibilities and make informed judgments in computing practice
based onlegal and ethical principles.
7
Sri Indu College of Engineering & Technology
Sheriguda (V), R.R.Dist
C22L1.1 Simulate and implement operating system concepts such as scheduling, deadlock
management, file management and memory management.
C22L1.2 Able to implement C programs using Unix system calls.
C22L1.4 Able to analyze the structure of operating system and design the
applications to run in parallel either using process or thread models of
different OS.
C22L1.5 Able to analyse the performance and apply different algorithms used in
major components of operating systems, such as scheduler, memory
manager.
C22L1.6 Able to analyse and justify the various device and resource management
techniques,
C22L1.7 Simulate the synchronization problems to ensure consistency of data using
Mutex and Semaphores.
C22L1.8 examine inter-process communication using pipe, shared memory, and
message passing mechanisms.
` Faculty
8
Sri Indu College of Engineering & Technology
Sheriguda (V), R.R.Dist
9
Academic Calendar
10
SRI INDU COLLEGE OF ENGINEERING AND TECHNOLOGY (AUTONOMOUS)
D E P AR T M E N T OF C O MP UT E R S C I EN C E AN D E N G I N E E R I N G ( CS)
DEPARTMENT CALENDAR – 2023-2024 (SEMESTER-I)
INDIA’S
SATURDAY 8 12 9 SPACE 14 11 9 13
EXPLORATI
ON BY ISRO
SCIENTIST
UNDER AI
CLUB
HOLIDAY
SUNDAY 9 HOLIDAY 13 HOLIDAY 10 HOLIDAY 15 HOLIDAY 12 10 HOLIDAY 14 HOLIDAY
DIWALI
IV-I SEM IV-I SEM
III-I SEM
COMMENCEM COMMENCEM HOLIDAY
COMMENCEMENT IV-I MID-II
MONDAY 10 ENT OF I 14 11
ENT OF II 16 13 11 15 MAKRA
OF II SPELL EXAMS
SPELL CLASS SPELL CLASS SANKRAN
CLASS WORK
WORK WORK THI
IV-I MID-II COMMENCEME HOLIDAY
INDEPENDENCE
TUESDAY 11 15 12 17 14 EXAMS 12 NT OF IV-II 16 MAKRA
DAY
CLASS WORK SANKRAN
THI
IV-I MID I IV-I MID-II II-I
MARKS EXAMS MID-
SUBMISSION ON II
III-I MID I MARKS
IV-I MINI OR BEFORE II-I MID I EXA
WEDNESDAY 12 16 13 18 SUBMISSION ON 15 13 17
PROJECT II-I EXAMS MS
OR BEFORE
COMMENCEME
NT OF CLASS
WORK
III-I LAB II-I
AI CLUB PREPARATION INTERNAL -II MID-
MEETING WITH HOLIDAYS & II
STUDENT IV-I MINI PRACTICAL EXA
THURSD 13 17 COORINATORS 14 PROJECT 19 16 EXAMINATIO 14 18 MS
AY & REVIEW I N AND RMT
IV-I MINI
PROJECT II-I MID I
EXAMS
IV-I MINI PREPARATION III-I LAB II-I
PROJECT HOLIDAYS & INTERNAL -II MID-
REVIEW I PRACTICAL II
DUSSHERA EXAMINATION EXA
IV-I MINI
FRIDAY 14 18 15 20 VACATION 17 AND RMT 15 19 MS
PROJECT
IV-I
II-I MID I
EXAMS 11
IV-I MINI PREPARATION II-I
PROJECT HOLIDAYS & PREPARATION
REVIEW I PRACTICAL HOLIDAYS &
DUSSHERA EXAMINATION PRACTICAL
IV-I MINI
SATURDAY 15 19 16 21 VACATION 18 AND RMT 16 20 EXAMINATION
PROJECT
IV-I AND RMT
II-I MID I
EXAMS
HOLIDAY
SUNDAY 16 HOLIDAY 20 HOLIDAY 17 HOLIDAY 22 19 HOLIDAY 17 HOLIDAY 21 HOLIDAY
DUSSHERA COMMENCEME
PREPARATION
VACATION IV- NT OF III-II
HOLIDAYS &
I&III-I CLASS WORK
PRACTICAL
IV-I EXAMINATION
BONALU IV-I MINI III-I MID-II PREPARATION
MONDAY 17 21 18 TECHNICAL 23 20 AND RMT 18 22
HOLIDAY PROJECT SEMINAR EXAMS HOLIDAYS &
PRACTICAL
II-I 2 ND SPELL
EXAMINATION
OF
AND RMT
INSTRUCTIONS
HOLIDAY DUSSHERA PREPARATION
PREPARATION
GANESH VACATION HOLIDAYS &
HOLIDAYS &
IV-I MINI CHATURTHI IV-I&III-I III-I MID-II PRACTICAL
TUESDAY 18 22 19 24 21 PRACTICAL 19 23
PROJECT EXAMS EXAMINATION
EXAMINATION
AND RMT
AND RMT
IV-I DUSSHERA PREPARATION
PREPARATION
TECHNICAL VACATION HOLIDAYS &
IV-I AIML HOLIDAYS &
SEMINAR IV-I&III-I III-I MID-II PRACTICAL
WEDNESDAY 19 23 INDUSTRIAL 20 25 22 PRACTICAL 20 24
EXAMS EXAMINATION
VISIT EXAMINATION
AND RMT
AND RMT
IV-I DUSSHERA IV-I END PREPARATIO PREPARATION
TECHNICAL VACATION EXAMINATION N HOLIDAYS HOLIDAYS &
SEMINAR IV-I&III-I & PRACTICAL
IV-I MINI
THURSDAY 20 24 21 26 23 21 PRACTICAL 25 EXAMINATION
PROJECT
II-I MID I EXAMINATIO AND RMT
MARKS N AND RMT
SUBMISSION
PREPARATIO PREPARATION
IV-I SEM N HOLIDAYS HOLIDAYS &
COMMENCEMENT IV-I END & PRACTICAL
FRIDAY 21 25 22 27 24 EXAMINATION 22 26
OF II SPELL PRACTICAL EXAMINATION
CLASS WORK EXAMINATIO AND RMT
N AND RMT
DUSSHERA PREPARATIO PREPARATION
VACATION IV-I MID I N HOLIDAYS HOLIDAYS &
IV-I MINI III-I MARKS & PRACTICAL
SATURDAY 22 26 PROJECT 23 28 25 SUBMISSION ON 23 PRACTICAL 27 EXAMINATION
OR BEFORE EXAMINATIO AND RMT
N AND RMT
HOLIDAY
SUNDAY 23 HOLIDAY 27 HOLIDAY 24 HOLIDAY 29 26 HOLIDAY 24 HOLIDAY 28 HOLIDAY
UNDER AI CLUB III-I SEM PREPARATIO
AQUIZ TO COMMENCEM N HOLIDAYS
EXPLORE WITH ENT OF II &
V-I END II-I END
MONDAY 24 28 AI & 25 30 SPELL CLASS 27 25 PRACTICAL 29
WORK EXAMINATION EXAMINATIO EXAMINATIONS
IV-I MINI N AND RMT
PROJECT
IV-I END PREPARATIO
NATIONAL EXAMINATI N HOLIDAYS
SPORTS DAY & ON & II-I END
TUESDAY 25 29 26 31 28 26 30
IV-I MINI PRACTICAL EXAMINATIONS
PROJECT EXAMINATIO
N AND RMT
III-I LAB IV-I END PREPARATIO
INTERNAL – EXAMINATI N HOLIDAYS
I ON & II-I END
WEDNESDAY 26 30 27 29 27 31
PRACTICAL EXAMINATIONS
EXAMINATIO
N AND RMT
IV-I END PREPARATION
EXAMINATI HOLIDAYS &
ON PRACTICAL
EXAMINATION
AND RMT
III-I LAB
THURSDAY 27 31 28 30 28
INTERNAL –I &
III-I MID II
MARKS
SUBMISSION O
OR BEFORE
PREPARATION
HOLIDAYS &
FRIDAY 28 29 31 29 PRACTICAL
EXAMINATION
AND RMT
PREPARATION
HOLIDAYS &
SATURDAY 29 30 30 PRACTICAL
12
EXAMINATION
AND RMT
MONDAY 31
OPERATING SYSTEMS
LAB MANUAL
13
SYLLABUS
14
15
GENERAL LABORATORY INSTRUCTIONS
1. Students are advised to come to the laboratory at least 5 minutes before (to the starting time),
those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to the lab with the
synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement, Aim, Program, Expected
Output, etc.,) filled in for the lab session.
b. Laboratory Record updated up to the last session experiments and other utensils (if any) needed
in the lab.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation note
book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain the
discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which should
be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract severe
punishment.
9. Students must take the permission of the faculty in case of any urgency to go out ; if anybody
found loitering outside the lab / class without permission during working hours will be treated
seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the lab
after completing the task (experiment) in all aspects. He/she must ensure the system / seat is kept
properly.
HOD PRINCIPAL
16
LAB Schedule
No. of
S.No Name Of The Experiment sessions
required
Write C programs to simulate the following CPU Scheduling algorithms. 6
1
a)FCFS
b)SJF
c)Round Robin
d)Priority
2 Write programs using the I/O system calls of UNIX/LINUX operating system (open, 3
read, write, close, fcntl, seek, stat, opendir, readdir)
17
Course
S.No Name Of The Experiment Mapped
Write C programs to simulate the following CPU Scheduling algorithms. C22L1.1
1
a)FCFS
b)SJF
c)Round Robin
d)Priority
2 Write programs using the I/O system calls of UNIX/LINUX operating system (open, C22L1.2
read, write, close, fcntl, seek, stat, opendir, readdir)
18
ADDITIONAL PROGRAMS
No. of
S.No Name Of The Experiment sessions
required
Simulate Algorithm for Deadlock Detection.
1
2 Simulate FIFO page replacement algorithms
19
Lab Manual
EXPERIMENT-1
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept the CPU burst time
4: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time
5: for each process in the Ready Q calculate
Waiting time for process (n)= waiting time of process (n-1) + Burst time of process(n-1)
Turnaround time for Process (n)= waiting time of Process(n)+ Burst time for process(n)
6: Calculate
Average waiting time = Total waiting Time / Number of process
Average Turnaround time = Total Turnaround Time / Number of process
7: Stop the process
20
Program:
#include<stdio.h>
int main()
{
int bt[10],tat[10],wt[10],i,n;
float wtavg,tatavg;
printf("Enter the processes---");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the burst time for process %d",i);
scanf("%d",&bt[i]);
}
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\tPROCESS\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
{
printf("\n\tp%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tat[i]);
}
printf("\n Average waiting time is---%.2f",wtavg/n);
printf("\n Average turnaround time is---%.2f",tatavg/n);
}
21
Output:
Viva Questions:
22
b) SJF (Shortest Job First)
Aim: Write a C program to implement the various process scheduling mechanisms such as SJF
Scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept the CPU burst time 4: Start
the Ready Q according the shortest Burst time by sorting according to lowest to highest burst
time.
5: Set the waiting time of the first process as ‘0’ and its turnaround time as its burst time.
6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
7: Calculate
(c) Average waiting time = Total waiting Time / Number of process
• Average Turnaround time = Total Turnaround Time / Number of process
8: Stop the process
23
Program:
#include<stdio.h>
void main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
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", tatavg/n);
24
}
Output:
VIVA QUESTIONS:
1) The optimum CPU scheduling algorithm is
(A) FIFO (B) SJF with preemption.
(C) SJF without preemption (D) Round Robin.
2) In terms of average wait time the optimum scheduling algorithm is
(A) FCFS (B) SJF (C) Priority (D) RR
3) What are the dis-advantages of SJF Scheduling Algorithm?
4) What are the advantages of SJF Scheduling Algorithm?
5) Define CPU Scheduling algorithm?
25
c) Round Robin
Aim: Write a C program to implement the various process scheduling mechanisms such as Round
Robin Scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
3: For each process in the ready Q, assign the process id and accept the CPU burst
time
(a) Waiting time for process (n) = waiting time of process(n-1)+ burst time
of process(n- 1 ) +the time difference in getting the CPU from process(n-
1)
(b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)+
thetime difference in getting CPU from process(n).
7: Calculate
(a) Average waiting time = Total waiting Time / Number of process (b)Average
Turnaround time = Total Turnaround Time / Number of process
8: Stop theprocess
26
Program:
#include<stdio.h>
int main()
{
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
printf("\t\t ROUND ROBIN SCHEDULING \n");
printf("Enter the number of Processes \n");
scanf("%d",&n);
n1=n;
printf("\n Enter the Timeslice \n");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the Burst Time for the process");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
27
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{ if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n The average Waiting Time=%.2f",awt);
printf("\n The average Turn around Time=%.2f",atat);
return 0;
}
28
Output:
VIVA QUESTIONS:
1. Round Robin scheduling is used in
(A) Disk scheduling. (B)CPU scheduling
(C) I/O scheduling. (D)Multitasking
2. What are the dis-advantages of RR Scheduling Algorithm?
3. What are the advantages of RR Scheduling Algorithm?
4. Super computers typically employ .
(A) Real time Operating system (B) Multiprocessors OS
(C) Desktop OS (D) None of the above
5. An optimal scheduling algorithm in terms of minimizing the average waiting time of a given
Set of processes is .
(A) FCFS scheduling algorithm (B) Round robin scheduling algorithm
(C) Shortest job - first scheduling algorithm (D) none of the above
29
d) Priority
Aim: Write a C program to implement the various process scheduling mechanisms such
asPriority Scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept the CPU burst time
4: Sortthe ready queue according to the priority number.
5: Set the waiting of the first process as ‘0’ and its burst time as its turnaround
time
6: For each process in the Ready Q calculate
(e)Waiting time for process (n)= waiting time of process (n-1) + Burst time of process(n-1)
(f)Turnaround time for Process (n)= waiting time of Process(n)+ Burst time for
process(n)
7: Calculate
g) Average waiting time = Total waiting Time / Number of process
h) Average Turnaround time = Total Turnaround Time / Number of process
8: Stop theprocess
30
Program:
#include<stdio.h>
void main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("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];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = 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("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME");
31
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
}
32
Output:
33
VIVA QUESTIONS:
1. Priority CPU scheduling would most likely be used in a os.
2. CPU allocated process to priority.
3. Calculate avg waiting time=
4. Maximum CPU utilization obtained with
5. Using algorithms find the min & max waiting time.
34
EXPERIMENT-2
Write programs using the I/O system calls of UNIX/LINUX operating system (open, read,
write, close, fcntl, seek, stat, opendir, readdir)
Theory:
There are 5 basic system calls that Unix provides for file I/O.
1. Create: Used to Create a new empty file Syntax :int creat(char *filename, mode_mode)
filename : name of the file which you want to create mode : indicates permissions of new file.
2. open: Used to Open the file for reading, writing or both.
Syntax: int open(char *path, int flags [ , int mode ] );
3. Path : path to file which you want to use flags : How you like to use
O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create
file if it doesn’t exist, O_EXCL: prevent creation if it already exists
4. close: Tells the operating system you are done with a file descriptor and Close the file which
pointed by fd.
Syntax: int close(int fd); fd :file descriptor
5. read: From the file indicated by the file descriptor fd, the read() function reads cnt bytes of input
into the memory area indicated by buf. A successful read() updates the access time for the file.
Syntax: int read(int fd, char *buf, int size);
fd: file descripter
buf: buffer to read data from cnt: length of buffer
write: Writes cnt bytes from buf to the file or socket associated with fd. cnt should not be greater
than INT_MAX (defined in the limits.h header file). If cnt is zero, write() simply returns 0 without
attempting any other action.
Syntax: int write(int fd, char *buf, int size); fd: file descripter
buf: buffer to write data to cnt: length of buffer
*File descriptor is integer that uniquely identifies an open file of the process.
35
Algorithm:
1. Star the program.
2. Open a file for O_RDWR for R/W,O_CREATE for creating a file ,O_TRUNC for truncate
a file.
3. Using getchar(), read the character and stored in the string[] array.
4. The string [] array is write into a file close it.
5. Then the first is opened for read only mode and read the characters and displayed it and
close the file.
6. Stop the program.
Program
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100];
f1=open("data",O_RDWR|O_CREAT|O_TRUNC);
while((c=getchar())!='\n')
{
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY);
read(f2,strin,0);
printf("\n%s\n",strin);
close(f2);
return 0;
Output:
Hai
Hai
36
b) Aim: C program using lseek
Theory:
lseek is a system call that is used to change the location of the read/write pointer of a file descriptor.
The location can be set either in absolute or relative terms.
Syntax : off_t lseek(int fildes, off_t offset, int whence);
int fildes : The file descriptor of the pointer that is going to be moved.
off_t offset : The offset of the pointer (measured in bytes).
int whence : Legal values for this variable are provided at the end which are
SEEK_SET (Offset is to be measured in absolute terms), SEEK_CUR (Offset is to be measured
relative to the current location of the pointer), SEEK_END (Offset is to be measured relative to
the end of the file)
Algorithm:
1. Start the program
2. Open a file in read mode
3. Read the contents of the file
4. Use lseek to change the position of pointer in the read process
5. Stop
37
Program:
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
{
int file=0;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
return 0;
}
Output:
38
0Aim: C program using opendir(), closedir(), readdir()
Theory:
The following are the various operations using directories
6. Creating directories.
Syntax : int mkdir(const char *pathname, mode_t mode);
7. The ‘pathname’ argument is used for the name of the directory.
8. Opening directories
Syntax : DIR *opendir(const char *name);
9. Reading directories.
Syntax: struct dirent *readdir(DIR *dirp);
10. Removing directories.
Syntax: int rmdir(const char *pathname);
11. Closing the directory.
Syntax: int closedir(DIR *dirp);
12. Getting the current working directory.
Syntax: char *getcwd(char *buf, size_t size);
Algorithm:
1. Start the program
2. Print a menu to choose the different directory operations
3. To create and remove a directory ask the user for name and create and remove the same
respectively.
4. To open a directory check whether directory exists or not. If yes open the directory .If it
does not exists print an error message.
5. Finally close the opened directory.
6. Stop
39
Program:
#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
main()
{
char d[10]; int c,op; DIR *e;
struct dirent *sd;
printf("**menu**\n1.create dir\n2.remove dir\n 3.read dir\n enter ur choice");
scanf("%d",&op);
switch(op)
{
case 1: printf("enter dir name\n"); scanf("%s",&d);
c=mkdir(d,777);
if(c==1)
printf("dir is not created");
else
printf("dir is created"); break;
case 2: printf("enter dir name\n"); scanf("%s",&d);
c=rmdir(d);
if(c==1)
printf("dir is not removed");
else
printf("dir is removed"); break;
case 3: printf("enter dir name to open");
scanf("%s",&d);
e=opendir(d);
if(e==NULL)
printf("dir does not exist"); else
{
printf("dir exist\n"); while((sd=readdir(e))!=NULL) printf("%s\t",sd->d_name);
}
closedir(e);
break;
}
}
40
Output:
41
VIVA QUESTIONS:
1. What is the purpose of system calls?
2. What are the different types of system calls?
3. When a process creates a new process using the fork () operation, which of the following state is
shared between the parent process and the child process?
4. What is the use of exec system call?
5. What system call is used for closing a file?
6. What is the value return by close system call?
7. What is the system call is used for writing to a file.
8. What are system calls used for creating and removing directories?
9. What is kernel?
10. What is a process? What are different types of processes?
42
EXPERIMENT-3
Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention
a) Aim: Write a C program to simulate the Bankers Algorithm for Deadlock Avoidance.
Data structures
1. n- Number of process, m-number of resource types.
2. Available: Available[j]=k, k – instance of resource type Rj is available.
3. Max: If max [i, j]=k, Pi may request at most k instances resource Rj.
4. Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj
5. Need: If Need[I, j]=k, Pi may need k more instances of resource type Rj,
6. Need [I, j] =Max [I, j]-Allocation [I, j];
Safety Algorithm
1. Work and Finish be the vector of length m and n respectively,
Work=Available andFinish[i] =False.
2. Find an i such that both
3. Finish[i] =False
4. Need<=Work
5. If no such I exist go to step 4.
6. work=work+Allocation, Finish[i] =True;
7. If Finish [1] =True for all I, then the system is in safe state.
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 restore.
43
Algorithm:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether it is possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. Or not if we allow the request.
10. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
int max[50][50];
int alloc[50][50];
int need[50][50];
int avail[50];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
clrscr();
printf("********** Baner's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
44
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
45
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
46
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
printf("The need martix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
printf(" %d", need[i][j]);
}
printf("\n");
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
47
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
48
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
OUTPUT
Enter the no. of processes 5
Enter the no.of resources instances 3
Enter the max matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the allocation matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available resources
3 3 2
VIVA QUESTIONS:
1. Differentiate deadlock avoidance and fragmentation
2. Tell me the real time example where this deadlock occurs?
3. How do we calculate the need for process?
4. What is the name of the algorithm to avoid deadlock?
5. Banker’s algorithm for resource allocation deals with
(A) Deadlock prevention (B) Deadlock avoidance.
(C) Deadlock recovery (D) Mutual exclusion
50
b) Aim: Write a C program to simulate Bankers Algorithm for Deadlock Prevention
Algorithm:
1. Start
2. Attacking Mutex condition: never grant exclusive access. But this may not be
possible forseveral resources.
3. Attacking preemption: not something you want to do.
4. Attacking hold and wait condition: make a process hold at the most 1 resource at a
time. Make all the requests at the beginning. All or nothing policy. If you feel, retry.
e.g. 2- phase locking 34
5. Attacking circular wait: Order all the resources. Make sure that the requests are issued
in the correct order so that there are no cycles present in the resource graph. Resources
numbered 1 ... n. Resources can be requested only in increasing order. i.e.You cannot
request a resource whose no is less than any you may be holding.
6. Stop
Program:
#include<stdio.h>
void fun(void);
int alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0,max[10][10];
void main()
{
printf("\n\n STIMULATION OF DEADLOCK PREVENTION\n");
printf("enter no of processes: ");
scanf("%d",&p);
printf("enter no of resources: ");
scanf("%d",&r);
printf("enter allocation matrix");
for(i=0;i<p;i++) for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter max matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter the available matrix");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun();
if(flag==0)
{ 51
if(finish[i]!=1)
{
printf("\n\n failing:mutual exclusion");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("\n by allocating required resources toprocess %d dead lock is prevented",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun();
printf("\n\n dead lock is prevented by allocating needed resources");
printf("\n\n failing:hold and wait condition");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("\n AVOIDING ANY ONE OF THE CONDITION,U CAN PREVENT DEADLOCK");
}
}
}
void fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else
break;
}
if(j==r)
52
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
}
INPUT
STIMULATION OF DEADLOCK PREVENTION
enter no of processes: 3
enter no of resources: 2
enter allocation matrix2 4 5
345
enter max matrix4 3 4
561
enter the available matrix2
5
OUTPUT:
failing: mutual exclusion
by allocating required resources to process 3 dead lock is prevented
Lack of pre-emption
VIVA QUESTIONS:
53
EXPERIMENT-4:
Aim:
Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls.
Algorithm:
1. The Semaphore mutex, full & empty are initialized.
2. In the case of producer process
3. Produce an item in to temporary variable.
If there is empty space in the buffer check the mutex value for enters
into the critical section. If the mutex value is 0, allow the producer to
add value in the temporary variable to the buffer.
4. In the case of consumer process
i) It should wait if the buffer is empty
ii) If there is any item in the buffer check for mutex value, if the
mutex==0, removeitem from buffer
iii) Signal the mutex value and reduce the empty value by 1.
iv) Consume the item.
5. Print the result
54
Program:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
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;
55
}
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);
}
56
Output:
57
VIVA QUESTIONS:
58
EXPERIMENT-5
Write C programs to illustrate the following IPC mechanisms
ALGORITHM:
59
a) PIPES
Program:
60
}
return(0); }
Output:
I am the child, about to call ps using execlp
I am the parent. The child just ended. I will now exit.
b) FIFO
Program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
int main()
{
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
{
perror("pipe");
exit(1);
}
printf("writing to file descriptor #%d\n", pfds[1]);
write(pfds[1],"test",5);
Output:
writing to file descriptor #4
reading from file descriptor #3
read"test"
61
c) Message Queue
Program:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MAX_TEXT 512
struct my_msg{
long int msg_type;
char some_text[MAX_TEXT];
};
int main()
{
int running=1;
int msgid;
struct my_msg some_data;
char buffer[50];
msgid=msgget((key_t)14534,0666|IPC_CREAT);
if (msgid == -1)
{
printf("Error in creating queue\n");
exit(0);
}
while(running)
{
printf("Enter some text:\n");
fgets(buffer,50,stdin);
62
some_data.msg_type=1;
strcpy(some_data.some_text,buffer);
red
Enter some text
hat
Enter some text
end
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct my_msg{
63
long int msg_type;
char some_text[BUFSIZ];
};
int main()
{
int running=1;
int msgid;
struct my_msg some_data;
long int msg_to_rec=0;
msgid=msgget((key_t)14534,0666|IPC_CREAT);
while(running)
{
msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_rec,0);
printf("Data received: %s\n",some_data.some_text);
if(strncmp(some_data.some_text,"end",3)==0)
{
running=0;
}
}
msgctl(msgid,IPC_RMID,0);
}
Output:
Data received
red
Data received
hat
Data received
end
64
d) shared memory
i) C Program for shared memory(Writer Process)
Program:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h> int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
Output:
HelloWorld
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
Output:
65
VIVA QUESTIONS:
1. What is IPC?
2. Define FIFO?
3. Define PIPE?
4. What are the two methods to implement inter process communication?
5. Define a) message queues b) shared memory
66
EXPERIMENT: 6
ALGORITHM:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
67
rempages = nop;
for(i=1;i<=np;i++)
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i]; printf("\nEnter pagetable for p[%d] --- ",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
getch();
68
OUTPUT:
69
b) Segmentation
Aim: To write a C program to implement memory management using segmentation
Algorithm:
Step1 : Start the program.
Step2 : Read the base address, number of segments, size of each segment, memory limit. Step3 :
If memory address is less than the base address display “invalid memory limit”.
Step4 : Create the segment table with the segment number and segment address and display it.
Step5 : Read the segment number and displacement.
Step6 : If the segment number and displacement is valid compute the real address and display the
same.
Step7 : Stop the program.
Program:
#include<stdio.h> #include<conio.h> struct list
{
int seg; int base; int limit;
struct list *next;
} *p;
void insert(struct list *q,int base,int limit,int seg)
{ if(p==NULL)
{
p=malloc(sizeof(Struct list));
p->limit=limit;
p- >base=base; p->seg=seg;>next=NULL;
}
else
{
while(q->next!=NULL)
{
Q=q->next; Printf(“yes”)
}
>next=malloc(sizeof(Struct list));
q->next ->limit=limit;
q- >next ->base=base;
q->next ->seg=seg;
70
q- >next ->next=NULL;
}}
int find(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->limit;
}
int search(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->base;
}
main()
{
p=NULL;
int seg,offset,limit,base,c,s,physical; printf(“Enter segment table/n”);
printf(“Enter -1 as segment value for termination\n”);
do
{
printf(“Enter segment number”); scanf(“%d”,&seg);
if(seg!=-1)
{
printf(“Enter base value:”);
scanf(“%d”,&base);
printf(“Enter value for limit:”);
scanf(“%d”,&limit);
insert(p,base,lmit,seg);
}}
while(seg!=-1)
printf(“Enter offset:”);
71
scanf(“%d”,&offset);
printf(“Enter bsegmentation number:”); scanf(“%d”,&seg);
c=find(p,seg); s=search(p,seg); if(offset<c)
{
physical=s+offset;
printf(“Address in physical memory %d\n”,physical);
}
else
{
printf(“error”);
}
OUTPUT:
Enter segment table
Enter -1 as segmentation value for termination Enter segment number:1
Enter base value:2000 Enter value for limit:100 Enter segment number:2 Enter base value:2500
Enter value for limit:100
Enter segmentation number:-1 Enter offset:90
Enter segment number:2
Address in physical memory 2590
VIVA QUESTIONS:
1. What is paging?
2. What is segmentation?
3. Differentiate between paging and segmentation?
4. What are the advantages of segmentation?
5. What is page replacement?
6. What is demand paging?
7. What are the disadvantages of paging
72
7.write a c program to simulate page replacement policies
a)FCFS
b)LRU
c)Optimal
A)AIM: Simulate FIFO page replacement algorithms
Algorithm
1. Enter all the processes and their burst time.
2. Find waiting time, WT of all the processes.
3. For the 1st process, WT = 0.
4. For all the next processes i, WT[i] = BT[i-1] + WT[i-1].
5. Calculate Turnaround time = WT + BT for all the processes.
6. Calculate average waiting time = total waiting time/no. of processes.
7. Calculate average turnaround time = total turnaround time/no. of processes.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int pages[30],frames[10],nop,nof,pf=0,ph=0,hit=0,i,j,k,index=0;
clrscr();
printf("\nEnter no. of pages:");
scanf("%d",&nop);
printf("\nEnter no. of frames:");
scanf("%d",&nof);
printf("\nEnter page string:");
for(i=0;i<nop;i++)
scanf("%d",&pages[i]);
for(i=0;i<nof;i++)
frames[i]=-1;
for(i=0;i<nop;i++)
{
hit=0;
for(j=0;j<nof;j++)
{
if(pages[i]==frames[j])
{
printf("\n");
ph++;
hit=1;
for(k=0;k<nof;k++)
printf("%4d",frames[k]);
}
}
if(hit==0)
{
pf++;
73
if(index<=(nof-1))
{
printf("\n");
frames[index]=pages[i];
for(j=0;j<nof;j++)
printf("%4d",frames[j]);
index++;
}
}
if(index==nof)
index=0;
74
}
printf("\n No.of page faults=%d\n",pf);
printf("\n No.of page hits=%d\n",ph);
getch();
}
OUTPUT:
1 -1 -1
1 2 -1
1 2 6
1 2 6
3 2 6
3 5 6
3 5 4
8 5 4
No.of page faults=7
75
B)AIM: Simulate LRU page replacement algorithms
1- Start traversing the pages.
i) If set holds less pages than capacity.
a) Insert page into the set one by one until
the size of set reaches capacity or all
page requests are processed.
b) Simultaneously maintain the recent occurred
index of each page in a map called indexes.
c) Increment page fault
ii) Else
If current page is present in set, do nothing.
Else
a) Find the page in the set that was least
recently used. We find it using index array.
We basically need to replace the page with
minimum index.
b) Replace the found page with current page.
c) Increment page faults.
d) Update index of current page.
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
clrscr();
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the Page sequence:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
76
}
if(c1==f)
{ c++;
if(k<f)
{ q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{ for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
77
}
}
for(r=0;r<f;r++)b[r]=c2[r]; for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r]; b[r]=b[j]; b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i]; printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe
no of page
faults is
%d",c);getch();
}
OUTPUT:
Enter no of
pages:7 Enter
the Page
sequence:2 3
51942
Enter no of frames:3
2 -1 -1
2 3 -1
2 3 5
1 3 5
1 9 5
1 9 4
2 9 4
The no of page faults is 7
78
C)OPTIMAL PAGE
#include<stdio.h>
int main()
{
int n,pg[30],fr[10];
int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;
fault=0;
dist=0;
k=0;
printf("Enter the total no pages:\t");
scanf("%d",&n);
printf("Enter the sequence:");
for(i=0;i<n;i++)
scanf("%d",&pg[i]);
printf("\nEnter frame size:");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
fr[i]=-1;
}
for(i=0;i<n;i++)
{
flag=0;
temp=pg[i];
for(j=0;j<f;j++)
{
if(temp==fr[j])
{
flag=1;
break;
}
}
if((flag==0)&&(k<f))
{
fault++;
fr[k]=temp;
k++;
}
else if((flag==0)&&(k==f))
{
fault++;
for(cnt=0;cnt<f;cnt++)
{
current=fr[cnt];
79
for(c=i;c<n;c++)
{
if(current!=pg[c])
count[cnt]++;
else
break;
}
}
max=0;
for(m=0;m<f;m++)
{
if(count[m]>max)
{
max=count[m];
p=m;
}
}
fr[p]=temp;
}
printf("\npage %d frame\t",pg[i]);
for(x=0;x<f;x++)
{
printf("%d\t",fr[x]);
}
}
printf("\nTotal number of faults=%d",fault);
return 0;
}
Output
80
VIVA QUESTIONS
81
VIVA QUESTIONS
82
35. What is semaphore?
36. What is reader’s writer’s problem?
37. What is dining philosopher problem?
38. What is deadlock?
39. What r necessary conditions for deadlock?
40. Describe the safe state?
41. Describe banker’s algorithm?
42. Explain deadlock prevention methods?
43. Explain about paging, segment?
44. What is page fault, thrashing?
45. Demand paging?
46. Describe page replacements fifo, lru, optimal, lfu?
47. What is mvt, mft?
48. Explain about allocation methods?
49. What are file attributes?
50. File operations?
51. What is memory management?
52. What are access methods?
53. What r mass storage devices?
54. What is disk scheduling?
55. Deadlock prevention methods.
56. Deadlock detection & recovery from deadlock?
57. Explain about logical address and physical address and swapping?
58. Process scheduling thread scheduling
83
Additional Programs
PROGRAM
#include <stdio.h>
#include<conio.h>
void main()
{
int request[10][10],allocation[10][10],available[10];
int np,nr,i,j,count=0,flag[10],p=0;
clrscr();
printf("DEADLOCK DETECTION\n");
printf("enter no. of processes\n");
scanf("%d",&np);
printf("enter the no.of resources\n");
84
scanf("%d",&nr);
printf("enter the request matrix\n");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&request[i][j]);
}
flag[i]=0;
}
printf("enter the allocation matrix\n");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&allocation[i][j]);
printf("enter the available matrix\n");
for(j=0;j<nr;j++)
scanf("%d",&available[j]);
for(i=0;i<np;i++)
{
count=0;
for(j=0;j<nr;j++)
{
if(allocation[i][j]==0)
count++;
}
if(count==nr)
flag[i]=1;
}
while(p<= np)
{
for(i=0;i<np;i++)
{
if(flag[i]==0)
{
count=0;
for(j=0;j<nr;j++)
{
if(available[j]>=request[i][j])
count++;
}
if(count==nr)
{
for(j=0;j<nr;j++)
available[j]+=allocation[i][j];
flag[i]=1;
}
85
}
OUTPUT
Enter the no.of process
5
Enter the no.of resources
3
Enter the request matrix
0 0 0
2 0 0
0 0 0
1 0 0
0 0 2
Enter the allocation matrix
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
Enter the available matrix
000
P[0] doesn’t create deadlock
P[1] doesn’t create deadlock
P[2] doesn’t create deadlock
P[3] doesn’t create deadlock
P[4] doesn’t create deadlock
86
2) AIM: Simulate FIFO page replacement algorithms
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int pages[30],frames[10],nop,nof,pf=0,ph=0,hit=0,i,j,k,index=0;
clrscr();
printf("\nEnter no. of pages:");
scanf("%d",&nop);
printf("\nEnter no. of frames:");
scanf("%d",&nof);
printf("\nEnter page string:");
for(i=0;i<nop;i++)
scanf("%d",&pages[i]);
for(i=0;i<nof;i++)
frames[i]=-1;
for(i=0;i<nop;i++)
{
hit=0;
for(j=0;j<nof;j++)
{
if(pages[i]==frames[j])
{
printf("\n");
ph++;
hit=1;
for(k=0;k<nof;k++)
printf("%4d",frames[k]);
}
}
if(hit==0)
{
pf++;
if(index<=(nof-1))
{
printf("\n");
frames[index]=pages[i];
for(j=0;j<nof;j++)
printf("%4d",frames[j]);
index++;
}
}
if(index==nof)
index=0;
87
}
printf("\n No.of page faults=%d\n",pf);
printf("\n No.of page hits=%d\n",ph);
getch();
}
OUTPUT:
1 -1 -1
1 2 -1
1 2 6
1 2 6
3 2 6
3 5 6
3 5 4
8 5 4
No.of page faults=7
88
3)AIM: Simulate LRU page replacement algorithms
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
clrscr();
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the Page sequence:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{ c++;
if(k<f)
{ q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{ for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
89
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
getch();
}
OUTPUT:
Enter no of pages:7
Enter the Page sequence:
2351942
Enter no of frames:3
2 -1 -1
2 3 -1
2 3 5
1 3 5
1 9 5
1 9 4
2 9 4
The no of page faults is 7
90
4)AIM: Simulate LFU page replacement algorithms.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int pages[30],frames[10],nop,nof,pf=0,ph=0,hit=0,i,j,k,index=0;
clrscr();
printf("\nEnter no. of pages:");
scanf("%d",&nop);
printf("\nEnter no. of frames:");
scanf("%d",&nof);
printf("\nEnter page string:");
for(i=0;i<nop;i++)
scanf("%d",&pages[i]);
for(i=0;i<nof;i++)
frames[i]=-1;
for(i=0;i<nop;i++)
{
hit=0;
for(j=0;j<nof;j++)
{ if(pages[i]==frames[j])
{
printf("\n");
ph++;
hit=1;
for(k=0;k<nof;k++)
printf("%4d",frames[k]);
}}
if(hit==0)
{
pf++;
if(index<=(nof-1))
{
printf("\n");
frames[index]=pages[i];
for(j=0;j<nof;j++)
printf("%4d",frames[j]);
index++;
} }
if(index==nof)
index=0;
}
printf("\n No.of page faults=%d\n",pf);
printf("\n No.of page hits=%d\n",ph);
91
getch();
}
OUTPUT:
92
5)AIM: Simulate the Indexed file allocation strategy.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int f[50],i,k,j,index[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");
93
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",index[k],f[index[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 index block: 9
Enter no. of files in index 3
1
2
3
Allocated
File indexed
9→1:1
9→2:1
9→3:1
94
MODEL END QUESTION PAPER
95