当前位置: 技术问答>linux和unix
一个简单非阻塞程序问题
来源: 互联网 发布时间:2016-12-17
本文导语: 请问下我把标准输出设置为非阻塞了,为什么err.txt出错文件里没EAGAIN出错信息 #include #include #include #include #include #define MAX 1000000 #define LEN 1024 int main(int argc, char **argv) { int fd1, fd2, flags; FILE *fp; char bu...
请问下我把标准输出设置为非阻塞了,为什么err.txt出错文件里没EAGAIN出错信息
#include
#include
#include
#include
#include
#define MAX 1000000
#define LEN 1024
int main(int argc, char **argv)
{
int fd1, fd2, flags;
FILE *fp;
char buf[MAX];
int n, rest;
char *p = buf;
char content[LEN];
if(argc != 3)
{
printf("expect argsn");
exit(1);
}
fd1 = open(argv[1], O_RDONLY);
if(fd1 == -1)
{
perror("fail to open");
exit(1);
}
fd2 = open(argv[2], O_WRONLY);
if(fd2 == -1)
{
perror("fail to open");
exit(1);
}
fp = fdopen(fd2, "w");
if(fp == NULL)
{
perror("fail to read");
exit(1);
}
flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
if(flags == -1)
{
perror("fail to fcntl");
exit(1);
}
flags |= O_NONBLOCK;
if(fcntl(STDOUT_FILENO, F_SETFL, 0) == -1)
{
perror("fail to fcntl");
exit(1);
}
rest = read(fd1, buf, MAX);
printf("get %d bytes from %sn", rest, argv[1]);
while(rest > 0)
{
errno = 0;
n = write(STDOUT_FILENO, p, rest);
fprintf(fp, "write %d, errno %sn", n, strerror(errno));
if(rest > 0)
{
p += n;
rest -= n;
}
}
printf("donen");
close(fd1);
fclose(fp);
return 0;
}
#include
#include
#include
#include
#include
#define MAX 1000000
#define LEN 1024
int main(int argc, char **argv)
{
int fd1, fd2, flags;
FILE *fp;
char buf[MAX];
int n, rest;
char *p = buf;
char content[LEN];
if(argc != 3)
{
printf("expect argsn");
exit(1);
}
fd1 = open(argv[1], O_RDONLY);
if(fd1 == -1)
{
perror("fail to open");
exit(1);
}
fd2 = open(argv[2], O_WRONLY);
if(fd2 == -1)
{
perror("fail to open");
exit(1);
}
fp = fdopen(fd2, "w");
if(fp == NULL)
{
perror("fail to read");
exit(1);
}
flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
if(flags == -1)
{
perror("fail to fcntl");
exit(1);
}
flags |= O_NONBLOCK;
if(fcntl(STDOUT_FILENO, F_SETFL, 0) == -1)
{
perror("fail to fcntl");
exit(1);
}
rest = read(fd1, buf, MAX);
printf("get %d bytes from %sn", rest, argv[1]);
while(rest > 0)
{
errno = 0;
n = write(STDOUT_FILENO, p, rest);
fprintf(fp, "write %d, errno %sn", n, strerror(errno));
if(rest > 0)
{
p += n;
rest -= n;
}
}
printf("donen");
close(fd1);
fclose(fp);
return 0;
}
|
考虑把输出定向到一个管道呢?
管道满了的时候,应该就EAGAIN了
管道满了的时候,应该就EAGAIN了
|
你好像还是没有明白kk的意思呢,出错和你读写的文件大小没有关系呢......只和是否可读写有关系