Advertisement
BorrowTheProgrammer

lab4_os

Dec 12th, 2021
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. /*
  2.  Модифицировать исходную программу так, чтобы метки процессов перемещались сверху-вниз.
  3.  */
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/timeb.h>
  7. #include <stdlib.h>
  8. #include <sys/wait.h>
  9. # include <time.h>
  10. /* clear standart text mode window */
  11.  
  12. void clrscr() {
  13. unsigned char esc[11];
  14.  
  15. /* obtain set cursor to left-top window corner */
  16. esc[0] = 27; esc[1] = '[';
  17. esc[2] = 'H';
  18. write(1,esc,3);
  19. /* obtain clear escape sequence */
  20. esc[2] = '2'; esc[3] = 'J';
  21. write(1,esc,4);
  22. return;
  23. }
  24. /* position cursor in standart text window */
  25. void gotoxy(int tx, int ty, char c) {
  26. unsigned char esc[16];
  27. static unsigned char ystr[3]; /* vertical cursor location */
  28. static unsigned char xstr[3]; /* horizontal cursor location */
  29. int i;                        /* ESC-sequence current index */
  30. int j;                        /* cursor location current index */
  31. /* convert cursor location to text format */
  32. if((tx > 99) || (ty > 99))
  33.   tx = ty = 99;
  34. if((tx < 1) || (ty < 1))
  35.   tx = ty = 1;
  36. xstr[0] = xstr[1] = xstr[2] = '\0';
  37. ystr[0] = ystr[1] = ystr[2] = '\0';
  38. sprintf((char *) xstr,"%d",tx);
  39. sprintf((char *) ystr,"%d",ty - 1);
  40.  
  41. //drop old letter
  42. esc[0] = 27; esc[1] = '[';
  43. i=2; j=0;
  44. while(ystr[j])
  45.     esc[i++]=ystr[j++];
  46. j=0;
  47. esc[i++]=';';
  48. while(xstr[j])
  49.     esc[i++]=xstr[j++];
  50. esc[i++]='H';
  51. esc[i++]='\b';
  52. esc[i++]=' ';
  53. esc[i++]=' ';
  54. esc[i] = '\0';
  55. write(1,esc,i);
  56.  
  57. //write new letter
  58.   sprintf((char *) xstr,"%d",tx);
  59.   sprintf((char *) ystr,"%d",ty);
  60.   esc[0] = 27; esc[1] = '[';
  61.   i=2; j=0;
  62.   while(ystr[j])
  63.     esc[i++]=ystr[j++];
  64.   j=0;
  65.   esc[i++]=';';
  66.   while(xstr[j])
  67.     esc[i++]=xstr[j++];
  68.   esc[i++]='H';
  69.   esc[i++] = c;
  70.   esc[i] = '\0';
  71.   write(1,esc,i);
  72. return;
  73. }
  74. int main(int argc, char* argv[]) {
  75. printf("1\n");
  76. int x = 1;       //номер строки и он же номер процесса
  77. int status;
  78. int i;
  79. int j = 0;
  80. int PROCNUM = atoi(argv[1]);
  81. int len = atoi(argv[2]);
  82. int* pid = sbrk((PROCNUM+1)*sizeof(int));
  83. char* lead = sbrk((PROCNUM+1)*sizeof(char));
  84. int dist = atoi(argv[2]);
  85. int p;
  86. char bell = '\007';
  87. struct timespec tp[1];
  88. int jump;
  89. printf("1\n");
  90. clrscr();
  91. printf("1\n");
  92. while(j < PROCNUM) {
  93.   if((pid[j] = fork()) == 0) {
  94.     usleep(PROCNUM - j);
  95.     while(x < dist) {
  96.       gotoxy(j + 1, x, 'A' + j);  //поменял сначала передаю позицию по строке потом по столбцу
  97.       clock_gettime(CLOCK_REALTIME, tp);
  98.       //ftime(tp);
  99.       if((((unsigned short)tp[0].tv_nsec/10) % (j + 'A')) != j)   //рандомим, чтобы сделать разную скорость для каждого процесса
  100.           continue;
  101.       x++;
  102.      for(i=0; i<1000000; i++);
  103.     }
  104.     exit('A' + j);
  105.   }
  106.   j++;
  107. }
  108. printf("1\n");
  109. j = 0;
  110. while((p = wait(&status)) != (-1)) {
  111.   for(i = 0; i < PROCNUM; i++)
  112.     if(pid[i] == p)
  113.       lead[j++] = (char) ((status >> 8) & '\377');
  114.   write(1,&bell,1);
  115. }
  116. lead[j] = '\n';
  117. sleep(1);
  118. gotoxy(1, len + 3, '\n');
  119. write(1, lead, len + 1);   //letters in order of completion
  120. exit(0);
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement