- 论坛徽章:
- 0
|
本帖最后由 DIYBYPERL 于 2014-01-16 17:58 编辑
代码如下,结果见后- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdarg.h>
- #include <errno.h>
- #include <sys/sysmacros.h>
- #include <sys/utsname.h>
- #include <pwd.h>
- #include <grp.h>
- #include <time.h>
- #include <sys/time.h>
- #define MAXLINE 1024
- #define FILE_MODE 0777
- #define BUFFSIZE 4096
- static int err_sys(char *fmt, ...);
- void pr_exit(int status);
- int main(int argc, char **argv)
- {
- int iret;
- pid_t pid;
- int status = 0;
- if((pid = fork()) < 0)
- err_sys("fork error");
- else if(pid == 0)
- {
- int cnt = -1;
- while(cnt ++ < 120)
- {
- printf("CHLD:%2d.\n", cnt);
- sleep(1);
- }
- _exit(0);
- }
- else
- {
- int cnt = -1;
- while(waitpid(pid, &status, WCONTINUED | WUNTRACED ) == pid)
- {
- pr_exit(status);
- }
- printf("PARENT OVER\n");
- }
- exit(0);
- }
- /****************************************************************
- *以下是错误处理函数
- ***************************************************************/
- static int err_sys(char *msg)
- {
- printf("%s\n", msg);
- exit(1);
- }
- /****************************************************************
- *以下获取子进程退出状态函数
- ***************************************************************/
- void pr_exit(int status)
- {
- if(WIFEXITED(status))
- {
- printf("normal terminatioin, exit status = %d\n", WEXITSTATUS(status));
- }
- else if(WIFSIGNALED(status))
- {
- printf("abnormal termination, signal number = %d%s\n",
- WTERMSIG(status),
- #ifdef WCOREDUMP
- WCOREDUMP(status) ? "(core file generated)" : "");
- #else
- "");
- #endif
- }
- else if(WIFSTOPPED(status))
- {
- printf("child stopped, signal number = %d\n", WSTOPSIG(status));
- }
- else if(WIFCONTINUED(status))
- {
- printf("child continue\n");
- }
- else
- {
- printf("unkown exit status\n");
- }
- fflush(stdout);
- }
复制代码 代码是在AIX6.1下测试的。直接用“kill -18 <子进程ID>” 给子进程SIGTSTP信号,用“kill -19 <子进程ID>” 给子进程SIGCONT信号。发送SIGTSTP信号,父进程都能waitpid到子进程相应的状态,但发送继续信号时收不到子进程相应的状态。但在随后再次发送SIGTSTP信号时,父进程却同时收到子进程的暂停状态和之前的继续状态, 这是是个什么意思呢?????在SIGTSTP时,收到子进程的暂停和继续状态, 在SIGCONT却收不到继续状态????
请大神们指点
以下是运行结果,- CHLD: 0.
- CHLD: 1.
- CHLD: 2.
- CHLD: 3.
- CHLD: 4.
- CHLD: 5.
- CHLD: 6.
- CHLD: 7.
- CHLD: 8.
- CHLD: 9.
- CHLD:10.
- CHLD:11.
- CHLD:12.
- child stopped, signal number = 18 子进程收到SIGTSTP信号,父进程也打印了相应的内容
- CHLD:13. 子进程收到SIGCONT信号继续运行,父进程却没有waitpid地到相应的状
- CHLD:14.
- CHLD:15.
- CHLD:16.
- CHLD:17.
- CHLD:18.
- CHLD:19.
- CHLD:20.
- CHLD:21.
- child stopped, signal number = 18 子进程第二次收到SIGTSTP信号,父进程同时收到了子进程暂停的状态和之前的继续的状态!!
- child continue 子进程第二次收到SIGTSTP信号,父进程同时收到了子进程暂停的状态和之前的继续的状态!!
- CHLD:22.
- CHLD:23.
- CHLD:24.
- CHLD:25.
- CHLD:26.
- CHLD:27.
- CHLD:28.
复制代码 |
|