评论/代码:
//wait for control+C
if (signal(SIGINT, handleSignal) == SIG_ERR)
是一个令人不安的开始。 signal() 函数为SIGINT 设置信号处理程序,但它不会等待信号到达。最简单的解决方法是在该代码块之后添加对 pause() 的调用。
在无限循环中,代码:
if (secondSel == 1)
{
pipe1 = pipe(fd);
}
if (pipe1 < 0)
{
printf("Error creating pipe 1 \n");
exit(1);
}
次优和/或令人困惑。由于pipe1 仅在调用pipe() 时设置,因此无需在每次迭代时对其进行测试。错误消息应报告标准错误,并且不应有尾随空格。代码应该是:
if (secondSel == 1)
{
if (pipe(fd) < 0)
{
fprintf(stderr, "Error creating pipe 1\n");
exit(1);
}
}
对保护fork()的同一个变量进行了类似的测试。
您的代码在第一个循环后小心地关闭了管道,但从不重新打开它。这就是第二次和后续迭代失败的最终原因。如果您不在每个周期都尝试做所有事情,您的代码会更好。此外,使用标准输出来调试信息会遇到各种问题;最好改用标准错误——尤其是当标准输出不能正常工作时。
仪表化代码
这是您的代码的检测版本。 err_syserr() 函数几乎是通用的;使用usleep() 的条件是此代码特有的,并确保终止错误消息的输出正常排序。我对可能失败的函数调用进行了测试——第一次关闭在第二个周期失败,因为管道描述符在第一个周期结束时全部关闭。 (请注意,在fork() 之后重用pipe() 没有任何帮助——父子节点中的管道不会相互连接。)
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void err_syserr(const char *fmt, ...);
int selection = 0;
int secondSel = 0;
int fd[2];
int pipe1 = 0;
pid_t fork1 = 0;
static void handleSignal(int sig)
{
if (sig == SIGINT)
{
selection = 1;
secondSel = 1;
}
if (sig == SIGTSTP)
{
selection = 1;
}
}
int main(void)
{
// wait for control+C
if (signal(SIGINT, handleSignal) == SIG_ERR)
{
printf("Error catching signal C\n");
exit(1);
}
//printf("Waiting for interrupt\n");
//pause();
while (1)
{
fprintf(stderr, "Looping: %d (%d)\n", (int)getpid(), selection);
// wait till control c is pressed
if (selection == 1)
{
signal(SIGINT, SIG_IGN);
if (secondSel == 1)
{
pipe1 = pipe(fd);
fprintf(stderr, "Created pipe: %d (%d, %d)\n", pipe1, fd[0], fd[1]);
}
if (pipe1 < 0)
{
printf("Error creating pipe 1\n");
exit(1);
}
if (secondSel == 1)
{
fork1 = fork();
fprintf(stderr, "Forked: %d (%d, %d)\n", fork1, (int)getpid(), (int)getppid());
}
if (fork1 < 0)
{
printf("Error with first fork.\n");
exit(1);
}
else if (fork1 == 0) // first child process
{
signal(SIGTSTP, handleSignal);
fprintf(stderr, "Pausing C: %d\n", (int)getpid());
pause();
fprintf(stderr, "Unpaused C: %d\n", (int)getpid());
printf("a\n");
if (close(fd[0]) != 0)
err_syserr("close(fd[0]=%d) failed", fd[0]);
if (dup2(fd[1], 1) < 0)
err_syserr("dup2(fd[1]=%d, 1) failed", fd[1]);
int randNum1 = rand() % 9 + 1;
fprintf(stderr, "Print C: %d\n", randNum1);
if (printf("%d\n", randNum1) < 2)
{
fprintf(stderr, "Print C: failed\n");
clearerr(stdout);
}
fflush(stdout);
if (close(fd[1]) != 0)
err_syserr("close(fd[1]=%d) failed", fd[1]);
}
else // parent
{
signal(SIGTSTP, handleSignal);
fprintf(stderr, "Pausing P: %d\n", (int)getpid());
pause();
fprintf(stderr, "Unpaused P: %d\n", (int)getpid());
printf("b\n");
if (close(fd[1]) != 0)
err_syserr("close(fd[1]=%d) failed", fd[1]);
if (dup2(fd[0], 0) < 0)
err_syserr("dup2(fd[0]=%d, 0) failed", fd[0]);
int f = 99;
if (scanf("%d", &f) != 1)
{
fprintf(stderr, "Scanf P: failed\n");
clearerr(stdin);
}
printf("Parent: %d\n", f);
if (close(fd[0]) < 0)
err_syserr("close(fd[0]=%d) failed", fd[0]);
}
secondSel = 0;
}
}
return 0;
}
#include <errno.h>
#include <string.h>
#include <stdarg.h>
static void err_syserr(const char *fmt, ...)
{
int errnum = errno;
va_list args;
if (fork1 != 0) /* Parent waits 1/4 second */
usleep(250000);
fprintf(stderr, "%d: ", (int)getpid());
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (errnum != 0)
fprintf(stderr, ": %d %s", errnum, strerror(errnum));
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
示例输出:
$ ./sigbug
Looping: 23528 (0)
Looping: 23528 (0)
…
…huge numbers of 'looping' messages omitted…
…
Looping: 23528 (0)
Looping: 23528 (0)
Looping: 23528 (0)
Looping: 23528 (0)
^CLooping: 23528 (0)
Looping: 23528 (0)
Looping: 23528 (0)
Looping: 23528 (0)
Created pipe: 0 (3, 4)
Forked: 23529 (23528, 45428)
Pausing P: 23528
Forked: 0 (23529, 23528)
Pausing C: 23529
^ZUnpaused C: 23529
Unpaused P: 23528
a
b
Print C: 5
Looping: 23529 (1)
Pausing C: 23529
Parent: 5
Looping: 23528 (1)
Pausing P: 23528
^ZUnpaused P: 23528
b
Unpaused C: 23529
23529: close(fd[0]=3) failed: 9 Bad file descriptor
23528: close(fd[1]=4) failed: 9 Bad file descriptor
$
Bug 修复代码
这是代码的修订版本,在我看来更合适的逻辑。它不包括err_syserr(),因为函数调用没有失败。不需要selection 和secondSel 变量;信号处理程序变成一个只包含return; 的存根(可以省略)。它不会在一开始就疯狂循环,因为我设置了一个暂停来等待中断。
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void handleSignal(int sig)
{
return;
}
int main(void)
{
int fd[2];
int pipe1 = 0;
pid_t fork1 = 0;
if (signal(SIGINT, handleSignal) == SIG_ERR)
{
printf("Error catching signal C\n");
exit(1);
}
printf("Waiting for interrupt\n");
pause();
signal(SIGINT, SIG_IGN);
pipe1 = pipe(fd);
if (pipe1 < 0)
{
fprintf(stderr, "Error creating pipe 1\n");
exit(1);
}
fprintf(stderr, "Created pipe: %d (%d, %d)\n", pipe1, fd[0], fd[1]);
fork1 = fork();
if (fork1 < 0)
{
fprintf(stderr, "Error with fork.\n");
exit(1);
}
fprintf(stderr, "Forked: %d (%d, %d)\n", fork1, (int)getpid(), (int)getppid());
signal(SIGTSTP, handleSignal);
if (fork1 == 0)
{
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
while (1)
{
fprintf(stderr, "Pausing C: %d\n", (int)getpid());
pause();
fprintf(stderr, "Unpaused C: %d\n", (int)getpid());
int randNum1 = rand() % 9 + 1;
fprintf(stderr, "Print C: %d\n", randNum1);
if (printf("%d\n", randNum1) < 2)
{
fprintf(stderr, "Print C: failed\n");
clearerr(stdout);
}
fflush(stdout);
}
}
else
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
while (1)
{
fprintf(stderr, "Pausing P: %d\n", (int)getpid());
pause();
fprintf(stderr, "Unpaused P: %d\n", (int)getpid());
int f = 99;
if (scanf("%d", &f) != 1)
{
fprintf(stderr, "Scanf P: failed\n");
clearerr(stdin);
}
else
printf("Parent: %d\n", f);
}
}
return 0;
}
示例输出:
$ ./sigbug-jl1
Waiting for interrupt
^CCreated pipe: 0 (3, 4)
Forked: 23554 (23553, 45428)
Pausing P: 23553
Forked: 0 (23554, 23553)
Pausing C: 23554
^ZUnpaused C: 23554
Print C: 5
Unpaused P: 23553
Pausing C: 23554
Parent: 5
Pausing P: 23553
^ZUnpaused C: 23554
Print C: 8
Unpaused P: 23553
Pausing C: 23554
Parent: 8
Pausing P: 23553
^ZUnpaused P: 23553
Unpaused C: 23554
Print C: 6
Pausing C: 23554
Parent: 6
Pausing P: 23553
^\Quit: 3
$
由于中断被禁用,我使用 SIGQUIT 终止程序。