1.什么是程序?什么进程?
程序是静态的概念,gcc xxx.c -o pro 磁盘中生成pro文件,叫做程序。(比如桌面上的快捷方式都是程序)
进程的动态的概念,进程是程序的一次运行活动。(程序跑起来了,系统中就多了一个进程)
2.如何查看系统中有哪些进程?
a.使用ps指令查看查看全部进程(ps -aux)。在实际中作中,配合grep来查找程序中是否存在某个进程。查找带有init进程(ps -aux|grep init)。
b.使用top指令查看,类似于windows任务管理器。
3.什么是进程的标识符?
每个进程都有一个非负整数表示的唯一ID,叫做pid,类似身份证。
pid=0:称为交换进程。作用--进程调度。
pid=1:init进程。 作用--系统初始化。
编程调用getpid()函数获取自身的进程标识符。getppid()获取父进程的进程标识符。
4.什么叫父进程,什么叫子进程?
进程A创建了进程B,那么A叫做父进程,B叫做子进程。
5.fork()
#include <unistd.h>
使用fork函数创建一个进程 pid_t fork();
fork函数调用成功,返回两次。
返回值为0;代表当前进程是子进程
返回值非负数;代表当前进程是父进程(并且返回值的pid 是子进程的pid号)
调用失败,返回-1
int main()
{
pid_t pid;
pid = getpid();
printf("beform fork process=%d\n",pid);
pid_t retpid;
retpid = fork();
pid_t pid2;
pid2 = getpid();
printf("after fork process=%d\n",pid2);
if(pid == pid2){
printf("this is father process,farher process=%d,retpid=%d\n",pid2,retpid);
}else{
printf("this is child process,child process=%d,retpid=%d\n",pid2,retpid);
}
return 0;
}
//s
beform fork process=18558
after fork process=18558
this is father process,farher process=18558,retpid=18559
after fork process=18559
this is child process,child process=18559,retpid=0
6.vfork
vfork函数也可以创建进程,与fork有什么区别?
区别一:vfork直接使用父进程存储空间,不拷贝。
区别二:vfork保证子进程先运行,当子进程调用exit()退出后,父进程才执行。
int main()
{
int cnt = 0;
pid_t pid;
pid = vfork();
if(pid > 0 ){
while(1){
printf("this is father process,farher process=%d\n",getpid());
sleep(1);
printf("cnt=%d\n",cnt);
}
}else if(pid == 0){
while(1){
printf("this is child process,child process=%d\n",getpid());
sleep(1);
cnt++;
if(cnt == 3){
exit(0);
}
}
}
return 0;
}
//this is child process,child process=19726
this is child process,child process=19726
this is child process,child process=19726
this is father process,farher process=19725
cnt=3
this is father process,farher process=19725
cnt=3
this is father process,farher process=19725
cnt=3
this is father process,farher process=19725
cnt=3
this is father process,farher process=19725
cnt=3
this is father process,farher process=19725
cnt=3
7.正常退出
1.main函数调用return
2.进程调用exit(),标准c库
3.进程调用_exit()或者_Exit()
4.进程最后一个线程返回,最后一个线程调用pthread_exit.
8.异常退出
1.调用abort
2.当进程收到某些信号时,如ctrl+c
3.最后一个线程对取消(cancellation)请求做出响应
9.父进程等待子进程退出。(在fork()函数创建的进程中)
1,收集子进程的退出状态:fork()创建时,有wait()函数来获取。先执行子进程在执行父进程,但是子进程执行完不是僵尸状态。
2,子进程退出状态不被收集(僵尸状态):vfork()创建时,子进程执行完,父进程执行,父进程没有wait()函数来获取的时,子进程变成了僵尸状态。
3.为啥要等待子进程退出?用来检测子进程干活(干完活正常退出,没干完异常退出。)
正常退出:有各种各样的退出码,父进程来捕获子进程退出码。
1.父进程如何获取子进程的退出状态!!用wait()函数
pid_t wait(int *status); 1:status为空(NULL)..不关心退出状态。
2:status非空(*status)..子进程退出状态放在放在它所指向的地址中。
2.父进程如何检测子进程退出的状态!! WEXITSTATUS(status).
//eg:
int main()
{
int cnt = 0;
int status = 10;
pid_t pid;
pid = fork(); //fork
if(pid > 0 ){
wait(&status); //如果没有wait()父子进程会交替执行。//当调用wait()父进程会柱塞在这里直到子进程退出//wait()获取子进程的退出状态
printf("child quit ,child status = %d\n",WEXITSTATUS(status)); //WEXITSTATUS(status) 检测子进程退出状态 //3
while(1){
printf("this is father process,farher process=%d\n",getpid());
sleep(1);
printf("cnt=%d\n",cnt);
}
}else if(pid == 0){
while(1){
printf("this is child process,child process=%d\n",getpid());
sleep(1);
cnt++;
if(cnt == 5){
exit(3); //假设子进程退出状态为3时
}
}
}
return 0;
}
10.孤儿进程
父进程如果不等待子进程退出,在子进程之间就结束了自己的“生命”,此时子进程叫做孤儿进程。
linux避免系统存在过多孤儿进程,init进程收留孤儿进程,变成孤儿进程的父进程。
11.execl族函数
12exec配合fork使用
实现功能,当父进程检测到输入为1的时候,创建子进程把配置文件的字段值改掉。
13.system()函数
int main()
{
int ret;
system("ps"); //与exec不同。execl("可执行文件名包括路径","可执行文件","想要执行的文件",NULL);
//system()。system("可执行文件"); //缺点:直接打在公屏上
printf("ret = %d\n",ret);
return 0;
}
14.popen()函数
int main()
{
char ret[1204] = {0};
FILE *fp;
fp = popen("ps","r"); //popen可以获取运行的输出结果 //优点:得到文件的结果可以存放在字符串内存中
int nread = fread(ret,1,1024,fp);
printf("nread=%d,ret=%s\n",nread,ret);
return 0;
}