NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("nEnter Process Burst Timen");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("nProcessttBurst TimetWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("nAverage Turnaround Time:%d",avtat);
printf("nnAverage Waiting Time:%d",avwt);
return 0;
}
2. For different arrival time
#include<stdio.h>
#include<string.h>
int main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter the ProcessName, Arrival Time& Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];

bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}
for(i=0; i<n; i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("nPName Arrtime Burtime WaitTime Start TAT Finish");
for(i=0; i<n; i++)
{
printf("n%st%3dt%3dt%3dt%3dt%6dt%6d",pn[i],arr[i],bur[i],wt[i],s
tar[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("nAverage Waiting time:%f",(float)totwt/n);
printf("nAverage Turn Around Time:%f",(float)tottat/n);
return 0;
}
3. SJF Algorithm
#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround
Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
4. SJF Pre emptive Algorithm
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;

double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("nEnter the Total Number of Processes:t");
scanf("%d", &limit);
printf("nEnter Details of %d Processesn", limit);
for(i = 0; i < limit; i++)
{
printf("nEnter Arrival Time:t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] <

burst_time[smallest] && burst_time[i] > 0)

{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] -

temp[smallest];

turnaround_time = turnaround_time + end -

arrival_time[smallest];
}

}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("nnAverage Waiting Time:t%lfn",
average_waiting_time);
printf("Average Turnaround Time:t%lfn",
average_turnaround_time);
return 0;
}

4. Priority Algorithm
#include<stdio.h>
void main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float wtavg,
tatavg;
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++){
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k]){
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];

pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("nPROCESSttPRIORITYtBURST TIMEtWAITING
TIMEtTURNAROUND TIME");
for(i=0;i<n;i++)
printf("n%d tt %d tt %d tt %d tt %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("nAverage Waiting Time is --- %f",wtavg/n); printf("nAverage
Turnaround Time is --- %f",tatavg/n);
}
5. Round Robin Algorithm
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process
Number %d :",count+1);
scanf("%d",&at[count]);

scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:t");
scanf("%d",&time_quantum);
printf("nnProcesst|Turnaround Time|Waiting Timenn");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]t|t%dt|t%dn",count+1,time-at[count],time-at[count]-bt[
count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;

}
printf("nAverage Waiting Time= %fn",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
Producer Consumer Problem
#include<stdio.h>
int main()
{
int bufsize, in, out, produce = 0, consume = 0 , counter = 0;
in = 0; out = 0;
printf("Please enter the size of buffer: ");
scanf("%d", &bufsize);
int buffer[bufsize] ;
char choice ;
printf ("Enter p to produce an item in buffer:n") ;
printf ("Enter c to consume an item from buffer:n") ;
printf ("Enter q to Quit:n") ;
do{
scanf("%c", &choice);
switch(choice){
case 'p':
// code for producer
if(counter == bufsize)
printf("Buffer is Fulln");
else { printf("Enter the value: ");
scanf("%d", &produce);

buffer[in] = produce;
in = (in+1)%bufsize;
counter++ ;
}
break ;
// code for consumer
case 'c':
if(counter == 0)
printf("Buffer is Emptyn");
else {
consume = buffer[out];
printf("The consumed value is %dn", consume);
out = (out+1)%bufsize;
counter-- ;
}
break ;
} // end of switch
}while( choice != 'q') ;
}

Dining Philosopher problem
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };

sem_t mutex;
sem_t S[N];
void test(int phnum)
{
if (state[phnum] == HUNGRY
&& state[LEFT] != EATING
&& state[RIGHT] != EATING) {
// state that eating
state[phnum] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and %dn",
phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is Eatingn", phnum + 1);
// sem_post(&S[phnum]) has no effect
// during takefork
// used to wake up hungry philosophers
// during putfork
sem_post(&S[phnum]);
}
}
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
// state that hungry
state[phnum] = HUNGRY;
printf("Philosopher %d is Hungryn", phnum + 1);
// eat if neighbours are not eating
test(phnum);

sem_post(&mutex);
// if unable to eat wait to be signalled
sem_wait(&S[phnum]);
sleep(1);
}
// put down chopsticks
void put_fork(int phnum)
{
sem_wait(&mutex);
// state that thinking
state[phnum] = THINKING;
printf("Philosopher %d putting fork %d and %d downn",
phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is thinkingn", phnum + 1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
void* philospher(void* num)
{
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);

put_fork(*i);
}
}
int main()
{
int i;
pthread_t thread_id[N];
// initialize the semaphores
sem_init(&mutex, 0, 1);
for (i = 0; i < N; i++)
sem_init(&S[i], 0, 0);
for (i = 0; i < N; i++) {
// create philosopher processes
pthread_create(&thread_id[i], NULL, philospher, &phil[i]);
printf("Philosopher %d is thinkingn", i + 1);
}
for (i = 0; i < N; i++)
pthread_join(thread_id[i], NULL);

}

Sleeping Barber’s Problem
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

#include <pthread.h>
#include <semaphore.h>
#define MAX_CUSTOMERS 25
// Function prototypes...
void *customer(void *num);
void *barber(void *);
void randwait(int secs);
// Define the semaphores.
// waitingRoom Limits the # of customers allowed
// to enter the waiting room at one time.
sem_t waitingRoom;
// barberChair ensures mutually exclusive access to
// the barber chair.
sem_t barberChair;
// barberPillow is used to allow the barber to sleep
// until a customer arrives.
sem_t barberPillow;
// seatBelt is used to make the customer to wait until
// the barber is done cutting his/her hair.
sem_t seatBelt;
// Flag to stop the barber thread when all customers
// have been serviced.
int allDone = 0;
int main(int argc, char *argv[]) {
pthread_t btid;
pthread_t tid[MAX_CUSTOMERS];
long RandSeed;
int i, numCustomers, numChairs;
int Number[MAX_CUSTOMERS];

printf("Enter the number of Customers : ");
scanf("%d",&numCustomers) ;
printf("Enter the number of Chairs : ");
scanf("%d",&numChairs);
// Make sure the number of threads is less than the number of
// customers we can support.
if (numCustomers > MAX_CUSTOMERS) {
printf("The maximum number of Customers is %d.n",
MAX_CUSTOMERS);
exit(-1);
}
// Initialize the numbers array.
for (i=0; i<MAX_CUSTOMERS; i++) {
Number[i] = i;
}
// Initialize the semaphores with initial values...
sem_init(&waitingRoom, 0, numChairs);
sem_init(&barberChair, 0, 1);
sem_init(&barberPillow, 0, 0);
sem_init(&seatBelt, 0, 0);
// Create the barber.
pthread_create(&btid, NULL, barber, NULL);
// Create the customers.
for (i=0; i<numCustomers; i++) {
pthread_create(&tid[i], NULL, customer, (void *)&Number[i]);
sleep(1);
}
// Join each of the threads to wait for them to finish.
for (i=0; i<numCustomers; i++) {
pthread_join(tid[i],NULL);
sleep(1);
}

// When all of the customers are finished, kill the
// barber thread.
allDone = 1;
sem_post(&barberPillow); // Wake the barber so he will exit.
pthread_join(btid,NULL);
}
void *customer(void *number) {
int num = *(int *)number;
// Leave for the shop and take some random amount of
// time to arrive.
printf("Customer %d leaving for barber shop.n", num);
randwait(2);
printf("Customer %d arrived at barber shop.n", num);
// Wait for space to open up in the waiting room...
sem_wait(&waitingRoom);
printf("Customer %d entering waiting room.n", num);
// Wait for the barber chair to become free.
sem_wait(&barberChair);
// The chair is free so give up your spot in the
// waiting room.
sem_post(&waitingRoom);
// Wake up the barber...
printf("Customer %d waking the barber.n", num);
sem_post(&barberPillow);
// Wait for the barber to finish cutting your hair.
sem_wait(&seatBelt);
// Give up the chair.
sem_post(&barberChair);
printf("Customer %d leaving barber shop.n", num);
}
void *barber(void *junk) {

// While there are still customers to be serviced...
// Our barber is omnicient and can tell if there are
// customers still on the way to his shop.
while (!allDone) {
// Sleep until someone arrives and wakes you..
printf("The barber is sleepingn");
sem_wait(&barberPillow);
// Skip this stuff at the end...
if (!allDone) {
// Take a random amount of time to cut the
// customer’s hair.
printf("The barber is cutting hairn");
randwait(2);
printf("The barber has finished cutting hair.n");
// Release the customer when done cutting...
sem_post(&seatBelt);
}else {
printf("The barber is going home for the day.n");
}
}
}
void randwait(int secs) {
int len;
// Generate a random number...
len = (int) ((1 * secs) + 1);
sleep(len);
}

First Fit Strategy
#include<stdio.h>
#define max 25
void main()

{
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
printf ("ntMemory Management Scheme - First Fit");
printf ("nEnter the number of blocks:");
scanf ("%d", &nb);
printf ("Enter the number of files:");
scanf ("%d", &nf);
printf ("nEnter the size of the blocks:-n");
for (i = 1; i <= nb; i++)
{
printf ("Block %d:", i);
scanf ("%d", &b[i]);
}
printf ("Enter the size of the files :-n");
for (i = 1; i <= nf; i++)
{
printf ("File %d:", i);
scanf ("%d", &f[i]);

}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
{
ff[i] = j;
break;
}
}
}
frag[i] = temp;
bf[ff[i]] = 1;
}
printf ("nFile_no:tFile_size :tBlock_no:tBlock_size:tFragement");

for (i = 1; i <= nf; i++)
printf ("n%dtt%dtt%dtt%dtt%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}

Best fit Strategy
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("n ----- Best Fit Strategy ---- n");
printf("nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("nEnter the size of the blocks:-n");
for(i=1;i<=nb;i++){
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)

{
ff[i]=j;
lowest=temp;
}
}}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("nFile NotFile Size tBlock NotBlockSizetFragment"); for(i=1;i<=nf
&& ff[i]!=0;i++)
printf("n%dtt%dtt%dtt%dtt%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
Worst Fit Strategy
#include<stdio.h>
#define max 25
void main ()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0;
static int bf[max], ff[max];

printf ("ntMemory Management Scheme - Worst Fit");
printf ("nEnter the number of blocks:");
scanf ("%d", &nb);
printf ("Enter the number of files:");
scanf ("%d", &nf);
printf ("nEnter the size of the blocks:-n");
for (i = 1; i <= nb; i++)
{

printf ("Block %d:", i);
scanf ("%d", &b[i]);
}
printf ("Enter the size of the files :-n");
for (i = 1; i <= nf; i++)
{
printf ("File %d:", i);
scanf ("%d", &f[i]);
}
for (i = 1; i <= nf; i++)
{

for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)//if bf[j] is not allocated

{
temp = b[j] - f[i];
if (temp >= 0)
if (highest < temp)
{
ff[i] = j;

highest = temp;
}
}
}
frag[i] = highest;
bf[ff[i]] = 1;
highest = 0;
}
printf ("nFile_no:tFile_size :tBlock_no:tBlock_size:tFragement");
for (i = 1; i <= nf; i++)
printf ("n%dtt%dtt%dtt%dtt%d", i, f[i], ff[i], b[ff[i]], frag[i]);

}

Optimal Page Replacement
#include<stdio.h>
int fr[3], n, m;
void display ();
void main ()
{
int i, j, page[20], fs[10];

int max, found = 0, lg[3], index, k, l, flag1 = 0, flag2 = 0, pf = 0;
float pr;

printf ("Enter length of the reference string: ");
scanf ("%d", &n);
printf ("Enter the reference string: ");
for (i = 0; i < n; i++)
scanf ("%d", &page[i]);
printf ("Enter no of frames: ");
scanf ("%d", &m);
for (i = 0; i < m; i++)
fr[i] = -1;
pf = m;

for (j = 0; j < n; j++)
{
flag1 = 0;
flag2 = 0;
for (i = 0; i < m; i++)
{
if (fr[i] == page[j])

{
flag1 = 1;

flag2 = 1;

break;
}
}
if (flag1 == 0)
{
for (i = 0; i < m; i++)

{
if (fr[i] == -1)
{
fr[i] = page[j];

flag2 = 1;

break;
}
}
}
if (flag2 == 0)
{
for (i = 0; i < m; i++)
lg[i] = 0;
for (i = 0; i < m; i++)

{

for (k = j + 1; k <= n; k++)

{
if (fr[i] == page[k])
{
lg[i] = k -j;

break;

}
}
}
found = 0;
for (i = 0; i < m; i++)

{
if (lg[i] == 0)
{
index = i;
found = 1;
break;
}
}
if (found == 0)
{
max = lg[0];
index = 0;
for (i = 0; i < m; i++)
{
if (max < lg[i])
{
max = lg[i];

index = i;
}
}
}
fr[index] = page[j];
pf++;
}
display ();
}
printf ("Number of page faults : %dn", pf);
pr = (float) pf / n * 100;
printf ("Page fault rate = %f n", pr);
}
void display ()
{
int i;
for (i = 0; i < m; i++)
printf ("%dt", fr[i]);
printf ("n");
}

FIFO Page Replacement
#include<stdio.h>
int fr[3];
void main ()
{
void display ();
int i, j, page[12] = { 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 };
int flag1 = 0, flag2 = 0, pf = 0, frsize = 3, top = 0;
for (i = 0; i < 3; i++)
{
fr[i] = -1;
}
for (j = 0; j < 12; j++)
{
flag1 = 0;
flag2 = 0;
for (i = 0; i < 12; i++)
{
if (fr[i] == page[j])

{
flag1 = 1;

flag2 = 1;
break;
}
}
if (flag1 == 0)

{
for (i = 0; i < frsize; i++)

{
if (fr[i] == -1)
{
fr[i] = page[j];
flag2 = 1;
break;
}
}
}
if (flag2 == 0)
{
fr[top] = page[j];
top++;
pf++;
if (top >= frsize)
top = 0;
}
display ();
}

printf ("Number of page faults : %d ", pf + frsize);
}

void display ()

{
int i;
printf ("n");
for (i = 0; i < 3; i++)
printf ("%dt", fr[i]);
}
LRU Page Replacement
#include<stdio.h>
int fr[3];
void main ()
{
void display ();
int p[12] = { 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 }, i, j, fs[3];
int index, k, l, flag1 = 0, flag2 = 0, pf = 0, frsize = 3;

for (i = 0; i < 3; i++)
{
fr[i] = -1;
}
for (j = 0; j < 12; j++)
{
flag1 = 0, flag2 = 0;

for (i = 0; i < 3; i++)
{
if (fr[i] == p[j])
{
flag1 = 1;
flag2 = 1;
break;

}
}
if (flag1 == 0)
{
for (i = 0; i < 3; i++)

{
if (fr[i] == -1)
{
fr[i] = p[j];

flag2 = 1;

break;
}
}
}
if (flag2 == 0)
{
for (i = 0; i < 3; i++)

fs[i] = 0;
for (k = j -1, l = 1; l <= frsize -1; l++, k-- )

{

for (i = 0; i < 3; i++)
{
if (fr[i] == p[k])

fs[i] = 1;

}

}

for (i = 0; i < 3; i++)

{
if (fs[i] == 0)
index = i;
}
fr[index] = p[j];
pf++;
}
display ();
}
printf ("n no of page faults :%d", pf + frsize);
}

void display ()
{

int i;
printf ("n");
for (i = 0; i < 3; i++)
printf ("t%d", fr[i]);
}

FCFS Disk Scheduling
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the no. of track movementn");
scanf("%d",&n);
printf("Enter the track sequencen");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head positionn");
scanf("%d",&initial);
// logic for FCFS disk scheduling
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is %d",TotalHeadMoment);
return 0;
}

SCAN Disk Scheduling Algorithm
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Tracksn");
scanf("%d",&n);
printf("Enter the position of Tracksn");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head positionn");
scanf("%d",&initial);
printf("Enter total disk sizen");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0n");
scanf("%d",&move);
// logic for Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{

if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

C-Scan Disk Scheduling
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of track movementsn");
scanf("%d",&n);
printf("Enter the track sequencen");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head positionn");
scanf("%d",&initial);
printf("Enter total disk sizen");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0n");
scanf("%d",&move);
// logic for C-Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];

RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
/*movement max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{

for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

LOOK Disk Scheduling Algorithm
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of TRACK movementn");
scanf("%d",&n);
printf("Enter the track sequencen");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head positionn");
scanf("%d",&initial);
printf("Enter total disk sizen");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0n");

scanf("%d",&move);
// logic for look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index-1;i>=0;i--)

{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

C-LOOK Disk Scheduling Algorithm
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of track movementsn");
scanf("%d",&n);
printf("Enter the track sequencen");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);

printf("Enter initial head positionn");
scanf("%d",&initial);
printf("Enter total disk sizen");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0n");
scanf("%d",&move);
// logic for C-look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.