#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
int main()
{
printf("main run pis%d\n",getpid());
execl("/usr/bin/ps","ps","-f",(char*)0);
printf("execl error");
exit (0);
}
常见信号:
信号响应方式
父子进程,子进程结束会给父进程发送信号,SIGCHLD
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<string.h>
5 #include<assert.h>
6 #include<signal.h>
7
8 void fun_sig(int sig)
9 {
10 printf("sig=%d\n",sig);
11 }
12 int main()
13 {
14 signal(SIGCHLD,fun_sig);
15 pid_t pid =fork();
16 assert(pid != -1);
17 if( pid==0 )
18 {
19 sleep(3);
20 exit(0);
21 }
22 for(int i=0;i<5;i++)
23 {
24 printf("main run\n");
25 sleep(1);
26 }
27 }
~
运行结果:
说明子进程结束,两个办法可以使子进程结束不变成僵死进程:1.忽略signal信号,子进程结束,只在linux系统上面有效,
2.在信号处理函数中取一个wait,调用wait函数
7_26
每一个进程启用后默认打开三个文件:0 stdin 1 stdout 2 stderr
3 a.txt:struct file r w coumt 偏移量 迫使inode
系统调用在内核里面实现,库函数在libxx.a,linxx.so中实现
exec替换 :execl execlp execle execv execvp -> execve
bash 命令解释器:fork+exec() 复制和替换:复制属性信息,新进程通过fork+exec实现
信号:通知进程产生了某种事件。发送信号->收到(捕获)信号->响应(处理)
./mykill+pid+序号:可以终止程序