0% found this document useful (0 votes)
113 views37 pages

Opearating System Manual

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)
113 views37 pages

Opearating System Manual

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/ 37

BLDEA's

V P Dr. P. G. Halakatti College of Engineering and


Technology, Vijayapur
Approved by AICTE New Delhi, Recognized by Govt. of Karnataka and Affiliated to VTU Belagavi

LABORATORY MANUAL
COURSE: OPERATING SYSTEMS

COURSE CODE: BCS303

SEMESTER: III

DEPARTMENT OF CSE (Artificial Intelligence and Machine Learning)


OPERATING SYSTEMS LAB MANUAL (BCS303)

1. Course Details
Year / Semester 2nd/ 3rd Academic Year 2022-23
Course Title Operating Systems Course Code BCS303
Course Type CIE Marks 50
(Theory/Practical Theory SEE Marks 50
/Integrated ) Total Marks 100
Teaching Hours/Week 3:0:2:0 Duration of SEE 3
(L:T:P:S)
Lab Manual Authors Sign - Date

Checked By Sign - Date

2. Institute Mission:
 To Enrich Students with the essence of science and engineering knowledge, Professional ethics
and social values.
 To instill creativity and Research Temperament to Reach the Greater Heights of Professional
Success.

3. Institute Vision: To Emerge as a Widely Acknowledged Centre in Technical Education and


Research to Cater the need of Society with a Futuristic outlook.

4. Program Objectives: The educational objectives of the Mechanical Engineering Program are to
prepare our graduates to:
PO1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems.

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

PO3: Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate consideration
for the public health and safety, cultural, societal and environmental considerations.

PO4: Conduct investigations of complex problems: Use of 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
engineering and IT tools including prediction and modeling to complex 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.

Department of CSE(AIML) 3
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL (BCS303)

PO7: Environment and sustainability: Understand the impact of the professional 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 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 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 to give and receive
clear instructions.

PO11: Project management and finance: Demonstrate knowledge and understanding of the
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.

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

6. Course Outcomes
CO1 Explain the structure and functionality of operating system
CO2 Apply appropriate CPU scheduling algorithms for the given problem.
CO3 Analyze the various techniques for process synchronization and deadlock handling
CO4 Apply the various techniques for memory management
CO5 Explain file and secondary storage management strategies.
CO6 Describe the need for information protection mechanisms

Department of CSE(AIML) 3
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL (BCS303)

7. Assessment Details
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
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 scored by 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
Department of CSE(AIML) 3
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

Title of the Program


Sl No.

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.

Develop a C program which demonstrates interprocess communication between a reader


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

Develop a C program to simulate the following contiguous memory allocation


6
Techniques: a) Worst fit b) Best fit c) First fit.

7 Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU

Simulate following File Organization Techniques a) Single level directory b) Two level
8
directory

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

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

Department of CSE(AIML) 4
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

1. Develop a C program to implements the Process system calls fork(), exec(), wait(),create
process, terminate process.

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

int main() {
// Create a child process
pid_t pid = fork();

if (pid == 0) {
// Child process
printf("I am the child process!\n");
execlp("/bin/ls", "/bin/ls", NULL); // Replace with the program you want to run
} else if (pid > 0) {
// Parent process
printf("I am the parent process!\n");
wait(NULL); // Wait for the child process to finish
} else {
// Error
perror("fork");
exit(1);
}

return 0;
}

Output
I am the child process!\
I am the parent process!
a.out program1.c program2.c

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

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

#include <stdio.h>
// Process structure
struct Process {
int process_id;
int burst_time;
int priority; // For Priority Scheduling
};

// Function to calculate waiting time and turnaround time for FCFS


void fcfs(struct Process processes[], int n, int waiting_time[], int turnaround_time[]) {
waiting_time[0] = 0;

for (int i = 1; i < n; i++) {


waiting_time[i] = waiting_time[i - 1] + processes[i - 1].burst_time;
}

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


turnaround_time[i] = waiting_time[i] + processes[i].burst_time;
}
}

// Function to calculate waiting time and turnaround time for SJF


void sjf(struct Process processes[], int n, int waiting_time[], int turnaround_time[]) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].burst_time > processes[j].burst_time) {
struct Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}

waiting_time[0] = 0;

for (int i = 1; i < n; i++) {


waiting_time[i] = waiting_time[i - 1] + processes[i - 1].burst_time;
}

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


turnaround_time[i] = waiting_time[i] + processes[i].burst_time;
}
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
// Function to calculate waiting time and turnaround time for Round Robin
void round_robin(struct Process processes[], int n, int quantum, int waiting_time[], int turnaround_time[]) {
int remaining_time[n];
for (int i = 0; i < n; i++) {
remaining_time[i] = processes[i].burst_time;
}

int time = 0;

while (1) {
int done = 1;

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


if (remaining_time[i] > 0) {
done = 0;

if (remaining_time[i] > quantum) {


time += quantum;
remaining_time[i] -= quantum;
} else {
time += remaining_time[i];
waiting_time[i] = time - processes[i].burst_time;
remaining_time[i] = 0;
}
}
}

if (done == 1) {
break;
}
}

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


turnaround_time[i] = waiting_time[i] + processes[i].burst_time;
}
}

// Function to calculate waiting time and turnaround time for Priority Scheduling
void priority_scheduling(struct Process processes[], int n, int waiting_time[], int turnaround_time[]) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].priority > processes[j].priority) {
struct Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
waiting_time[0] = 0;

for (int i = 1; i < n; i++) {


waiting_time[i] = waiting_time[i - 1] + processes[i - 1].burst_time;
}

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


turnaround_time[i] = waiting_time[i] + processes[i].burst_time;
}
}

// Function to print the results


void print_results(struct Process processes[], int n, int waiting_time[], int turnaround_time[]) {
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");

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


printf("%d\t%d\t\t%d\t\t%d\n", processes[i].process_id, processes[i].burst_time, waiting_time[i],
turnaround_time[i]);
}
}

int main() {
int n, quantum;

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


scanf("%d", &n);

struct Process processes[n];


int waiting_time[n], turnaround_time[n];

// Input process details


for (int i = 0; i < n; i++) {
processes[i].process_id = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &processes[i].burst_time);
printf("Enter priority for process %d: ", i + 1);
scanf("%d", &processes[i].priority);
}

// FCFS
fcfs(processes, n, waiting_time, turnaround_time);
printf("\nFCFS Scheduling:\n");
print_results(processes, n, waiting_time, turnaround_time);

// SJF
sjf(processes, n, waiting_time, turnaround_time);
printf("\nSJF Scheduling:\n");
print_results(processes, n, waiting_time, turnaround_time);

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
// Round Robin
printf("\nEnter time quantum for Round Robin: ");
scanf("%d", &quantum);
round_robin(processes, n, quantum, waiting_time, turnaround_time);
printf("\nRound Robin Scheduling:\n");
print_results(processes, n, waiting_time, turnaround_time);

// Priority Scheduling
priority_scheduling(processes, n, waiting_time, turnaround_time);
printf("\nPriority Scheduling:\n");
print_results(processes, n, waiting_time, turnaround_time);

return 0;
}

Output
Enter the number of processes: 3
Enter burst time for process 1: 24
Enter priority for process 1: 1
Enter burst time for process 2: 3
Enter priority for process 2: 2
Enter burst time for process 3: 3
Enter priority for process 3: 3

FCFS Scheduling:
Process Burst Time Waiting Time Turnaround Time
1 24 0 24
2 3 24 27
3 3 27 30
SJF Scheduling:
Process Burst Time Waiting Time Turnaround Time
2 3 0 3
3 3 3 6
1 24 6 30
Enter time quantum for Round Robin: 4
Round Robin Scheduling:
Process Burst Time Waiting Time Turnaround Time
2 3 0 3
3 3 3 6
1 24 6 30
Priority Scheduling:
Process Burst Time Waiting Time Turnaround Time
1 24 0 24
2 3 24 27
3 3 27 30

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

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

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

#define BUFFER_SIZE 5

sem_t empty, full, mutex;


int buffer[BUFFER_SIZE];
int in = 0, out = 0;

void *producer(void *arg) {


int item;

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


item = rand() % 100; // Produce a random item
sem_wait(&empty);
sem_wait(&mutex);

// Critical section: Produce item and insert into the buffer


buffer[in] = item;
printf("Produced item %d at index %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&full);
}

pthread_exit(NULL);
}

void *consumer(void *arg) {


int item;

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


sem_wait(&full);
sem_wait(&mutex);

// Critical section: Consume item from the buffer


item = buffer[out];
printf("Consumed item %d from index %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&empty);
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;

// Initialize semaphores
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);

// Create producer and consumer threads


pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);

// Wait for threads to finish


pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);

// Destroy semaphores
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);

return 0;
}

Output

Produced item 83 at index 0


Produced item 86 at index 1
Produced item 77 at index 2
Produced item 15 at index 3
Produced item 93 at index 4
Consumed item 83 from index 0
Consumed item 86 from index 1
Consumed item 77 from index 2
Consumed item 15 from index 3
Consumed item 93 from index 4
Produced item 35 at index 0
Produced item 86 at index 1
Produced item 92 at index 2
Produced item 49 at index 3
Produced item 21 at index 4
Consumed item 35 from index 0
Consumed item 86 from index 1
Consumed item 92 from index 2
Consumed item 49 from index 3
Consumed item 21 from index 4

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

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

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#define FIFO_NAME "myfifo"

void writer_process() {
int fd;
char message[] = "Hello, reader!";

// Create the FIFO if it doesn't exist


mkfifo(FIFO_NAME, 0666);

// Open the FIFO for writing


fd = open(FIFO_NAME, O_WRONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

// Write the message to the FIFO


write(fd, message, sizeof(message));

// Close the FIFO


close(fd);

// Remove the FIFO


unlink(FIFO_NAME);
}

void reader_process() {
int fd;
char buffer[50];

// Open the FIFO for reading


fd = open(FIFO_NAME, O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

// Read data from the FIFO


read(fd, buffer, sizeof(buffer));

// Display the received message


printf("Reader received: %s\n", buffer);

// Close the FIFO

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
close(fd);
}

int main() {
pid_t pid;

// Fork a child process


pid = fork();

if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}

if (pid > 0) {
// Parent process (writer)
writer_process();
} else {
// Child process (reader)
reader_process();
}

return 0;
}

Output
Reader received: Hello, reader!

...Program finished with exit code 0


Press ENTER to exit console

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

5. Develop a C program to simulate Bankers algorithm for Deadlock avoidance

#include<stdio.h>

int main() {
/* array will store at most 5 process with 3 resoures if your process or
resources is greater than 5 and 3 then increase the size of array */
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5], terminate = 0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
// p is process and c is diffrent resources
printf("enter allocation of resource of all process %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & alc[i][j]);
}
}
printf("enter the max resource process required %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & max[i][j]);
}
}
printf("enter the available resource");
for (i = 0; i < c; i++)
scanf("%d", & available[i]);

printf("\n need resources matrix are\n");


for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);
}
printf("\n");
}
/* once process execute variable done will stop them for again execution */
for (i = 0; i < p; i++) {
done[i] = 0;
}
while (count < p) {
for (i = 0; i < p; i++) {
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
if (done[i] == 0) {
for (j = 0; j < c; j++) {
if (need[i][j] > available[j])
break;
}
//when need matrix is not greater then available matrix then if j==c will true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available resources */
for (j = 0; j < c; j++) {
available[j] += alc[i][j];
}
count++;
terminate = 0;
} else {
terminate++;
}
}
}
if (terminate == (p - 1)) {
printf("safe sequence does not exist");
break;
}

}
if (terminate != (p - 1)) {
printf("\n available resource after completion\n");
for (i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n safe sequence are\n");
for (i = 0; i < p; i++) {
printf("p%d\t", safe[i]);
}
}

return 0;
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

output
Enter the number of process and resources
53
enter allocation of resource of all process 5x3 matrix
010
200
302
211
002
enter the max resource process required 5x3 matrix
753
322
902
422
533
enter the available resource 3 3 2

need resources matrix are


7 4 3
1 2 2
6 0 0
2 1 1
5 3 1

available resource after completion


10 5 7
safe sequence are
p1 p3 p4 p0 p2

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

6. Develop a C program to simulate the following contiguous memory allocation


techniques: a)Worst fit b)Best fit c) First fit

a)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<temp)
{
ff[i]=j;
highest=temp;
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
}
}
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++)
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

b) 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("\n\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
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)
{
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]);
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
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

c) 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]);
}
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
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]);
}

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

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

7. Develop a C program to simulate page replacement algorithms: a)FIFO b)LRU


a)FIFO
#include < stdio.h >
int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}

Output:
Incoming Frame 1 Frame 2 Frame 3
4 4 - -
1 4 1 -
2 4 1 2
4 4 1 2
5 5 1 2
Total Page Faults: 4

a)LRU
#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];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
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++;
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
}
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;
}
}
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])
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}

OUTPUT:

Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2

The no of page faults is 10

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

8. Simulate following file organization techniques a)single level directory b) two level
directory

a) Single Level Directory:


#include<stdio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
printf("Do you want to enter another file(yes - 1 or no - 0):");
scanf("%d",&ch);
}
while(ch==1);
printf("Directory name is:%s\n",mdname);
printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
Output:
Enter the directory name:sss
Enter the number of files:3
Enter file name to be created:aaa
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:bbb
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:ccc
Do you want to enter another file(yes - 1 or no - 0):0
Directory name is:sss
Files names are:
aaa
bbb
ccc

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

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
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)
{
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++)
{
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
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
{
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);
}
}
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
Output:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- Aiml


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory -- Aiml


Enter name of the file -- os
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory -- Aiml


Enter name of the file -- java
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- cse


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory -- cse


Enter name of the file -- cg
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 5

Directory files
Directory Files
Aiml os
java
cse cg
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 3

Enter name of the directory – aiml


Enter name of the file --java
File java is deleted

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory -- Aiml


Enter the name of the file -- cn
File cn not found

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

9. Develop a C program to simulate the linked file allocation strategies


#include <stdio.h>
#include <stdlib.h>
int main(){
int f[50], p, i, st, len, j, c, k, a;
for (i = 0; i < 50; i++)
f[i] = 0;
printf("Enter how many blocks to be 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;
else
exit(0);
return 0;
}

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

output
Enter how many blocks to be allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter more file(Yes - 1/No - 0)0

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL

10. Develop 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)
{
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;
}
}
}
Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
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);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

Output
Enter the max range of disk
200
Enter the initial head position
53
Enter the size of queue request
8
Enter the queue of disk positions to be read

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur
OPERATING SYSTEMS LAB MANUAL
98
183
37
122
14
124
65
67
Disk head moves from 53 to 65 with seek
12
Disk head moves from 65 to 67 with seek
2
Disk head moves from 67 to 98 with seek
31
Disk head moves from 98 to 122 with seek
24
Disk head moves from 122 to 124 with seek
2
Disk head moves from 124 to 183 with seek
59
Disk head moves from 183 to 200 with seek
17
Disk head moves from 200 to 37 with seek
163
Disk head moves from 37 to 14 with seek
23
Disk head moves from 14 to 0 with seek
14
Total seek time is 347
Average seek time is 43.375000

Department of CSE(AIML) 36
BLDEA’s V P Dr PGHCET, Vijayapur

You might also like