检查管道是否为空,UNIX
Check if pipe is empty, UNIX
我有 2 个进程,父进程和子进程。父进程应该等待用户插入一些字符串,然后它应该通过管道将这个字符串发送给子进程。
我已经成功地做到了这一点,但我想要实现的是检查管道是否为空,如果它为空 60 秒,那么子进程应该向其父进程发送一个信号。
到目前为止我的代码:
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[100];
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
else if(childpid > 0) //parent process
{
close(fd[0]);
printf ("Insert a string: ");
scanf("%s",string);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else // child process
{
close(fd[1]);
// check if pipe is empty for 60 seconds
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}
这是一个示例,其中 parent sleep()
s 持续随机秒数,然后 write()
s 到 child 通过管道。如果child在5.5秒内没有收到消息,则发送SIGUSR1
给parent,parent不写消息。这将重复指定的次数。
请注意,child 等待 5.5 秒,而不是 5 秒,因为我们在 parent 中使用 sleep()
,这需要整整几秒。这(通常)会留下足够的时间间隔,以避免 parent sleep()
和 child 等待 5 秒,并且它们的电线在中间交叉的情况。我们可以在 parent 中使用 nanosleep()
,并让 child 等待整数秒。
备注:
我们可以在 child 进程中使用带有超时的 select()
来等待指定的时间量。 select()
将 return 就绪文件描述符的数量,因此如果我们只检查管道并且它 returns 0
,我们知道管道中没有任何内容可供读取.
我们可以在parent中使用sleep()
来测试一下。 sleep()
returns 0 如果它睡了请求的时间, non-zero 如果它被信号打断,所以我们可以检查 return 值来确定是哪种情况它是。对于您的特定情况,如果您希望 parent 从用户那里获取输入,read()
将 return -1
并将 errno
设置为 EINTR
如果它正在等待用户输入并接收到信号,那么您可以通过这种方式检测超时。当然,在这个特定的用例中,parent 更容易在 STDIN_FILENO
上调用 select()
本身,超时为 60 秒,而不是让 child 等待管道并发送信号。
在child中,我们每次循环都要FD_ZERO
fd_set
并重新填充struct timeval
,因为select()
可能会修改它们。
我们需要为 SIGUSR1
注册一个信号处理程序(什么都不做)以避免进程在接收到信号时被终止。我们不需要信号处理程序做任何事情,因为 sleep()
会在它被信号中断时通过 return 值告诉我们,这就是我们需要的所有信息。
代码:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/select.h>
#define READ_END 0
#define WRITE_END 1
#define SEC_THRESHOLD 5
#define LOOP_TIMES 5
#define BUFFER_SIZE 512
void handler(int signum)
{
(void) signum; // Ignore unused argument
}
int main(void)
{
int p[2];
if ( pipe(p) == -1 ) {
perror("pipe() failed");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if ( pid == -1 ) {
perror("fork() failed");
exit(EXIT_FAILURE);
}
else if ( pid == 0 ) {
if ( close(p[WRITE_END]) == -1 ) {
perror("failed to close pipe end in child");
exit(EXIT_FAILURE);
}
for ( int i = 0; i < LOOP_TIMES; ++i ) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(p[READ_END], &fds);
struct timeval timeout;
timeout.tv_sec = SEC_THRESHOLD;
timeout.tv_usec = 500000;
printf("Loop %d: child waiting for %d.5 seconds.\n",
i, SEC_THRESHOLD);
int status = select(p[READ_END] + 1, &fds, NULL, NULL, &timeout);
if ( status == -1 ) {
perror("select() failed in child");
exit(EXIT_FAILURE);
}
else if ( status == 0 ) {
printf("Loop %d: timed out in child, sending signal.\n", i);
kill(getppid(), SIGUSR1);
}
else {
char buffer[BUFFER_SIZE] = {0};
if ( read(p[READ_END], buffer, BUFFER_SIZE - 1) == -1 ) {
perror("read() failed in child");
exit(EXIT_FAILURE);
}
printf("Loop %d: child read: [%s]\n", i, buffer);
}
}
if ( close(p[READ_END]) == -1 ) {
perror("failed to close read end of pipe in child");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else {
if ( close(p[READ_END]) == -1 ) {
perror("failed to close pipe end in parent");
exit(EXIT_FAILURE);
}
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, NULL);
srand((unsigned) time(NULL));
for ( int i = 0; i < LOOP_TIMES; ++i ) {
const char * msg = "Message to child";
const int wait_time = rand() % 6 + 3;
printf("Loop %d: parent waiting for %d seconds...\n", i, wait_time);
int status = sleep(wait_time);
if ( status == 0 ) {
printf("Loop %d: parent sending message to child.\n", i);
if ( write(p[WRITE_END], msg, strlen(msg)) == -1 ) {
perror("write() error in parent");
exit(EXIT_FAILURE);
}
}
else {
printf("Loop %d: parent interrupted by signal.\n", i);
}
}
if ( close(p[WRITE_END]) == -1 ) {
perror("failed to close write end of pipe in parent");
exit(EXIT_FAILURE);
}
}
if ( waitpid(pid, NULL, 0) == -1 ) {
perror("waitpid() failed");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
和示例输出会话:
paul@horus:~/src/sandbox$ ./sigpipe
Loop 0: parent waiting for 7 seconds...
Loop 0: child waiting for 5.5 seconds.
Loop 0: timed out in child, sending signal.
Loop 1: child waiting for 5.5 seconds.
Loop 0: parent interrupted by signal.
Loop 1: parent waiting for 7 seconds...
Loop 1: timed out in child, sending signal.
Loop 2: child waiting for 5.5 seconds.
Loop 1: parent interrupted by signal.
Loop 2: parent waiting for 6 seconds...
Loop 2: timed out in child, sending signal.
Loop 3: child waiting for 5.5 seconds.
Loop 2: parent interrupted by signal.
Loop 3: parent waiting for 5 seconds...
Loop 3: parent sending message to child.
Loop 4: parent waiting for 3 seconds...
Loop 3: child read: [Message to child]
Loop 4: child waiting for 5.5 seconds.
Loop 4: parent sending message to child.
Loop 4: child read: [Message to child]
paul@horus:~/src/sandbox$
因此我们可以看到,如果 child 达到其 5.5 秒超时,则 parent 的每次迭代都会被中断,并在所有其他情况下成功发送消息。
我有 2 个进程,父进程和子进程。父进程应该等待用户插入一些字符串,然后它应该通过管道将这个字符串发送给子进程。 我已经成功地做到了这一点,但我想要实现的是检查管道是否为空,如果它为空 60 秒,那么子进程应该向其父进程发送一个信号。
到目前为止我的代码:
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[100];
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
else if(childpid > 0) //parent process
{
close(fd[0]);
printf ("Insert a string: ");
scanf("%s",string);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else // child process
{
close(fd[1]);
// check if pipe is empty for 60 seconds
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}
这是一个示例,其中 parent sleep()
s 持续随机秒数,然后 write()
s 到 child 通过管道。如果child在5.5秒内没有收到消息,则发送SIGUSR1
给parent,parent不写消息。这将重复指定的次数。
请注意,child 等待 5.5 秒,而不是 5 秒,因为我们在 parent 中使用 sleep()
,这需要整整几秒。这(通常)会留下足够的时间间隔,以避免 parent sleep()
和 child 等待 5 秒,并且它们的电线在中间交叉的情况。我们可以在 parent 中使用 nanosleep()
,并让 child 等待整数秒。
备注:
我们可以在 child 进程中使用带有超时的
select()
来等待指定的时间量。select()
将 return 就绪文件描述符的数量,因此如果我们只检查管道并且它 returns0
,我们知道管道中没有任何内容可供读取.我们可以在parent中使用
sleep()
来测试一下。sleep()
returns 0 如果它睡了请求的时间, non-zero 如果它被信号打断,所以我们可以检查 return 值来确定是哪种情况它是。对于您的特定情况,如果您希望 parent 从用户那里获取输入,read()
将 return-1
并将errno
设置为EINTR
如果它正在等待用户输入并接收到信号,那么您可以通过这种方式检测超时。当然,在这个特定的用例中,parent 更容易在STDIN_FILENO
上调用select()
本身,超时为 60 秒,而不是让 child 等待管道并发送信号。在child中,我们每次循环都要
FD_ZERO
fd_set
并重新填充struct timeval
,因为select()
可能会修改它们。我们需要为
SIGUSR1
注册一个信号处理程序(什么都不做)以避免进程在接收到信号时被终止。我们不需要信号处理程序做任何事情,因为sleep()
会在它被信号中断时通过 return 值告诉我们,这就是我们需要的所有信息。
代码:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/select.h>
#define READ_END 0
#define WRITE_END 1
#define SEC_THRESHOLD 5
#define LOOP_TIMES 5
#define BUFFER_SIZE 512
void handler(int signum)
{
(void) signum; // Ignore unused argument
}
int main(void)
{
int p[2];
if ( pipe(p) == -1 ) {
perror("pipe() failed");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if ( pid == -1 ) {
perror("fork() failed");
exit(EXIT_FAILURE);
}
else if ( pid == 0 ) {
if ( close(p[WRITE_END]) == -1 ) {
perror("failed to close pipe end in child");
exit(EXIT_FAILURE);
}
for ( int i = 0; i < LOOP_TIMES; ++i ) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(p[READ_END], &fds);
struct timeval timeout;
timeout.tv_sec = SEC_THRESHOLD;
timeout.tv_usec = 500000;
printf("Loop %d: child waiting for %d.5 seconds.\n",
i, SEC_THRESHOLD);
int status = select(p[READ_END] + 1, &fds, NULL, NULL, &timeout);
if ( status == -1 ) {
perror("select() failed in child");
exit(EXIT_FAILURE);
}
else if ( status == 0 ) {
printf("Loop %d: timed out in child, sending signal.\n", i);
kill(getppid(), SIGUSR1);
}
else {
char buffer[BUFFER_SIZE] = {0};
if ( read(p[READ_END], buffer, BUFFER_SIZE - 1) == -1 ) {
perror("read() failed in child");
exit(EXIT_FAILURE);
}
printf("Loop %d: child read: [%s]\n", i, buffer);
}
}
if ( close(p[READ_END]) == -1 ) {
perror("failed to close read end of pipe in child");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else {
if ( close(p[READ_END]) == -1 ) {
perror("failed to close pipe end in parent");
exit(EXIT_FAILURE);
}
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, NULL);
srand((unsigned) time(NULL));
for ( int i = 0; i < LOOP_TIMES; ++i ) {
const char * msg = "Message to child";
const int wait_time = rand() % 6 + 3;
printf("Loop %d: parent waiting for %d seconds...\n", i, wait_time);
int status = sleep(wait_time);
if ( status == 0 ) {
printf("Loop %d: parent sending message to child.\n", i);
if ( write(p[WRITE_END], msg, strlen(msg)) == -1 ) {
perror("write() error in parent");
exit(EXIT_FAILURE);
}
}
else {
printf("Loop %d: parent interrupted by signal.\n", i);
}
}
if ( close(p[WRITE_END]) == -1 ) {
perror("failed to close write end of pipe in parent");
exit(EXIT_FAILURE);
}
}
if ( waitpid(pid, NULL, 0) == -1 ) {
perror("waitpid() failed");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
和示例输出会话:
paul@horus:~/src/sandbox$ ./sigpipe
Loop 0: parent waiting for 7 seconds...
Loop 0: child waiting for 5.5 seconds.
Loop 0: timed out in child, sending signal.
Loop 1: child waiting for 5.5 seconds.
Loop 0: parent interrupted by signal.
Loop 1: parent waiting for 7 seconds...
Loop 1: timed out in child, sending signal.
Loop 2: child waiting for 5.5 seconds.
Loop 1: parent interrupted by signal.
Loop 2: parent waiting for 6 seconds...
Loop 2: timed out in child, sending signal.
Loop 3: child waiting for 5.5 seconds.
Loop 2: parent interrupted by signal.
Loop 3: parent waiting for 5 seconds...
Loop 3: parent sending message to child.
Loop 4: parent waiting for 3 seconds...
Loop 3: child read: [Message to child]
Loop 4: child waiting for 5.5 seconds.
Loop 4: parent sending message to child.
Loop 4: child read: [Message to child]
paul@horus:~/src/sandbox$
因此我们可以看到,如果 child 达到其 5.5 秒超时,则 parent 的每次迭代都会被中断,并在所有其他情况下成功发送消息。