Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 17

FCFS

#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 Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&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];
}

printf("\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];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}

#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(maximum 20):";
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;
}

//////////////////////////////////////////////////
SJFS

#include<stdio.h>

void 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; //contains process number
}

//sorting burst time in ascending order using selection sort


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; //waiting time for first process will be zero

//calculate waiting time


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; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

/////////////////////////////////////////////////

RR

#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("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
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%d\t|\t%d\n",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= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;
}

//////////////////////////////////////////

SRTF

#include < stdio.h >

int main()
{

int n, ari[10], bur[10], total = 0, i, j, small, temp, procs[100], k,


waiting[10], finish[10];
float tavg = 0.0, wavg = 0.0;

printf("ENTER THE NUMBER OF PROCESSES:");

scanf("%d", & n);

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

printf("ENTER THE ARRIVAL TIME OF PROCESS %d:\t", i);

scanf("%d", & ari[i]);

printf("ENTER THE BURST TIME OF PROCESS %d:\t", i);

scanf("%d", & bur[i]);

waiting[i] = 0;

total += bur[i];

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

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

if (ari[i] > ari[j])

temp = ari[i];

ari[i] = ari[j];

ari[j] = temp;

temp = bur[i];

bur[i] = bur[j];

bur[j] = temp;

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

{
small = 3200;

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

if ((bur[j] != 0) && (ari[j] <= i) && (bur[j] < small))

small = bur[j];
k = j;

bur[k]--;

procs[i] = k;

k = 0;

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

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

if (procs[i] == j)

finish[j] = i;

waiting[j]++;

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

printf("\n PROCESS %d:-FINISH TIME==> %d TURNAROUND TIME==>%d WAITING TIME==>


%d\n", i + 1, finish[i] + 1, (finish[i] - ari[i]) + 1, (((finish[i] + 1) -
waiting[i]) - ari[i]));

wavg = wavg + (((finish[i] + 1) - waiting[i]) - ari[i]);

tavg = tavg + ((finish[i] - ari[i]) + 1);


}

printf("\n WAvG==>\t%f\n TAVG==>\t%f\n", (wavg / n), (tavg / n));

return 0;

/////////////////////////////////////

PRIORITY

#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending order using
selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero


//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

////////////////////////////////////

full project

#include<stdio.h>
#include<stdlib.h>

typedef struct process{


char name[5];
int bt;
int at;
int prt;
int wt,ta;
int flag;
}processes;

void b_sort(processes temp[],int n)


{
processes t;
int i,j;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++){
if(temp[j].at > temp[j+1].at){
t = temp[j];
temp[j] = temp[j+1];
temp[j+1] = t;
}
}
}

int accept(processes P[]){


int i,n;
printf("\n Enter total no. of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n PROCESS [%d]",i+1);
printf(" Enter process name : ");
scanf("%s",&P[i].name);
printf(" Enter burst time : ");
scanf("%d",&P[i].bt);
printf(" Enter arrival time : ");
scanf("%d",&P[i].at);
printf(" Enter priority : ");
scanf("%d",&P[i].prt);
}
printf("\n PROC.\tB.T.\tA.T.\tPRIORITY");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d\t%d",P[i].name,P[i].bt,P[i].at,P[i].prt);
return n;
}

// FCFS Algorithm
void FCFS(processes P[],int n){
processes temp[10];
int sumw=0,sumt=0;
int x = 0;
float avgwt=0.0,avgta=0.0;
int i,j;
for(i=0;i<n;i++)
temp[i]=P[i];

b_sort(temp,n);

printf("\n\n PROC.\tB.T.\tA.T.");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d",temp[i].name,temp[i].bt,temp[i].at);

sumw = temp[0].wt = 0;
sumt = temp[0].ta = temp[0].bt - temp[0].at;

for(i=1;i<n;i++){
temp[i].wt = (temp[i-1].bt + temp[i-1].at + temp[i-1].wt) -
temp[i].at;;
temp[i].ta = (temp[i].wt + temp[i].bt);
sumw+=temp[i].wt;
sumt+=temp[i].ta;
}
avgwt = (float)sumw/n;
avgta = (float)sumt/n;
printf("\n\n PROC.\tB.T.\tA.T.\tW.T\tT.A.T");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d\t%d\t
%d",temp[i].name,temp[i].bt,temp[i].at,temp[i].wt,temp[i].ta);

printf("\n\n GANTT CHART\n ");


for(i=0;i<n;i++)
printf(" %s ",temp[i].name);
printf("\n ");

printf("0\t");
for(i=1;i<=n;i++){
x+=temp[i-1].bt;
printf("%d ",x);
}
printf("\n\n Average waiting time = %0.2f\n Average turn-around =
%0.2f.",avgwt,avgta);
}

//SJF Non Pre-emptive


void SJF_NP(processes P[],int n){
processes temp[10];
processes t;
int sumw=0,sumt=0;
int x = 0;
float avgwt=0.0,avgta=0.0;
int i,j;

for(i=0;i<n;i++)
temp[i]=P[i];

b_sort(temp,n);

for(i=2;i<n;i++)
for(j=1;j<n-i+1;j++){
if(temp[j].bt > temp[j+1].bt){
t = temp[j];
temp[j] = temp[j+1];
temp[j+1] = t;
}
}

printf("\n\n PROC.\tB.T.\tA.T.");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d",temp[i].name,temp[i].bt,temp[i].at);

sumw = temp[0].wt = 0;
sumt = temp[0].ta = temp[0].bt - temp[0].at;

for(i=1;i<n;i++){
temp[i].wt = (temp[i-1].bt + temp[i-1].at + temp[i-1].wt) -
temp[i].at;;
temp[i].ta = (temp[i].wt + temp[i].bt);
sumw+=temp[i].wt;
sumt+=temp[i].ta;
}
avgwt = (float)sumw/n;
avgta = (float)sumt/n;
printf("\n\n PROC.\tB.T.\tA.T.\tW.T\tT.A.T");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d\t%d\t
%d",temp[i].name,temp[i].bt,temp[i].at,temp[i].wt,temp[i].ta);

printf("\n\n GANTT CHART\n ");


for(i=0;i<n;i++)
printf(" %s ",temp[i].name);
printf("\n ");

printf("0\t");
for(i=1;i<=n;i++){
x+=temp[i-1].bt;
printf("%d ",x);
}
printf("\n\n Average waiting time = %0.2f\n Average turn-around =
%0.2f.",avgwt,avgta);
}

//Priority Non Pre-emptive


void PRT_NP(processes P[],int n)
{
processes temp[10];
processes t;
int sumw=0,sumt=0;
float avgwt=0.0,avgta=0.0;
int i,j;
int x = 0;

for(i=0;i<n;i++)
temp[i]=P[i];

b_sort(temp,n);

for(i=2;i<n;i++)
for(j=1;j<n-i+1;j++){
if(temp[j].prt > temp[j+1].prt){
t = temp[j];
temp[j] = temp[j+1];
temp[j+1] = t;
}
}

printf("\n\n PROC.\tB.T.\tA.T.");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d",temp[i].name,temp[i].bt,temp[i].at);

sumw = temp[0].wt = 0;
sumt = temp[0].ta = temp[0].bt - temp[0].at;

for(i=1;i<n;i++){
temp[i].wt = (temp[i-1].bt + temp[i-1].at + temp[i-1].wt) -
temp[i].at;;
temp[i].ta = (temp[i].wt + temp[i].bt);
sumw+=temp[i].wt;
sumt+=temp[i].ta;
}
avgwt = (float)sumw/n;
avgta = (float)sumt/n;
printf("\n\n PROC.\tB.T.\tA.T.\tW.T\tT.A.T");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d\t%d\t
%d",temp[i].name,temp[i].bt,temp[i].at,temp[i].wt,temp[i].ta);

printf("\n\n GANTT CHART\n ");


for(i=0;i<n;i++)
printf(" %s ",temp[i].name);
printf("\n ");

printf("0\t");
for(i=1;i<=n;i++){
x+=temp[i-1].bt;
printf("%d ",x);
}
printf("\n\n Average waiting time = %0.2f\n Average turn-around =
%0.2f.",avgwt,avgta);
}

//Round Robin Scheduling


void RR(processes P[],int n)
{
int pflag=0,t,tcurr=0,k,i,Q=0;
int sumw=0,sumt=0;
float avgwt=0.0,avgta=0.0;
processes temp1[10],temp2[10];

for(i=0;i<n;i++)
temp1[i]=P[i];

b_sort(temp1,n);

for(i=0;i<n;i++)
temp2[i]=temp1[i];

printf("\n Enter quantum time : ");


scanf("%d",&Q);

for(k=0;;k++){
if(k>n-1)
k=0;
if(temp1[k].bt>0)
printf(" %d %s",tcurr,temp1[k].name);
t=0;
while(t<Q && temp1[k].bt > 0){
t++;
tcurr++;
temp1[k].bt--;
}
if(temp1[k].bt <= 0 && temp1[k].flag != 1){
temp1[k].wt = tcurr - temp2[k].bt - temp1[k].at;
temp1[k].ta = tcurr - temp1[k].at;
pflag++;
temp1[k].flag = 1;
sumw+=temp1[k].wt;
sumt+=temp1[k].ta;
}
if(pflag == n)
break;
}
printf(" %d",tcurr);
avgwt = (float)sumw/n;
avgta = (float)sumt/n;
printf("\n\n Average waiting time = %0.2f\n Average turn-around =
%0.2f.",avgwt,avgta);
}
//Shortest Job First - Pre-emptive
void SJF_P(processes P[],int n){
int i,t_total=0,tcurr,b[10],min_at,j,x,min_bt;
int sumw=0,sumt=0;
float avgwt=0.0,avgta=0.0;
processes temp[10],t;

for(i=0;i<n;i++){
temp[i]=P[i];
t_total+=P[i].bt;
}

b_sort(temp,n);

for(i=0;i<n;i++)
b[i] = temp[i].bt;

i=j=0;
printf("\n GANTT CHART\n\n %d %s",i,temp[i].name);
for(tcurr=0;tcurr<t_total;tcurr++){

if(b[i] > 0 && temp[i].at <= tcurr)


b[i]--;

if(i!=j)
printf(" %d %s",tcurr,temp[i].name);

if(b[i]<=0 && temp[i].flag != 1){

temp[i].flag = 1;
temp[i].wt = (tcurr+1) - temp[i].bt - temp[i].at;
temp[i].ta = (tcurr+1) - temp[i].at;
sumw+=temp[i].wt;
sumt+=temp[i].ta;
}
j=i; min_bt = 999;
for(x=0;x<n;x++){

if(temp[x].at <= (tcurr+1) && temp[x].flag != 1){

if(min_bt != b[x] && min_bt > b[x]){


min_bt = b[x];
i=x;
}
}
}

}
printf(" %d",tcurr);
avgwt = (float)sumw/n; avgta = (float)sumt/n;
printf("\n\n Average waiting time = %0.2f\n Average turn-around =
%0.2f.",avgwt,avgta);
}

void PRT_P(processes P[],int n){


int i,t_total=0,tcurr,b[10],j,x,min_pr;
int sumw=0,sumt=0;
float avgwt=0.0,avgta=0.0;
processes temp[10],t;

for(i=0;i<n;i++){
temp[i]=P[i];
t_total+=P[i].bt;
}

b_sort(temp,n);

for(i=0;i<n;i++)
b[i] = temp[i].bt;

i=j=0;
printf("\n GANTT CHART\n\n %d %s",i,temp[i].name);
for(tcurr=0;tcurr<t_total;tcurr++)
{

if(b[i] > 0 && temp[i].at <= tcurr)


b[i]--;

if(i!=j)
printf(" %d %s",tcurr,temp[i].name);

if(b[i]<=0 && temp[i].flag != 1)


{
temp[i].flag = 1;
temp[i].wt = (tcurr+1) - temp[i].bt - temp[i].at;
temp[i].ta = (tcurr+1) - temp[i].at;
sumw+=temp[i].wt;
sumt+=temp[i].ta;
}
j=i;
min_pr = 999;
for(x=0;x<n;x++){

if(temp[x].at <= (tcurr+1) && temp[x].flag != 1){

if(min_pr != temp[x].prt && min_pr > temp[x].prt){


min_pr = temp[x].prt;
i=x;
}
}
}

}
printf(" %d",tcurr);
avgwt = (float)sumw/n;
avgta = (float)sumt/n;
printf("\n\n Average waiting time = %0.2f\n Average turn-around =
%0.2f.",avgwt,avgta);
}

int main(){

processes P[10];
int ch,n;
do{
printf("\n\n SIMULATION OF CPU SCHEDULING ALGORITHMS\n");
printf("\n Options:");
printf("\n 0. Enter process data.");
printf("\n 1. FCFS");
printf("\n 2. SJF (Pre-emptive)");
printf("\n 3. SJF (Non Pre-emptive)");
printf("\n 4. Priority Scheduling (Pre-emptive)");
printf("\n 5. Priority Scheduling (Non Pre-emptive)");
printf("\n 6. Round Robin");
printf("\n 7. Exit\n Select : ");
scanf("%d",&ch);
switch(ch){
case 0:
n=accept(P);
break;
case 1:
FCFS(P,n);
break;
case 2:
SJF_P(P,n);
break;
case 3:
SJF_NP(P,n);
break;
case 4:
PRT_P(P,n);
break;
case 5:
PRT_NP(P,n);
break;
case 6:
RR(P,n);
break;
case 7:exit(0);
}
}while(ch != 7);
getch();
return 0;
}

////////////////////////

A drive cannot make a motor produce more than its maximum torque. Max torque or
Stall Torque is generally not shown on the motor's nameplate. The iron in the motor
can only sustain a certain amount of magnetic flux density, even if iron is driven
harder, the flux density will not go up.

Benefits of AC drive
As AC drive usage in HVAC applications has increased, fans, pumps, air handlers,
and chillers can benefit from speed control. AC drives provide the following basic
advantages:
energy savings
low motor starting current
reduction of thermal and mechanical stresses on motors and belts during starts
simple installation
high power factor
lower KVA

The advantages by using AC drives:


AC drives provide the most energy efficient means of capacity control.
AC drives have the lowest starting current of any starter type.
AC drives reduce thermal and mechanical stresses on motors and belts.
AC drive installation is as simple as connecting the power supply to the AC drive.
AC drives with AFE technology can meet even the most stringent harmonic standards
and reduce backup generator sizing.
AC drives provide high power factor, eliminating the need for external power factor
correction capacitors.
AC drives provide lower KVA, helping alleviate voltage sags and power outages.

5 Critical Variable Frequency Drive Limitations


There are 5 critical variable frequency drive (VFD) limitations that should be
considered when selecting the proper VFD for motor speed control applications.

A VFD is an electronic circuit that controls the speed of an electric motor by


adjusting both the voltage and the frequency applied to the motor. Prior to VFDs,
motor speed was controlled through inefficient voltage regulators (think light
dimmer switch!) or mechanical means – pulleys, gears, or transmission systems. The
original VFDs filled large cabinets and were unreliable. Today, VFDs are smaller
than a shoe box, reliable, and are used in almost all industrial applications,
pumps, fans, conveyors, machining, compressors, etc. VFDs can also be employed on
linear motors to generate vibration, which is often critical in “shaker machines”
used in product separation and packaging. However, VFD’s are not miracle workers
and there are important limitations. If you are already using VFDs or are
considering their use for the first time, these limitations should be considers.
Learn more facts about VFDs in my paper 5 Basic VFD Facts.

single-phase-OEM-drive
SINGLE PHASE OEM DRIVE DESIGNED BY INVENTION HOUSE
A VFD can make a motor run slower than rated speed, however a major point of
consideration at low speed is cooling. A typical electric motor is cooled by a fan
on its shaft; at low speed the fan moves less air and at some point the motor will
over heat. This is generally not a problem with centrifugal loads such as fans,
pumps, and blowers because the torque required by the load drops drastically with
decreasing speed. In this case, the motor is doing less work, and there is less
waste heat to dispose of.

A VFD can drive a motor faster than its nameplate speed, however, above the rated
speed, the motor looses torque (twisting force). At higher speeds, less and less
torque is possible. The maximum continuous power (speed times torque) is limited by
the motor design, therefore a VFD cannot deliver more power than which the motor is
thermally capable.

A VFD can also make a motor more efficient, but only at reduced load and/or reduced
speed. A drive system will not use less power when the motor is running at rated
speed and rated load. In fact, it uses slightly more due to the losses within the
drive.
A VFD can make a motor reverse without the use of contactors, but it can’t make the
reversal faster than the combination of the motor’s max torque and the load’s
inertia allows.

A VFD can make a motor produce more than its rated torque but only briefly. The
amount of time is limited by either the drive’s overload capacity or the motor’s
thermal capacity. A drive cannot make a motor produce more than its maximum torque.
Max torque or Stall Torque is generally not shown on the motor’s nameplate. The
iron in the motor can only sustain a certain amount of magnetic flux density, even
if iron is driven harder, the flux density will not go up. The amount of torque a
motor creates at the flux limit is the most torque that can be achieved.

What is the disadvantage of DC drive?


The disadvantage of D.C. drives is the presence of a mechanical commutator which
limits the maximum power rating and the speed.

You might also like