1.什么是进程,什么是程序,有什么区别?
程序是静态的概念 gcc xx.c -o pro
磁盘中生成的pro文件,叫程序
进程是程序的一次运行活动,通俗的说就是程序跑起来了,系统就多了一个进程。
2.如何查看系统中有哪些进程?
ps -aux|grep a.out//查看a.out进程详细信息
A. 使用ps指令查看
实际使用中,配合grep 来查看程序中是否存在某个进程
B.使用top指令查看,类似window任务管理器
3.什么是进程标识符?
每个进程都有一个非负整数表示的唯一ID,叫做pid,类似身份证
Pid= 0:称为交换进程(swapper)
作用——进程调度
Pid=1:init(初始化)进程
作用——系统初始化
编程调用getpid()函数获取自身的进程标识符,getpid获取父进程的进程标识符
4. 什么叫父进程,什么叫子进程
进程A创建了进程B,那么A叫做父进程,B叫做子进程,父子进程是相对关系,理解为人类中的父子关系。
fork()函数的使用
#include <stdio.h>
#include <sys/types.h>//getpid()需要这个头文件和#include <unistd.h>在man手册中可查
#include <unistd.h>//fork()的头文件
int main()
{
pid_t pid;
printf("father id is %d\n",getpid());
pid=fork();//创建子进程
if (pid > 0)//pid>0说明此刻是父进程在运行
{
printf("this father pid is %d \n",getpid());//打印父进程的pid
}
else if(pid == 0)//pid=0说明在跑子进程
{
printf("this child pid is %d \n",getpid());打印子进程的pid
}
return 0;
}
输出结果:
exit()函数的使用,vfork()函数, wait()的使用
exit()–立刻终止调动进程
vfork()–保证子进程先运行,父进程会被挂起,直到子进程调用exit()或者exec()系列函数
wait()–一般用在父进程中等待回收子进程的资源,而防止僵尸进程的产生。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int cnt = 0;
int status = 10;//exit() tui chu de zhuang tai ma
pid=vfork();
if (pid > 0)
{
wait(&status);//g收集子进程的状态防止其变成僵尸进程
printf("child exit child status = %d \n",WEXITSTATUS(status));//WEXITSTATUS为宏定义 只有使用了这个才会正确的打出exit退出的状态码也相当于返回值)
while(1)
{
printf("this father pid is %d \n",getpid());
sleep(1);
printf("cnt = %d \n",cnt);
}
}
else if(pid == 0)
{
while(1)
{
printf("this child pid is %d \n",getpid());
sleep(1);
cnt++;
if(cnt>3)
{
exit(3);
}
}
}
return 0;
}
输出结果:
exec族函数函数:
(优秀参考博文-----原文链接:https://blog.csdn.net/u014530704/article/details/73848573)
exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量
我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。
int execl(const char *path, const char *arg, …);
int execlp(const char *file, const char *arg, …);
int execle(const char *path, const char *arg,…, char * const envp[]);
int execv(const char *path, char *const argv[]);//暂时不太常用
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);//暂时不太常用
execl()的使用:
带l的一类exac函数(l表示list),包括execl、execlp、execle,要求将新程序的每个命令行参数都说明为 一个单独的参数。这种参数表以空指针结尾。
示例1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
if(execl("./echoarg","echoarg","abc",NULL) == -1)//第一个参数表示,echoarg的环境变量,第二个第三个参数表示执行echoarg的变量,最后以NULL结尾
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
#include <stdio.h>
int main(int argc,char *argv[])
{
int i = 0;
for(i = 0; i < argc; i++)
{
printf("argv[%d]: %s\n",i,argv[i]);
}
return 0;
}
我们先用gcc编译echoarg.c,生成可执行文件echoarg并放在当前路径bin目录下。文件echoarg的作用是打印命令行参数。然后再编译execl.c并执行execl可执行文件。用execl 找到并执行echoarg,将当前进程main替换掉,所以”after execl” 没有在终端被打印出来。
示例2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
if(execl("/bin/ls","ls",NULL,NULL) == -1)//Ls的运行只需要ls这个参数,因为他就是在Liunx下执行ls而已
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
编译执行后相当于使用了ls指令
execlp()的使用
带p的一类exac函数,包括execlp、execvp、execvpe,如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。举个例子,PATH=/bin:/usr/bin
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("befor echo\n");
if(execlp("ps","ps",NULL,NULL) == -1)//exexlp p 代表通过path这个环境变量自动寻找ps的位置,所以不需要知道文件的位置也可以使用
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
execvp()的使用
带v不带l的一类exac函数,包括execv、execvp、execve,应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
如char *arg[]这种形式,且arg最后一个元素必须是NULL,例如char *arg[] = {“ls”,”-l”,NULL};
execvp()只是将execl后面的参数放在一个数组内而已
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execvp(const char *file, char *const argv[]);
int main(void)
{
printf("befor echo\n");
char *argv[] = {"ps","-l",NULL};//第二个为NULL也可以
if(execvp("ps",argv) == -1)
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
system函数的使用
优秀参考博文-----原文链接:https://www.cnblogs.com/leijiangtao/p/4051387.html
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int data=10;
while(1)
{
printf("please input data\n");
scanf("%d",&data);
if(data == 1)
{
pid = fork();
if(pid>0)
{
wait(NULL);
}
else if(pid ==0)
{
// execl("./changedata","changedata","config.txt",NULL);
system("./changedata config.txt");//system()的使用同样需要知道我们使用的这个changedata的位置,加上我们使用这个changedata程序的所需要的参数或者文件是什么即可,
}
}
else
{
printf("this error data\n");
}
}
return 0;
}
popen()函数的使用
优秀参考博文-----原文链接:https://blog.csdn.net/libinbin_1014/article/details/51490568
原型: FILE popen( const char command, const char* mode )
command: 是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用 -c 标志,shell 将执行这个命令。
mode: 只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 “r” 则文件指针连接到 command 的标准输出;如果 type 是 “w” 则文件指针连接到 command 的标准输入。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
// size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
int main(void)
{
char ret[1024]={0};
FILE *fp;
fp = popen("ps","r");
int n_read = fread(ret,1,1024,fp);
printf("n_read is %d byte , ret=%s\n",n_read,ret);
return 0;
}