FIFO 进程间通讯过程设置了阻塞,没有效果 的问题与解决
if (mkfifo(SERVER_FIFO_NAME, 0666) == -1)
{
fprintf(stderr, "Sorry, create server fifo failure!\n");
exit(EXIT_FAILURE);
}
server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY );
if (server_fifo_fd == -1)
{
fprintf(stderr, "Sorry, server fifo open failure!\n");
exit(EXIT_FAILURE);
}
while (1)
{
res = read(server_fifo_fd, &MC1, sizeof(MessageClient) );//出问题的地方,没有阻塞效果
if( res > 0 )//有内容的情况
{
...
}
else//keep try open //没有内容的情况 ,无奈地睡了一下
{
sleep(1);//need to sleep for a few second for it will make 100%cpu
printf( "\tsleeping res = %d \t",res);
}
}
{
fprintf(stderr, "Sorry, create server fifo failure!\n");
exit(EXIT_FAILURE);
}
server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY );
if (server_fifo_fd == -1)
{
fprintf(stderr, "Sorry, server fifo open failure!\n");
exit(EXIT_FAILURE);
}
while (1)
{
res = read(server_fifo_fd, &MC1, sizeof(MessageClient) );//出问题的地方,没有阻塞效果
if( res > 0 )//有内容的情况
{
...
}
else//keep try open //没有内容的情况 ,无奈地睡了一下
{
sleep(1);//need to sleep for a few second for it will make 100%cpu
printf( "\tsleeping res = %d \t",res);
}
}
效果通讯不及时了。。。
后来读过
提及打开一个 读的来写 会解决这个问题。
上面的打开读后面,加入一句 open 写的
server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY );
int dummy = open(SERVER_FIFO_NAME, O_WRONLY );/* this will never used*/
int dummy = open(SERVER_FIFO_NAME, O_WRONLY );/* this will never used*/
测试之后,发现真解决了问题。