Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Модифицировать исходную программу так, чтобы метки процессов перемещались сверху-вниз.
- */
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/timeb.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- # include <time.h>
- /* clear standart text mode window */
- void clrscr() {
- unsigned char esc[11];
- /* obtain set cursor to left-top window corner */
- esc[0] = 27; esc[1] = '[';
- esc[2] = 'H';
- write(1,esc,3);
- /* obtain clear escape sequence */
- esc[2] = '2'; esc[3] = 'J';
- write(1,esc,4);
- return;
- }
- /* position cursor in standart text window */
- void gotoxy(int tx, int ty, char c) {
- unsigned char esc[16];
- static unsigned char ystr[3]; /* vertical cursor location */
- static unsigned char xstr[3]; /* horizontal cursor location */
- int i; /* ESC-sequence current index */
- int j; /* cursor location current index */
- /* convert cursor location to text format */
- if((tx > 99) || (ty > 99))
- tx = ty = 99;
- if((tx < 1) || (ty < 1))
- tx = ty = 1;
- xstr[0] = xstr[1] = xstr[2] = '\0';
- ystr[0] = ystr[1] = ystr[2] = '\0';
- sprintf((char *) xstr,"%d",tx);
- sprintf((char *) ystr,"%d",ty - 1);
- //drop old letter
- esc[0] = 27; esc[1] = '[';
- i=2; j=0;
- while(ystr[j])
- esc[i++]=ystr[j++];
- j=0;
- esc[i++]=';';
- while(xstr[j])
- esc[i++]=xstr[j++];
- esc[i++]='H';
- esc[i++]='\b';
- esc[i++]=' ';
- esc[i++]=' ';
- esc[i] = '\0';
- write(1,esc,i);
- //write new letter
- sprintf((char *) xstr,"%d",tx);
- sprintf((char *) ystr,"%d",ty);
- esc[0] = 27; esc[1] = '[';
- i=2; j=0;
- while(ystr[j])
- esc[i++]=ystr[j++];
- j=0;
- esc[i++]=';';
- while(xstr[j])
- esc[i++]=xstr[j++];
- esc[i++]='H';
- esc[i++] = c;
- esc[i] = '\0';
- write(1,esc,i);
- return;
- }
- int main(int argc, char* argv[]) {
- printf("1\n");
- int x = 1; //номер строки и он же номер процесса
- int status;
- int i;
- int j = 0;
- int PROCNUM = atoi(argv[1]);
- int len = atoi(argv[2]);
- int* pid = sbrk((PROCNUM+1)*sizeof(int));
- char* lead = sbrk((PROCNUM+1)*sizeof(char));
- int dist = atoi(argv[2]);
- int p;
- char bell = '\007';
- struct timespec tp[1];
- int jump;
- printf("1\n");
- clrscr();
- printf("1\n");
- while(j < PROCNUM) {
- if((pid[j] = fork()) == 0) {
- usleep(PROCNUM - j);
- while(x < dist) {
- gotoxy(j + 1, x, 'A' + j); //поменял сначала передаю позицию по строке потом по столбцу
- clock_gettime(CLOCK_REALTIME, tp);
- //ftime(tp);
- if((((unsigned short)tp[0].tv_nsec/10) % (j + 'A')) != j) //рандомим, чтобы сделать разную скорость для каждого процесса
- continue;
- x++;
- for(i=0; i<1000000; i++);
- }
- exit('A' + j);
- }
- j++;
- }
- printf("1\n");
- j = 0;
- while((p = wait(&status)) != (-1)) {
- for(i = 0; i < PROCNUM; i++)
- if(pid[i] == p)
- lead[j++] = (char) ((status >> 8) & '\377');
- write(1,&bell,1);
- }
- lead[j] = '\n';
- sleep(1);
- gotoxy(1, len + 3, '\n');
- write(1, lead, len + 1); //letters in order of completion
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement