0% found this document useful (0 votes)
12 views47 pages

All Problem

The document contains various implementations of concurrent programming problems in C, including Peterson's solution for mutual exclusion, the Reader-Writer problem, the Producer-Consumer problem, the Dining Philosophers problem, and memory allocation strategies such as Best Fit, Worst Fit, and First Fit. Additionally, it covers page replacement algorithms like FIFO and LRU. Each section provides code examples demonstrating the respective algorithms and their functionalities.

Uploaded by

Md Ariful Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views47 pages

All Problem

The document contains various implementations of concurrent programming problems in C, including Peterson's solution for mutual exclusion, the Reader-Writer problem, the Producer-Consumer problem, the Dining Philosophers problem, and memory allocation strategies such as Best Fit, Worst Fit, and First Fit. Additionally, it covers page replacement algorithms like FIFO and LRU. Each section provides code examples demonstrating the respective algorithms and their functionalities.

Uploaded by

Md Ariful Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

******************Peterson*******************

#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;

**********************Reader Writter problem***************


#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#include<stdio.h>
sem_t wrt;
pthread_mutex_t mutex;//mutex lock
int cnt=1,numreader=0;
void *writer(void *wno)
{
sem_wait(&wrt);
cnt=cnt*2;
printf("writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);

}
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.
*/

#define MaxItems 5 // Maximum items a producer can produce or a consumer


can consume
#define BufferSize 5 // Size of the buffer

sem_t empty;// counting semaphore


sem_t full;// counting semaphore
int in = 0;
int out = 0;
int buffer[BufferSize];
pthread_mutex_t mutex;

void *producer(void *pno)


{
int item;
for(int i = 0; i < MaxItems; i++) {
item = rand(); // Produce an random item
sem_wait(&empty);//decrease
pthread_mutex_lock(&mutex);//

buffer[in] = item;// Critical section


printf("Producer %d: Insert Item %d at %d\n", *((int
*)pno),buffer[in],in);
in = (in+1)%BufferSize;

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

for(int i = 0; i < 5; i++) {


pthread_create(&pro[i], NULL, (void *)producer, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) {
pthread_create(&con[i], NULL, (void *)consumer, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) {
pthread_join(pro[i], NULL);
}
for(int i = 0; i < 5; i++) {
pthread_join(con[i], NULL);
}

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;

printf("philosopher %d wants to eat\n",ph);


printf("philosopher %d tries to pick left chopstick\n",ph);
sem_wait(&chopstick[ph]);// Semapr decrement. if value of smpr is 0 then
current thread block until smphr value is greater then 0.
printf("philosopher %d picks the left chopstick\n",ph);
printf("philosopher %d tries to pick the right chopstick\n",ph);
sem_wait(&chopstick[(ph+1)%5]);
printf("philosopher %d picks the right chopstick\n",ph);

eat(ph);
sleep(2);

printf("Philosopher %d has finished eating\n",ph);


sem_post(&chopstick[(ph+1)%5]);// unlocked the semaphore
printf("philosopher %d leaves the right chopstick\n",ph);
sem_post(&chopstick[ph]);
printf("philosopher %d leaves the left chopstrick\n",ph);

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");
}

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();

}
*********************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;

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],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");

printf("Enter the no of Proceses:");


scanf("%d",&n);
printf("Enter the no of Resources:");
scanf("%d",&m);
printf("Enter the Allocation Matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
scanf("%d",&alloc[i][j]);
}
printf("Enter the Max Matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
scanf("%d",&max[i][j]);
}
printf("Enter the Available Matrix");
for(i=0; i<m; i++)
scanf("%d",&avail[i]);
int finish[n], safesequence[n],work[m],need[n][m];
//calculating NEED matrix
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
printf("NEED matrix is");
for (i = 0; i < n; i++)
{
printf("\n");
for (j = 0; j < m; j++)
printf(" %d ",need[i][j]);
}
for(i=0; i<m; i++)
{
work[i]=avail[i];
}
for (i = 0; i < n; i++)
{
finish[i] = 0;
}

for (k = 0; k < n; k++)


{
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > work[j])// not available
{
flag = 1;
break;
}
}
if (flag == 0)
{
safesequence[ind++] = i;
for (y = 0; y < m; y++)
work[y] += alloc[i][y];
finish[i] = 1;
}
}
}
}
printf("\nFollowing is the SAFE Sequence\n");
for (i = 0; i <= n - 1; i++)
printf(" P%d ", safesequence[i]);
}
*********************FCFS********************
#include<stdio.h>
#include<stdlib.h>
struct PCB
{
int pid,at,bt,tat,wait;
};
void pline(int x);
int main()
{
int n,i,j;
float avg=0.0,sum=0.0;
struct PCB p[10],temp;
printf("Enter the total number of processes: ");//3
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arrival time and burst time for each rocess %d:
\n",i+1);
scanf("%d %d",&p[i].at,&p[i].bt);// 1 3

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;
}

}
}

// calculating turn arround time


for(int i=0;i<n;i++)
{
p[i].wait=sum;
sum=sum+p[i].bt;
p[i].tat=sum;
}
sum=0;
pline(44);
printf("PID\tArrival\tBurst\tTurnarround\n");
pline(44);
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].at,p[i].bt,p[i].tat);
sum+=p[i].tat;
}
pline(44);
avg=sum/(float)n;
printf("\ntotal turn arround time: %f ",sum);
printf("\n average turn around time: %.3f ",avg);

}
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");

printf("\nEnter the number of process:\n");


scanf("%d",&n);
for(int i=0;i<n;i++)
{
pid[i]=i;
printf("Enter the burst time and Priority of the Pid= %d\n",i);
scanf("%d\t\t\t%d",&bt[i],&pr[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(pr[j]>pr[j+1])
{
int temp=pr[j];
pr[j]=pr[j+1];
pr[j+1]=temp;

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>

//Function to swap two variables


void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);

// 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;

//Finding out highest priority element and placing it at its desired


position
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}

//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}

// T stores the starting time of process


int t=0;

//Printing scheduled process


printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d
%d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}
*********************ROUND ROBIN***************
#include<stdio.h>
#include<conio.h>
int main()
{
int ts,pid[10],need[10],wt[10],tat[10],n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
printf("------------------ Round Robin Scheduling-------------------------------
--\n");
printf("Enter the numbe of Processes\n");
scanf("%d",&n);
n1=n;
printf("\nEnter the Time Qunantum\n");
scanf("%d",&ts);
for(int i=1;i<=n;i++)
{
printf("Enter the process ID: %d\n",i);
scanf("%d",&pid[i]);
printf("Enter the burst time for the process\n");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(int i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(int i=1;i<=n;i++)
{

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");
}

You might also like