- 論壇徽章:
- 0
|
本帖最后由 DIYBYPERL 于 2014-01-16 17:58 編輯
代碼如下,結(jié)果見后- #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);
- }
- /****************************************************************
- *以下是錯誤處理函數(shù)
- ***************************************************************/
- static int err_sys(char *msg)
- {
- printf("%s\n", msg);
- exit(1);
- }
- /****************************************************************
- *以下獲取子進(jìn)程退出狀態(tài)函數(shù)
- ***************************************************************/
- 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);
- }
復(fù)制代碼 代碼是在AIX6.1下測試的。直接用“kill -18 <子進(jìn)程ID>” 給子進(jìn)程SIGTSTP信號,用“kill -19 <子進(jìn)程ID>” 給子進(jìn)程SIGCONT信號。發(fā)送SIGTSTP信號,父進(jìn)程都能waitpid到子進(jìn)程相應(yīng)的狀態(tài),但發(fā)送繼續(xù)信號時收不到子進(jìn)程相應(yīng)的狀態(tài)。但在隨后再次發(fā)送SIGTSTP信號時,父進(jìn)程卻同時收到子進(jìn)程的暫停狀態(tài)和之前的繼續(xù)狀態(tài), 這是是個什么意思呢?????在SIGTSTP時,收到子進(jìn)程的暫停和繼續(xù)狀態(tài), 在SIGCONT卻收不到繼續(xù)狀態(tài)????
請大神們指點(diǎn)
以下是運(yùn)行結(jié)果,- 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 子進(jìn)程收到SIGTSTP信號,父進(jìn)程也打印了相應(yīng)的內(nèi)容
- CHLD:13. 子進(jìn)程收到SIGCONT信號繼續(xù)運(yùn)行,父進(jìn)程卻沒有waitpid地到相應(yīng)的狀
- CHLD:14.
- CHLD:15.
- CHLD:16.
- CHLD:17.
- CHLD:18.
- CHLD:19.
- CHLD:20.
- CHLD:21.
- child stopped, signal number = 18 子進(jìn)程第二次收到SIGTSTP信號,父進(jìn)程同時收到了子進(jìn)程暫停的狀態(tài)和之前的繼續(xù)的狀態(tài)!
- child continue 子進(jìn)程第二次收到SIGTSTP信號,父進(jìn)程同時收到了子進(jìn)程暫停的狀態(tài)和之前的繼續(xù)的狀態(tài)!!
- CHLD:22.
- CHLD:23.
- CHLD:24.
- CHLD:25.
- CHLD:26.
- CHLD:27.
- CHLD:28.
復(fù)制代碼 |
|