Os Lab Pratical Full - Repaired
Os Lab Pratical Full - Repaired
EXPERIMENT 1
3. Get the value for burst time of each process from the user
4. Having allocated the burst time(bt) for individual processes , Start with the first process
from it’s initial position let other process to be in queue
tat(pi) = wt(pi) + bt(pi) (i.e tat of current process = wt of current process + bt of current
process)
6. Calculate the total and average waiting time and turnaround time
Program:
#include<iostream.h>
#include<conio.h>
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
findavgTime(processes, n, burst_time);
return 0;
}
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 2
Algorithm:
3. Sort the processes according to the burst time and allocate the one with shortest burst to
execute first
4. If two processes have same burst length then FCFS scheduling algorithm is used
5. Calculate the total and average waiting time and turnaround time
Program:
// C++ program to implement Shortest Job first with Arrival
// Time
#include <iostream>
using namespace std;
int mat[10][6];
int main()
{
int num, temp;
arrangeArrival(num, mat);
completionTime(num, mat);
cout << "Final Result...\n";
cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
"Time\tTurnaround Time\n";
for (int i = 0; i < num; i++) {
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
<< mat[i][5] << "\n";
}
}
Output
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 3
Algorithm:
4. Sort the processes according to the priority and allocate the one with highest priority to
execute first
5. If two process have same priority then FCFS scheduling algorithm is used
6. Calculate the total and average waiting time and turnaround time
Program:
struct Process
{
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};
findavgTime(proc, n);
}
// Driver code
int main()
{
Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0;
}
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
Output
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 4
Algorithm:
5. Make the CPU scheduler go around the ready queue allocating CPU to each process for the
time interval specified
6. Make the CPU scheduler pick the first process and set time to interrupt after quantum. And
after it's expiry dispatch the process
7. If the process has burst time less than the time quantum then the process is released by the
CPU
8. If the process has burst time greater than time quantum then it is interrupted by the OS and
the process is put to the tail of ready queue and the schedule selects next process from head of
the queue
9. Calculate the total and average waiting time and turnaround time
Program:
{
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
// Driver code
int main()
{
// process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Output
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 5
AIM: Program to implement Deadlock.
Program:
#include <bits/stdc++.h>
int minResources = 0;
return minResources;
// Driver code
int main()
return 0;
Output
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 6
AIM: Study of classical inter process communication problem (producer consumer)
Algorithm:
The producer-consumer problem illustrates the need for synchronization in systems where
many processes share a resource. In the problem, two processes share a fixed-size buffer. One
process produces information and puts it in the buffer, while the other process consumes
information from the buffer. These processes do not take turns accessing the buffer, they both
work concurrently. Here in lies the problem. What happens if the producer tries to put an item
into a full buffer? What happens if the consumer tries to take an item from an empty buffer?
In order to synchronize these processes, we will block the producer when the buffer is full,
and we will block the consumer when the buffer is empty. So the two processes, Producer
and Consumer, should work as follows:
(1) The consumer checks to see if the buffer is empty. If so, the consumer will put itself
to sleep until the producer wakes it up. A "wakeup" will occur if the producer finds
the buffer empty after it puts an item into the buffer.
(2) Then, the consumer will remove a widget from the buffer. The consumer will never
try to remove a widget from an empty buffer because it will not wake up until the
buffer is full.
(3) If the buffer was full before it removed the widget, the consumer will wake the
producer.
(4) Finally, the consumer will consume the widget. As was the case with the producer, an
interrupt could occur between any of these steps, allowing the producer to run.
std::mutex mtx;
std::condition_variable buffer_full;
std::condition_variable buffer_empty;
int widget_id = 1;
void producer() {
while (true) {
// Simulate time to produce an item
std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 1000 + 500));
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
std::unique_lock<std::mutex> lock(mtx);
void consumer() {
while (true) {
std::unique_lock<std::mutex> lock(mtx);
lock.unlock();
int main() {
srand(static_cast<unsigned>(time(nullptr)));
std::thread producer_thread(producer);
std::thread consumer_thread(consumer);
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
producer_thread.join();
consumer_thread.join();
return 0;
}
Output: -
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 7
AIM: Program to implement classical inter process communication problem (Reader Writers).
Problem: -
An integer variable read_count:- used to maintain the number of readers currently accessing
the resource. The variable read_count is initialized to 0.
2. If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it
keeps on waiting.
while (TRUE)
{
wait(w);
/* perform the write operation */
signal(w);
}
b. Reader Process :
1. Reader requests the entry to critical section.
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
2. If allowed:
i. It increments the count of number of readers inside the critical section. If this reader is the
first reader entering, it locks the w semaphore to restrict the entry of writers if any reader is
inside.
ii. It then, signals mutex as any other reader is allowed to enter while others are already
reading.
iii. After performing reading, it exits the critical section. When exiting, it checks if no more
reader is inside, it signals the semaphore was now, writer can enter the critical section.
while (TRUE)
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
Program :-
#include <iostream>
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
// Function prototypes
// Shared variables
int sh_var = 5;
// Semaphores
// Threads
sem_wait(&z);
sem_wait(&rsem);
sem_wait(&x);
readcount++;
if (readcount == 1)
sem_wait(&wsem);
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
sem_post(&x);
sem_post(&rsem);
sem_post(&z);
// Reading section
cout << "\n[Reader " << reader_id << "] Reading value: " << sh_var << endl;
sleep(1);
sem_wait(&x);
readcount--;
if (readcount == 0)
sem_post(&wsem);
sem_post(&x);
return nullptr;
sem_wait(&y);
writecount++;
if (writecount == 1)
sem_wait(&rsem);
sem_post(&y);
sem_wait(&wsem);
// Writing section
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
cout << "\n[Writer " << writer_id << "] Writing..." << endl;
sh_var += 5;
cout << "[Writer " << writer_id << "] Updated value: " << sh_var << endl;
sleep(1);
sem_post(&wsem);
sem_wait(&y);
writecount--;
if (writecount == 0)
sem_post(&rsem);
sem_post(&y);
return nullptr;
int main() {
// Initialize semaphores
sem_init(&x, 0, 1);
sem_init(&y, 0, 1);
sem_init(&z, 0, 1);
sem_init(&rsem, 0, 1);
sem_init(&wsem, 0, 1);
// Create threads
// Join threads
// Destroy semaphores
sem_destroy(&x);
sem_destroy(&y);
sem_destroy(&z);
sem_destroy(&rsem);
sem_destroy(&wsem);
return 0;
Output: -
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 8
AIM: Program to implement classical inter process communication problem (Dining Philosophers).
Prevents deadlock (where each philosopher holds one fork and waits indefinitely),
Ensures mutual exclusion (no two philosophers use the same fork at the same time),
Maintains concurrency (multiple philosophers can eat simultaneously if forks are available).
Algorithm: -
1. Start:
2. Thinking:
3. Get Hungry:
o If not, wait.
5. Eating:
7. Repeat:
Program:
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
void test(int i) {
state[(i + 1) % N] != EATING) {
state[i] = EATING;
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
sleep(1);
cout << "[Philosopher " << i << "] takes forks " << (i + 4) % N
<< " and " << i << " and starts EATING\n";
sem_post(&S[i]);
void take_forks(int i) {
sem_wait(&mutex);
state[i] = HUNGRY;
test(i);
sem_post(&mutex);
sem_wait(&S[i]);
void put_forks(int i) {
sem_wait(&mutex);
state[i] = THINKING;
cout << "[Philosopher " << i << "] puts down forks and starts THINKING\n";
test((i + 4) % N);
test((i + 1) % N);
sem_post(&mutex);
int i = *(int*)num;
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
while (true) {
sleep(1);
take_forks(i);
sleep(2);
put_forks(i);
return nullptr;
int main() {
pthread_t thread_id[N];
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&S[i], 0, 0);
pthread_join(thread_id[i], NULL);
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
return 0;
Output: -
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 9
AIM: Program to implement FIFO page replacement algorithm.
ALGORITHM
4. Check the need of replacement from old page to new page in memory
Program:
#include <iostream>
#include <queue>
#include <unordered_set>
using namespace std;
int pageFaults = 0;
indexQueue.push(currentPage);
pageFaults++;
cout << "Page " << currentPage << " added -> Page Fault!" << endl;
} else {
cout << "Page " << currentPage << " already in memory -> No Page Fault." << endl;
}
}
cout << "\nTotal Page Faults = " << pageFaults << endl;
}
int main() {
int pages[] = {1, 3, 0, 3, 5, 6};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity;
fifoPageReplacement(pages, n, capacity);
return 0;
}
Output: -
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
EXPERIMENT 10
AIM: Program to implement LRU page replacement algorithm.
ALGORITHM
Program:
#include <iostream>
#include <list>
#include <unordered_map>
using namespace std;
int pageFaults = 0;
cout << "Page " << currentPage << " added -> Page Fault!" << endl;
SAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY
} else {
// Page already in memory, move it to front (most recently used)
memory.erase(pageMap[currentPage]);
memory.push_front(currentPage);
pageMap[currentPage] = memory.begin();
cout << "Page " << currentPage << " accessed -> No Page Fault." << endl;
}
}
cout << "\nTotal Page Faults = " << pageFaults << endl;
}
int main() {
int pages[] = {1, 2, 3, 2, 4, 1, 5};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity;
lruPageReplacement(pages, n, capacity);
return 0;
}
Output: -