SHARE
TWEET

Untitled

a guest Oct 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <sys/timeb.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6. #define  DIST 20
  7.  
  8. /* clear standart text mode window */
  9.  
  10. void clrscr() {
  11. unsigned char esc[11];
  12.  
  13. /* obtain set cursor to left-top window corner */
  14.  
  15. esc[0] = 27; esc[1] = '[';
  16. esc[2] = 'H';
  17. write(1,esc,3);
  18.  
  19. /* obtain clear escape sequence */
  20.  
  21. esc[2] = '2'; esc[3] = 'J';
  22. write(1,esc,4);
  23.  
  24. return;
  25.  
  26. }
  27.  
  28. /* position cursor in standart text window */
  29.  
  30. void gotoxy(int tx, int ty, char c) {
  31. unsigned char esc[16];
  32. static unsigned char ystr[3]; /* vertical cursor location */
  33. static unsigned char xstr[3]; /* horizontal cursor location */
  34. int i;                        /* ESC-sequence current index */
  35. int j;                        /* cursor location current index */
  36.  
  37. /* convert cursor location to text format */
  38.  
  39. if((tx > 99) || (ty > 99))
  40.   tx = ty = 99;
  41. if((tx < 1) || (ty < 1))
  42.   tx = ty = 1;
  43. xstr[0] = xstr[1] = xstr[2] = '\0';
  44. ystr[0] = ystr[1] = ystr[2] = '\0';
  45.  
  46. sprintf((char *) xstr,"%d",tx);
  47. sprintf((char *) ystr,"%d",ty - 1);
  48.  
  49. //drop old letter
  50. // esc[0] = 27; esc[1] = '[';
  51. // i=2; j=0;
  52. // while(ystr[j])
  53. //     esc[i++]=ystr[j++];
  54. // j=0;
  55. // esc[i++]=';';
  56. // while(xstr[j])
  57. //     esc[i++]=xstr[j++];
  58. // esc[i++]='H';
  59. // esc[i++]='\b';
  60. // esc[i++]=' ';
  61. // esc[i++]=' ';
  62. // esc[i] = '\0';
  63. // write(1,esc,i);
  64.  
  65. //write new letter
  66.   sprintf((char *) xstr,"%d",tx);
  67.   sprintf((char *) ystr,"%d",ty);
  68.  
  69.   esc[0] = 27; esc[1] = '[';
  70.   i=2; j=0;
  71.   while(ystr[j])
  72.     esc[i++]=ystr[j++];
  73.   j=0;
  74.   esc[i++]=';';
  75.   while(xstr[j])
  76.     esc[i++]=xstr[j++];
  77.   esc[i++]='H';
  78.   esc[i++] = c;
  79.   esc[i] = '\0';
  80.   write(1,esc,i);
  81.  
  82. return;
  83. }
  84.  
  85. int main(int argc, char* argv[]) {
  86. int x = 1;       //номер строки Рё РѕРЅ же номер процесса
  87. int status;
  88. int i;
  89. int j = 0;
  90. int PROCNUM = atoi(argv[1]);
  91. int len = DIST;
  92. int* pid = sbrk((PROCNUM+1)*sizeof(int));
  93. char* lead = sbrk((PROCNUM+1)*sizeof(char));
  94. int dist = DIST;
  95. char symb = argv[2][0];
  96. int p;
  97. char bell = '\007';
  98. struct timeb tp[1];
  99. int jump;
  100.  
  101. clrscr();
  102. while(j < PROCNUM) {
  103.   while(x < dist) {
  104.     gotoxy(j + 1, x, symb);
  105.     x++;
  106.   }
  107.   x=1;
  108.   j++;
  109. }
  110.  
  111. x = 1;
  112. j = 0;
  113.  
  114. while(j < PROCNUM) {
  115.   if((pid[j] = fork()) == 0) {
  116.     usleep(PROCNUM - j);
  117.     while(x < dist) {
  118.       gotoxy(j + 1, x, ' ');  //поменял сначала передаю позицию РїРѕ строке потом РїРѕ столбцу
  119.       ftime(tp);
  120.       if((tp[0].millitm % (j + 'A')) != j)   //рандомим, чтобы сделать разную скорость для каждого процесса
  121.           continue;
  122.       x++;
  123.      for(i=0; i<1000000; i++);
  124.     }
  125.     exit('A' + j);
  126.   }
  127.   j++;
  128. }
  129.  
  130. j = 0;
  131. while((p = wait(&status)) != (-1)) {
  132.   for(i = 0; i < PROCNUM; i++)
  133.     if(pid[i] == p)
  134.       lead[j++] = (char) ((status >> 8) & '\377');
  135.   write(1,&bell,1);
  136. }
  137. lead[j] = '\n';
  138. sleep(1);
  139. gotoxy(1, len + 3, '\n');
  140. //write(1, lead, len + 1);   //letters in order of completion
  141. exit(0);
  142. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top