-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_03.cpp
More file actions
115 lines (99 loc) · 2.68 KB
/
lab_03.cpp
File metadata and controls
115 lines (99 loc) · 2.68 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include "getch.cpp"
using namespace std;
const size_t BUFF_SIZE = 128;
void* some_p1(void * param);
void* some_p2(void * param);
struct p_args
{
bool work;
int* fd[2];
};
int main(int argc, char* argv[])
{
pthread_t my_thread_1, my_thread_2;
pthread_attr_t my_attr_1;
pthread_attr_init(&my_attr_1);
int pipe_fd[2];
pipe(pipe_fd);
// int flag = fcntl(pipe_fd[0], F_GETFL, 0);
fcntl(pipe_fd[0], F_SETFL,
fcntl(pipe_fd[0], F_GETFL, 0) | O_NONBLOCK);
// flag = fcntl(pipe_fd[1], F_GETFL, 0);
fcntl(pipe_fd[1], F_SETFL,
fcntl(pipe_fd[1], F_GETFL, 0) | O_NONBLOCK);
p_args p1_args = {true, pipe_fd};
p_args p2_args = {true, pipe_fd};
if(pthread_create(&my_thread_1, &my_attr_1, some_p1, (void*)&p1_args) == 0)
{
cout << "Поток 1 начал работу" << endl;
}
if(pthread_create(&my_thread_2, &my_attr_1, some_p2, (void*)&p2_args) == 0)
{
cout << "Поток 2 начал работу" << endl;
}
getch();
p1_args.work = false;
p2_args.work = false;
void* status_1, *status_2;
pthread_join(my_thread_1, &status_1);
pthread_join(my_thread_2, &status_2);
cout << endl;
cout << "Поток-1 вернул: " << *(int*)status_1 << endl;
cout << "Поток-2 вернул: " << *(int*)status_2 << endl;
return 0;
}
void* some_p1(void* param)
{
sleep(1);
int cnt = 0;
p_args* args = (p_args*)param;
timespec tr = {0, 1000*1000*250};
char ch = 'a';
while(args->work)
{
// if(write(*(*(args->fd)+1), &ch, 1) == -1)
if(write(*(*(args->fd)+1), nullptr, 0) <= 0)
perror("[Поток 1]");
else
// printf("\n[Поток 1]-[запись]: %c\n", ch);
cout << "[Поток 1]-[запись]: " << ch << endl;
cout.flush();
if (ch < 'Z')
ch++;
else
{
ch = 'A';
}
// sleep(1);
usleep(500*1000);
cnt++;
}
int* ret_status = new int;
*ret_status = cnt;
pthread_exit((void*)ret_status);
}
void* some_p2(void* param)
{
sleep(1);
int cnt = 0;
timespec tr = {0, 1000*1000*500};
p_args* args = (p_args*)param;
while(args->work)
{
char ch;
if(read(*(*(args->fd)+0), &ch, 1) == -1)
perror("[Поток 2]");
else
cout << "[Поток 2]-[Чтение]: " << ch << endl;
cnt++;
usleep(500*1000);
}
int* ret_status = new int;
*ret_status = cnt;
pthread_exit((void*)ret_status);
}