OS Journal
OS Journal
CERTIFICATE
Place: Date:
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
2
Practical : 1: Write a program to give a solution to the producer-consumer problem
using shared memory.
OUTPUT-
PRACTICAL 2
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.
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:
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");
OUTPUT:
import java.util.*;
class SJF {
// number of process
static int SIZE = 4;
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");
10
OUTPUT:
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];
11
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;
// Driver Method
public static void main(String[] args)
{
// process id's
int processes[] = { 1, 2, 3};
int n = processes.length;
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
}
}
13
OUTPUT:
import java.util.*;
class BA
{
// Number of processes
static int P = 5;
// Number of resources
static int R = 3;
14
// Function to calculate need matrix
calculateNeed(need, maxm, allot);
found = true;
}
}
}
return true;
}
// Driver code
public static void main(String[] args)
{
int processes[] = {0, 1, 2, 3, 4};
OUTPUT:
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);
indexes.poll();
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:
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);
while (itr.hasNext()) {
int temp = itr.next();
if (indexes.get(temp) < lru)
{
lru = indexes.get(temp);
val = temp;
}
}
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;
OUTPUT:
21