0% found this document useful (0 votes)
3 views23 pages

DPOS Assignment and Project

The document contains laboratory assignments for a course on Design Principles of Operating Systems (CSE 3249), submitted by Subham Nayak. It includes detailed objectives and tasks for assignments on process management in Linux, synchronization using semaphores, and various programming exercises related to multi-threading and process scheduling. Additionally, it outlines the end-term project requirements and includes code snippets for implementing scheduling algorithms like FCFS and Round Robin.

Uploaded by

frozen.pangolinn
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)
3 views23 pages

DPOS Assignment and Project

The document contains laboratory assignments for a course on Design Principles of Operating Systems (CSE 3249), submitted by Subham Nayak. It includes detailed objectives and tasks for assignments on process management in Linux, synchronization using semaphores, and various programming exercises related to multi-threading and process scheduling. Additionally, it outlines the end-term project requirements and includes code snippets for implementing scheduling algorithms like FCFS and Round Robin.

Uploaded by

frozen.pangolinn
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

Laboratory Assignment #No.

4
On
Design Principles of Operating System
CSE 3249)

Submitted by

Name : Subham Nayak


Reg. No. : 2341014121
Semester : 5th
Branch : [Link] CSE
Section : 23412K3
Session : 2025-2026
Admission Batch : 2023

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


FACULTY OF ENGINEERING & TECHNOLOGY (ITER)
SIKSHA ‘O’ ANUSANDHAN DEEMED TO BE UNIVERSITY
BHUBANESWAR, ODISHA – 751030
Laboratory Assignment #No. 5
On
Design Principles of Operating System
CSE 3249)

Submitted by

Name : Subham Nayak


Reg. No. : 2341014121
Semester : 5th
Branch : [Link] CSE
Section : 23412K3
Session : 2025-2026
Admission Batch : 2023

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


FACULTY OF ENGINEERING & TECHNOLOGY (ITER)
SIKSHA ‘O’ ANUSANDHAN DEEMED TO BE UNIVERSITY
BHUBANESWAR, ODISHA – 751030
Laboratory Assignments 4
Subject: Design Principles of Operating Systems
Subject code: CSE 3249

Assignment 4: Familiarization with Process Management in Linux


environment.
Objective of this Assignment:

 To trace the different states of a process during its execution.


 To learn the use of different system calls such as (fork(),vfork(),wait(),exec()) for
process handling in Unix/Linux environment.

1. Write a C program to create a child process using fork() system call. The child process will
print the message “Child” with its process identifier and then continue in an indefinite loop.
The parent process will print the message “Parent” with its process identifier and then
continue in an indefinite loop.
a) Run the program and trace the state of both processes.
b) Terminate the child process. Then trace the state of processes.
c) Run the program and trace the state of both processes. Terminate the parent process. Then
trace the state of processes.
d) Modify the program so that the parent process after displaying the message will wait for
child process to complete its task. Again run the program and trace the state of both
processes.
e) Terminate the child process. Then trace the state of processes.
2. Trace the output of the following codes:

a) int main( ) b) int main( )


{ {
if(fork()==0) if(vfork()==0)
printf("1"); {
else printf("1");
printf("2"); _exit(0);
printf("3");
return 0; }
} else
printf("2");
printf("3");
}
c) int main( )
{ d) int main( )
pid_t pid; {
int i=5; pid_t pid;
pid=fork(); int i=5;
i=i+1; pid=vfork();
if(pid= =0) i=i+1;
{ if(pid==0)
printf("Child: %d",i); {
} printf("Child: %d",i);
else _exit(0);
}
{
wait(NULL); else
printf("Parent: %d",i); {
} printf("Parent: %d",i);
return 0; }
} return 0;
}
e) int main( ) f) int main( )
{ {
pid_t pid; pid_t pid;
int i=5; int i=5;
pid=fork(); pid=vfork();
if(pid= =0) if(pid==0)
{ {
i=i+1; i=i+1;
printf("Child: %d",i); printf("Child: %d",i);
} _exit(0);
else }
{ else
wait(NULL); {
printf("Parent: %d",i); printf("Parent: %d",i);
} }
return 0; return 0;
} }
g) int main( ) h) int main( )
{ {
int i=5; int i=5;
if(fork( )==0) if(vfork( )==0)
{ {
printf("Child: %d",i); printf("Child: %d",i);
} _exit(0);
else }
{ else
printf("Parent: %d",i); {
} printf("Parent: %d",i);
return 0; }
} return 0;
}
i) int main( ) j) int main( )
{ {
if(fork( )==0) if(vfork( )==0)
{ {
printf("1"); printf("1");
} _exit(0);
else }
{ else
wait(NULL); {
printf("2"); printf("2");
printf("3"); printf("3");
} }
return 0; return 0;
} }
k) int main( ) l) int main( )
{ {
pid_t c1; pid_t c1;
int n=10; int n=10;
c1=fork( ); c1=vfork( );
if(c1==0) if(c1==0)
{ {
printf(" Child\n"); printf(" Child\n");
n=20; n=20;
printf("n=%d \n",n); printf("n=%d \n",n);
} _exit(0);
else }
{ else
wait(NULL); {
printf("Parent\n"); printf("Parent\n");
printf("n=%d \n",n); printf("n=%d \n",n);
} }
return 0; return 0;
} }
m) int main( ) n) int main( )
{ {
int i=5; pid_t pid;
fork(); int i=5;
i=i+1; pid=vfork();
fork(); if(pid==0)
printf ( "% d",i); {
return 0; printf("Child: %d",i);
} _exit(0);
}
else
{
i=i+1;
printf("Parent: %d",i);
}
return 0;
}
o) int main( ) p) int main( )
{ {
int i=5; int i=5;
if(fork()==0) if(vfork()==0)
i=i+1; {
else i=i+1;
i=i-1; _exit(0);
printf("%d",i); }
return 0; else
} i=i-1;
fprintf(stderr,"%d",i);
return 0;
}
q) int main( ) r) int main( )
{ {
int j,i=5; int j,i=5;
for(j=1;j<3;j++) for(j=1;j<3;j++)
{ {
if(fork()==0) if(fork()!=0)
{ {
i=i+1; i=i-1;
break; break;
} }
else
wait(NULL); }
} fprintf(stderr,"%d",i);
printf("%d",i); return 0;
return 0; }
}
s) int main( ) t) void fun1(){
{ fork();
if(fork() == 0) fork();
if(fork()) printf("1\n");
printf("1\n"); }
return 0;
} int main() {
fun1();
printf("1\n");
return 0;
}
3. Write a C program that will create three child process to perform the following operations
respectively:
- First child will copy the content of file1 to file2
- Second child will display the content of file2
- Third child will display the sorted content of file2 in reverse order.
- Each child process being created will display its id and its parent process id with
appropriate message.
- The parent process will be delayed for 1 second after creation of each child process. It
will display appropriate message with its id after completion of all the child
processes.

4. Write a C program that will create a child process to generate a Fibonacci series of specified
length and store it in an array. The parent process will wait for the child to complete its task
and then display the Fibonacci series and then display the prime Fibonacci number in the
series along with its position with appropriate message.
Laboratory Assignments
Subject: Design Principles of Operating Systems
Subject code: CSE 3249

Assignment 5: Implementation of synchronization using semaphore:

Objective of this Assignment:


 To implement the concept of multi-threading in a process.
 To learn the use of semaphore i.e., to control access to shared resources.

1. Producer-Consumer problem

Problem: Write a C program to implement the producer-consumer program where:

 Producer generates integers from 1 to 50.


 Consumer processes the numbers.

Requirements:

 Use a shared buffer with a maximum size of 10.


 Use semaphores and mutex to ensure thread-safe access to the buffer.
 Print the number that producer is producing and consumer is consuming.
 Both producer and consumer will continue for 20 iterations

2. Alternating Numbers with Two Threads

Problem: Write a program to print 1, 2, 3 … upto 20. Create threads where two threads print
numbers alternately.

 Thread A prints odd numbers: 1, 3, 5 ...


 Thread B prints even numbers: 2, 4, 6 ...

Requirements:

 Use semaphores to control the order of execution of the threads.


 Ensure no race conditions occur.
3. Alternating Characters

Problem: Write a program to create two threads that print characters (A and B) alternately such as
ABABABABA…. upto 20. Use semaphores to synchronize the threads.

 Thread A prints A.
 Thread B prints B.

Requirements:

 Use semaphores to control the order of execution of the threads.


 Ensure no race conditions occur.

4. Countdown and Countup

Problem: Write a program create two threads where:

 Thread A counts down from 10 to 1.


 Thread B counts up from 1 to 10.

Both threads should alternate execution.

Requirements:

 Use semaphores to control the order of execution of the threads.


 Ensure no race conditions occur.

5. Sequence Printing using Threads

Problem: Write a program that creates three threads: Thread A, Thread B, and Thread C. The
threads must print numbers in the following sequence: A1, B2, C3, A4, B5, C6 … upto 20
numbers.

 Thread A prints A1, A4, A7, …


 Thread B prints B2, B5, B8, …
 Thread C prints C3, C6, C9, ...

Requirements:

 Use semaphores to control the order of execution of the threads.


 Ensure no race conditions occur.
Assignment 4
1)

2a)

2b)

2c)

2d)

2e)

2f)

2g)

2h)

2i)

2j)
2k)

2l)

2m)

2n)

2o)

2p)

2q)

2r)

2s)

2t)

3)
4)
Assignment 5
1)

2)
3)

4)
5)
End Term Project
On
Design Principles of Operating System (CSE 3249)

Submitted by

Name : Subham Nayak


Reg. No. : 2341014121
Branch : [Link] CSE
Semester : 5th Semeter
Section : 23412K3
Session : 2025-2026
Admission Batch : 2023

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


FACULTY OF ENGINEERING & TECHNOLOGY (ITER)
SIKSHA ‘O’ ANUSANDHAN DEEMED TO BE UNIVERSITY
BHUBANESWAR, ODISHA – 751030
PROJECT 1
Code-
#include <stdio.h>

#define MAX 10

typedef struct {
int pid;
int arrival;
int burst;
int remaining;
int start;
int completion;
int waiting;
int turnaround;
int response;
} Process;
void fcfs(Process p[], int n) {
int time = 0;
float avgWT = 0, avgTAT = 0, avgRT = 0;
printf("\nGantt Chart:\n|");
for (int i = 0; i < n; i++) {
if (time < p[i].arrival)
time = p[i].arrival;
p[i].start = time;
p[i].response = p[i].start - p[i].arrival;
time += p[i].burst;
p[i].completion = time;
p[i].turnaround = p[i].completion - p[i].arrival;
p[i].waiting = p[i].turnaround - p[i].burst;
printf(" P%d |", p[i].pid);
avgWT += p[i].waiting;
avgTAT += p[i].turnaround;
avgRT += p[i].response;
}
printf("\n");
printf("\nAverage Waiting Time: %.2f", avgWT / n);
printf("\nAverage Turnaround Time: %.2f", avgTAT / n);
printf("\nAverage Response Time: %.2f\n", avgRT / n);
}
void roundRobin(Process p[], int n, int tq) {
int time = 0, completed = 0;
float avgWT = 0, avgTAT = 0, avgRT = 0;
for (int i = 0; i < n; i++) {
p[i].remaining = p[i].burst;
p[i].start = -1;
}
printf("\nGantt Chart:\n|");
while (completed < n) {
int done = 1;
for (int i = 0; i < n; i++) {
if (p[i].arrival <= time && p[i].remaining > 0) {
done = 0;
if (p[i].start == -1) {
p[i].start = time;
p[i].response = p[i].start - p[i].arrival;
}
printf(" P%d |", p[i].pid);
if (p[i].remaining > tq) {
time += tq;
p[i].remaining -= tq;
} else {
time += p[i].remaining;
p[i].remaining = 0;
p[i].completion = time;
p[i].turnaround = p[i].completion - p[i].arrival;
p[i].waiting = p[i].turnaround - p[i].burst;
avgWT += p[i].waiting;
avgTAT += p[i].turnaround;
avgRT += p[i].response;
completed++;
}
}
}
if (done)
time++;
}
printf("\n");
printf("\nAverage Waiting Time: %.2f", avgWT / n);
printf("\nAverage Turnaround Time: %.2f", avgTAT / n);
printf("\nAverage Response Time: %.2f\n", avgRT / n);
}
int main() {
int n, choice, tq;
Process p[MAX];
printf("Enter number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for P%d: ", i + 1);
p[i].pid = i + 1;
scanf("%d %d", &p[i].arrival, &p[i].burst);
}
do {
printf("\n--- CPU Scheduling Menu ---");
printf("\n1. First Come First Serve (FCFS)");
printf("\n2. Round Robin (RR)");
printf("\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
fcfs(p, n);
break;
case 2:
printf("Enter Time Quantum: ");
scanf("%d", &tq);
roundRobin(p, n, tq);
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}
OUTPUT
End term project
Subject: Design Principles of Operating Systems
Subject code: CSE 3249

Objective of this Assignment:


 To design a CPU scheduler for simulating a few CPU scheduling policies.
 Implementation of Banker’s algorithm to avoid deadlock.

Overview of the Project:


1. One of the main tasks of an operating system is scheduling processes to run on the CPU. The
goal of this programming project is to build a program (use C or Java programming language) to
implement a simulator with different scheduling algorithms discussed in theory. The simulator
should select a process to run from the ready queue based on the scheduling algorithm chosen at
runtime. Since the assignment intends to simulate a CPU scheduler, it does not require any actual
process creation or execution.

2. The goal of this programming project is to build a program (use C or Java programming
language) to implement banker’s algorithms discussed in theory. Create 5 process that request and
release resources from the bank. The banker will grant the request only if it leaves the system in a
safe state. It is important that shared data be safe from concurrent access. To ensure safe access to
shared data, you can use mutex locks.
Project Description 1: The C program provides an interface to the user to implement the
followingscheduling policies as per the choice provided:
1. First Come First Served (FCFS)
2. Round Robin (RR)

Appropriate option needs to be chosen from a switch case based menu driven program with an
option of “Exit from program” in case 5 and accordingly a scheduling policy will print the Gantt
chart and the average waiting time, average turnaround time and average response time. The
program will take Process ids, its arrival time, and its CPU burst time as input. For implementing
RR scheduling, user also needs to specify the time quantum. Assume that the process ids should be
unique for all processes. Each process consists of a single CPU burst (no I/O bursts), and processes
are listed in order of their arrival time. Further assume that an interrupted process gets placed at the
back of the Ready queue, and a newly arrived process gets placed at the back of the Ready queue as
well. The output should be displayed in a formatted way for clarity of understanding and visual.

Test Cases:
The program should able to produce correct answer or appropriate error message corresponding to
the following test cases:
1. Consider the set of processes with arrival time (in milliseconds), CPU burst time (in
milliseconds), and time quantum = 2ms as shown below.

Process Arrival time Burst Time


P1 0 3
P2 2 6
P3 4 4
P4 6 5
P5 8 2
 Input choice 1, and print the Gantt charts that illustrate the execution of these
processes using the FCFS scheduling algorithm and then print the average
turnaround time, average waiting time and average response time.
 Input choice 2, and print the Gantt charts that illustrate the execution of these
processes using the RR scheduling algorithm and then print the average turnaround
time, average waiting time and average response time.
 Analyze the results and determine which of the algorithms results in the minimum
average waiting time over all processes?

Project Description 2:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all resources,
then makes an state” check to test for possible activities, before deciding whether allocation should
be allowed to continue.

Example: Snapshot at the initial stage:

1. Consider the following resource allocation state with 5 processes and 4 resources: There are
total existing resources of 6 instances of type R1, 7 instances of type R2, 12 instance of type
R3 and 12 instances of type R4.

Process Allocation Max


R1 R2 R3 R4 R1 R2 R3 R4
P1 0 0 1 2 0 0 1 2
P2 2 0 0 0 2 7 5 0
P3 0 0 3 4 6 6 5 6
P4 2 3 5 4 4 3 5 6
P5 0 3 3 2 0 6 5 2

a) Find the content of the need matrix.


b) Is the system in a safe state? If so, give a safe sequence of the process.
c) If P3 requests for 1 more instance of each type R2 and R4, can the request be granted
immediately or not?
PROJECT 2
Code
#include <stdio.h>

int main() {
int alloc[5][4] = {
{0,0,1,2},
{2,0,0,0},
{0,0,3,4},
{2,3,5,4},
{0,3,3,2}
};
int max[5][4] = {
{0,0,1,2},
{2,7,5,0},
{6,6,5,6},
{4,3,5,6},
{0,6,5,2}
};
int total[4] = {6,7,12,12};
int avail[4], need[5][4], finish[5]={0}, safe[5];
for(int j=0;j<4;j++){
int sum=0;
for(int i=0;i<5;i++)
sum+=alloc[i][j];
avail[j]=total[j]-sum;
}
for(int i=0;i<5;i++)
for(int j=0;j<4;j++)
need[i][j]=max[i][j]-alloc[i][j];
printf("Need Matrix:\n");
for(int i=0;i<5;i++){
for(int j=0;j<4;j++)
printf("%d ",need[i][j]);
printf("\n");
}

int count=0;
while(count<5){
int found=0;
for(int i=0;i<5;i++){
if(!finish[i]){
int j;
for(j=0;j<4;j++)
if(need[i][j]>avail[j])
break;
if(j==4){
for(int k=0;k<4;k++)
avail[k]+=alloc[i][k];
safe[count++]=i;
finish[i]=1;
found=1;
}
}
}
if(!found) break;
}
if(count==5){
printf("\nSystem is in SAFE state\nSafe Sequence: ");
for(int i=0;i<5;i++)
printf("P%d ",safe[i]);
printf("\n");
} else {
printf("\nSystem is NOT in safe state\n");
}
int request[4] = {0, 1, 0, 1};
int p = 2;
int canGrant = 1;
for(int j=0;j<4;j++){
if(request[j] > need[p][j] || request[j] > avail[j]){
canGrant = 0;
break;
}
}
if(!canGrant){
printf("\nRequest by P3 CANNOT be granted (exceeds limits).\n");
}
return 0;
}
OUTPUT

You might also like