0% found this document useful (0 votes)
43 views21 pages

OS Journal

Uploaded by

pshiv09098
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)
43 views21 pages

OS Journal

Uploaded by

pshiv09098
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/ 21

JNAN VIKAS MANDAL’S

PADMASHREE DR. R.T.DOSHI DEGREE COLLEGE OF


INFORMATION TECHNOLOGY

MOHANLAL RAICHAND MEHTA COLLEGE OF COMMERCE

DIWALIMAA DEGREE COLLEGE OF SCIENCE

CERTIFICATE

This is to certify that the Mr./Miss. of


S.Y.B.Sc.CS Semester III has completed the practical work in the subject of
OPERATING SYSTEMSduring the Academic year 2024-25 under the guidance
of PROF.SARITA SARANG being the partial requirement for the fulfillment of
the curriculum of Degree of Bachelor of Science in Computer Science, Universityof
Mumbai.

Place: Date:

Sign of Subject In Charge Sign of External Examiner

Sign of Incharge / H.O.D

1
INDEX
Sr.N Name Of Practicals Date Signature
o.
1 Write a program to give a solution to the
producer- consumer problem
using shared memory. 10/07/2024
2 Write a program to work with a single
thread.
Write a program to work with multi
threads. 24/07/2024
The Fibonacci sequence is the series of
numbers 0, 1, 1, 2, 3, 5. 8,... Formally it
can be expressed as: fib0= 0, fib11, fibn
fibn-1+ fibn-2. Write multithreaded
program that generates the Fibonacci
sequence.
3 Write a program that implements FCFS
scheduling algorithm.
07/08/2024
4 Write a program that implements
(with no premption) scheduling 14/08/2024
algorithm.
5 Write a program that implement RR
scheduling algorithm. 21/08/2024

6 Write a program that implements the


banker's algorithm 28/08/2024

7 Write a program that implements the


FIFO page- replacement algorithm. 4/09/2024

8 Write a program that implements the


LRU page- 04/09/2024
replacement algorithm.

2
Practical : 1: Write a program to give a solution to the producer-consumer problem
using shared memory.

from threading import Thread, Semaphore


import random,time
full = Semaphore(0)
empty = Semaphore(10)
mutex = Semaphore(1)
class producerThread(Thread):
def init (this,buffer):
Thread. init (this)
this.buffer = buffer
def run(this):
nums = range(5)
while(True):
empty.acquire()
mutex.acquire()
num = random.randint(0,5)
this.buffer.append(num)
print("Produced ", num)
mutex.release()
full.release()
time.sleep(1)
class consumerThread(Thread):
def init (this,buffer):
Thread. init (this)
this.buffer = buffer
def run(this):
while(True):
full.acquire()
mutex.acquire()
print("Consumed ", this.buffer.pop())
mutex.release()
empty.release()
time.sleep(1)
3
buffer = []
producerThread(buffer).start()
consumerThread(buffer).start()

OUTPUT-

PRACTICAL 2

a. Write a program to work with a single thread.

import java.io.*;
class GFG extends Thread
{
public void run()
{
System.out.print("Welcome to Thread");
}
public static void main(String[] args)
{
GFG g = new GFG(); // creating thread
g.start(); // starting thread
}
}

Output:

4
b. Write a program to work with multi threads.

class MultithreadingDemo extends Thread


{
public void run()
{
try
{
System.out.println("Thread "+ Thread.currentThread().getId()+ " is running");
}
catch (Exception e)
{
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class Multithread
{
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i<n; i++)
{
MultithreadingDemo object= new MultithreadingDemo();
object.start();
}
}
}

OUTPUT:

5
c. The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5. 8,... Formally it can be expressed as:
fib0= 0, fib1 = 1, fibn fibn-1+ fibn-2. Write a multithreaded program that generates the
Fibonacci sequence.

class FibonacciExample
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}

OUTPUT:

PRACTICAL 3: Write a program that implements FCFS scheduling algorithm.

class FCFS
{
static void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n; i++)
{
wt[i] = bt[i - 1] + wt[i - 1];
6
}
}
// Function to calculate turn around time
static void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
}
}
//Function to calculate average time
static void findavgTime(int processes[], int n, int bt[])
{
int wt[] = new int[n], tat[] = new int[n];
int total_wt = 0, total_tat = 0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
System.out.printf("Processes Burst time Waiting"
+" time Turn around time\n");

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
System.out.printf("; %d ", (i + 1));
System.out.printf(" %d ", bt[i]);

System.out.printf(" %d", wt[i]);


System.out.printf(" %d\n", tat[i]);
}
float s = (float)total_wt /(float) n;
int t = total_tat / n;
System.out.printf("Average waiting time = %f", s);
System.out.printf("\n");
System.out.printf("Average turn around time = %d ", t);
}

public static void main(String[] args)


{

int processes[] = {1, 2, 3};


int n = processes.length;
7
//Burst time of all processes
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);
}
}

OUTPUT:

PRACTICAL 4: Write a program that implements (with no premption)


scheduling algorithm.

import java.util.*;

class SJF {
// number of process
static int SIZE = 4;

// A class to represent the


// process information
static class proinfo {
String pname; // process name
int atime; // arrival time
int btime; // burst time
proinfo(String pname, int atime, int btime)
{
this.pname = pname;
this.atime = atime;
this.btime = btime;
}
}

static void swap(int i, int idx, proinfo[] arr)


{
8
proinfo tmp = arr[i];
arr[i] = arr[idx];
arr[idx] = tmp;
}

// This function schedules the


// process according to the SJF
// scheduling algorithm.
static void sjfNonpremetive(proinfo[] arr)
{
// Used to sort the processes
// according to arrival time
int index = 0;
for (int i = 0; i < SIZE - 1; i++) {
index = i;
for (int j = i + 1; j < SIZE; j++) {
if (arr[j].atime < arr[index].atime) {
index = j;
}
}
swap(i, index, arr);
}

// ctime stores the current run time


int ctime = arr[0].atime;

// priority queue, wait, is used


// to store all the processes that
// arrive <= ctime (current run time)
// this is a minimum priority queue
// that arranges values according to
// the burst time of the processes.
PriorityQueue<proinfo> wait
= new PriorityQueue<proinfo>(
(a, b) -> { return a.btime - b.btime; });

// The first process is


// pushed in the wait queue.
wait.add(new proinfo(arr[0].pname, arr[0].atime,
arr[0].btime));
arr[0].atime = -1;

System.out.print("Process id" + "\t");


System.out.print("Arrival time" + "\t");
System.out.println("Burst time\t");

while (!wait.isEmpty()) {

9
System.out.print("\t");
System.out.print(wait.peek().pname + "\t\t");
System.out.print(wait.peek().atime + "\t\t");
System.out.println(wait.peek().btime + "\t\t");

// ctime is increased with


// the burst time of the
// currently executed process.
ctime += wait.peek().btime;

// The executed process is


// removed from the wait queue.
wait.remove();

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


if (arr[i].atime <= ctime
&& arr[i].atime != -1) {
wait.add(new proinfo(arr[i].pname,
arr[i].atime,
arr[i].btime));

// When the process once


// enters the wait queue
// its arrival time is
// assigned to -1 so that
// it doesn't enter again
// int the wait queue.
arr[i].atime = -1;
}
}
}
}

public static void main(String[] args)


{
// an array of process info structures.
proinfo[] arr = new proinfo[SIZE];
arr[0] = new proinfo("p1", 4, 3);
arr[1] = new proinfo("p2", 0, 8);
arr[2] = new proinfo("p3", 5, 4);
arr[3] = new proinfo("p4", 9, 2);

System.out.print("Process scheduling ");


System.out.println("according to SJF is: \n");
sjfNonpremetive(arr);
}

10
OUTPUT:

PRACTICAL 5: Write a program that implements RR scheduling algorithm.

public class RR
{
// Method to find the waiting time for all
// processes
static void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining burst times.
int rem_bt[] = new int[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

int t = 0; // Current time

// Keep traversing processes in round robin manner


// until all of them are not done.
while(true)
{
boolean done = true;

// Traverse all processes one by one repeatedly


for (int i = 0 ; i < n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = false; // There is a pending process

11
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;

// Decrease the burst_time of current process


// by quantum
rem_bt[i] -= quantum;
}

// If burst time is smaller than or equal to


// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];

// Waiting time is current time minus time


// used by this process
wt[i] = t - bt[i];

// As the process gets fully executed


// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}

// If all processes are done


if (done == true)
break;
}
}

// Method to calculate turn around time


static void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

// Method to calculate average time


static void findavgTime(int processes[], int n, int bt[],
12
int quantum)
{
int wt[] = new int[n], tat[] = new int[n];
int total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt, quantum);

// Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details


System.out.println("PN " + " B " +
" WT " + " TAT");

// Calculate total waiting time and total turn


// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
System.out.println(" " + (i+1) + "\t\t" + bt[i] +"\t " +
wt[i] +"\t\t " + tat[i]);
}

System.out.println("Average waiting time = " +


(float)total_wt / (float)n);
System.out.println("Average turn around time = " +
(float)total_tat / (float)n);
}

// Driver Method
public static void main(String[] args)
{
// process id's
int processes[] = { 1, 2, 3};
int n = processes.length;

// Burst time of all processes


int burst_time[] = {10, 5, 8};

// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
}
}

13
OUTPUT:

PRACTICAL 6: Write a program that implements the banker's algorithm

import java.util.*;

class BA
{

// Number of processes
static int P = 5;

// Number of resources
static int R = 3;

// Function to find the need of each process


static void calculateNeed(int need[][], int maxm[][],
int allot[][])
{
// Calculating Need of each P
for (int i = 0 ; i < P ; i++)
for (int j = 0 ; j < R ; j++)

// Need of instance = maxm instance -


// allocated instance
need[i][j] = maxm[i][j] - allot[i][j];
}

// Function to find the system is in safe state or not


static boolean isSafe(int processes[], int avail[], int maxm[][],
int allot[][])
{
int [][]need = new int[P][R];

14
// Function to calculate need matrix
calculateNeed(need, maxm, allot);

// Mark all processes as infinish


boolean []finish = new boolean[P];

// To store safe sequence


int []safeSeq = new int[P];

// Make a copy of available resources


int []work = new int[R];
for (int i = 0; i < R ; i++)
work[i] = avail[i];

// While all processes are not finished


// or system is not in safe state.
int count = 0;
while (count < P)
{
// Find a process which is not finish and
// whose needs can be satisfied with current
// work[] resources.
boolean found = false;
for (int p = 0; p < P; p++)
{
// First check if a process is finished,
// if no, go for next condition
if (finish[p] == false)
{
// Check if for all resources of
// current P need is less
// than work
int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;

// If all needs of p were satisfied.


if (j == R)
{
// Add the allocated resources of
// current P to the available/work
// resources i.e.free the resources
for (int k = 0 ; k < R ; k++)
work[k] += allot[p][k];

// Add this process to safe sequence.


safeSeq[count++] = p;
15
// Mark this p as finished
finish[p] = true;

found = true;
}
}
}

// If we could not find a next process in safe


// sequence.
if (found == false)
{
System.out.print("System is not in safe state");
return false;
}
}

// If system is in safe state then


// safe sequence will be as below
System.out.print("System is in safe state.\nSafe"
+" sequence is: ");
for (int i = 0; i < P ; i++)
System.out.print(safeSeq[i] + " ");

return true;
}

// Driver code
public static void main(String[] args)
{
int processes[] = {0, 1, 2, 3, 4};

// Available instances of resources


int avail[] = {3, 3, 2};

// Maximum R that can be allocated


// to processes
int maxm[][] = {{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};

// Resources allocated to processes


int allot[][] = {{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
16
{2, 1, 1},
{0, 0, 2}};

// Check system is in safe state or not


isSafe(processes, avail, maxm, allot);
}
}

OUTPUT:

PRACTICAL 7: Write a program that implements the FIFO page-replacement algorithm.

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
class PageReplacement
{
// Method to find page faults using FIFO
static int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet<Integer> s = new HashSet<>(capacity);

// To store the pages in FIFO manner


Queue<Integer> indexes = new LinkedList<>() ;

// Start from initial page


int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
17
if (!s.contains(pages[i]))
{
s.add(pages[i]);

// increment page fault


page_faults++;

// Push the current page into the queue


indexes.add(pages[i]);
}
}

// If the set is full then need to perform FIFO


// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.contains(pages[i]))
{
//Pop the first page from the queue
int val = indexes.peek();

indexes.poll();

// Remove the indexes page


s.remove(val);

// insert the current page


s.add(pages[i]);

// push the current page into


// the queue
indexes.add(pages[i]);

// Increment page faults


page_faults++;
}
}
}

return page_faults;
}

// Driver method
public static void main(String args[])
{
18
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};

int capacity = 4;
System.out.println(pageFaults(pages, pages.length, capacity));
}
}

OUTPUT:

PRACTICAL 8: Write a program that implements the LRU page-replacement algorithm.

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

class LRU
{
// Method to find page faults using indexes
static int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet<Integer> s = new HashSet<>(capacity);

// To store least recently used indexes


// of pages.
HashMap<Integer, Integer> indexes = new HashMap<>();

// Start from initial page


int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
19
// already which represents page fault
if (!s.contains(pages[i]))
{
s.add(pages[i]);

// increment page fault


page_faults++;
}

// Store the recently used index of


// each page
indexes.put(pages[i], i);
}

// If the set is full then need to perform lru


// i.e. remove the least recently used page
// and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.contains(pages[i]))
{
// Find the least recently used pages
// that is present in the set
int lru = Integer.MAX_VALUE, val=Integer.MIN_VALUE;

Iterator<Integer> itr = s.iterator();

while (itr.hasNext()) {
int temp = itr.next();
if (indexes.get(temp) < lru)
{
lru = indexes.get(temp);
val = temp;
}
}

// Remove the indexes page


s.remove(val);
//remove lru from hashmap
indexes.remove(val);
// insert the current page
s.add(pages[i]);

// Increment page faults


page_faults++;
}
20
// Update the current page index
indexes.put(pages[i], i);
}
}

return page_faults;
}

// Driver method
public static void main(String args[])
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};

int capacity = 4;

System.out.println(pageFaults(pages, pages.length, capacity));


}
}

OUTPUT:

21

You might also like