日期:2014-05-16 浏览次数:20634 次
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
int pip[] = {-1, -1};
void sleep_select(int i)
{
struct timeval timeout;
timeout.tv_sec = i;
timeout.tv_usec = 0;
select(0, NULL, NULL, NULL, &timeout);
}
static void *threadFunction(void* userdata)
{
char data[5] = {0};
fd_set fds;
struct timeval timeout = {1,0};
int ret;
FD_ZERO(&fds);
FD_SET(pip[0], &fds);
printf("p0: %d\n", pip[0]);
printf("p1: %d\n", pip[1]);
while (1)
{
ret = select(pip[0] + 1, &fds, NULL, NULL, &timeout);
if (ret < 0)
{
printf("select error\n");
break;
}
else if (ret == 0)
{
//printf("no fd ready\n");
continue;
}
else
{
if (FD_ISSET(pip[0], &fds) > 0)
{
read(pip[0], data, 5);
printf("data: %s\n", data);
}
}
}
}
void *mysignal()
{
printf("signal\n");
char data[] = "data";
while (1)
{
sleep_select(2);
//printf("sowelflsdfsldfsdlfsdl\n");
write(pip[1], data, 5);
}
}
int main(int argc, char* argv[])
{
char data[] = "data";
pthread_t id;
if (pipe(pip)) {