-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess2.c
98 lines (88 loc) · 2.12 KB
/
process2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "process.h"
#define m 2
#define n 2
void process2()
{
printf("\nProcess 2 running...\n");
int A[m][n], B[m][n], C[m][n];
int i, j;
char c = ' ';
srand(time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
A[i][j] = rand() % 10;
B[i][j] = rand() % 10;
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
C[i][j] = A[i][j] - B[i][j];
}
}
printf("Matrix 1:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("Matrix 2:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
printf("C matrices:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
while (1)
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout;
timeout.tv_sec = TIMEOUT_SECONDS;
timeout.tv_usec = TIMEOUT_MICROSECONDS;
int ready = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout);
if (ready == -1) {
perror("select");
break;
} else if (ready == 0) {
printf("loop running...\n");
} else {
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == 27) {
printf("ESC key pressed. Stopping the loop.\n");
break;
} else {
printf("Key pressed: %c\n", c);
}
}
}
sleep(5);
switch_context();
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}