0% found this document useful (0 votes)
18 views42 pages

Document From .

The document is a laboratory manual for the Operating Systems course (BCS303) at Jain Institute of Technology, Davanagere, for the academic year 2023-2024. It outlines the program outcomes, course objectives, teaching-learning processes, detailed modules covering various aspects of operating systems, practical components, assessment details, and suggested learning resources. The manual aims to equip students with the necessary skills and knowledge in operating systems through theoretical and practical applications.

Uploaded by

Nayana Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views42 pages

Document From .

The document is a laboratory manual for the Operating Systems course (BCS303) at Jain Institute of Technology, Davanagere, for the academic year 2023-2024. It outlines the program outcomes, course objectives, teaching-learning processes, detailed modules covering various aspects of operating systems, practical components, assessment details, and suggested learning resources. The manual aims to equip students with the necessary skills and knowledge in operating systems through theoretical and practical applications.

Uploaded by

Nayana Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Arka Educational and Cultural Trust

Jain Institute of Technology, Davanagere


(A Unit of Jain Group of Institutions, Bengaluru )

Affiliated to VTU, Belagavi | Approved by AICTE, New Delhi | Recognized by UGC


under (2f) | Recognized by Govt. of Karnataka

Department of IS&E
OPEARATING SYSTEMS
BCS303
LABORATORY MANUAL

FOR THIRD SEMESTER

2023-2024

PREPARED BY :
Mrs. Meghana G R, Asst. Prof., Department of IS&E
Arka Educational & Cultural Trust (Regd.)
JAIN INSTITUTE OF TECHNOLOGY, DAVANAGERE
(A Unit of Jain Group of Institutions, Bangalore)

Program Outcomes
Information Science & Engineering Graduates will be able to:

POs Program Outcomes

PO1 Engineering knowledge: Apply the knowledge of mathematics, science,


engineering fundamentals, and computer science and engineering to the solution of
complex engineering problems.

PO2 Problem analysis: Identify, formulate, review research literature, and analyze
complex computer engineering problems reaching substantiated conclusions using
first principles of mathematics, natural sciences, and engineering sciences.

PO3 Design/development of solutions: Design solutions for complex computer


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.

PO4 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.

PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern computer engineering and IT tools including prediction and modeling to
complex computer engineering activities with an understanding of the limitations.

PO6 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.

PO7 Environment and sustainability: Understand the impact of the professional


computer engineering solutions in societal and environmental contexts, and
demonstrate the knowledge of, and need for sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the computer engineering practice.

PO9 Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings.

PO10 Communication: Communicate effectively on complex computer 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.

PO11 Project management and finance: Demonstrate knowledge and understanding of


the computer 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.

PO12 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.
Vision and Mission of the Institution
Vision of the Institute:

Technical manpower development to build professionally excellent, globally competitive,


socially responsible engineers and entrepreneurs with human values.

Mission of the Institute:

To provide quality education through innovation in teaching to create technologically


M1
competent engineers.
To Achieve excellence in research and development to advance science and
M2
technology to the ever changing needs of society.
To create outstanding professionals by facilitating state of the art platform capable of
M3
working in multi-cultural environment.
M4 To produce quality engineers with high ethical standards and professionalism.

Vision & Mission of the Program


Vision of the Department:

To develop professionally excellent, socially responsible Information Science Engineers and


Entrepreneurs through teaching, research and development and cognitive skills with human
eminence

Mission of the Department:

To foster the development of accountable and proficient professionals in the field


M1 of information science and engineering by emphasizing experiential learning and
a solid foundation in the fundamentals.
To infuse research and entrepreneurship through the ongoing creation of
M2
innovative solutions via intellectual tools and research-driven education.
To promote social responsibility and ethical conduct, nurturing a culture of
M3 integrity, empathy, and sustainable principles by fostering a compassionate global
community.
14.09.2023
MKV-TEMPLATE for IPCC (26.04.2022) Annexure-III

OPERATING SYSTEMS Semester 3


Course Code BCS303 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 3:0:2:0 SEE Marks 50
Total Hours of Pedagogy 40 hours Theory + 20 hours practicals Total Marks 100
Credits 04 Exam Hours 3
Examination nature (SEE) Theory

Course objectives:
● To Demonstrate the need for OS and different types of OS
● To discuss suitable techniques for management of different resources
● To demonstrate different APIs/Commands related to processor,
memory, storage and file system management.

Teaching-Learning Process (General Instructions)


Teachers can use the following strategies to accelerate the attainment of the various course outcomes.
1. Lecturer methods (L) need not to be only traditional lecture method, but alternative effective
teaching methods could be adopted to attain the outcomes.
2. Use of Video/Animation to explain functioning of various concepts.
3. Encourage collaborative (Group Learning) Learning in the class.
4. Adopt Problem Based Learning (PBL), which fosters students’ Analytical skills, develop design
thinking skills such as the ability to design, evaluate, generalize, and analyze information rather
than simply recall it.
5. Role play for process scheduling.
6. Demonstrate the installation of any one Linux OS on VMware/Virtual Box

MODULE-1 8 Hours
Introduction to operating systems, System structures: What operating systems do; Computer System
organization; Computer System architecture; Operating System structure; Operating System operations;
Process management; Memory management; Storage management; Protection and Security; Distributed
system; Special-purpose systems; Computing environments.

Operating System Services: User - Operating System interface; System calls; Types of system calls;
System programs; Operating system design and implementation; Operating System structure; Virtual
machines; Operating System debugging, Operating System generation; System boot.

Textbook 1: Chapter – 1 (1.1-1.12), 2 (2.2-2.11)

MODULE-2 8 Hours
Process Management: Process concept; Process scheduling; Operations on processes; Inter process
communication

Multi-threaded Programming: Overview; Multithreading models; Thread Libraries; Threading issues.

Process Scheduling: Basic concepts; Scheduling Criteria; Scheduling Algorithms; Thread scheduling;
Multiple-processor scheduling,

Textbook 1: Chapter – 3 (3.1-3.4), 4 (4.1-4.4), 5 (5.1 -5.5)

MODULE-3 8 Hours

1
14.09.2023
MKV-TEMPLATE for IPCC (26.04.2022) Annexure-III

Process Synchronization: Synchronization: The critical section problem; Peterson’s solution;


Synchronization hardware; Semaphores; Classical problems of synchronization;

Deadlocks: System model; Deadlock characterization; Methods for handling deadlocks; Deadlock
prevention; Deadlock avoidance; Deadlock detection and recovery from deadlock.

Textbook 1: Chapter – 6 (6.1-6.6), 7 (7.1 -7.7)

MODULE-4 8 Hours

Memory Management: Memory management strategies: Background; Swapping; Contiguous memory


allocation; Paging; Structure of page table; Segmentation.

Virtual Memory Management: Background; Demand paging; Copy-on-write; Page replacement;


Allocation of frames; Thrashing.

Textbook 1: Chapter -8 (8.1-8.6), 9 (9.1-9.6)


MODULE-5 8 Hours
File System, Implementation of File System: File system: File concept; Access methods; Directory and
Disk structure; File system mounting; File sharing; Implementing File system: File system structure; File
system implementation; Directory implementation; Allocation methods; Free space management.

Secondary Storage Structure, Protection: Mass storage structures; Disk structure; Disk attachment; Disk
scheduling; Disk management; Protection: Goals of protection, Principles of protection, Domain of
protection, Access matrix.

Textbook 1: Chapter – 10 (10.1-10.5) ,11 (11.1-11.5),12 (12.1-12.5), 14 (14.1-14.4)

2
14.09.2023
MKV-TEMPLATE for IPCC (26.04.2022) Annexure-III

PRACTICAL COMPONENT OF IPCC(May cover all / major modules)

Sl.N Experiments
O
1 Develop a c program to implement the Process system calls (fork (), exec(), wait(), create process,
terminate process)

2 Simulate the following CPU scheduling algorithms to find turnaround time and waiting time a) FCFS
b) SJF c) Round Robin d) Priority.

3 Develop a C program to simulate producer-consumer problem using semaphores.

4 Develop a C program which demonstrates interprocess communication between a reader


process and a writer process. Use mkfifo, open, read, write and close APIs in your program.
5
Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
6 Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.
7 Develop a C program to simulate page replacement algorithms:
a) FIFO b) LRU

8 Simulate following File Organization Techniques


a) Single level directory b) Two level directory
9 Develop a C program to simulate the Linked file allocation strategies.

10 Develop a C program to simulate SCAN disk scheduling algorithm.

Course outcomes (Course Skill Set):


At the end of the course, the student will be able to:
CO 1. Explain the structure and functionality of operating system
CO 2. Apply appropriate CPU scheduling algorithms for the given problem.
CO 3. Analyse the various techniques for process synchronization and deadlock handling.
CO 4. Apply the various techniques for memory management
CO 5. Explain file and secondary storage management strategies.
CO 6. Describe the need for information protection mechanisms

Assessment Details (both CIE and SEE)


The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%. The
minimum passing mark for the CIE is 40% of the maximum marks (20 marks out of 50) and for the SEE
minimum passing mark is 35% of the maximum marks (18 out of 50 marks). A student shall be deemed to
have satisfied the academic requirements and earned the credits allotted to each subject/ course if the student
secures a minimum of 40% (40 marks out of 100) in the sum total of the CIE (Continuous Internal Evaluation)
and SEE (Semester End Examination) taken together.

CIE for the theory component of the IPCC (maximum marks 50)
● IPCC means practical portion integrated with the theory of the course.
● CIE marks for the theory component are 25 marks and that for the practical component is 25 marks.
● 25 marks for the theory component are split into 15 marks for two Internal Assessment Tests (Two Tests,
each of 15 Marks with 01-hour duration, are to be conducted) and 10 marks for other assessment methods
3
14.09.2023
MKV-TEMPLATE for IPCC (26.04.2022) Annexure-III

mentioned in 22OB4.2. The first test at the end of 40-50% coverage of the syllabus and the second test
after covering 85-90% of the syllabus.
● Scaled-down marks of the sum of two tests and other assessment methods will be CIE marks for the theory
component of IPCC (that is for 25 marks).
● The student has to secure 40% of 25 marks to qualify in the CIE of the theory component of IPCC.
CIE for the practical component of the IPCC

● 15 marks for the conduction of the experiment and preparation of laboratory record, and 10 marks for the
test to be conducted after the completion of all the laboratory sessions.
● On completion of every experiment/program in the laboratory, the students shall be evaluated including
viva-voce and marks shall be awarded on the same day.
● The CIE marks awarded in the case of the Practical component shall be based on the continuous evaluation
of the laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all experiments’
write-ups are added and scaled down to 15 marks.
● The laboratory test (duration 02/03 hours) after completion of all the experiments shall be conducted for
50 marks and scaled down to 10 marks.
● Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory
component of IPCC for 25 marks.
● The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of the IPCC.
SEE for IPCC
Theory SEE will be conducted by University as per the scheduled timetable, with common question papers for
the course (duration 03 hours)
1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a maximum
of 3 sub-questions), should have a mix of topics under that module.
3. The students have to answer 5 full questions, selecting one full question from each module.
4. Marks scoredby the student shall be proportionally scaled down to 50 Marks
The theory portion of the IPCC shall be for both CIE and SEE, whereas the practical portion will have a
CIE component only. Questions mentioned in the SEE paper may include questions from the practical
component.
Suggested Learning Resources:
Textbooks
1. Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, Operating System Principles 8th edition,
Wiley-India, 2015
Reference Books
1. Ann McHoes Ida M Fylnn, Understanding Operating System, Cengage Learning, 6th Edition
2. D.M Dhamdhere, Operating Systems: A Concept Based Approach 3rd Ed, McGraw- Hill, 2013.
3. P.C.P. Bhatt, An Introduction to Operating Systems: Concepts and Practice 4th Edition, PHI(EEE),
2014.
4. William Stallings Operating Systems: Internals and Design Principles, 6th Edition, Pearson.

Web links and Video Lectures (e-Resources):

1. https://youtu.be/mXw9ruZaxzQ

4
14.09.2023
MKV-TEMPLATE for IPCC (26.04.2022) Annexure-III

2. https://youtu.be/vBURTt97EkA
3. https://www.youtube.com/watch?v=783KAB-
tuE4&list=PLIemF3uozcAKTgsCIj82voMK3TMR0YE_f
4. https://www.youtube.com/watch?v=3-
ITLMMeeXY&list=PL3pGy4HtqwD0n7bQfHjPnsWzkeRn6mkO

Activity Based Learning (Suggested Activities in Class)/ Practical Based learning


● Assessment Methods
o Case Study on Unix Based Systems (10 Marks)
o Lab Assessment (25 Marks)

5
OPERATING SYSTEMS LAB MANUAL(BCS303)

1. To write c program to implement the Process system calls (fork (), exec(), wait(), create
process, terminate process)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

main(void)
{
pid_t pid = 0;
int status;
pid = fork();

if (pid == 0)
{
printf("I am the child.");
execl("/bin/ls", "ls", "-l", "/home/ubuntu/", (char *) 0);
perror("In exec(): ");
}

if (pid > 0)
{
printf("I am the parent, and the child is %d.\n", pid);
pid = wait(&status);
printf("End of process %d: ", pid);

if (WIFEXITED(status))
{
printf("The process ended with exit(%d).\n", WEXITSTATUS(status));
}

if (WIFSIGNALED(status))
{
printf("The process ended with kill -%d.\n", WTERMSIG(status));
}
}

if (pid < 0)
{
perror("In fork():");
}

exit(0);
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 1


OPERATING SYSTEMS LAB MANUAL(BCS303)

2. Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority

a) FCFS

#include<stdio.h>

void main()
{

int i,j,bt[10],n,wt[10],tt[10],w1=0,t1=0;
float aw,at;

printf("enter no. of processes:\n");


scanf("%d",&n);

printf("enter the burst time of processes:");


for(i=0;i<n;i++)
scanf("%d",&bt[i]);

for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);

INPUT
Enter no of processes
3
enter bursttime
12
8
20

EXPECTED OUTPUT
bt wt tt
12 0 12
8 12 20
20 20 40

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 2


OPERATING SYSTEMS LAB MANUAL(BCS303)

aw=10.666670
at=24.00000

b) SJF

#include<stdio.h>

void main()
{

int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0;
float aw,at;

printf("enter no. of processes:\n");


scanf("%d",&n);

printf("enter the burst time of processes:");


for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
if(bt[i]>bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
}
for(i=0;i<n;i++)
printf("%d",bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);
}

INPUT:
Enter no of processes
3
enter burst time

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 3


OPERATING SYSTEMS LAB MANUAL(BCS303)

12
8
20

OUTPUT:
bt wt tt
12 8 20
8 0 8
20

20 40
aw=9.33
at=22.64

c) Round Robin

#include<stdio.h>
void main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;

printf("Enter number of processes:");


scanf("%d",&n);

printf("Enter burst time for sequences:");


for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}

printf("Enter time quantum:");


scanf("%d",&tq);

while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 4


OPERATING SYSTEMS LAB MANUAL(BCS303)

st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}

awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time");
for(i=0;i<n;i++)
printf("\n%d\t %d\t %d\t %d",i+1,bt[i],wt[i],tat[i]);
printf("\nAvg wait time is %f Avg turn around time is %f",awt,atat);

Input:
Enter no of jobs
4
Enter burst time
5
12
8
20

Output:
bt wt tt
5 0 5
12 5 13
8 13 25
20 25 45
aw=10.75000
at=22.000000

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 5


OPERATING SYSTEMS LAB MANUAL(BCS303)

d) Priority

#include<stdio.h>

void main()
{
int i,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],w1=0,t1=0,s;
float aw,at;

printf("Enter the number of processes:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("The process %d:\n",i+1);
printf("Enter the burst time of processes:");
scanf("%d",&bt[i]);
printf("Enter the priority of processes %d:",i+1);
scanf("%d",&prior[i]); pno[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(prior[i]<prior[j])
{
s=prior[i];
prior[i]=prior[j];
prior[j]=s;

s=bt[i];
bt[i]=bt[j];
bt[j]=s;

s=pno[i];
pno[i]=pno[j];
pno[j]=s;
}
}
}
for(i=0; i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
aw=w1/n;
at=t1/n;

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 6


OPERATING SYSTEMS LAB MANUAL(BCS303)

}
printf(" \n job \t bt \t wt \t tat \t prior\n");
for(i=0;i<n;i++)
printf("%d \t %d \t %d\t %d\t %d\n",pno[i],bt[i],wt[i],tt[i],prior[i]);
printf("aw=%f \t at=%f \n",aw,at);
}

Input:
Enter no of jobs
4
Enter bursttime
10
2
4
7
Enter priority values
4
2
1
3

Output:
Bt priority wt tt
4 1 0 4
2 2 4 6
7 3 6 13
10 4 13 23
aw=5.750000
at=12.500000

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 7


OPERATING SYSTEMS LAB MANUAL(BCS303)

3. Write a C program to simulate producer-consumer problem using semaphores

#include <stdio.h>
#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

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;
}

int wait(int s)
{
return (--s);
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 8


OPERATING SYSTEMS LAB MANUAL(BCS303)

int signal(int s)
{
return(++s);
}

void producer()
{
mutex=wait(mutex);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
full=signal(full);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
printf("\nConsumer consumes item %d",x);
x--;
empty=signal(empty);
mutex=signal(mutex);
}
Output
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer produces the item 1

Enter your choice:2


Consumer consumes item 1

Enter your choice:2


Buffer is empty!!

Enter your choice:1


Producer produces the item 1

Enter your choice:1


Producer produces the item 2

Enter your choice:1


Producer produces the item 3

Enter your choice:1


Buffer is full!!
Enter your choice:3

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 9


OPERATING SYSTEMS LAB MANUAL(BCS303)

4. Write a C program which demonstrates interproccess communication between a


reader process and a writer process.
Use mkfifo, open, read, write and close APIs in your program

/*Writer Process*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
char* fifoFile = “myfifo”; // Name of the FIFO file
int fd;

// Create the FIFO (if it doesnt exist)


mkfifo(fifoFile, 0666);

// Open the FIFO for writing


fd = open(fifoFile, O_WRONLY);

if (fd == -1)
{
perror(“Failed to open FIFO for writing”);
return 1;
}

// Write data to the FIFO


char message[] = “Hello, reader!”;
write(fd, message, strlen(message) + 1);

// Close the FIFO


close(fd);
return 0;
}

/* Reader Process */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
char* fifoFile = “myfifo”; // Name of the FIFO file
int fd;
char message[100]; // Buffer to read the message

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 10


OPERATING SYSTEMS LAB MANUAL(BCS303)

// Open the FIFO for reading


fd = open(fifoFile, O_RDONLY);
if (fd == -1)
{
Perror(”Failed to open FIFO for reading”);
return 1;
}

// Read data from the FIFO


read(fd, message, sizeof(message));
printf”Received: %s\n”, message);

// Close the FIFO


close(fd);
return 0;
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 11


OPERATING SYSTEMS LAB MANUAL(BCS303)

5. Write a C program to simulate Bankers Algorithm for Dead Lock Avoidance and
Prevention

#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10],
completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;

printf("Enter the no of processes : ");


scanf("%d", &p);

for(i = 0; i< p; i++)


completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("\n\nEnter the Available Resources :");


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];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 12


OPERATING SYSTEMS LAB MANUAL(BCS303)

for( j = 0; j < r; j++)


printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1; count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");

for( i = 0; i < p; i++)


printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}

Output:

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 13


OPERATING SYSTEMS LAB MANUAL(BCS303)

Enter the no of processes : 5


Enter the no of resources : 3
Enter the Max Matrix for each process :
For process 1 : 7
5
3

For process 2 : 3
2
2
For process 3 : 7
0
2
For process 4 : 2
2
2
For process 5 : 4
3
3
Enter the allocation for each process :
For process 1 : 0
1
0
For process 2 : 2
0
0
For process 3 : 3
0
2
For process 4 : 2
1
1
For process 5 : 0
0
2
Enter the Available Resources : 3
3
2
Max matrix: Allocation matrix:
753 010
322 200
702 302
222 211
433 002
Process 2 runs to completion!
Max matrix: Allocation matrix:
753 010
000 000
702 302
222 211

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 14


OPERATING SYSTEMS LAB MANUAL(BCS303)

433 002

Process 3 runs to completion!


Max matrix: Allocation matrix:
753 010
000 000
000 000
222 211
433 002
Process 4 runs to completion!
Max matrix: Allocation matrix:
753 010
000 000
000 000
000 000
433 002
Process 1 runs to completion!
Max matrix: Allocation matrix:
000 000
000 000
000 000
000 000
433 002

Process 5 runs to completion!


The system is in a safe state!!
Safe Sequence: < 2 3 4 1 5 >

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 15


OPERATING SYSTEMS LAB MANUAL(BCS303)

6. Write a C program to simulate the following contiguous memory allocation Techniques:


a) Worst fit b) Best fit c) First fit.

FIRST-FIT
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];

printf("\n\tMemory Management Scheme - First Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
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]);
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 16


OPERATING SYSTEMS LAB MANUAL(BCS303)

INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:- File 1: 1 File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3

BEST-FIT
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];

printf("\nEnter the number of blocks:");


scanf("%d",&nb);

printf("Enter the number of files:");


scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");


for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}

printf("Enter the size of the files :-\n");


for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}

for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 17


OPERATING SYSTEMS LAB MANUAL(BCS303)

{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}

INPUT
Enter the number of blocks: 3
Enter the number of files: 2

Enter the size of the blocks:-


Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the files:-


File 1: 1
File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 18


OPERATING SYSTEMS LAB MANUAL(BCS303)

WORST-FIT
#include<stdio.h>
#define max 25

void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];

printf("\n\tMemory Management Scheme - Worst Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);

printf("Enter the number of files:");


scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");


for(i=1;i<=nb;i++)
{
printf("Block %d:",i); scanf("%d",&b[i]);
}

printf("Enter the size of the files :-\n");


for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}

for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<=nf)
{
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++)

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 19


OPERATING SYSTEMS LAB MANUAL(BCS303)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}

INPUT
Enter the number of blocks: 3
Enter the number of files: 2

Enter the size of the blocks:-


Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the files:-


File 1: 1
File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 20


OPERATING SYSTEMS LAB MANUAL(BCS303)

7. Write a C program to simulate page replacement algorithms:


a) FIFO b) LRU

#include<stdio.h>
#include<stdlib.h>
void FIFO(char [ ],char [ ],int,int);
void lru(char [ ],char [ ],int,int);
void opt(char [ ],char [ ],int,int);
int main()
{
int ch,YN=1,i,l,f;
char F[10],s[25];

printf("\n\n\tEnter the no of empty frames:");


scanf("%d",&f);

printf("\n\n\tEnter the length of the string: ");


scanf("%d",&l);

printf("\n\n\tEnter the string: ");


scanf("%s",s);

for(i=0;i<f;i++)
F[i]=-1;
do
{
printf("\n\n\t*********** MENU ***********");
printf("\n\n\t1:FIFO\n\n\t2:LRU\n\n\t4:EXIT");
printf("\n\n\tEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: for(i=0;i<f;i++)
{
F[i]=-1;
}
FIFO(s,F,l,f);
break;

case 2: for(i=0;i<f;i++)
{
F[i]=-1;
}
lru(s,F,l,f);
break;
case 4: exit(0);
}
printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 : ");

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 21


OPERATING SYSTEMS LAB MANUAL(BCS303)

scanf("%d",&YN);
}
while(YN==1);
return(0);
}
//FIFO
void FIFO(char s[],char F[],int l,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}
if(flag==0)
{
printf("\n\t%c\t",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
printf("\tPage-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
printf("\tNo page-fault");
}
if(j==f)
j=0;
}
}
//LRU
void lru(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,cnt=0,top=0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for(i=0;i<l;i++)

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 22


OPERATING SYSTEMS LAB MANUAL(BCS303)

{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
{
flag=1;
break;
}
}
printf("\n\t%c\t",s[i]);
if(j!=f && flag!=1)
{
F[top]=s[i];
j++;
if(j!=f)
top++;
}
else
{
if(flag!=1)
{
for(k=0;k<top;k++)
{
F[k]=F[k+1];
}
F[top]=s[i];
}
if(flag==1)
{
for(m=k;m<top;m++)
{
F[m]=F[m+1];
}
F[top]=s[i];
}
}
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
if(flag==0)
{
printf("\tPage-fault%d",cnt);
cnt++;
}
else
printf("\tNo page fault");
flag=0;
}
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 23


OPERATING SYSTEMS LAB MANUAL(BCS303)

Output:

Enter the no of empty frames: 3


Enter the length of the string: 5
Enter the string: hello

*********** MENU ***********


1:FIFO
2:LRU
4:EXIT

Enter your choice: 1


PAGE FRAMES FAULTS
h h Page-fault 0
e h e Page-fault 1
l h e l Page-fault 2
l h e l No page-fault
o o e l Page-fault 3

Do u want to continue IF YES PRESS 1


IF NO PRESS 0 : 1

*********** MENU ***********


1:FIFO
2:LRU
4:EXIT
Enter your choice: 2
PAGE FRAMES FAULTS
h h Page-fault 0
e h e Page-fault 1
l h e l Page-fault 2
l h e l No page fault
o e l o Page-fault 3

Do u want to continue IF YES PRESS 1


IF NO PRESS 0 : 1

*********** MENU ***********


1:FIFO
2:LRU
4:EXIT
Enter your choice: 4

8. Simulate following File Organization Techniques


a) Single level directory b) Two level directory

#include<stdio.h>

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 24


OPERATING SYSTEMS LAB MANUAL(BCS303)

void main()
{
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;

printf("enter number of directorios:");


scanf("%d",&master);

printf("enter names of directories:");


for(i=0;i<master;i++)
scanf("%s",&d[i]);

printf("enter size of directories:");


for(i=0;i<master;i++)
scanf("%d",&s[i]);

printf("enter the file names :");


for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");

printf(" directory\tsize\tfilenames\n");

printf("*************************************************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
}
printf("\t\n");
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 25


OPERATING SYSTEMS LAB MANUAL(BCS303)

b) Two level directory


#include<string.h>
#include<stdlib.h>
#include<stdio.h>
struct
{

char dname[10],fname[10][10];
int fcnt;
}dir[10];

void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 26


OPERATING SYSTEMS LAB MANUAL(BCS303)

if(strcmp(d,dir[i].dname)==0)
{

printf("Enter name of the file -- ");


scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{

if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt
strcpy(dir[i].fname[k],dir[i].fname[dir[i],
fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 27


OPERATING SYSTEMS LAB MANUAL(BCS303)

printf("\nNo Directory's ");


else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}

9. Write a C program to simulate the Linked file allocation strategies.


#include <stdio.h>
#include <stdlib.h>

#define MAX_BLOCKS 100

// Structure to represent a block on disk


struct Block {
int block_number;
int file_id;
struct Block* next;
};

// Function to initialize blocks


void initialize_blocks(struct Block* blocks[], int num_blocks) {
for (int i = 0; i < num_blocks; i++) {
blocks[i] = NULL;
}
}

// Function to allocate a block to a file


void allocate_block(struct Block* blocks[], int file_id, int block_number) {
struct Block* new_block = (struct Block*)malloc(sizeof(struct Block));
new_block->block_number = block_number;
new_block->file_id = file_id;
new_block->next = NULL;

if (blocks[block_number] == NULL) {

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 28


OPERATING SYSTEMS LAB MANUAL(BCS303)

blocks[block_number] = new_block;
} else {
struct Block* temp = blocks[block_number];
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_block;
}
}

// Function to display file allocation


void display_allocation(struct Block* blocks[], int num_blocks) {
printf("File Allocation Table:\n");
for (int i = 0; i < num_blocks; i++) {
if (blocks[i] != NULL) {
printf("Block %d allocated to File %d\n", i, blocks[i]->file_id);
struct Block* temp = blocks[i]->next;
while (temp != NULL) {
printf(" Block %d allocated to File %d\n", temp->block_number, temp-
>file_id);
temp = temp->next;
}
}
}
}

int main() {
struct Block* blocks[MAX_BLOCKS];
int num_blocks, num_files;

printf("Enter the total number of blocks on disk: ");


scanf("%d", &num_blocks);
printf("Enter the total number of files: ");
scanf("%d", &num_files);

initialize_blocks(blocks, num_blocks);

for (int i = 0; i < num_files; i++) {


int num_blocks_allocated;
printf("Enter the number of blocks allocated to File %d: ", i + 1);
scanf("%d", &num_blocks_allocated);
printf("Enter the block numbers allocated to File %d: ", i + 1);
for (int j = 0; j < num_blocks_allocated; j++) {
int block_number;
scanf("%d", &block_number);
allocate_block(blocks, i + 1, block_number);

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 29


OPERATING SYSTEMS LAB MANUAL(BCS303)

}
}

display_allocation(blocks, num_blocks);

return 0;
}

Output:
Enter the total number of blocks on disk: 10
Enter the total number of files: 3
Enter the number of blocks allocated to File 1: 3
Enter the block numbers allocated to File 1: 2 5 8
Enter the number of blocks allocated to File 2: 2
Enter the block numbers allocated to File 2: 1 4
Enter the number of blocks allocated to File 3: 4
Enter the block numbers allocated to File 3: 0 3 6 9
File Allocation Table:
Block 0 allocated to File 3
Block 3 allocated to File 3
Block 1 allocated to File 2
Block 4 allocated to File 2
Block 2 allocated to File 1
Block 5 allocated to File 1
Block 6 allocated to File 3
Block 9 allocated to File 3
Block 8 allocated to File 1

10. Write a C program to simulate SCAN disk scheduling algorithm.

#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)

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 30


OPERATING SYSTEMS LAB MANUAL(BCS303)

{
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);

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 31


OPERATING SYSTEMS LAB MANUAL(BCS303)

avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

Mrs. Meghana G R, Department of IS&E, JIT, Davanagere 32

You might also like