All Problem
All Problem
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<stdbool.h>
bool flag[2];
int turn;
void* P0_processor(void* arg)
{
while(true)
{
flag[0]=true;//1
turn=1;
while(flag[1]&&turn==1)
{
;//infinite loop
}
// critical section
printf("Process 0 is in critical section for 5 second\n");
for(int i=0;i<5;i++)
{
sleep(1);
printf("Process 0 on critical Section [time]: %d",i+1);
printf("\n");
}
flag[0]=false;
printf("Process 0 is in remainder section\n\n");
}
}
// Processor 1
void* P1_processor(void *arg)//flag[0]=1 , flag[1]=1;
{
while(true)
{
flag[1]=true;//1
turn=0;//0
while(flag[0]&&turn==0)
{
;//infinite loop
}
// critical section
printf("Process 1 is in critical section for 5 second\n");
for(int i=0;i<5;i++)
{
//this_thread::sleep_for::chrono::seconds(1);
sleep(1);
printf("Process 1 on critical Section [time]: %d\n",i+1);
//printf("\n");
}
flag[1]=false;
printf("Process 1 is in remainder section\n\n");
}
}
int main()
{
flag[0]=false;
flag[1]=false;
pthread_t t1,t2;
pthread_create(&t1,NULL,P0_processor,NULL);
pthread_create(&t2,NULL,P1_processor,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
void *reader(void *rno)
{
pthread_mutex_lock(&mutex);//mutex accquired lock//down(mutux)
numreader++;
if(numreader==1)
{
sem_wait(&wrt);//while(s) if(s<=0){;},else s--
}
pthread_mutex_unlock(&mutex);//mutex relace the unlock
// CS
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
pthread_mutex_lock(&mutex);
numreader--;
if(numreader==0)
{
sem_post(&wrt);
}
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex,NULL);// initilize default attribute for
mutex lock
sem_init(&wrt,0,1);// Address of the semaphore,shared between threads,
initilize value 1
int a[10]={1,2,3,4,5,6,7,8,9,10};//just used for numbering reader and
writter
for(int i=0;i<10;i++)
{
pthread_create(&read[i],NULL,(void *)reader,(void *)&a[i]);
}
for(int i=0;i<5;i++)
{
pthread_create(&write[i],NULL,(void *)writer,(void *)&a[i]);
}
for(int i=0;i<10;i++)
{
pthread_join(read[i],NULL);
}
for(int i=0;i<5;i++)
{
pthread_join(write[i],NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}
************************ Producer consumer Problem *********
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
/*
This program provides a possible solution for producer-consumer problem
using mutex and semaphore.
I have used 5 producers and 5 consumers to demonstrate the solution. You
can always play with these values.
*/
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void *consumer(void *cno)
{
for(int i = 0; i < MaxItems; i++) {
// Entry Section
sem_wait(&full);//decrease
pthread_mutex_lock(&mutex);
// CS
int item = buffer[out];
printf("Consumer %d: Remove Item %d from %d\n",*((int *)cno),item,
out);
out = (out+1)%BufferSize;
//Exit section
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main()
{
pthread_t pro[5],con[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&empty,0,BufferSize);
sem_init(&full,0,0);
int a[5] = {1,2,3,4,5}; //Just used for numbering the producer and
consumer
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
********************Dyning philosofar****************
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
sem_t chopstick[5];// Declare Semaphore variable
void eat(int ph)
{
printf("Philospher %d begins to eat\n",ph);
}
void *philos(void *n)
{
int ph=*(int *)n;
eat(ph);
sleep(2);
int main()
{
int i,n[5];
pthread_t T[5];// Every philosopher is one thread
for(i=0;i<5;i++)
{
sem_init(&chopstick[i],0,1);//Semaphorem, Semaphore shere between
process or bwtn threads, initialize
}
for(i=0;i<5;i++){
n[i]=i;
pthread_create(&T[i],NULL,philos,(void *)&n[i]);
}
for(i=0;i<5;i++)
{
pthread_join(T[i],NULL);
}
}
*******************Best Fit************
//minimum fagmantation
#include<stdio.h>
int main()
{
printf("Enter the number of block:\n");
int n;scanf("%d",&n);
int
block_no[n+1],block_size[n+1],allocation[n+1];
for(int i=0;i<n;i++)
{
block_no[i]=i+1;
printf("Enter the block size :");
scanf("%d",&block_size[i]);
allocation[i]=-1;
}
printf("Enter the number of process:\n");
int pno;
scanf("%d",&pno);
int process_size[pno];
for(int i=0;i<pno;i++)
{
printf("Enter the process size
P%d:",(i+1));
scanf("%d",&process_size[i]);
}
//printf("Process_No\t\t
Process_size\t\tBlock_No\n");
for(int i=0;i<pno;i++)
{
int mn=1e9,indx=-1;
for(int j=0;j<n;j++)
{
if(block_size[j]>=process_size[i])
{
int k=block_size[j]-
process_size[i];
if(k<=mn)
{
mn=k;
indx=j;
}
}
}
if(indx!=-1)
{
allocation[i]=indx;
block_size[indx]-=process_size[i];
}
}
printf("Process_No\tProcess_size\tBlock_No\n");
for(int i=0;i<pno;i++)
{
printf("%d\t\t%d\t\t",i+1,process_size[i]);
if(allocation[i]!=-1)
printf("%d\n",allocation[i]+1);
else
printf("Not Allocation\n");
}
}
****************Worst Fit***********************
//maximum fagmentation
#include<stdio.h>
int main()
{
printf("Enter the number of block:\n");
int n;scanf("%d",&n);
int block_no[n+1],block_size[n+1],allocation[n+1];
for(int i=0;i<n;i++)
{
block_no[i]=i+1;
printf("Enter the block size :");
scanf("%d",&block_size[i]);
allocation[i]=-1;
}
printf("Enter the number of process:\n");
int pno;
scanf("%d",&pno);
int process_size[pno];
for(int i=0;i<pno;i++)
{
printf("Enter the process size P%d:",(i+1));
scanf("%d",&process_size[i]);
}
//printf("Process_No\t\t Process_size\t\tBlock_No\n");
for(int i=0;i<pno;i++)
{
int mn=0,indx=-1;
for(int j=0;j<n;j++)
{
if(block_size[j]>=process_size[i])
{
int k=block_size[j]-process_size[i];
if(k>=mn)
{
mn=k;
indx=j;
}
}
}
if(indx!=-1)
{
allocation[i]=indx;
block_size[indx]-=process_size[i];
}
}
printf("Process_No\tProcess_size\tBlock_No\n");
for(int i=0;i<pno;i++)
{
printf("%d\t\t%d\t\t",i+1,process_size[i]);
if(allocation[i]!=-1)
printf("%d\n",allocation[i]+1);
else
printf("Not Allocation\n");
}
}
************************First Fit********************
//
#include<stdio.h>
int main()
{
printf("Enter the number of block:\n");
int n;scanf("%d",&n);
int block_no[n+1],block_size[n+1],allocation[n+1];
for(int i=0;i<n;i++)
{
block_no[i]=i+1;
printf("Enter the block size :");
scanf("%d",&block_size[i]);
allocation[i]=-1;
}
printf("Enter the number of process:\n");
int pno;
scanf("%d",&pno);
int process_size[pno];
int bolean[n];
memset(bolean,0,sizeof(bolean));
for(int i=0;i<pno;i++)
{
printf("Enter the process size P%d:",(i+1));
scanf("%d",&process_size[i]);
}
//printf("Process_No\t\t Process_size\t\tBlock_No\n");
for(int i=0;i<pno;i++)
{
for(int j=0;j<n;j++)
{
if(block_size[j]>=process_size[i]&&bolean[j]==0)
{
allocation[i]=j;
bolean[j]=-1;
block_size[j]-=process_size[i];
break;
}
}
}
printf("Process_No\tProcess_size\tBlock_No\n");
for(int i=0;i<pno;i++)
{
printf("%d\t\t%d\t\t",i+1,process_size[i]);
if(allocation[i]!=-1)
printf("%d\n",allocation[i]+1);
else
printf("Not Allocation\n");
}
}
****************FIFO Page Replacement************
#include<stdio.h>
int main()
{
int n,frame,hit=0,page_fault=0;
printf("Please Enter the number of page replacement string size
(n):\n");
scanf("%d",&n);
int page_string[n];
printf("Enter the reference string:\n");
for(int i=0; i<n; i++)
{
scanf("%d",&page_string[i]);
}
printf("Enter the number of Frames:\n");
scanf("%d",&frame);
int frame_array[frame];
for(int i=0; i<frame; i++)
{
frame_array[i]=-1;
}
int indx=0;
for(int i=0; i<n; i++)
{
int ok=0;
for(int j=0; j<frame; j++)
{
if(frame_array[j]==page_string[i])
{
hit++;
ok=1;
break;
}
if(ok==0)
{
//printf("Page hit= ",page_string);
page_fault++;
frame_array[indx]=page_string[i];
indx++;//
indx=indx%frame;//
}
for(int j=0; j<frame; j++)
{
printf("%d ",frame_array[j]);
}
if(ok==1)
{
printf("Number of Hit= %d",hit);
}
printf("\n");
}
double hit_ratio=((double) hit/n)*100;
double page_ratio=((double) page_fault/n)*100;
printf("\n\nNumber of page Hit= %d\n",hit);
printf("Number of page Hit Ratio = %lf\n",hit_ratio);
printf("Number of Page_fault= %d\n",page_fault);
printf("Number of Page_fault Ratio= %lf\n",page_ratio);
getch();
}
*****************LRU Page Replacement*********************
//Least Recently Used
#include<stdio.h>
#include <string.h>
// 19 = 3 2 1 3 4 1 6 2 4 3 4 2 1 4 5 2 1 3 4 // 3
int main()
{
int n,frame,hit=0,page_fault=0,mx_val=0;
printf("Please Enter the number of page replacement string size
(n):\n");
scanf("%d",&n);
int page_string[n];
printf("Enter the reference string:\n");
for(int i=0; i<n; i++)
{
scanf("%d",&page_string[i]);
if(mx_val<=page_string[i])// mx value calculate
mx_val=page_string[i];
}
printf("Enter the number of Frames:\n");
scanf("%d",&frame);
int frame_array[frame];
for(int i=0; i<frame; i++)
{
frame_array[i]=-1;
}
int indx=0;
for(int i=0; i<n; i++)
{
int frequency[mx_val+1],cnt=0;//
memset(frequency,0,sizeof(frequency));// frequencey array all
element are 0 set
int ok=0;
for(int j=0; j<frame; j++)// 0 to 3
{
if(frame_array[j]==page_string[i])// page hit point
{
hit++;
ok=1;
break;
}
}
if(ok==0)// page fault point
{
page_fault++;
if(indx>=frame)
{
for(int j=i-1;j>=0;j--)//
{
if(frequency[page_string[j]]==0)
{
frequency[page_string[j]]++;
int value=page_string[j];
cnt++;
if(cnt==frame)
{
for(int k=0;k<frame;k++)
{
if(value==frame_array[k])
{
frame_array[k]=page_string[i];
break;
}
}
break;
}
}
}
}
else{
//printf("Aise= %d\n",indx);
frame_array[indx]=page_string[i];
indx++;
//printf("value= %d\n",indx);
}
}
for(int j=0;j<frame;j++)
{
printf("%d ",frame_array[j]);
}
if(ok==1)
{
printf("Number of Hit= %d",hit);
}
printf("\n");
}
getch();
}
*********************Optimal Page Replacement*************
//
#include<stdio.h>
#include <string.h>
// 19 = 3 2 1 3 4 1 6 2 4 3 4 2 1 4 5 2 1 3 4 // 3
int main()
{
int n,frame,hit=0,page_fault=0,mx_val=0;
int frame_array[frame],total_frequency[mx_val+1];
for(int i=0; i<frame; i++)
{
frame_array[i]=-1;
}
int indx=0;
int frequency[mx_val+1], demo[mx_val+1];
memset(frequency,0,sizeof(frequency));// frequencey array all
element are 0 set
for(int i=0; i<n; i++)
{
int cnt=0;//
int ok=0;
for(int j=0; j<frame; j++)// 0 to frame
{
if(frame_array[j]==page_string[i])// page hit point
{
hit++;
ok=1;
break;
}
}
if(ok==0)// page fault point
{
page_fault++;
int okkk=0;
if(i<frame)//3 2 1 3 4 1 6 2 4 3 4 2 1 4 5 2 1 3 4
{
frame_array[i]=page_string[i];
frequency[frame_array[i]]++;//
}
else
{
for(int j=i+1;j<n;j++)
{
if(frequency[page_string[j]]>=1)
{
frequency[page_string[j]]--;
cnt++;
if(cnt==frame)
{
int value= frame_array[j];
for(int k=0;k<frame;k++)
{
if(value==frame_array[k])
{
okkk=1;
frame_array[k]=page_string[i];
break;
}
}
break;
}
}
}
if(okkk==0)
{
for(int k=0;k<frame;k++)
{
if(frequency[frame_array[k]]!=0)
{
frequency[frame_array[k]]--;
frame_array[k]=page_string[i];
okkk=1;
break;
}
}
}
for(int j=0;j<frame;j++)
{
frequency[frame_array[j]]++;
}
}
}
for(int j=0;j<frame;j++)
{
printf("%d ",frame_array[j]);
}
if(ok==1)
{
printf("Number of Hit= %d",hit);
}
printf("\n");
}
double hit_ratio=((double) hit/n)*100;
double page_ratio=((double) page_fault/n)*100;
printf("\n\nNumber of page Hit= %d\n",hit);
printf("Number of page Hit Ratio = %lf\n",hit_ratio);
printf("Number of Page_fault= %d\n",page_fault);
printf("Number of Page_fault Ratio= %lf\n",page_ratio);
getch();
}
*********************Banker’s Algorithm******************
#include <stdio.h>
int main()
{
int n, m, i, j, k, y,alloc[20][20],max[20][20],avail[20],ind=0;
printf("---------------Banker Algorithm-----------------\n");
p[i].pid=i+1;
}
// sort the process according to arrival time
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void pline(int x)
{
int i;
for(i=0;i<x;i++)
printf("-");
printf("\n");
}
****************************Priority Schedule (non P.)**********
#include<stdio.h>
#include<conio.h>
#define mx 30
void pline(int x);
int main()
{
int i,j,n,temp,pid[mx],bt[mx],wt[mx],pr[mx],tat[mx],t,twt=0,ttat=0;
float awt=0,atat=0;
printf("\n----------------PRIORITY SCHEDULING----------------\n");
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=pid[j];
pid[j]=pid[j+1];
pid[j+1]=temp;
}
}
}
tat[0]=bt[0];
wt[0]=0;
for(int i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
}
pline(44);
printf("pid Priority\tBurst\tWait\tTAT\n");
pline(44);
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\t%d\t%d\n",pid[i],pr[i],bt[i],wt[i],tat[i]);
ttat+=tat[i];
twt+=wt[i];
}
pline(44);
printf("\n\n");
awt=(float)twt/n;
atat =(float)ttat/n;
printf("\Total Waiting time: %d\nTotal turn Arround time:
%d\n",twt,ttat);
printf("\nAvg. Waiting time: %f\nAvg. turn Arround time:
%f\n",awt,atat);
getch();
}
void pline(int x)
{
int i;
for(i=0;i<x;i++)
printf("-");
printf("\n");
}
///////////////////////Priority Schedule//////////////////////
#include <stdio.h>
// b is array for burst time, p for priority and index for process id
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}
if(need[i]>=ts)//
{
for(int j=1;j<=n;j++)
{
if(i!=j&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else {
for(int j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1&&need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
}
}
}
for(int i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat+=tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("ProcessID Burst_time Waiting turn_arround_time\n");
for(int i=1;i<=n1;i++)
{
//printf("\n%5d\t%5d\t%5d\t%5d\n",i,pid[i],bt[i],wt[i],tat[i]);
printf("%d\t%d\t%d\t%d\n",pid[i],bt[i],wt[i],tat[i]);
}
printf("\nThe average waiting time= %f",awt);
printf("\nThe average turn around time = %f",atat);
}
********************Shortest Remaining Time*************
#include<stdio.h>
int main()
{
int at[10], bt[10], rt[10], completionTime, i, smallest = 0;
int remain = 0, n, t, sum_wait = 0, sum_turnaround = 0;
printf("Enter no of Processes: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter arrival time for Process P%d: ", i + 1);
scanf("%d", &at[i]);
printf("Enter burst time for Process P%d: ", i + 1);
scanf("%d", &bt[i]);
rt[i] = bt[i];
}
printf("\n\nProcess\t| Turnaround Time | Waiting Time\n\n");
for(t = 0; remain != n; t++)
{
smallest = -1;
for(i = 0; i < n; i++)
{
if(at[i] <= t && rt[i] && (smallest == -1 || rt[i] < rt[smallest]))
{
smallest = i;
}
}
if(smallest == -1)
{
continue;
}
rt[smallest]--;
if(rt[smallest] == 0)
{
remain++;
completionTime = t + 1;
printf("P[%d]\t| %d\t\t | %d\n", smallest + 1, completionTime -
at[smallest], completionTime - bt[smallest] - at[smallest]);
sum_wait += completionTime - bt[smallest] - at[smallest];
sum_turnaround += completionTime - at[smallest];
}
}
printf("\n\nAverage waiting time = %lf\n", sum_wait * 1.0 / n);
return 0;
}
*************************SJF**************************
#include<stdio.h>
struct PCB{
int bt,pid,wait,tat;
};
void pline(int);
int main()
{
struct PCB p[10],temp;
int i,j=1,sum=0,w_total=0,tat_total=0,n;
float w_avg=0,tat_avg=0;
printf("Enter the total number of process: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the burst time of process %d: \n",i+1);
scanf("%d",&p[i].bt);
p[i].pid=i+1;
}
// sort the process according to burst time
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].bt>p[j+1].bt)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
//
for(int i=0;i<n;i++)
{
p[i].wait=sum;
sum=sum+p[i].bt;
p[i].tat=sum;
}
pline(45);
printf("PID\tBurst\t wait \t turn_a_time\n");
pline(45);
for(int i=0;i<n;i++)
{
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].bt,p[i].wait,p[i].tat);
w_total+=p[i].wait;
tat_total+=p[i].tat;
}
pline(45);
w_avg=w_total/(float)n;
tat_avg=tat_total/(float)n;
printf("\nTotal waiting time: %d",w_total);
printf("\nAverage waiting time: %.3f",w_avg);
printf("\nTotal Turnarround time: %d",tat_total);
printf("\nAverage Turnarround time: %.3f",tat_avg);
}
void pline(int x)
{
int i;
for(i=0;i<x;i++)
printf("-");
printf("\n");
}