OS Lab Manual
OS Lab Manual
EXP NO: 1 Program for FCFS CPU Scheduling Algorithms to find turnaround time and
Date: waiting time.
#include<iostream>
// processes
wt[0] = 0;
1
Operating System
// bt[i] + wt[i]
<< " Waiting time " << " Turn around time\n";
// around time
2
Operating System
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
// Driver code
int main()
//process id's
findavgTime(processes, n, burst_time);
return 0;
Output:
Processes Burst time Waiting time Turn around time //The Output is Wrong please correct it
1 10 0 10
2 5 10 15
3 8 15 23
#include <iostream>
#include <conio.h>
4
Operating System
cout << "Enter the starting block and the length of the files: ";
if (files[j] == 0)
flag++;
if(len == flag){
if (files[k] == 0){
files[k] = 1;
if (k != (startBlock+len-1))
else
cout << "The file is not allocated to the disk" << endl;
5
Operating System
cout << "Do you want to enter more files?" << endl;
int ch;
if (ch == 1)
recurse(files);
else
exit(0);
return;
int main()
int files[50];
for(int i=0;i<50;i++)
files[i]=0;
recurse(files);
getch();
return 0;
6
Operating System
EXP NO: 3
Program for File Allocation Strategies - Indexed Memory
Date:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
void recurse1();
void recurse2();
void recurse1(){
if (files[indBlock] != 1){
cout << "Enter the number of blocks and the number of files needed for the index " << indBlock << " on the disk:
";
cin >> n;
}
7
Operating System
else{
recurse1();
recurse2();
void recurse2(){
int flag = 0;
if (files[indexBlock[i]] == 0)
flag++;
if (flag == n){
files[indexBlock[j]] = 1;
8
Operating System
cout << indBlock << " ------> " << indexBlock[k] << ": " << files[indexBlock[k]] << endl;
else{
recurse2();
cout << "Do you want to enter more files?" << endl;
int ch;
if (ch == 1)
recurse1();
else
exit(0);
return;
9
Operating System
int main()
for(int i=0;i<50;i++)
files[i]=0;
recurse1();
return 0;
10
Operating System
EXP NO: 4
Program to simulate the following contiguous memory allocation techniques
Date: Worst-fit
#include<bits/stdc++.h>
// algorithm
int n)
// process
int allocation[n];
11
Operating System
if (wstIdx == -1)
wstIdx = j;
wstIdx = j;
if (wstIdx != -1)
12
Operating System
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
else
13
Operating System
// Driver code
int main() {
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
return 0 ; }
Output
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated
EXP NO: 5
Program to simulate the following contiguous memory allocation techniques First-
Date: fit
#include<bits/stdc++.h>
14
Operating System
int allocation[n];
15
Operating System
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
16
Operating System
if (allocation[i] != -1)
else
// Driver code
int main()
17
Operating System
return 0 ;
Output :
EXP NO: 6
Program for bankers algorithm
Date:
#include<iostream>
using namespace std;
// Number of processes
const int P = 5;
// Number of resources
const int R = 3;
{
// Calculating Need of each P
for (int i = 0 ; i < P ; i++)
for (int j = 0 ; j < R ; j++)
int allot[][R])
int need[P][R];
int safeSeq[P];
19
Operating System
int work[R];
work[i] = avail[i];
int count = 0;
// work[] resources.
20
Operating System
if (finish[p] == 0)
// than work
int j;
break;
if (j == R)
21
Operating System
work[k] += allot[p][k];
safeSeq[count++] = p;
finish[p] = 1;
found = true;
// sequence.
if (found == false)
22
Operating System
return false;
return true;
// Driver code
int main()
23
Operating System
// to processes
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}};
24
Operating System
return 0;
Output:
System is in safe state.
Safe sequence is: 1 3 4 0 2
25
Operating System
EXP NO: 7
Program to simulate disk scheduling algorithms SCAN
Date:
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),b;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
int temp=h;
a.push_back(h);
a.push_back(m);
a.push_back(0);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
26
Operating System
if(k<n/2){
for(i=k;i<a.size();i++){
b.push_back(a[i]);
}
for(i=k-1;i>=0;i--){
b.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
b.push_back(a[i]);
}
for(i=k+1;i<a.size();i++){
b.push_back(a[i]);
}
}
temp=b[0];
cout<<temp;
for(i=1;i<b.size();i++){
cout<<" -> "<<b[i];
sum+=abs(b[i]-temp);
temp=b[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
cout<<"Average head movement = "<<(float)sum/n<<'\n';
return 0;
}
OUTPUT:
27
Operating System
EXP NO: 8
Program for Page Replacement Algorithms LRU
Date:
#include<bits/stdc++.h>
28
Operating System
unordered_set<int> s;
// of pages.
int page_faults = 0;
29
Operating System
if (s.find(pages[i])==s.end())
s.insert(pages[i]);
page_faults++;
// each page
indexes[pages[i]] = i;
else
30
Operating System
if (s.find(pages[i]) == s.end())
lru = indexes[*it];
val = *it;
31
Operating System
s.erase(val);
s.insert(pages[i]);
page_faults++;
indexes[pages[i]] = i;
return page_faults;
// Driver code
32
Operating System
int main()
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
return 0;
Output:
6
EXP NO: 9
Program to simulate producer-consumer problem using semaphores
Date:
#include<bits/stdc++.h>
33
Operating System
#include<pthread.h>
#include<semaphore.h>
#include <unistd.h>
// Declaration
int r1,total_produced=0,total_consume=0;
// Semaphore declaration
sem_t notEmpty;
// Producer Section
while(1){
sem_post(¬Empty);
34
Operating System
sleep(rand()%100*0.01);
// Consumer Section
while(1){
sem_wait(¬Empty);
sleep(rand()%100*0.01);
// thread declaration
35
Operating System
pthread_t producer,consumer;
// Declaration of attribute......
pthread_attr_t attr;
// semaphore initialization
sem_init(¬Empty,0,0);
// pthread_attr_t initialization
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
// Creation of process
r1=pthread_create(&producer,&attr,produce,NULL);
if(r1){
exit(-1);
36
Operating System
r1=pthread_create(&consumer,&attr,consume,NULL);
if(r1){
exit(-1);
pthread_attr_destroy(&attr);
r1=pthread_join(producer,NULL);
if(r1){
exit(-1);
r1=pthread_join(consumer,NULL);
37
Operating System
if(r1){
exit(-1);
// Exiting thread
pthread_exit(NULL);
return 0;
EXP NO: 10
Program to simulate the concept of Dining-Philosophers problem.
Date:
38
Operating System
#include <bits/stdc++.h>
#include <pthread.h>
#include <unistd.h>
#define N 10
#define THINKING 2
#define HUNGRY 1
#define EATING 0
// Philosopher index
int phil[N];
class monitor {
39
Operating System
int state[N];
pthread_cond_t phcond[N];
pthread_mutex_t condLock;
public:
if (state[(phnum + 1) % 5] != EATING
40
Operating System
state[phnum] = EATING;
pthread_cond_signal(&phcond[phnum]);
pthread_mutex_lock(&condLock);
// Indicates it is hungry
state[phnum] = HUNGRY;
test(phnum);
41
Operating System
if (state[phnum] != EATING) {
pthread_cond_wait(&phcond[phnum], &condLock);
<< endl;
pthread_mutex_unlock(&condLock);
pthread_mutex_lock(&condLock);
state[phnum] = THINKING;
42
Operating System
test(RIGHT);
test(LEFT);
pthread_mutex_unlock(&condLock);
// constructor
monitor()
state[i] = THINKING;
pthread_cond_init(&phcond[i], NULL);
43
Operating System
pthread_mutex_init(&condLock, NULL);
// destructor
~monitor()
pthread_cond_destroy(&phcond[i]);
pthread_mutex_destroy(&condLock);
phil_object;
44
Operating System
int c = 0;
int i = *(int*)arg;
sleep(1);
phil_object.take_fork(i);
sleep(0.5);
phil_object.put_fork(i);
c++;
int main()
// Declaration...
45
Operating System
pthread_t thread_id[N];
pthread_attr_t attr;
// Initialization...
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);
phil[i] = i;
// Creating...
&phil[i]);
<< endl;
46
Operating System
// Joining....
pthread_join(thread_id[i], NULL);
// Destroying
pthread_attr_destroy(&attr);
pthread_exit(NULL);
return 0;
47