我试图让我的子进程生成一个介于 1 -9 之间的随机数,然后将其发送到我的父进程,每次按下 Control + Z 时,它都会将其显示在屏幕上。我还使用 dup2() 将流从 printf 和 scanf 更改为每个进程的管道的读写端。
代码的工作原理如下,你按控制C启动主程序。现在,每次从子级发送随机数并在父级显示时,它都会等待按下控件 Z。我面临的问题是,当再次按下控件 Z 时,孩子似乎只运行一次,它只运行父代码。管道也只显示相同的数字,并且永远不会改变,因此不会更新。我还注意到,如果我删除 dup 函数 printf("a ")之前的 printf 行,它会打印出随机数......但是,孩子跑步一次的问题仍然存在。
如果有人能在这方面帮助我,将不胜感激。干杯。
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
int selection = 0;
int secondSel = 0;
int fd[2];
int pipe1;
pid_t fork1;
void handleSignal(int sig)
{
if (sig == SIGINT)
{
selection = 1;
secondSel = 1;
}
if (sig == SIGTSTP)
{
selection = 1;
}
}
int main()
{
int firstPipe[2];
int secondPipe[2];
//wait for control+C
if (signal(SIGINT, handleSignal) == SIG_ERR)
{
printf("Error catching signal C n");
exit(0);
}
while(1)
{
//wait till control c is pressed
if (selection == 1)
{
signal(SIGINT, SIG_IGN);
if (secondSel == 1)
{
pipe1 = pipe(fd);
}
if (pipe1 < 0)
{
printf("Error creating pipe 1 n");
exit(1);
}
if (secondSel == 1)
{
fork1 = fork();
}
if (fork1 < 0)
{
printf("Error with first fork. n");
exit(1);
}
else if (fork1 == 0) //first child process
{
signal(SIGTSTP, handleSignal);
pause();
printf("a n");
int randNum1;
close(fd[0]);
dup2(fd[1], 1);
randNum1 = rand() % 9 + 1;
printf("%d ", randNum1);
fflush(stdout);
close(fd[1]);
}
else //parent
{
signal(SIGTSTP, handleSignal);
pause();
printf("b n");
int f;
close(fd[1]);
dup2(fd[0], 0);
scanf("%d n", &f);
printf("%d n", f);
close(fd[0]);
}
secondSel = 0;
}
}
}
注释/代码:
//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);
}
次优和/或令人困惑。 由于仅在调用pipe()
时设置pipe1
因此无需在每次迭代中对其进行测试。 错误消息应报告标准错误,并且不应有尾随空格。代码应为:
if (secondSel == 1)
{
if (pipe(fd) < 0)
{
fprintf(stderr, "Error creating pipe 1n");
exit(1);
}
}
对保护fork()
的同一变量进行了类似的测试。
代码在第一个循环后小心地关闭管道,但从不重新打开它。 这就是第二次和后续迭代失败的最终原因。 如果你不尝试在每个周期上做所有事情,你的代码会更好。 此外,使用标准输出调试信息会遇到各种问题;最好改用标准误差 - 特别是当标准输出无法正常工作时。
检测代码
下面是代码的检测版本。 err_syserr()
函数几乎是通用的;使用 usleep()
的条件是此代码特有的,可确保终止错误消息的输出正常排序。 我将测试放在将失败的函数调用上 — 第二个周期的第一次关闭失败,因为管道描述符在第一个周期结束时全部关闭。 (请注意,重用pipe()
在fork()
后没有帮助 - 父管道和子管道不会相互连接。
#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 Cn");
exit(1);
}
//printf("Waiting for interruptn");
//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 1n");
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: %dn", (int)getpid());
pause();
fprintf(stderr, "Unpaused C: %dn", (int)getpid());
printf("an");
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: %dn", randNum1);
if (printf("%dn", randNum1) < 2)
{
fprintf(stderr, "Print C: failedn");
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: %dn", (int)getpid());
pause();
fprintf(stderr, "Unpaused P: %dn", (int)getpid());
printf("bn");
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: failedn");
clearerr(stdin);
}
printf("Parent: %dn", 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
$
错误修复代码
这是代码的修订版本,在我看来更合适的逻辑。 它不包括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 Cn");
exit(1);
}
printf("Waiting for interruptn");
pause();
signal(SIGINT, SIG_IGN);
pipe1 = pipe(fd);
if (pipe1 < 0)
{
fprintf(stderr, "Error creating pipe 1n");
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: %dn", (int)getpid());
pause();
fprintf(stderr, "Unpaused C: %dn", (int)getpid());
int randNum1 = rand() % 9 + 1;
fprintf(stderr, "Print C: %dn", randNum1);
if (printf("%dn", randNum1) < 2)
{
fprintf(stderr, "Print C: failedn");
clearerr(stdout);
}
fflush(stdout);
}
}
else
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
while (1)
{
fprintf(stderr, "Pausing P: %dn", (int)getpid());
pause();
fprintf(stderr, "Unpaused P: %dn", (int)getpid());
int f = 99;
if (scanf("%d", &f) != 1)
{
fprintf(stderr, "Scanf P: failedn");
clearerr(stdin);
}
else
printf("Parent: %dn", 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 终止程序,因为中断被禁用。
您遇到的问题是由于在第一个数字从子级发送到父级后关闭管道。
但是,主要问题是您的代码杂乱无章,这使得更难弄清楚发生了什么并增加了出错的机会。 与其将所有内容都放在一个大循环中,我建议按如下方式重组您的代码:
wait for SIGINT
create pipe
fork
if child:
set up stdout using dup2()
while true:
wait for SIGTSTP
write to pipe
else:
set up stdin using dup2()
while true:
wait for SIGTSTP
read from pipe
etc.