Os File PDF 4 10
Os File PDF 4 10
#include <iostream>
using namespace std;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {
int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0;
while (1){
bool done = true; // Current time
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = false; // There is a pending process
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
} else {
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "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];
cout << " " << i + 1 << "\t\t" << bt[i] << "\t " << wt[i] << "\t\t "
<< tat[i] << endl;
}
cout << "Average waiting time = " << (float)total_wt / (float)n;
cout << "\nAverage turn around time = " << (float)total_tat / (float)n;
}
int main() {
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
OUTPUT:
PROGRAM 5
AIM: Write a program for page replacement policy using a) LRU b)
FIFO c) Optimal.
#include <bits/stdc++.h>
using namespace std;
int pageFaults(int pages[], int n, int capacity) {
unordered_set<int> s;
unordered_map<int, int> indexes;
int page_faults = 0;
for (int i = 0; i < n; i++) {
if (s.size() < capacity) {
if (s.find(pages[i]) == s.end()) {
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
} else {
if (s.find(pages[i]) == s.end()) {
int lru = INT_MAX, val;
for (auto it = s.begin(); it != s.end(); it++) {
if (indexes[*it] < lru) {
lru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
}
return page_faults;
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity) << endl;
return 0;
}
OUTPUT:
B) Program for page replacement policy using FIFO:
#include<bits/stdc++.h>
using namespace std;
return page_faults;
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}
OUTPUT:
#include <bits/stdc++.h>
using namespace std;
// Function to check if a page is present in the frame
bool search(int key, vector<int>& fr) {
for (int i = 0; i < fr.size(); i++) {
if (fr[i] == key) {
return true;
}
}
return false;
}
// Function to predict which page to replace (Optimal Page Replacement)
int predict(int pg[], vector<int>& fr, int pn, int index) {
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
// If the page is not found in the future, it's the one to replace
if (j == pn) {
return i;
}
}
return (res == -1) ? 0 : res;
}
// Function to implement the Optimal Page Replacement algorithm
void optimalPage(int pg[], int pn, int fn) {
vector<int> fr; // The frame (memory)
int hit = 0; // Number of page hits
OUTPUT:
PROGRAM 6
AIM: Write a program to implement first fit, best fit and worst fit
algorithm for memory management.
#include <bits/stdc++.h>
using namespace std;
// Function to implement the First Fit memory allocation algorithm
void firstFit(int blockSize[], int numBlocks, int processSize[], int
numProcesses) {
int allocation[numProcesses];
// Initialize the allocation array to -1 (indicating no block is allocated)
memset(allocation, -1, sizeof(allocation));
// Iterate through each process
for (int i = 0; i < numProcesses; i++) {
// Iterate through each block
for (int j = 0; j < numBlocks; j++) {
// If the current block is large enough to hold the process
if (blockSize[j] >= processSize[i]) {
// Allocate the block to the process
allocation[i] = j;
// Update the remaining space in the block
blockSize[j] -= processSize[i];
break; // Move on to the next process
}
}
}
// Print the allocation results
cout << "\nMemory Allocation Table:\n";
cout << "---------------------------------------\n";
cout << "Process No.\tProcess Size\tBlock No.\n";
cout << "---------------------------------------\n";
for (int i = 0; i < numProcesses; i++) {
cout << " " << i + 1 << "\t\t\t\t" << processSize[i] << "\t\t\t";
if (allocation[i] != -1) {
cout << allocation[i] + 1; // Print the allocated block number
} else {
cout << "Not Allocated"; // Print "Not Allocated" if no block was found
}
cout << endl;
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int numBlocks = sizeof(blockSize) / sizeof(blockSize[0]);
int numProcesses = sizeof(processSize) / sizeof(processSize[0]);
firstFit(blockSize, numBlocks, processSize, numProcesses);
return 0;
}
OUTPUT:
#include <bits/stdc++.h>
using namespace std;
// Function to implement the Best Fit memory allocation algorithm
void bestFit(int blockSize[], int numBlocks, int processSize[], int
numProcesses) {
int allocation[numProcesses];
// Initialize the allocation array to -1 (indicating no block is
allocated)
memset(allocation, -1, sizeof(allocation));
// Iterate through each process
for (int i = 0; i < numProcesses; i++) {
int bestIdx = -1;
// Iterate through each block
for (int j = 0; j < numBlocks; j++) {
// If the current block is large enough to hold the process
if (blockSize[j] >= processSize[i]) {
// If it's the first suitable block, mark it as the best
if (bestIdx == -1) {
bestIdx = j;
}
// If the current block is smaller than the current best,
update the best
else if (blockSize[bestIdx] > blockSize[j]) {
bestIdx = j;
}
}
}
// If a best fit block was found
if (bestIdx != -1) {
// Allocate the block to the process
allocation[i] = bestIdx;
// Update the remaining space in the block
blockSize[bestIdx] -= processSize[i];
} else {
// If no suitable block was found for the process
cout << "Process " << i + 1 << " (size: " << processSize[i] << ")
could not be allocated.\n";
}
}
OUTPUT:
#include<bits/stdc++.h>
using namespace std;
if (wstIdx != -1) {
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
OUTPUT:
PROGRAM 7
AIM: Write a program to implement reader/writer problem using
semaphore.
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
class monitor {
private:
int rcnt, wcnt, waitr, waitw;
pthread_cond_t canread, canwrite;
pthread_mutex_t condlock;
public:
monitor() {
rcnt = wcnt = waitr = waitw = 0;
pthread_cond_init(&canread, NULL);
pthread_cond_init(&canwrite, NULL);
pthread_mutex_init(&condlock, NULL);
}
void beginread(int i) {
pthread_mutex_lock(&condlock);
if (wcnt == 1 || waitw > 0) {
waitr++;
pthread_cond_wait(&canread, &condlock);
waitr--;
}
rcnt++;
cout << "reader " << i << " is reading\n";
pthread_mutex_unlock(&condlock);
pthread_cond_broadcast(&canread);
}
void endread(int i) {
pthread_mutex_lock(&condlock);
if (--rcnt == 0)
pthread_cond_signal(&canwrite);
pthread_mutex_unlock(&condlock);
}
void beginwrite(int i) {
pthread_mutex_lock(&condlock);
if (wcnt == 1 || rcnt > 0) {
++waitw;
pthread_cond_wait(&canwrite, &condlock);
--waitw;
}
wcnt = 1;
cout << "writer " << i << " is writing\n";
pthread_mutex_unlock(&condlock);
}
void endwrite(int i) {
pthread_mutex_lock(&condlock);
wcnt = 0;
if (waitr > 0)
pthread_cond_signal(&canread);
else
pthread_cond_signal(&canwrite);
pthread_mutex_unlock(&condlock);
}
};
monitor M;
void* reader(void* id) {
int c = 0;
int i = *(int*)id;
while (c < 5) {
usleep(1);
M.beginread(i);
M.endread(i);
c++;
}
return NULL; // Return NULL to signal thread completion
}
void* writer(void* id) {
int c = 0;
int i = *(int*)id;
while (c < 5) {
usleep(1);
M.beginwrite(i);
M.endwrite(i);
c++;
}
return NULL; // Return NULL to signal thread completion
}
int main() {
pthread_t r[5], w[5];
int id[5];
for (int i = 0; i < 5; i++) {
id[i] = i;
pthread_create(&r[i], NULL, &reader, &id[i]);
pthread_create(&w[i], NULL, &writer, &id[i]);
}
for (int i = 0; i < 5; i++) {
pthread_join(r[i], NULL);
}
for (int i = 0; i < 5; i++) {
pthread_join(w[i], NULL);
}
return 0; // Return 0 to signal successful program termination
}
OUTPUT:
PROGRAM 8
AIM: Write a program to implement Producer-Consumer problem using
semaphores.
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer() {
#pragma omp critical
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces item %d", x);
++mutex;
}
}
void consumer() {
#pragma omp critical
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item %d", x);
x--;
++mutex;
}
}
int main() {
int n, i;
printf("\n1. Press 1 for Producer \n2. Press 2 for Consumer \n3. Press 3 for
Exit");
#pragma omp parallel
{
#pragma omp critical
for (i = 1; i > 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1) && (empty != 0)) {
producer();
} else {
printf("Buffer is full!");
}
break;
case 2:
if ((mutex == 1) && (full != 0)) {
consumer();
} else {
printf("Buffer is empty!");
}
break;
case 3:
printf("Exiting...\n");
exit(0);
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
return 0;
}
OUTPUT:
PROGRAM 9
AIM: Write a program to implement Banker’s algorithm for deadlock
avoidance.
#include<iostream>
using namespace std;
const int P = 5;
const int R = 3;
void calculateNeed(int need[P][R], int maxm[P][R], int allot[P][R])
{
for (int i = 0 ; i < P ; i++)
for (int j = 0 ; j < R ; j++)
need[i][j] = maxm[i][j] - allot[i][j];
}
bool isSafe(int processes[], int avail[], int maxm[][R], int allot[][R])
{
int need[P][R];
calculateNeed(need, maxm, allot);
bool finish[P] = {0};
int safeSeq[P];
int work[R];
for (int i = 0; i < R ; i++)
work[i] = avail[i];
int count = 0;
while (count < P)
{
bool found = false;
for (int p = 0; p < P; p++)
{
if (finish[p] == 0)
{
int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;
if (j == R)
{
for (int k = 0 ; k < R ; k++)
work[k] += allot[p][k];
safeSeq[count++] = p;
finish[p] = 1;
found = true;
}
}
}
if (found == false)
{
cout << "System is not in safe state";
return false;
}
}
cout << "System is in safe state.\nSafe"
" sequence is: ";
for (int i = 0; i < P ; i++)
cout << safeSeq[i] << " ";
return true;
}
int main()
{
int processes[] = {0, 1, 2, 3, 4};
int avail[] = {3, 3, 2};
int maxm[][R] = {{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};
int allot[][R] = {{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}};
isSafe(processes, avail, maxm, allot);
return 0;
}
OUTPUT:
PROGRAM 10
AIM: Write C programs to implement the various File
Organization Techniques.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
char name[20];
} Record;
void createSequentialFile() {
FILE *fp;
Record record;
fp = fopen("sequential_file.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
exit(1);
}
printf("Enter the number of records: ");
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter ID and name for record %d: ", i + 1);
scanf("%d %s", &record.id, record.name);
fwrite(&record, sizeof(Record), 1, fp);
}
fclose(fp);
}
void displaySequentialFile() {
FILE *fp;
Record record;
fp = fopen("sequential_file.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
exit(1);
}
while (fread(&record, sizeof(Record), 1, fp) == 1) {
printf("ID: %d, Name: %s\n", record.id, record.name);
}
fclose(fp);
}
int main() {
createSequentialFile();
displaySequentialFile();
return 0;
}
OUTPUT:
Enter the number of records: 3
Enter ID and name for record 1: 1 John
Enter ID and name for record 2: 2 Alice
Enter ID and name for record 3: 3 Bob
ID: 1, Name: John
ID: 2, Name: Alice
ID: 3, Name: Bob