Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <termios.h>
- struct termios savetty;
- struct termios tty;
- #define ABS(N) (((N) < 0) ? (-(N)) : ((N)))
- #define GOTOXY(x, y) printf("\033" "[%d;%dH", y, x);
- #define PLIFT 0
- #define GLIFT 1
- #define NONE -1
- char stateBuf[22];
- int end = 0;
- typedef struct
- {
- const int floors; // количество этажей, на которое рассчитан лифт
- double speed;
- int floor;
- char moving;
- int isEmpty;
- } Elevator;
- // лифты и их начальные состояния
- // кол-во этажей, скорость, тек. этаж, движется, пустой
- Elevator plift = {10, 10.0, 1, 0, 1}; // пассажирский
- Elevator glift = {10, 5.0, 1, 0, 1}; // грузовой
- // клавиши для управления пассажирским и грузовым лифтом соответственно
- char lp_inside[11] = {'D', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
- char lg_inside[11] = {'D', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';'};
- char lift_move = NONE;
- int floor_move = 1;
- char from_move = 1;
- void printState()
- {
- system("clear");
- for (int i = 0; i < plift.floors; i++)
- {
- if (i != (plift.floor - 1))
- {
- stateBuf[i] = '-';
- }
- else
- {
- if ((plift.isEmpty))
- {
- stateBuf[i] = '0';
- }
- else
- {
- stateBuf[i] = '1';
- }
- }
- }
- stateBuf[plift.floors] = '\n';
- for (int i = 0; i < glift.floors; i++)
- {
- if (i != (glift.floor - 1))
- {
- stateBuf[i + plift.floors + 1] = '-';
- }
- else
- {
- if ((glift.isEmpty))
- {
- stateBuf[i + plift.floors + 1] = '0';
- }
- else
- {
- stateBuf[i + plift.floors + 1] = '1';
- }
- }
- }
- stateBuf[21] = '\n';
- write(1, stateBuf, 21);
- }
- // отправить лифт на указанный этаж
- char go(Elevator *lift, int destination, char from)
- {
- if (destination <= 0 || destination > lift->floors)
- {
- printf("Недоступное место назначения лифта: %d\n", destination);
- return 0;
- }
- int steps = ABS(destination - lift->floor);
- int step = (destination > lift->floor) ? 1 : -1;
- for (int i = 0; i < steps; i++)
- {
- lift->moving = 1;
- sleep(10 / lift->speed);
- lift->floor += step;
- }
- if (from == 0)
- {
- lift->isEmpty = 1;
- }
- else if (from == 1)
- {
- lift->isEmpty = 0;
- }
- lift->moving = 0;
- return 1;
- }
- int parse_floor(char floor_char, char *which_lift, char *from)
- {
- for (int i = 1; i < 11; i++)
- {
- if (floor_char == lp_inside[i])
- {
- *from = 0;
- *which_lift = PLIFT;
- return i;
- }
- if (floor_char == lg_inside[i])
- {
- *from = 0;
- *which_lift = GLIFT;
- return i;
- }
- }
- *from = 1;
- int result = atoi(&floor_char);
- return (result != 0 ? result : 10);
- }
- void *view(void *attr)
- {
- while (!end)
- {
- printState();
- sleep(1);
- }
- pthread_exit(0);
- }
- void *calls(void *attr)
- {
- char which_lift = PLIFT;
- char from = 1;
- char floor_chars[2];
- char none;
- floor_chars[1] = '\0';
- while (!end)
- {
- read(0, &floor_chars, 1);
- if (floor_chars[0] == 'C')
- {
- end = 1;
- pthread_exit(0);
- }
- int floor = parse_floor(floor_chars[0], &which_lift, &from);
- lift_move = NONE;
- floor_move = floor;
- if (from == 1)
- {
- from_move = 1;
- if (plift.moving && glift.moving)
- {
- while (plift.moving && glift.moving)
- {
- }
- }
- if (plift.moving)
- {
- lift_move = GLIFT;
- }
- else if (glift.moving)
- {
- lift_move = PLIFT;
- }
- else
- {
- if (ABS(plift.floor - floor) > ABS(glift.floor - floor))
- {
- lift_move = GLIFT;
- }
- else
- {
- lift_move = PLIFT;
- }
- }
- }
- else
- {
- from_move = 0;
- if (which_lift == PLIFT)
- {
- if (plift.isEmpty == 1)
- {
- GOTOXY(1, 4);
- printf("Пассажирский лифт пустой, невозможно вызвать его изнутри!\n");
- }
- else
- {
- lift_move = PLIFT;
- }
- }
- else
- {
- if (glift.isEmpty == 1)
- {
- GOTOXY(1, 4);
- printf("Грузовой лифт пустой, невозможно вызвать его изнутри!\n");
- }
- else
- {
- lift_move = GLIFT;
- }
- }
- }
- }
- }
- void *plift_move(void *attr)
- {
- while (!end)
- {
- if (lift_move == PLIFT)
- {
- go(&plift, floor_move, from_move);
- if (lift_move != GLIFT)
- {
- lift_move = NONE;
- }
- }
- }
- pthread_exit(0);
- }
- void *glift_move(void *attr)
- {
- while (!end)
- {
- if (lift_move == GLIFT)
- {
- go(&glift, floor_move, from_move);
- if (lift_move != PLIFT)
- {
- lift_move = NONE;
- }
- }
- }
- pthread_exit(0);
- }
- int main(int argc, char *argv[])
- {
- system("clear");
- tcgetattr(0, &tty);
- savetty = tty;
- tty.c_lflag &= ~(ICANON | ECHO | ISIG);
- tty.c_cc[VMIN] = 1;
- tcsetattr(0, TCSAFLUSH, &tty);
- pthread_attr_t pattr;
- pthread_attr_init(&pattr);
- pthread_attr_setscope(&pattr, PTHREAD_SCOPE_SYSTEM);
- pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
- pthread_t view_pid, calls_pid, plift_pid, glift_pid;
- pthread_create(&calls_pid, &pattr, calls, NULL);
- pthread_create(&plift_pid, &pattr, plift_move, NULL);
- pthread_create(&glift_pid, &pattr, glift_move, NULL);
- pthread_create(&view_pid, &pattr, view, NULL);
- pthread_join(calls_pid, NULL);
- pthread_join(view_pid, NULL);
- pthread_join(plift_pid, NULL);
- pthread_join(glift_pid, NULL);
- tcsetattr(0, TCSAFLUSH, &savetty);
- system("clear");
- exit(0);
- }
Add Comment
Please, Sign In to add comment