Skip to content

Commit f4e876b

Browse files
author
Zuo Pengpeng
committed
Add fifo example
1 parent 0598a12 commit f4e876b

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed

user_space/fifo/Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CFLAGS = -Wall
2+
LDFLAGS = -lpthread
3+
4+
target = _main
5+
srcs = main.c debug.c
6+
objs = $(srcs:.c=.o)
7+
headers = $(wildcard *.h)
8+
9+
10+
.PHONY: all
11+
all: $(target)
12+
13+
$(target): $(objs) $(headers) FORCE
14+
$(CC) -o $@ $(objs) $(LDFLAGS)
15+
@-rm -f *.o
16+
17+
$(objs):%.o:%.c
18+
@$(CC) $(CFLAGS) -c -o $@ $<
19+
20+
.PHONY: FORCE
21+
FORCE:
22+
23+
clean:
24+
rm -f $(target) *.o
25+

user_space/fifo/debug.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <stdarg.h>
5+
#include "debug.h"
6+
7+
8+
void _daemon_printf(const char *format, ...)
9+
{
10+
va_list vlist;
11+
char buf[512];
12+
char buf1[512];
13+
14+
15+
va_start(vlist, format);
16+
vsprintf(buf, format, vlist);
17+
va_end(vlist);
18+
19+
sprintf(buf1, "echo %s > "CONSOLE_DEVICE" 2>&1", buf);
20+
system(buf1);
21+
}
22+
23+
24+
#define _DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
25+
void _hex_dump(char* buf, int len)
26+
{
27+
int i;
28+
_DEBUG("**********************start**********************\n");
29+
for(i = 0; i < len; i++)
30+
{
31+
_DEBUG("%02X ", (unsigned char)buf[i]);
32+
if(i%8 == 7)
33+
{
34+
_DEBUG(" ");
35+
}
36+
if(i%16 == 15)
37+
{
38+
_DEBUG("\n");
39+
}
40+
}
41+
if (! ((--i)%16 == 15))
42+
{
43+
_DEBUG("\n");
44+
}
45+
_DEBUG("********************** end **********************\n");
46+
}
47+
48+
49+
/*
50+
static void test_hexdump()
51+
{
52+
char buf[128];
53+
54+
memset(buf, 0, 128);
55+
56+
buf[0] = 'Z';
57+
buf[125] = 'P';
58+
buf[126] = 'P';
59+
60+
HEX_DUMP(buf, 127);
61+
}
62+
63+
static void test_daemon()
64+
{
65+
DAEMON_PRINTF("%d %s\n", 33, "kkk");
66+
}
67+
*/

user_space/fifo/debug.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
3+
// more message if debug level if bigger
4+
#define DEBUG_OFF 0
5+
#define DEBUG_ERROR 1
6+
#define DEBUG_WARN 2
7+
#define DEBUG_TRACE 3
8+
#define DEBUG_INFO 4
9+
#define DEBUG_LOUD 5
10+
11+
12+
// set upper bound of debug message
13+
#define G_DEBUG_LEVEL DEBUG_INFO
14+
15+
16+
#define DEBUG_RAW(fmt, ...) fprintf(stderr, "%s %d: "fmt, __FILE__, __LINE__, ##__VA_ARGS__)
17+
18+
19+
#define LEVEL_DEBUG(level, ...) \
20+
do { \
21+
if (level <= G_DEBUG_LEVEL) \
22+
{ \
23+
DEBUG_RAW(__VA_ARGS__); \
24+
} \
25+
} while (0)
26+
27+
#define debug_error(...) LEVEL_DEBUG(DEBUG_ERROR, __VA_ARGS__)
28+
#define debug_warn(...) LEVEL_DEBUG(DEBUG_WARN, __VA_ARGS__)
29+
#define debug_trace(...) LEVEL_DEBUG(DEBUG_TRACE, __VA_ARGS__)
30+
#define debug_info(...) LEVEL_DEBUG(DEBUG_INFO, __VA_ARGS__)
31+
#define debug_loud(...) LEVEL_DEBUG(DEBUG_LOUD, __VA_ARGS__)
32+
#define debug(...) debug_info(__VA_ARGS__)
33+
34+
35+
36+
37+
void _hex_dump(char* buf, int len);
38+
// for hexdump
39+
#define HEX_DUMP(buf, len) \
40+
do { \
41+
fprintf(stderr, "%s %d:\n", __FILE__, __LINE__); \
42+
_hex_dump(buf, len); \
43+
} while (0) \
44+
45+
46+
void _daemon_printf(const char *format, ...);
47+
// for printf in daemon process
48+
#define DAEMON_PRINT(fmt, args...) _daemon_printf("%s %d: "fmt"", __FILE__, __LINE__, ##args)
49+
#define CONSOLE_DEVICE "/dev/tty1"
50+

user_space/fifo/main.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <fcntl.h>
4+
#include <pthread.h>
5+
#include <sys/stat.h>
6+
#include <sys/types.h>
7+
#include "debug.h"
8+
9+
#define FIFO_PATH "/tmp/test_fifo"
10+
11+
void *read_fifo1(void *dummy)
12+
{
13+
int fd = open(FIFO_PATH, O_RDONLY);
14+
int ret;
15+
char read_buf[1024];
16+
17+
debug("fifo open for reading ok\n");
18+
while ((ret = read(fd, read_buf, 1024)) > 0)
19+
{
20+
debug_info("reader got: %s\n", read_buf);
21+
}
22+
close(fd);
23+
24+
return NULL;
25+
}
26+
27+
void *read_fifo2(void *_fd)
28+
{
29+
int fd = dup((int)_fd);
30+
int ret;
31+
char read_buf[1024];
32+
33+
if ((ret = read(fd, read_buf, 1024)) > 0)
34+
{
35+
debug_info("reader got: %s\n", read_buf);
36+
}
37+
close(fd);
38+
39+
return NULL;
40+
}
41+
42+
43+
void test1()
44+
{
45+
pthread_t tid;
46+
int fd;
47+
48+
if (access(FIFO_PATH, F_OK) != 0)
49+
{
50+
mkfifo(FIFO_PATH, 0666);
51+
}
52+
53+
pthread_create(&tid, NULL, read_fifo1, NULL);
54+
55+
fd = open(FIFO_PATH, O_WRONLY);
56+
debug("fifo open for writing ok\n");
57+
write(fd, "111", 4);
58+
close(fd);
59+
60+
pthread_join(tid, NULL);
61+
}
62+
63+
void test2()
64+
{
65+
pthread_t tid;
66+
int fd;
67+
68+
fd = open(FIFO_PATH, O_RDWR);
69+
pthread_create(&tid, NULL, read_fifo2, (void *)fd);
70+
write(fd, "222", 4);
71+
sleep(1);
72+
close(fd);
73+
pthread_join(tid, NULL);
74+
}
75+
76+
77+
void test3()
78+
{
79+
int fd;
80+
int ret;
81+
82+
if (access(FIFO_PATH, F_OK) != 0)
83+
{
84+
mkfifo(FIFO_PATH, 0666);
85+
}
86+
87+
if (fork() > 0)
88+
{
89+
fd = open(FIFO_PATH, O_WRONLY);
90+
write(fd, "333", 4);
91+
close(fd);
92+
}
93+
else
94+
{
95+
char read_buf[1024];
96+
fd = open(FIFO_PATH, O_RDONLY);
97+
if ((ret = read(fd, read_buf, 1024)) > 0)
98+
{
99+
debug_info("reader got: %s\n", read_buf);
100+
}
101+
close(fd);
102+
}
103+
}
104+
105+
106+
107+
int main(void)
108+
{
109+
test1();
110+
test2();
111+
test3();
112+
113+
return 0;
114+
}
115+

0 commit comments

Comments
 (0)