车辆调度c语言_C语言中的循环调度程序
车辆调度c语言
In this tutorial you will learn about round robin scheduling program in C.
在本教程中,您将学习C语言中的循环调度程序。
Process scheduling is an important component for process management. In a multi-user and a time-sharing system, response time is one of the most important objective to be accomplished.
流程计划是流程管理的重要组成部分。 在多用户和分时系统中,响应时间是要实现的最重要的目标之一。
There are many scheduling algorithms in C for process management such as:
C中有许多用于流程管理的调度算法,例如:
1. First Come First Serve 2. Shortest Job First 3. Priority Scheduling 4. Round Robin Scheduling
1. 先到先服务 2. 最短工作优先 3. 优先级调度 4.循环调度
However, this tutorial will get you clear with understanding of Round Robin Scheduling program in C.
但是,本教程将使您清楚地理解C语言中的Round Robin Scheduling程序。
循环调度算法 (Round Robin Scheduling Algorithm)
1. The queue structure in ready queue is of First In First Out (FIFO) type.
1.就绪队列中的队列结构为先进先出(FIFO)类型。
2. A fixed time is allotted to every process that arrives in the queue. This fixed time is known as time slice or time quantum.
2.固定时间分配给队列中到达的每个进程。 此固定时间称为时间片或时间量。
3. The first process that arrives is selected and sent to the processor for execution. If it is not able to complete its execution within the time quantum provided, then an interrupt is generated using an automated timer.
3.选择第一个到达的进程并将其发送到处理器以执行。 如果它无法 在提供的时间范围内完成其执行,则使用自动计时器生成中断。
4. The process is then stopped and is sent back at the end of the queue. However, the state is saved and context is thereby stored in memory. This helps the process to resume from the point where it was interrupted.
4.然后,该过程停止,并在队列末尾发送回去。 然而,状态被保存并且上下文由此被存储在存储器中。 这有助于该过程从被中断的点恢复。
5. The scheduler selects another process from the ready queue and dispatches it to the processor for its execution. It is executed until the time Quantum does not exceed.
5.调度程序从就绪队列中选择另一个进程,并将其分派给处理器以执行。 它会一直执行到Quantum时间不超过。
6. The same steps are repeated until all the process are finished.
6.重复相同的步骤,直到完成所有过程。
The round robin algorithm is simple and the overhead in decision making is very low. It is the best scheduling algorithm for achieving better and evenly distributed response time.
循环算法很简单,决策开销很低。 它是实现更好和均匀分布的响应时间的最佳调度算法。
Example
例
Lets take one example to understand it.
让我们举一个例子来理解它。
Time Quantum = 2
时间量子= 2
Process | Arrival Time | Burst Time |
P1 | 0 | 9 |
P2 | 1 | 5 |
P3 | 2 | 3 |
P4 | 3 | 4 |
处理 | 到达时间 | 爆发时间 |
P1 | 0 | 9 |
P2 | 1个 | 5 |
P3 | 2 | 3 |
P4 | 3 | 4 |
Process | Arrival Time | Burst Time (x) | Turnaround Time(t) | Normalized Turnaround Time(t/x) | Waiting Time |
P1 | 0 | 9 | 21 | 2.34 | 12 |
P2 | 1 | 5 | 17 | 3.4 | 12 |
P3 | 2 | 3 | 11 | 3.67 | 8 |
P4 | 3 | 4 | 12 | 3 | 8 |
处理 | 到达时间 | 爆发时间[x] | 周转时间(t) | 标准化周转时间(t / x) | 等待的时间 |
P1 | 0 | 9 | 21 | 2.34 | 12 |
P2 | 1个 | 5 | 17 | 3.4 | 12 |
P3 | 2 | 3 | 11 | 3.67 | 8 |
P4 | 3 | 4 | 12 | 3 | 8 |
Average Turnaround Time = 15.25 Average Normalized Turnaround Time = 3.10 Average Waiting Time = 10
平均周转时间= 15.25 平均归一化周转时间= 3.10 平均等待时间= 10
C语言中的循环调度程序 (Round Robin Scheduling Program in C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#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 ;
}
|
Output
输出量
If you found anything incorrect or have any doubts regarding above round robin scheduling program in C then comment below.
如果您发现任何错误或对C中的上述循环调度程序有任何疑问,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2015/09/round-robin-scheduling-program-in-c.html
车辆调度c语言