进程编程学习总结:
1、进程与程序的区别
(1)进程是动态的,程序是静态的
(2)进程是暂时的,程序是长久的
(3)进程与程序组成不同:进程的组成包括程序、数据和进程控制块
(4)进程与程序的对应关系:通过多次执行,一个程序可对应多个进程;
通过调用关系,一个进程可包含多个程序;
2、什么是死锁?
多个进程因资源竞争而形成的一种僵局
3、创建一个进程有几种方法?
(1)调用fork()函数
(2)调用vfork()函数
4、fork()函数与vfork()函数有什么区别?
fork()函数创建的子进程将父进程的资源拷贝了一遍,父子进程把所有代码执行一遍
vfork()函数创建的子进程和父进程共享数据空间,不是重新拷贝
5、有哪几种函数可以返回子进程的进程号pid?
(1)fork()函数的返回值如果>0,那么即为子进程的进程号
(2)wait()函数成功返回值即为子进程的进程号
(3)waitpid()函数成功返回即为子进程的进程号
一、 进程的创建Fork(),vfork()两个函数都可以进行进程的创建,但是却稍有不同
Fork函数创建的子进程是将父进程的资源拷贝一份
Vfork函数创建的子进程是与父进程共享数据空间,而不是单纯的拷贝
此外,在调用vfork函数的时候,子进程必须要先退出,父进程才能继续运行
二、进程的执行
1.execl
函数的作用: 执行一个文件,
函数原型: int execl(const char *path, const char *arg,..)
参数说明: path:代表的文件路径;
arg: 表示argv[0], argv[1],...
最后一个以NULL结束;
返回值:成功函数没有返回,出错-1;
2.execv
函数的作用:
函数原型:int exev(const char *path, char * const argv[])
函数的参数: path:代表的文件路径;
argv:是一个数组里的指针传递过来;
返回值: 成功不返还,出错-1
注:exec函数族都是与进程执行相关的函数,这里只列举了其中两个,关于exec函数组的整理在之前的博客中有写到过
3. system
函数的作用:执行一个命令
函数的原型: int system(const char * string);
三、进程的等待
1.wait
函数的作用: 进程的等待,阻塞进程,等待某个子进程退出;
函数的原型: pid_t wait(int *status);
返回值:成功返回子进程PID,出错-1;
2.waitpid
函数的作用:等待退出,等待信号,或者指定的进程结束
函数的原型:pid_t waitpid(pid_t pid , int *status, int options);
函数的参数:
pid <-1 :等待进程的Pid绝对值的任何的子进程;
pid=-1, 任何子进程,---等于wait;
pid=0,
pid >0, 等待子进程为pid的子进程退出
options:
WNOHANG:如果没有子进程退出,马上返回不等待
WUNTRACED:如果子进程进入暂停执行情况,马上返回,
返回值:如果执行成功返回的是子进程的PID,失败-1;
如果使用WNOHANG的时候,没有子进程退出则返回0。
所以,实际上wait函数是waitpid函数的一个特例。
四、进程的退出
1.exit
函数的作用:正常结束进程
函数原型: void exit(int status)
_exit()函数和exit()函数的区别在于,exit()函数在退出后会将IO缓冲区一并清除,而_exit()函数并不会这样
五、进程相关函数的使用
1.如何新创建一个进程?
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
/* 进程创建 */
void main(void)
{
pid_t child;
int status;
printf("This will demostrate how to get child status\n");
/* 创建子进程 */
if((child=fork())==-1)
{
printf("Fork Error : %s\n", strerror(errno));
exit(1);
}
else if(child==0) // 子进程
{
int i;
printf("I am the child: %s\n", getpid());
for(i=0;i<1000000;i++) sin(i);
i=5;
printf("I exit with %d\n", i);
exit(i);
}
while(((child=wait(&status))==-1)&(errno==EINTR)); //子进程未结束
if(child==-1)
printf("Wait Error: %s\n", strerror(errno));
else if(!status) // 子进程退出值为0
printf("Child %ld terminated normally return status is zero\n", child);
else if(WIFEXITED(status)) // 子进程退出值0
printf("Child %ld terminated normally return status is %d\n", child, WEXITSTATUS(status));
else if(WIFSIGNALED(status)) // 子进程未获信号而退出
printf("Chlid %ld terminated due to signal %d not caught\n", child, WTERMSIG(status));
}
2.有哪几个函数返回的是子进程的pid?
总共有4个函数
①wait:函数执行成功时返回的是子进程的pid
②waitpid:执行成功时返回子进程的pid
③fork:如果返回值>0,则返回的是子进程的进程号
④vfork:同fork
3.wait函数的使用实例
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(void)
{
pid_t pid;
int status,i;
if(fork()==0)
{
printf(“This is the child process .pid =%d\n”,getpid());
exit(5);
}
else
{
sleep(1);
printf(“This is the parent process ,wait for child...\n”;
pid=wait();
// i=WEXITSTATUS(status);
printf(“child’s pid =%d .exit status=^d\n”,pid,i);
}
return 0;
}
4.waitpid函数的使用实例
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
void die(const char *msg)
{
perror(msg);
exit(1);
}
void child2_do()
{
printf("In child2: execute 'date'\n");
sleep(5);
if (execlp("date", "date", NULL) < 0) {
perror("child2 execlp");
}
}
void child1_do(pid_t child2, char *argv)
{
pid_t pw;
do {
if (*argv == '1') {
pw = waitpid(child2, NULL, 0);
}
else {
pw = waitpid(child2, NULL, WNOHANG);
}
if (pw == 0) {
printf("In child1 process:\nThe child2 process has not exited!\n");
sleep(1);
}
}while (pw == 0);
if (pw == child2) {
printf("Get child2 %d.\n", pw);
sleep(5);
if (execlp("pwd", "pwd", NULL) < 0) {
perror("child1 execlp");
}
}
else {
printf("error occured!\n");
}
}
void father_do(pid_t child1, char *argv)
{
pid_t pw;
do {
if (*argv == '1') {
pw = waitpid(child1, NULL, 0);
}
else {
pw = waitpid(child1, NULL, WNOHANG);
}
if (pw == 0) {
printf("In father process:\nThe child1 process has not exited.\n");
sleep(1);
}
}while (pw == 0);
if (pw == child1) {
printf("Get child1 %d.\n", pw);
if (execlp("ls", "ls", "-l", NULL) < 0) {
perror("father execlp");
}
}
else {
printf("error occured!\n");
}
}
int main(int argc, char *argv[])
{
pid_t child1, child2;
if (argc < 3) {
printf("Usage: waitpid [0 1] [0 1]\n");
exit(1);
}
child1 = fork();
if (child1 < 0) {
die("child1 fork");
}
else if (child1 == 0) {
child2 = fork();
if (child2 < 0) {
die("child2 fork");
}
else if (child2 == 0) {
child2_do();
}
else {
child1_do(child2, argv[1]);
}
}
else {
father_do(child1, argv[2]);
}
return 0;
}