[問題] 請問有關signal的問題, 感謝感謝
以下是照著apue p.19 打的程式碼 @@
#include "apue.h"
#include <sys/wait.h>
#include "my_err.h"
static void sig_int(int); /* our signal-catching function */
int
main(void)
{
char buf[MAXLINE]; /* from apue.h */
pid_t pid;
int status;
if (signal(SIGINT, sig_int) == SIG_ERR)
err_sys("signal error");
printf("%% "); /* print prompt (printf requires %% to print %) */
while (fgets(buf, MAXLINE, stdin) != NULL) {
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0; /* replace newline with null */
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* child */
execlp(buf, buf, (char *)0);
err_ret("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
void
sig_int(int signo)
{
printf("interrupt\n%% ");
}
這個程式碼是用來模擬一個簡單的shell, 編譯執行都沒有甚麼問題
可是書上說, 程式執行的時候. 如果按ctrl+c. 只會顯示出interrupt
不會中斷程式的執行
可是我在執行程式的時候, 按了ctrl+c, 顯示出interrupt的同時程式也執行結束
請問是哪裡有錯嗎 @@>??
跟書上寫的不一樣 OTZ
感謝大家回答 <(_ _)>
ps. 我是用ubuntu 8.04
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.243.43
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章