-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.c
56 lines (49 loc) · 1.4 KB
/
main.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
/* Copyright (c) [2023] [Syswonder Community]
* [Ruxos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
const int ROUND = 5;
void *ChildFunc(void *arg)
{
int *fd = (int *)arg;
int i = 0;
char buf[32];
while (i++ < ROUND) {
sprintf(buf, "Child thread send message(%d)", i);
puts(buf);
sprintf(buf, "I am child(%d)!", i);
write(fd[1], buf, strlen(buf) + 1);
sleep(1);
}
close(fd[1]);
return NULL;
}
void main()
{
int fd[2];
int ret = pipe(fd);
if (ret != 0) {
puts("Fail to create pipe");
return;
}
pthread_t t1;
pthread_create(&t1, NULL, ChildFunc, (void *)fd);
char msg[100];
int j = 0;
while (j++ < ROUND) {
read(fd[0], msg, 15);
char buf[64];
sprintf(buf, "Main thread recieve (%d): %s", j, msg);
puts(buf);
}
puts("(C)Pipe tests run OK");
return;
}