【发布时间】:2016-10-24 13:05:11
【问题描述】:
有一些基于 curses 的程序在其中创建一个进程链,使用 fork/exec/wait。
当xterm 调整大小并且只有第一个程序运行时,一切正常。但是,当第二个(或第三个)程序运行时:
savetty{};
endwin();
// forking
if (fork() != 0) {
// at parent
wait(&status);
} else {
// child
if (execlp(cmd, cmd, (char*) NULL) < 0) {
exit(1);
}
}
// child exited: parent refresh its screen
resetty(); refresh();
所有程序似乎都在尝试同时刷新它们的屏幕。从这一点开始,屏幕变得一团糟。
每个程序在exec 下一个程序之前需要做什么,才能“冻结”诅咒直到wait 返回?
编辑:
当我将 fork/exec/wait 替换为 system() 时,一切正常。但我不能保留它(这只是一个测试),它是一个很大的遗留系统,强烈依赖于fork/exec/wait 系统调用。
我也试过运行execlp("/bin/sh", "-c", cmd),问题还是一样。
编辑 2:从头开始的完整示例代码:
// Uncomment to switch from fork/exec/wait to system() function
//#define USE_SYSTEM
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <curses.h>
#include <term.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void mybox(int y, int x, int height, int width);
void msg(char* format, ...);
void draw();
int myexec(char* cmd);
int spawn_new_instance(char* cmd);
char* argid = NULL;
int main(int argc, char* argv[]) {
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
draw();
if (argc > 1) {
argid=argv[1];
msg("That is a child process");
}
int pid;
int key;
do {
key = getch();
switch(key) {
case KEY_RESIZE:
clear(); draw();
msg("KEY_RESIZE");
break;
case 'r':
case 'R':
pid = spawn_new_instance(argv[0]);
if (argid) {
#ifdef USE_SYSTEM
msg("Came back from system()");
#else
msg("Came back from pid %d", pid);
#endif
} else {
msg("Came back from pid %d - THAT IS THE ROOT PROCESS", pid);
}
break;
default:
msg("Unsupported key '%d'. Type '.' (dot) to exit", key);
}
} while (key != '.');
endwin();
}
void fullbox(void) {
mybox(0, 0, LINES, COLS);
}
void mybox(int y, int x, int height, int width) {
int x2 = x + width - 1;
int y2 = y + height - 1;
for (int ct = x; ct < x2; ct++) {
mvaddch(y, ct, ACS_HLINE);
mvaddch(y2, ct, ACS_HLINE);
}
for (int ct = y; ct < y2; ct++) {
mvaddch(ct, x, ACS_VLINE);
mvaddch(ct, x2, ACS_VLINE);
}
mvaddch(y, x, ACS_ULCORNER);
mvaddch(y, x2, ACS_URCORNER);
mvaddch(y2, x, ACS_LLCORNER);
mvaddch(y2, x2, ACS_LRCORNER);
refresh();
}
void msg(char* format, ...) {
for (int ct = 2; ct < COLS - 2; ct++) {
mvaddch(LINES-3, ct, ACS_CKBOARD);
}
char buffer[512];
va_list argptr;
va_start(argptr, format);
vsprintf(buffer, format, argptr);
int msglen = strlen(buffer) + 2;
int msgx = (COLS - msglen)/2;
mvprintw(LINES-3, msgx, " %s ", buffer);
}
void draw() {
mybox(0, 0, LINES, COLS);
char sbuf[128];
sprintf(sbuf, "PID: %d, LINES: %d, COLS: %d", getpid(), LINES, COLS);
int msglen = strlen(sbuf);
int msgy = (LINES - 1) / 2;
int msgx = (COLS - msglen)/2;
mvprintw(msgy, msgx, "%s", sbuf);
mybox(msgy-2, msgx-2, 1 + 4, msglen + 4);
mybox((LINES - LINES/3)/2, (COLS - COLS/3)/2, LINES/3, COLS/3);
mybox(LINES-4, 1, 3, COLS-2);
msg("Resize the terminal emulator, or type R to chain new process instance");
refresh();
}
int spawn_new_instance(char* cmd) {
savetty();
endwin();
int pid;
#ifdef USE_SYSTEM
char buf[512];
sprintf(buf, "%s child", cmd);
system(buf);
// we haven't pid using system()
pid=0;
#else
pid = myexec(cmd);
#endif
resetty();
refresh();
return pid;
}
int myexec(char* cmd) {
sigset_t blockSigchld;
sigset_t previousBlock;
sigaddset(&blockSigchld, SIGCHLD);
sigprocmask(SIG_BLOCK, &blockSigchld, &previousBlock);
int ret = 0, status = 0;
int retries = 4;
int pid;
while ((pid = fork()) == -1) {
if (errno == EAGAIN) {
if (--retries >= 0) {
sleep(1);
continue;
} else {
msg("Cannot open the process now.");
return -1;
}
} else if (errno == ENOMEM) {
msg("Not enough memory.");
return -1;
} else {
msg("Errno = %u", errno);
return -1;
}
}
if (pid != 0) { /* Parent */
ret = waitpid(pid, &status, 0);
sigprocmask(SIG_SETMASK, &previousBlock, (sigset_t *) 0);
if (ret == -1) {
return -1;
}
return pid;
} else { /* Child */
sigprocmask(SIG_SETMASK, &previousBlock, (sigset_t *) 0);
if (execlp(cmd, cmd, "child", (char*) NULL) < 0) {
exit(1);
}
}
}
说明:
- 键入“R”以生成其自身的链式新实例。
- 键入“。” (点)退出并返回父进程。
- 在第一个(主)进程中,调整终端的大小。一切正常。
- 问题:打开很多实例(10 个或更多),调整终端大小。看它多次刷新/重绘屏幕,可能每个实例运行一次。尝试返回父级,似乎从这一点开始他们都在尝试读取 de stdin 并同时重绘其屏幕,真正的混乱发生了。如果运气好的话,你可以按
CTRL+C或者输入pkill testprogram。 - 使用 system() 函数尝试上述操作。问题不会发生。出于测试目的,第一行是一个#define,可以轻松地从 fork/exec/wait 切换到 system()。
【问题讨论】:
-
那里没有足够的代码来猜测您的程序可能在某些时候忽略了等待。
-
你试过在最后一行交换
resetty(); refresh();-->>refresh(); resetty();吗? -
@joop:不。不起作用。我添加了一个完整的示例,您可以自己尝试。
-
@ThomasDickey:我添加了完整的示例代码
-
也许在孩子的
wait期间简单地忽略SIGWINCH就足够了,然后恢复处理程序