首先创建两个管道:p1,p2
lkmao@ubuntu:/big/libevent/libevent-2.1.12-stable/_install/learn$ mkfifo p1 p2
lkmao@ubuntu:/big/libevent/libevent-2.1.12-stable/_install/learn$ ls -lshF p1 p2
0 prw-rw-r-- 1 lkmao lkmao 0 6月 29 20:39 p1|
0 prw-rw-r-- 1 lkmao lkmao 0 6月 29 20:39 p2|
lkmao@ubuntu:/big/libevent/libevent-2.1.12-stable/_install/learn$
看到p1和p2名字后面都有一根竖线,这是参数-F的作用,表示这两个文件是管道文件。
测试代码:
#include <sys/types.h>
#include <event2/event-config.h>
#include <stdio.h>
#include <event.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
struct private_data{
struct event ev;
char buffer[1024];
};
struct event_base *base = NULL;
void stdin_callback(int fd, short event, void *argc)
{
struct private_data *p = (struct private_data *)argc;
memset(p->buffer, 0, sizeof(p->buffer));
int res = read(fd,p->buffer,sizeof(p->buffer));
DEBUG_INFO("res = %d",res);
if(res == 0){
close(fd);
free(p);
}else{
DEBUG_INFO("read = %d,buffer = %s",fd,p->buffer);
event_add(&p->ev, NULL);
}
}
void* add_fd_thread(void *arg){
int i = 0;
int fd = 0;
char **argv = (char**)arg;
while(base == NULL){
sleep(1);
}
for(int i = 1; i < 3;i++){
fd = open(argv[i],O_RDONLY);
if(fd < 0){
perror("open");
exit(-1);
}
DEBUG_INFO("open %s ok",argv[i]);
struct private_data *p = malloc(sizeof(struct private_data));
event_set(&p->ev, fd, EV_READ, stdin_callback,p);
event_base_set(base, &p->ev);
event_add(&p->ev, NULL);
}
DEBUG_INFO("open all file");
return NULL;
}
static void
signal_cb(evutil_socket_t sig, short events, void *user_data)
{
struct event_base *base = user_data;
struct timeval delay = { 2, 0 };
printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
event_base_loopexit(base, &delay);
}
void *run_thread(void *arg){
int res = 0;
struct event *signal_event;
base = event_init();
signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
if (!signal_event || event_add(signal_event, NULL)<0) {
fprintf(stderr, "Could not create/add a signal event!\n");
return NULL;
}
DEBUG_INFO("start");
do{
res = event_base_dispatch(base);
if(res < 0){
perror("event_base_dispatch");
DEBUG_INFO("res = %d", res);
exit(-1);
}
DEBUG_INFO("res = %d",res);
}while(0);
event_free(signal_event);
event_base_free(base);
base = NULL;
DEBUG_INFO("end");
return NULL;
}
int main(int argc,char *argv[])
{
pthread_t t1,t2;
int res = 0;
DEBUG_INFO("pid = %d",getpid());
if(argc < 3){
DEBUG_INFO("Usage:%s p1 p2",argv[0]);
return -1;
}
res = pthread_create(&t1,NULL,add_fd_thread,argv);
if(res < 0){
perror("1:pthread_create");
return -1;
}
res = pthread_create(&t2,NULL,run_thread,NULL);
if(res < 0){
perror("2:pthread_create");
return -1;
}
sleep(1);
while(1){
sleep(1);
if(base == NULL){
break;
}
}
DEBUG_INFO("bye bye");
return 0;
}
运行代码:
./_build_/app p1 p2
测试第一个管道
测试第二个管道
测试结束
小结