操作系统进程调度算法c语言代码
时间: 2024-06-20 13:04:34 浏览: 144
操作系统中进程调度算法有多种,其中比较常见的有先来先服务(FCFS)、短作业优先(SJF)、时间片轮转(RR)、优先级调度等。下面是这些算法的简单介绍和C语言代码示例。
1. 先来先服务(FCFS)
先来先服务是一种非抢占式的调度算法,按照进程到达的先后顺序分配CPU时间片。如果当前正在执行的进程没有完成,那么后续到达的进程就必须等待。这种算法的优点是简单易实现,但如果有长时间的进程到来,则会导致后续进程长时间等待。
C语言代码示例:
```c
#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; //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 and average waiting 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;
}
```
2. 短作业优先(SJF)
短作业优先是一种非抢占式的调度算法,按照进程的执行时间长度来进行调度。当有多个进程的执行时间相同时,按照到达时间的先后顺序进行调度。这种算法的优点是可以减少平均等待时间,但如果有长时间的进程到来,则会导致后续进程长时间等待。
C语言代码示例:
```c
#include<stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], avwt=0, avtat=0, i, j, pos, temp;
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]);
}
//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;
}
wt=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 and average waiting 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;
}
```
3. 时间片轮转(RR)
时间片轮转是一种抢占式的调度算法,每个进程被分配一个时间片,当时间片用完时,该进程就会被挂起,并且把CPU分配给下一个进程。如果进程在时间片用完之前完成了任务,则它会被立即切换下一个进程。这种算法适用于处理多个短进程。
C语言代码示例:
```c
#include<stdio.h>
int main() {
int i, n, qt, count=0, time, remain, flag=0, wt=0, tat=0, at[20], bt[20], rt[20];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++) {
printf("Enter Arrival Time and Burst Time for Process Number %d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("Enter Time Quantum:\t");
scanf("%d",&qt);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,i=0;remain!=0;) {
if(rt[i]<=qt && rt[i]>0) {
time+=rt[i];
rt[i]=0;
flag=1;
} else if(rt[i]>0) {
rt[i]-=qt;
time+=qt;
}
if(rt[i]==0 && flag==1) {
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
wt+=time-at[i]-bt[i];
tat+=time-at[i];
flag=0;
}
if(i==n-1)
i=0;
else if(at[i+1]<=time)
i++;
else
i=0;
}
printf("\nAverage Waiting Time=%f\n",wt*1.0/n);
printf("Average Turnaround Time=%f\n",tat*1.0/n);
return 0;
}
```
4. 优先级调度
优先级调度是一种非抢占式的调度算法,每个进程被分配一个优先级,按照优先级高低进行调度。如果有多个进程优先级相同,则按照到达时间的先后顺序进行调度。这种算法的缺点是可能导致低优先级的进程长时间等待。
C语言代码示例:
```c
#include<stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], p[20], i, j, pos, temp;
float avwt=0, avtat=0;
printf("Enter total number of processes(maximum 20):");
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", &p[i]);
}
//Sorting burst time and priority in ascending order using selection sort
for(i=0;i<n;i++) {
pos=i;
for(j=i+1;j<n;j++) {
if(p[j]<p[pos])
pos=j;
}
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
}
wt=0; //waiting time for first process is zero
//calculating waiting time
for(i=1;i<n;i++) {
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
avwt+=wt[i];
}
avwt/=i; //average waiting time
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
//calculating turnaround time and average turnaround time
for(i=0;i<n;i++) {
tat[i]=bt[i]+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]);
}
avtat/=i; //average turnaround time
printf("\n\nAverage Waiting Time=%f", avwt);
printf("\nAverage Turnaround Time=%f", avtat);
return 0;
}
```
阅读全文