---------------------------------------UNIX
FILE SYSTEM-----------------------------------
---------------------------------------Fage replacement -----------------------------------
#include <stdio.h>
#include <iostream>
//#include <Algorithm>
#include <bits/stdc++.h>
using namespace std;
int findLRU(int time[], int n)
{
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i)
{
if(time[i] < minimum)
{
minimum = time[i];
pos = i;
}
}
return pos;
}
void fifo()
{
cout<<"\n-------PAGE REPLACEMENT ALGORITHM [FIFO]-------
:\n\n";
int n, a[50], frame[10], no, k, avail, count = 0;
cout<<"\n ENTER THE NUMBER OF PAGES :\n";
cin>>n;
cout<<"\n ENTER THE VALUE FOR PAGE NUMBER \n";
for (int i = 1; i <= n; i++)
{
cout<<"Page no a[" << i << "] ";
cin>>a[i];
}
cout<<"\nENTER THE NUMBER OF FRAMES\n";
cin>>no;
for (int i = 0; i < no; i++)
{
frame[i] = -1;
}
int j = 0;
cout << "\tRef Pg Number:\t Page frames:\n";
for (int i = 1; i <= n; i++)
{
cout << "\t\t" << a[i];
avail = 0;
for (k = 0; k < no; k++)
{
if (frame[k] == a[i])
avail = 1;
}
if (avail == 0)
{
frame[j] = a[i];
j = (j + 1) % no;
count++;
for (k = 0; k < no; k++)
{
cout << "\t" << frame[k];
}
}
cout << endl;
}
cout << "\nNUMBER OF PAGE FAULTS : " << count<<endl;
cout<<"----------------------------------------------------------------------
"<<endl;
}
void LRU()
{
cout<<"\n-------PAGE REPLACEMENT ALGORITHM [LRU]------- :\n\n";
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0,
time[10], flag1, flag2, i, j, pos, faults = 0;
cout<<"\nENTER THE NUMBER OF FRAMES: ";
cin>>no_of_frames;
cout<<"\nENTER THE NUMBER OF PAGES: ";
cin>>no_of_pages;
cout<<"\ENTER THE REFERENCE STRING: ";
for(i = 0; i < no_of_pages; ++i)
{
cin>>pages[i];
}
for(i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
cout<<"\n";
for(j = 0; j < no_of_frames; ++j)
{
cout<<"\t"<<frames[j];
}
}
cout<<"\n\nTOTAL FAGE FAULTS: "<<faults<<endl;
cout<<"----------------------------------------------------------------------
"<<endl;
}
void Optimal()
{
cout<<"\n-------PAGE REPLACEMENT ALGORITHM [OPTIMAL]-------
:\n\n";
int l;
cout<<"\nENTER THE LENGTH OF REFERENCE STRING :";
cin>>l;
int
arr[l],pf=0,ph=0,flag1=0,flag2=0,flag3=0,count1=0,count2=0,count3=0;
cout<<"\nENTER THE REFERENCE STRING:\n";
for(int i=0;i<l;i++)
{
cout<<"-> ";
cin>>arr[i];
}
cout<<endl;
int a=-1,b=-1,c=-1,d=0,i=0,f1=0,f2=1,f3=1;
while(i<l)
{
d=arr[i];
if(a==-1)
{
a=d;
pf++;
cout<<a<<endl;
}
else if(b==-1)
{
b=d;
pf++;
cout<<a<<"\t"<<b<<endl;
}
else if(c==-1)
{
c=d;
pf++;
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
}
if(i>2)
{
if(d==a || d==b || d==c)
{
ph++;
cout<<endl;
}
else
{
flag1=0,flag3=0,flag2=0;
count1=0,count2=0,count3=0;
for(int k=i; k<l; k++)
{
if(arr[k]==a && flag1==0)
{
flag1=1;
}
else if(arr[k]==b && flag2==0)
{
flag2=1;
}
else if(arr[k]==c && flag3==0)
{
flag3=1;
}
if(flag1==0)
{
count1++;
}
if(flag2==0)
{
count2++;
}
if(flag3==0)
{
count3++;
}
if(flag1==1 && flag2==1 && flag3==1)
{
break;
}
}
if(count1>count2 && count1>count3)
{
a=d;
pf++;
}
else if(count2>count1 && count2>count3)
{
b=d;
pf++;
}
else if(count3>count2 && count3>count1)
{
c=d;
pf++;
}
else{
if(flag1==0 ){
a=d;
}
else if(flag2==0){
b=d;
}
else{
c=d;
}
pf++;
}
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
}
}
i++;
}
cout<<"\nPAGE FAULTS:"<<pf;
cout<<"\nPAGE HITS:"<<ph<<endl;
};
int main()
{
cout << "\n\t*****----------PAGE REPLACEMENT
ALGORITHM----------*****\n";
int ch;
//int exit=0;
while (1)
{
cout << "\n 1. FIFO \n 2. LRU \n 3. OPTIMAL \n 4. EXIT
\n";
cout << "\n ENETR YOUR CHOICE: ";
cin >> ch;
switch (ch)
{
case 1:
{
fifo();
break;
}
case 2:
{
LRU();
break;
}
case 3:
{
Optimal();
break;
}
case 4:
{
exit(1);
break;
}
}
}
}
---------------------------------------FIFO------------------------------------------
FIFO
#include<iostream>
using namespace std;
int main()
{
// cout<<"\nPage Replacement Algorithm (FIFO) :\n\n";
cout<<"ENTER THE LENGTH OF REFRENCE STRING:"<<endl;
int l;
cin>>l;
//cout<<"\nEnter the Length of the reference string :";
//cin>>l;
int pf=0,ph=0,flag1=0,flag2=0,flag3=0,count1=0,count2=0,count3=0;
int arr[l];
// cin>>arr[l];
cout<<"\nEnter the Reference String:";
for(int i=0;i<l;i++)
{
//cout<<"-> ";
cin>>arr[i];
}
cout<<endl;
int a=-1,b=-1,c=-1,d=0,i=0,f1=0,f2=1,f3=1;
while(i<l)
{
d=arr[i];
if(a==-1)
{
a=d;
pf++;
cout<<a<<endl;
}
else if(b==-1)
{
b=d;
pf++;
cout<<a<<"\t"<<b<<endl;
}
else if(c==-1)
{
c=d;
pf++;
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
}
if(i>2)
{
if(d==a || d==b || d==c)
{
ph++;
cout<<endl;
}
else
{
flag1=0,flag3=0,flag2=0;
count1=0,count2=0,count3=0;
for(int k=i; k<l; k++)
{
if(arr[k]==a && flag1==0)
{
flag1=1;
}
else if(arr[k]==b && flag2==0)
{
flag2=1;
}
else if(arr[k]==c && flag3==0)
{
flag3=1;
}
if(flag1==0)
{
count1++;
}
if(flag2==0)
{
count2++;
}
if(flag3==0)
{
count3++;
}
if(flag1==1 && flag2==1 && flag3==1)
{
break;
}
}
if(count1>count2 && count1>count3)
{
a=d;
pf++;
}
else if(count2>count1 && count2>count3)
{
b=d;
pf++;
}
else if(count3>count2 && count3>count1)
{
c=d;
pf++;
}
else{
if(flag1==0 ){
a=d;
}
else if(flag2==0){
b=d;
}
else{
c=d;
}
pf++;
}
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
}
}
i++;
}
cout<<"\nPage Faults :"<<pf;
cout<<"\nPage Hits :"<<ph<<endl;
return 0;
}
---------------------------------------LRU -----------------------------------
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"\nenter the no of pages=";
cin>>n;
int arr[n];
int nq;
bool ter;
for(int i=0;i<n;i++)
{
cout<<"\nEnter page no=";
cin>>arr[i];
}
cout<<"\n enter the size of page=";
cin>>nq;
int que[nq];
int page_hit=0;
int page_fault=0;
int count=0;
int qp=0;
for(int i=0;i<n;i++)
{ter=false;
if(i<nq)
{
que[count]=arr[i];
count++;
page_fault++;
}
else
{
for(int j=0;j<nq;j++)
{
if(que[j]==arr[i])
{
page_hit++;
ter=true;
break;
}
}
if(ter==false)
{
int temp[nq]={0,0,0};
int count2=0;
while(count2<nq)
{
for(int l=i-1;l>=0;l--)
{
if(arr[l]==que[count2])
{
temp[count2]=l;
break;
}
}
count2++;
}
int min,min_pos;
min=temp[0];
min_pos=0;
for(int k=1;k<nq;k++)
{
if(min>temp[k])
{
min=temp[k];
min_pos=k;
}
}
que[min_pos]=arr[i];
page_fault++;
}
}
cout<<"\n temp=";
for(int i=0;i<nq;i++)
{
cout<<que[i]<<" ";
}
}
cout<<"\n page hit="<<page_hit;
cout<<"\n page fault="<<page_fault;
cout<<"\n quee=";
for(int i=0;i<nq;i++)
{
cout<<que[i]<<" ";
}
}
// 7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1
-----------------------------------✌✌✌✌✌✌✌✌✌
--------------------------------------OPTIMAL -----------------------------------
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"\nenter the no of pages=";
cin>>n;
int arr[n];
int nq;
bool ter;
for(int i=0;i<n;i++)
{
cout<<"\nEnter page no=";
cin>>arr[i];
}
cout<<"\n enter the size of page=";
cin>>nq;
int que[nq];
int page_hit=0;
int page_fault=0;
int count=0;
int qp=0;
for(int i=0;i<n;i++)
{ter=false;
if(i<nq)
{
que[count]=arr[i];
count++;
page_fault++;
}
else
{
for(int j=0;j<nq;j++)
{
if(que[j]==arr[i])
{
page_hit++;
ter=true;
break;
}
}
if(ter==false)
{
int temp[nq]={0};
int count2=0;
while(count2<nq)
{
for(int l=i+1;l<n;l++)
{
if(arr[l]==que[count2])
{
temp[count2]=l;
break;
}
}
count2++;
}
int max,max_pos;
max=temp[0];
max_pos=0;
for(int k=1;k<nq;k++)
{
if(temp[k]==0)
{
max=temp[k];
max_pos=k;
}
if(max<temp[k])
{
max=temp[k];
max_pos=k;
}
}
que[max_pos]=arr[i];
page_fault++;
}
}
cout<<"\n temp=";
for(int i=0;i<nq;i++)
{
cout<<que[i]<<" ";
}
}
cout<<"\n page hit="<<page_hit;
cout<<"\n page fault="<<page_fault;
cout<<"\n quee=";
for(int i=0;i<nq;i++)
{
cout<<que[i]<<" ";
}
}
// 7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1
---------------------------------------Bankers✌✌✌✌✌----------------------------------
#include<iostream>
using namespace std;
int alloc[10][10],Max[10][10],need[10][10],avail[1][10];
int N,R,finish[10];
void calculateNeed(){
for(int i=0;i<N;i++){
for(int j=0;j<R;j++){
need[i][j]=0;
need[i][j]=Max[i][j]-alloc[i][j];
}
}
}
void sequence(){
int cp=0,cur=0;
cout<<"\n The safe sequence is : ";
while(cp<N){
if(finish[cur]==0){
bool flag=false;
for(int j=0;j<R;j++){
if(need[cur][j]>avail[0][j]){
flag=false;
break;
}
else
flag=true;
}
if(flag){
cout<<"\t P"<<cur;
cp++;
finish[cur]=1;
for(int i=0;i<R;i++){
avail[0][i]+=alloc[cur][i];
}
}
}
cur=(cur+1)%N;
}
}
int main(){
cout<<"\n Enter the no. of process : ";
cin>>N;
cout<<"\n Enter the no. of Resources : ";
cin>>R;
for(int i=0;i<N;i++){
cout<<"\n";
finish[i]=0;
for(int j=0;j<R;j++){
cout<<" Enter the no of instances of resource "<<j<<" alloated to P "<<i<<" : ";
cin>>alloc[i][j];
}
}
for(int i=0;i<N;i++){
cout<<"\n";
for(int j=0;j<R;j++){
cout<<" Enter the max no of instances of resource "<<j<<" required to P "<<i<<" : ";
cin>>Max[i][j];
}
}
cout<<"\n";
for(int j=0;j<R;j++){
cout<<" Enter the available instances of resource "<<j<<" : ";
cin>>avail[0][j];
}
calculateNeed();
cout<<"\n Allocaation :-";
for(int i=0;i<N;i++){
cout<<"\n";
for(int j=0;j<R;j++){
cout<<alloc[i][j]<<"\t";
}
}
cout<<"\n Max:- ";
for(int i=0;i<N;i++){
cout<<"\n";
for(int j=0;j<R;j++){
cout<<Max[i][j]<<"\t";
}
}
cout<<"\n Need :- ";
for(int i=0;i<N;i++){
cout<<"\n";
for(int j=0;j<R;j++){
cout<<need[i][j]<<"\t";
}
}
sequence();
return 0;
}
--------------------------------------Barber ✌✌✌✌✌✌✌✌✌-----------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<semaphore.h>
#include<pthread.h>
using namespace std;
sem_t empty;
pthread_mutex_t mutex;
int val;
void *none;
void * cutting(void *cno){
printf("\nCustomer %d is arrived.",*((int*)cno));
sem_getvalue(&empty,&val);
if(val==5){
sem_wait(&empty);
pthread_mutex_lock(&mutex);
printf("\n Barber is sleeping ...");
printf("\n Customer %d wake ups barber",*((int*)cno));
printf("\n Barber cuts hair of customer %d",*((int*)cno));
printf("\n Hair cutting of customer %d is completed",*((int*)cno));
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
else if(val>0){
sem_wait(&empty);
pthread_mutex_lock(&mutex);
printf("\n Customer %d is waiting ....",*((int*)cno));
printf("\n Barber cuts hair of customer %d",*((int*)cno));
printf("\nHair cutting of customer %d is completed",*((int*)cno));
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
else{
printf("\n no empty chairs are there so, Customer %d is leaving.",*((int*)cno));
}
return none;
}
int main(){
int a[8]={1,2,3,4,5,6,7,8};
pthread_t customer[8];
sem_init(&empty,0,5);
for(int i=0;i<8;i++){
pthread_create(&customer[i],NULL,&cutting,(void *)&a[i]);
}
for(int i=0;i<8;i++){
pthread_join(customer[i],NULL);
}
return 0;
}
✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌==-----PIPE-------------------------
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
void processA(int writefd){
char buff[80];
int n=9;
printf("you are in the process A \n Enter the string : ");
scanf("%s",&buff);
write(writefd,buff,n);
}
void processB(int readfd){
char buff[80];
int n;
n=read(readfd,buff,80);
printf("you are in the process_B \n reading from a process_A : %s %d \n",buff,n);
printf("The reversed string is : \t");
for(int i=n;i>=0;i--){
printf("%c",buff[i]);
}
}
int main(){
int fd[2];
pid_t pid;
pipe(fd);
pid=fork();
if(pid>0){
processA(fd[1]);
}
else{
processB(fd[0]);
}
return 0;
}
✌✌✌✌✌✌✌✌✌ FCFS-------------------------
#include<iostream>
using namespace std;
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cout<<"Enter total number of processes:";
cin>>n;
cout<<"\nEnter Process Burst Time\n";
for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
}
avwt/=i;
avtat/=i;
cout<<"\n\nAverage Waiting Time:"<<avwt;
cout<<"\nAverage Turnaround Time:"<<avtat;
return 0;
}