《操作系统》的实验代码。
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.

77 lines
1.8 KiB

10 years ago
10 years ago
  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 1000
  8. unsigned int acc[TOTAL];
  9. int status[TOTAL];
  10. int pids[TOTAL];
  11. static void
  12. spin_delay(void)
  13. {
  14. int i;
  15. volatile int j;
  16. for (i = 0; i != 200; ++ i)
  17. {
  18. j = !j;
  19. }
  20. }
  21. int
  22. main(void) {
  23. int i,time;
  24. memset(pids, 0, sizeof(pids));
  25. lab6_set_priority(TOTAL + 1);
  26. for (i = 0; i < TOTAL; i ++) {
  27. acc[i]=0;
  28. if ((pids[i] = fork()) == 0) {
  29. lab6_set_priority(i + 1);
  30. acc[i] = 0;
  31. while (1) {
  32. spin_delay();
  33. ++ acc[i];
  34. if(acc[i]%4000==0) {
  35. if((time=gettime_msec())>MAX_TIME) {
  36. cprintf("child pid %d, acc %d, time %d\n",getpid(),acc[i],time);
  37. exit(acc[i]);
  38. }
  39. }
  40. }
  41. }
  42. if (pids[i] < 0) {
  43. goto failed;
  44. }
  45. }
  46. cprintf("main: fork ok,now need to wait pids.\n");
  47. for (i = 0; i < TOTAL; i ++) {
  48. status[i]=0;
  49. waitpid(pids[i],&status[i]);
  50. cprintf("main: pid %d, acc %d, time %d\n",pids[i],status[i],gettime_msec());
  51. }
  52. cprintf("main: wait pids over\n");
  53. cprintf("stride sched correct result:");
  54. for (i = 0; i < TOTAL; i ++)
  55. {
  56. cprintf(" %d", (status[i] * 2 / status[0] + 1) / 2);
  57. }
  58. cprintf("\n");
  59. return 0;
  60. failed:
  61. for (i = 0; i < TOTAL; i ++) {
  62. if (pids[i] > 0) {
  63. kill(pids[i]);
  64. }
  65. }
  66. panic("FAIL: T.T\n");
  67. }