os_kernel_lab/labcodes_answer/lab6_result/user/priority.c
Junjie Mao d9a83bd7be Adjust the time slice to 50ms
The original time slice (200ms) is too large for the priority test to
generate a satisfactory result in 20s. If we only schedule 5 times a
second, there are only 100 pick_next calls to the scheduler.

I believe making scheduling more frequently does little harm to the
system. Actually more scheduling opportunities may also reveal bugs
which are not triggered previously. Adopting smaller time slices also
allows us to reduce the time spent on the priority test, which can
benefit the autotesting system.

Signed-off-by: Junjie Mao <junjie.mao@hotmail.com>
2015-03-24 10:52:27 +08:00

78 lines
1.8 KiB
C

#include <ulib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TOTAL 5
/* to get enough accuracy, MAX_TIME (the running time of each process) should >1000 mseconds. */
#define MAX_TIME 1000
unsigned int acc[TOTAL];
int status[TOTAL];
int pids[TOTAL];
static void
spin_delay(void)
{
int i;
volatile int j;
for (i = 0; i != 200; ++ i)
{
j = !j;
}
}
int
main(void) {
int i,time;
memset(pids, 0, sizeof(pids));
lab6_set_priority(TOTAL + 1);
for (i = 0; i < TOTAL; i ++) {
acc[i]=0;
if ((pids[i] = fork()) == 0) {
lab6_set_priority(i + 1);
acc[i] = 0;
while (1) {
spin_delay();
++ acc[i];
if(acc[i]%4000==0) {
if((time=gettime_msec())>MAX_TIME) {
cprintf("child pid %d, acc %d, time %d\n",getpid(),acc[i],time);
exit(acc[i]);
}
}
}
}
if (pids[i] < 0) {
goto failed;
}
}
cprintf("main: fork ok,now need to wait pids.\n");
for (i = 0; i < TOTAL; i ++) {
status[i]=0;
waitpid(pids[i],&status[i]);
cprintf("main: pid %d, acc %d, time %d\n",pids[i],status[i],gettime_msec());
}
cprintf("main: wait pids over\n");
cprintf("stride sched correct result:");
for (i = 0; i < TOTAL; i ++)
{
cprintf(" %d", (status[i] * 2 / status[0] + 1) / 2);
}
cprintf("\n");
return 0;
failed:
for (i = 0; i < TOTAL; i ++) {
if (pids[i] > 0) {
kill(pids[i]);
}
}
panic("FAIL: T.T\n");
}