0% found this document useful (0 votes)
12 views53 pages

Os Prinout

Uploaded by

vaishnavi.pran
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)
12 views53 pages

Os Prinout

Uploaded by

vaishnavi.pran
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/ 53

“OPERATING SYSTEMS

LAB”

Subcode: BCS303

Prepared By

Prof. Lakshmi K K Prof. Usha C R


Assistant Professor Assistant Professor

Department of Artificial Intelligence and Machine


Learning
K S Institute of Technology,
Bangalore-109.
Document Log

OPERATING SYSTEM
Name of the document LABORATORY

Syllabus Scheme 2022

Subject code BCSL303

Editorial Committee Prof. Lakshmi KK

Approved by HOD, Dept. of AIML

Lab faculty Prof. Lakshmi KK

Computer Programmer Mr. Ganesh M L


K. S. INSTITUTE OF TECHNOLOGY
#14, Raghuvanahalli, Kanakapura Main Road, Bengaluru-5600109
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MCHINE LEARNING
Course: OPERATING SYSTEMS
Type: IPCC Course Code: BCS303
Name of the Faculty: LAKSHMI K K SEM : 3
No of Hours per week
Theory Practical/Field
Total/Week Total teaching hours
(Lecture Class) Work/Allied Activities
3 2 5 40T+20P
Marks
Internal Assessment Examination Total Credits
50 50 100 4
Aim/Objective of the Course:

 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.
Course Learning Outcomes: After completing the course, the students will be able to,
K - Level
CO# Course Outcomes

Explain the structure and functionality of operating Understang


BCS303.1 (K2)
system
BCS303.2 Applying (K3)
Apply appropriate CPU scheduling algorithms for the given
problem.
BCS303.3 Apply the various techniques for process synchronization Applying (K3)
and deadlock handling.
BCS303.4 Apply the various techniques for memory management Applying (K3)

BCS303.5 Illustrate file and secondary storage management strategies Applying (K3)
and the need for information protection mechanisms.
Syllabus Content:
CO1
Module 1: 08hrs
Introduction to operating systems, System structures: What operating systems do; PO1 –3
Computer System PO2 –2
PO3 –1
organization; Computer System architecture; Operating System structure; P12 –1
Operating System operations; PSO1-2
Process management; Memory management; Storage management; Protection PSO2-2

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: CO2
Process Management: Process concept; Process scheduling; Operations on 08 hrs.
processes; Inter process communication PO1 –3
Multi-threaded Programming: Overview; Multithreading models; Thread PO2 –2
Libraries; Threading issues. PO3 – 1
Process Scheduling: Basic concepts; Scheduling Criteria; Scheduling PSO1 -2
PSO2 -2
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:
CO3

Process Synchronization: Synchronization: The critical section problem; Peterson’s 08hrs


solution; Synchronization hardware; Semaphores; Classical problems of synchronization; PO1 –3
Deadlocks: System model; Deadlock characterization; Methods for handling deadlocks; PO2 –2
Deadlock prevention; Deadlock avoidance; Deadlock detection and recovery from PO3 –1
deadlock. P12 –1
PSO1-2
Textbook 1: Chapter – 6 (6.1-6.6), 7 (7.1 -7.7) PSO2-2

Module 4: CO4
Memory Management: Memory management strategies: Background; Swapping;
Contiguous memory allocation; Paging; Structure of page table; Segmentation. 08hrs
Virtual Memory Management: Background; Demand paging; Copy-on-write; PO1 –3
PO2 –2
Page replacement; Allocation of frames; Thrashing. PO3 –1
Textbook 1: Chapter -8 (8.1-8.6), 9 (9.1-9.6) P12 –1
PSO1-2
PSO2-2
CO5
Module 5:
08 hrs
File System, Implementation of File System: File system: File concept; Access
methods; Directory and Disk structure; File system mounting; File sharing; PO1 –3
PO2 –2
Implementing File system: File system structure; File system implementation;
PO3 –1
Directory implementation; Allocation methods; Free space management. P12 –1
Secondary Storage Structure, Protection: Mass storage structures; Disk structure; PSO1-2
Disk attachment; Disk scheduling; Disk management; Protection: Goals of PSO2-2
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)
Text Books: - (specify minimum two foreign authors text books)
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.
Useful Websites
1. https://youtu.be/mXw9ruZaxzQ

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

Teaching and Learning Methods:


1. Lecture class: 40hrs.
2. Practical classes :20hrs

Assessment:
Type of test/examination: Written examination
Continuous Internal Evaluation(CIE) : 50 marks (Average of two tests will be considered)
Test marks:15
Assignment:10
Practicals:25

Semester End Exam(SEE) : 100 marks (students have to answer all main questions)
which will be reduced to 50 marks
Test duration: 1 :30 hrs
Examination duration: 3 hrs
CO to PO Mapping
PO1: Science and engineering PO7:Environment and Society
Knowledge PO8:Ethics
PO2: Problem Analysis PO9:Individual& Team Work
PO10: Communication
PO3: Design & Development
PO11:Project Mngmt& Finance
PO4:Investigations of Complex PO12:Life long Learning
Problems
PO5: Modern Tool Usage
PO6: Engineer & Society

PSO1: Ability to apply the fundamental concepts of Artificial Intelligence and Machine Learning
todesign and develop solutions to multidisciplinary problems of social concern.

PSO2: Ability to use the inculcated experiential learning for research and development activities
incompliance with National Education Policy.

CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
BCS303.1 3 2 1 - - - - - - - - 1 2 2
BCS303.2 3 2 1 - - - - - - - - 1 2 2
BCS303.3 3 2 1 - - - - - - - - 1 2 2
BCS303.4 3 2 1 - - - - - - - - 1 2 2
BCS303.5 3 2 1 - - - - - - - - 1 2 2
BCS303 3 2 1 - - - - - - - - 1 2 2

CO-PO MAPPING Justification Table


Sl. CO PO Number Of Key Elements of PO Mapped To Justification
No. CO
CO1: Explain the structure and functionality of operating system
1 CO 1 1 Strongly agreed as ability to enhance 3
operating system services.
Moderately agreed, as problem analysis
2 process management, memory man- 2
agement.
Weakly agreed as design in virtual ma-
3 1
chines.
Weakly, as life long learning knowledge
12 1
enhancement is obtained.
2 PSO 1 Moderately agreed as coding ability is 2
enhanced.
3
PSO2 Moderately, agreed as it depends on individual 2
ability to act for social causes.

CO2: Apply appropriate CPU scheduling algorithms for the given problem.
4 CO2 1 Strongly agreed as process management 3
and multi threading in operating system.
Moderately agreed, as problem analysis
2 2
skill in process scheduling.
Weakly agreed as design of multi
3 1
threading models.
Strongly, as individual knowledge en-
12 1
hancement is obtained.
5 PSO 1 Moderately relevant as problem solving ability 2
is enhanced.

6 Moderately, agreed as it depends on individual 2


PSO2 ability to act for social causes.

CO3: Apply the various techniques for process synchronization and deadlock handling.
7 CO3 1 Strongly agreed as ability to apply engi- 3
neering knowledge in deadlock and pro-
cess synchronization.
Moderately agreed, as problem analysis
2 2
skill in programming is strengthened.
Weakly agreed as programming exper-
3 1
tise alone cannot make designs.
12 Strongly, as individual knowledge enhancement 1
is obtained.
8 PSO 1 Strongly relevant as coding ability is enhanced. 2

9 Moderately, agreed as it depends on individual 2


PSO2 ability to act for social causes.
CO4: Apply the various techniques for memory management
10 CO4 1 Strongly agreed as ability to apply engi- 3
neering knowledge in memory man-
agement Techniques
Moderately agreed, as problem analysis
2 skill in deadlock detection. 2

Weakly agreed to design process syn-


3 chronization and deadlock algorithms. 1

12 Weekly agreed life long learning knowledge in 1


process synchronization.
11 PSO 1 Moderately relevant as coding ability is 2
enhanced.

12 Moderately, agreed as it depends on individual 2


PSO2 ability to act for social causes.

CO5: Illustrate file and secondary storage management strategies and the need for information
protection mechanisms.
13 CO5 1 Strongly agreed as ability to apply engi- 3
neering knowledge in file system and
disk management.
Moderately agreed, as problem analysis
2 2
skill in disk management.
Weakly agreed to design disk structure
3 1
and directory structure.
Weekly agreed life long learning
12 knowledge in disk structure of operating 1
system.
14 PSO 1 Moderately relevant as coding ability is 2
enhanced.
15 Moderately, agreed as it depends on individual 2
PSO2 ability to act for social causes.

CO PO mapping for the events conducted after gap Identification

Sl. NO Gap Identification CO Relevant PO Mapping


1 Quiz CO1, CO2, CO3, CO4, CO5 PO9

Signature of Course in-charge Signature of Module Coordinator Signature of H.O.D


OPERATING SYSTEMS LAB BCS303

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

#include <stdio.h> // Standard input/output definitions


#include <stdlib.h> // Standard library for system calls
#include <sys/types.h> // Definitions of data types used in system calls
#include <unistd.h> // Standard symbolic constants and types #include
<sys/wait.h> // Declarations for waiting

int main()
{
pid_t pid; // pid_t is used to store process IDs
int status; // Used to store the status of the child process
pid = fork(); // Create a new process
if (pid == -1)
{
// If fork() returns -1, an error occurred
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
// If fork() returns 0, we are in the child process
printf("Child process, PID = %u\n", getpid());
execlp("ls", "ls", NULL);
// If exec() is successful, it doesn't return; if it does return, there was an error
perror("exec failed");
exit(EXIT_FAILURE);
}

else
{
// If fork() returns a positive number, we are in the parent process
// and the return value is the PID of the newly created child process
printf("Parent process, PID = %u\n", getpid());

// The parent process waits for the child to complete


if (waitpid(pid, &status, 0) == -1)
{
perror("wait failed");
exit(EXIT_FAILURE);

Dept. of AIML, KSIT Page 1


OPERATING SYSTEMS LAB BCS303

// Verify if the child process ended normally

if (WIFEXITED(status))
{
printf("Child exited with status %d\n", WEXITSTATUS(status));
}
else
{
printf("Child terminated abnormally\n");
}
// At this point, the child has finished.
exit(EXIT_SUCCESS);
}
}
output

Dept. of AIML, KSIT Page 2


OPERATING SYSTEMS LAB 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>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20): ");
scanf("%d",&n);
printf("\nEnter Process Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]: ",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");


//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}

Dept. of AIML, KSIT Page 3


OPERATING SYSTEMS LAB BCS303

Output:

b) SJF
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process: ");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d: ",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];

Dept. of AIML, KSIT Page 4


OPERATING SYSTEMS LAB BCS303

p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
return 0;
}

Output

Dept. of AIML, KSIT Page 5


OPERATING SYSTEMS LAB BCS303

c) Round Robin

#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else

Dept. of AIML, KSIT Page 6


OPERATING SYSTEMS LAB BCS303

count=0;
}

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);


printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}

Output

d) Priority

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;

Dept. of AIML, KSIT Page 7


OPERATING SYSTEMS LAB BCS303

}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}

Dept. of AIML, KSIT Page 8


OPERATING SYSTEMS LAB BCS303

Output:

Dept. of AIML, KSIT Page 9


OPERATING SYSTEMS LAB BCS303

Program-3
Develop a C program to simulate producer-consumer problem using
semaphores.
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf("\n 1. Produce \t 2. Consume \t3. Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\n Buffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}

Dept. of AIML, KSIT Page 10


OPERATING SYSTEMS LAB BCS303

Output

Dept. of AIML, KSIT Page 11


OPERATING SYSTEMS LAB BCS303

PROGRAM 4
Develop a C program which demonstrates inter-process 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 <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char buf[1024];
/* create the FIFO (named pipe) */
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi"));
/* write "Hi" to the FIFO */
close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}

/* Reader Process*/
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF 1024
int main()
{
int fd;
/* A temp FIFO file is not created in reader */
char *myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);

Dept. of AIML, KSIT Page 12


OPERATING SYSTEMS LAB BCS303

printf("Writer: %s\n", buf);


close(fd);
return 0;
}

OUTPUT

Dept. of AIML, KSIT Page 13


OPERATING SYSTEMS LAB BCS303

PROGRAM 5
Develop a C program to simulate Bankers Algorithm for DeadLock
Avoidance.
#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++)

Dept. of AIML, KSIT Page 14


OPERATING SYSTEMS LAB BCS303

printf("%d ", Max[i][j]);


printf("\t\t");
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]);

Dept. of AIML, KSIT Page 15


OPERATING SYSTEMS LAB BCS303

printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
OUTPUT

Dept. of AIML, KSIT Page 16


OPERATING SYSTEMS LAB BCS303

PROGRAM-6
Develop a C program to simulate the following contiguous memory
allocation
Techniques
a) Worst fit

#include <stdio.h>
int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{

Dept. of AIML, KSIT Page 17


OPERATING SYSTEMS LAB BCS303

for(n = 0; n < number_of_blocks; n++)


{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");
return 0;
}

Dept. of AIML, KSIT Page 18


OPERATING SYSTEMS LAB BCS303

OUTPUT

b) Best Fit

#include<stdio.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}

Dept. of AIML, KSIT Page 19


OPERATING SYSTEMS LAB BCS303

for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

OUTPUT:

Dept. of AIML, KSIT Page 20


OPERATING SYSTEMS LAB BCS303

c) First 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 - 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) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");

Dept. of AIML, KSIT Page 21


OPERATING SYSTEMS LAB BCS303

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

OUTPUT

Dept. of AIML, KSIT Page 22


OPERATING SYSTEMS LAB BCS303

Program-7
Develop a C program to simulate page replacement algorithms

a) FIFO
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

Dept. of AIML, KSIT Page 23


OPERATING SYSTEMS LAB BCS303

OUTPUT

b) LRU
#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n………………………………..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;

Dept. of AIML, KSIT Page 24


OPERATING SYSTEMS LAB BCS303

} for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}

Dept. of AIML, KSIT Page 25


OPERATING SYSTEMS LAB BCS303

for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
OUTPUT

Dept. of AIML, KSIT Page 26


OPERATING SYSTEMS LAB BCS303

Program-8

Simulate following File Organization Techniques


a) Single level directory
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]); break; } }
if(i==dir.fcnt) printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)

Dept. of AIML, KSIT Page 27


OPERATING SYSTEMS LAB BCS303

{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);

break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}

Dept. of AIML, KSIT Page 28


OPERATING SYSTEMS LAB BCS303

OUTPUT

Dept. of AIML, KSIT Page 29


OPERATING SYSTEMS LAB 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++)
{
if(strcmp(d,dir[i].dname)==0)

Dept. of AIML, KSIT Page 30


OPERATING SYSTEMS LAB BCS303

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)
printf("\nNo Directory's ");
else
{

Dept. of AIML, KSIT Page 31


OPERATING SYSTEMS LAB BCS303

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

OUTPUT

Dept. of AIML, KSIT Page 32


OPERATING SYSTEMS LAB BCS303

Program-9

Develop a C program to simulate the Linked file allocation strategies


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;

Dept. of AIML, KSIT Page 33


OPERATING SYSTEMS LAB BCS303

else
exit(0);
getch();
}

OUTPUT

Dept. of AIML, KSIT Page 34


OPERATING SYSTEMS LAB BCS303

Program-10

Develop a C program to simulate SCAN disk scheduling algorithm


#include <stdio.h>
int request[50];
int SIZE;
int pre;
int head;
int uptrack;
int downtrack;
struct max{
int up;
int down;
} kate[50];
int dist(int a, int b){
if (a > b)
return a - b;
return b - a;
}
void sort(int n){
int i, j;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (request[j] > request[j + 1]){
int temp = request[j];
request[j] = request[j + 1];
request[j + 1] = temp;
}
}
}
j = 0;
i = 0;
while (request[i] != head){
kate[j].down = request[i];
j++;
i++;
}
downtrack = j;
i++;
j = 0;
while (i < n){
kate[j].up = request[i];
j++;
i++;
}

Dept. of AIML, KSIT Page 35


OPERATING SYSTEMS LAB BCS303

uptrack = j;
}
void scan(int n)
{
int i;
int seekcount = 0;

printf("SEEK SEQUENCE = ");


sort(n);
if (pre < head){
for (i = 0; i < uptrack; i++){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
for (i = downtrack - 1; i > 0; i--){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
}
else{
for (i = downtrack - 1; i >= 0; i--){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
for (i = 0; i < uptrack - 1; i++){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
}
printf(" %d\nTOTAL DISTANCE :%d", head, seekcount);
}
int main(){
int n, i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d", &SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d", &n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for (i = 0; i < n; i++)
scanf("%d", &request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d", &head);

Dept. of AIML, KSIT Page 36


OPERATING SYSTEMS LAB BCS303

request[n] = head;
request[n + 1] = SIZE - 1;
request[n + 2] = 0;
printf("ENTER THE PRE REQUEST :\n");
scanf("%d", &pre);
scan(n + 3);
}

OUTPUT

Dept. of AIML, KSIT Page 37


OPERATING SYSTEMS LAB BCS303

VIVA QUESTIONS
1 What is Operating System
2 What is a Kernel
3 What is a process
4 What are the different states of the process
5 What is a thread
6 What is the difference between process and thread
7 What are the benefits of Multithread programming
8 What is a thrashing
9 What is Buffer
10 What is Virtual Memory
11 What is demand paging
12 What are the different scheduling algorithm
13 Describe the objective of multi programming
14 What is fragmentation
15 What is the basic function of Paging
16 What is direct access method
17 What is multitasking
18 What is caching
19 What are interrupts
20 What are the advantages of Semaphores
21 What is IPC
22 What is a Zombie process
23 What is context stretching
24 What is a critical section
25 What are the advantages of multi threating
26 Define the term bounded waiting
27 What is concurrence
28 What is a deadlock
29 Write down the advantages of virtual memory
30 How to recover from a dead lock

Dept. of AIML, KSIT Page 38

You might also like