- 论坛徽章:
- 0
|
- #include<stdio.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<signal.h>
- #include <sys/types.h>
- void handle(int signum);
- void handle(int signum)
- {
- printf("child process execute\n");
- }
- int main()
- {
- pid_t pd;
- if((pd = fork()) == 0){
- //子进程执行
- printf("I am child process\n");
- signal(SIGINT,handle);
- while(1);
- }else{
- //父进程执行
- while(1){
- kill(pd,SIGINT);
- sleep(1);
- }
- }
- }
复制代码 上面这段代码执行是没有结果的,
但是下面这段代码就可以了。- #include<stdio.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<signal.h>
- #include <sys/types.h>
- void handle(int signum);
- int main()
- {
- pid_t pd;
- if((pd = fork()) == 0){
- //子进程执行
- printf("I am child process\n");
- signal(SIGINT,handle);
- while(1);
- }else{
- //父进程执行
- while(1){
- kill(pd,SIGINT);
- sleep(1);
- }
- }
- }
- void handle(int signum)
- {
- printf("child process execute\n");
- }
复制代码 不知道为什么,难道是fork之拷贝fork()函数之后的父进程的数据和代码空间吗?,实在搞不懂。请大神帮忙看下。 |
|