《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 rivejä
1.9 KiB

12 vuotta sitten
  1. #include <ulib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define TOTAL 5
  6. /* to get enough accuracy, MAX_TIME (the running time of each process) should >1000 mseconds. */
  7. #define MAX_TIME 2000
  8. #define SLEEP_TIME 400
  9. unsigned int acc[TOTAL];
  10. int status[TOTAL];
  11. int pids[TOTAL];
  12. static void
  13. spin_delay(void)
  14. {
  15. int i;
  16. volatile int j;
  17. for (i = 0; i != 200; ++ i)
  18. {
  19. j = !j;
  20. }
  21. }
  22. int
  23. main(void) {
  24. int i,time;
  25. cprintf("priority process will sleep %d ticks\n",SLEEP_TIME);
  26. sleep(SLEEP_TIME);
  27. memset(pids, 0, sizeof(pids));
  28. lab6_set_priority(TOTAL + 1);
  29. for (i = 0; i < TOTAL; i ++) {
  30. acc[i]=0;
  31. if ((pids[i] = fork()) == 0) {
  32. lab6_set_priority(i + 1);
  33. acc[i] = 0;
  34. while (1) {
  35. spin_delay();
  36. ++ acc[i];
  37. if(acc[i]%4000==0) {
  38. if((time=gettime_msec())>MAX_TIME) {
  39. cprintf("child pid %d, acc %d, time %d\n",getpid(),acc[i],time);
  40. exit(acc[i]);
  41. }
  42. }
  43. }
  44. }
  45. if (pids[i] < 0) {
  46. goto failed;
  47. }
  48. }
  49. cprintf("main: fork ok,now need to wait pids.\n");
  50. for (i = 0; i < TOTAL; i ++) {
  51. status[i]=0;
  52. waitpid(pids[i],&status[i]);
  53. cprintf("main: pid %d, acc %d, time %d\n",pids[i],status[i],gettime_msec());
  54. }
  55. cprintf("main: wait pids over\n");
  56. cprintf("stride sched correct result:");
  57. for (i = 0; i < TOTAL; i ++)
  58. {
  59. cprintf(" %d", (status[i] * 2 / status[0] + 1) / 2);
  60. }
  61. cprintf("\n");
  62. return 0;
  63. failed:
  64. for (i = 0; i < TOTAL; i ++) {
  65. if (pids[i] > 0) {
  66. kill(pids[i]);
  67. }
  68. }
  69. panic("FAIL: T.T\n");
  70. }