EOF를 보낼 수 없으면 '비 차단 고양이'를 사용할 수 있습니다. 나는 here (물론 크레디트는 원저자에게 간다)라는 C 테스트 버전을 포함시켰다. 마법은 다음과 같습니다. fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK).
이 비 차단 고양이의 첫 번째 인수는 다시 나가기 전에 대기 할 시간 (초)입니다.
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
void read_loop(int fFile, double wWait)
{
if (fFile < 0) return;
double max_time = wWait, total_time = 0;
struct timespec cycle_time = { 0, 50 * 1000 * 1000 };
double add_time = (double) cycle_time.tv_sec + (double) cycle_time.tv_nsec/1000000000.;
char next_line[1024];
FILE *input_file = fdopen(fFile, "r");
while (total_time < max_time)
{
while (fgets(next_line, 1024, input_file))
{
write(STDOUT_FILENO, next_line, strlen(next_line));
total_time = 0;
}
nanosleep(&cycle_time, NULL);
total_time += add_time;
}
fclose(input_file);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "%s [max time] (files...)\n", argv[0]);
return 1;
}
int max_wait = strtoul(argv[1],0, 10);
if (argc == 2)
{
fprintf(stderr, "%s: using standard input\n", argv[0]);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
read_loop(STDIN_FILENO, max_wait);
return 0;
}
int current = 2;
while (current < argc)
{
fprintf(stderr, "%s: switch to file '%s'\n", argv[0], argv[current]);
int next_file = open(argv[current++], O_RDONLY | O_NONBLOCK);
read_loop(next_file, max_wait);
close(next_file);
}
return 0;
}
나는이 정도까지 올 것으로 기대하지 않았다. 나는 거기에 리눅스 명령이 내장되기를 바랬다. 어쨌든, 나는이 "비 블로킹 고양이"를 이미 사용하고 있습니다. 나는 그것을 조금 바꿨다. 나는 부동 소수점 연산을 좋아하지 않았고 그래서 그것을 정수 연산으로 바꾸었고 또한 시간 인수를 변경하여 밀리 초를 받아 들였다. 감사. – GetFree