linux编程基础——进程管理、同步

进程管理

exec函数族包括以下六个函数:

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 execve(const char *path, char * const argv[], char * const envp[]);

示例:创建一个子进程,父进程打印自己的pid,子进程通过exec获取系统命令文件执行ls命令

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
    pid_t tempPid;
    tempPid=fork();
    if(tempPid == -1){   
        perror("fork error");
        exit(1);
    } else if(tempPid > 0) {   
        printf("parent process:pid=%d\n", getpid());
    } else {   
        printf("child process:pid=%d\n", getpid());
        char *arg[]={"-a","-l","main.c", NULL};
        execvp("ls", arg);
        perror("error exec\n");
        printf("child process:pid=%d\n", getpid());
    } //of if  
    return 0;
} //of main

运行结果

另外,使用exec函数族调用时不会产生返回值,因此会出现调用失败的情况,比如下面这种:

 进程退出比较简单,调用exit()实现,且调用exit()较为安全。

进程同步

wait()函数会阻塞进程

调用成功会返回子进程的pid,失败返回-1

 示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(){
	pid_t tempPid, tempW;
	tempPid = fork();
	if(tempPid == -1){
		perror("fork error");
		exit(1);
	}else if(tempPid == 0){//child
		sleep(3);
		printf("Child process: pid = %d, ppid = %d\n", getpid(), getppid());//子进程的pid和ppid信息
	}else{//parent 
		tempW = wait(NULL);
        printf("my child pid = %d",tempPid); //验证当前父进程的子进程的pid
		printf("Catched a child process: pid = %d\n", tempW);//验证捕获到的子进程的pid
	}//of if
	printf("------finish------\n");
	return 0;
}//of main

 

子进程沉睡三秒后输出了pid和ppid信息,在父进程中调用wait()来捕获僵尸态的子进程信息,因此结果中父进程捕获到的僵尸进程是自己的子进程,即回收子进程。

wait函数的参数不为空,可以获取子进程的退出状态,退出状态存放在参数status的低八位中。
int WIFEXITED(int status) 判断子进程是否正常退出,若是,返回非0值,否则返回0
int WEXITSTATUS(int status)和WIFEXITED配合使用,WIFEXITED返回非0值,则使用该宏提取子进程的返回值。

示例:

#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
    int tempStatus;
    pid_t tempPid, tempW;
    tempPid = fork();
    if(tempPid == -1){
        perror("fork error");
        exit(1);
    } else if(tempPid == 0){//子
        sleep(3);
        printf("Child process: pid=%d\n",getpid());
        exit(5);  //子进程退出状态码
 	}  else{//父
        tempW = wait(&tempStatus); //保存子进程pid,退出状态存入参数
        if(WIFEXITED(tempStatus)){   //正常退出
            printf("Child process pid=%d exit normally.\n", tempW );
            printf("Return Code:%d\n",WEXITSTATUS(tempStatus));
        } else {
            printf("Child process pid=%d exit abnormally.\n", tempW);
        }//of if
    }//of if
    return 0;
}//of main

结果:子进程正常退出且成功返回状态码。 

waitpid()弥补wait()的缺点,可以等待指定的子进程,也可以在父进程不阻塞的情况下获取子进程的状态。
pid_t waitpid(pid_t pid, int *status, int options)

示例:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(){
	pid_t tempPid, tempP, tempW;
	tempPid= fork();							//创建第一个子进程
	if (tempPid == -1){							
		perror("fork1 error");
		exit(1);
	} else if (tempPid == 0){						//子进程沉睡
		sleep(5);
		printf("First child process:pid=%d\n", getpid());
	} else {						//父进程继续创建进程
		int i;
		tempP = tempPid;
		for (i = 0; i < 3; i++){					//由父进程创建3个子进程
			if ((tempPid = fork()) == 0){
				break;
			}//of if
		}//of for i
		if (tempPid == -1){						//出错
			perror("fork error");
			exit(2);
		} else if (tempPid == 0){					//子进程
			printf("Child process:pid=%d\n", getpid());
			exit(0);
		} else {					//父进程
			tempW = waitpid(tempP, NULL, 0);			//等待第一个子进程执行
			if (tempW == tempP){
				printf("Catch a child Process: pid=%d\n", tempW);
			}else{
				printf("waitpid error\n");
			}//of if
		}//of if
	}//of if
	return 0;
}//of main

结果:第一个子程序先沉睡,因此先输出三个子程序的pid,后输出第一个子进程的pid,使用waitpid()指定等待第一个子进程退出。 

示例:

获取状态

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
	pid_t tempPid, tempW;
	tempPid = fork();
	if (tempPid == -1){
		perror("fork error");
		exit(1);
	} else if (tempPid == 0){
		sleep(3);
		printf("Child process:pid=%d\n", getpid());
		exit(0);
	} else {
		do{
			tempW = waitpid(tempPid, NULL, WNOHANG);
			if (tempW == 0){
				printf("No child exited\n");
				sleep(1);
			}//of if
		} while (tempW == 0);
		if (tempW == tempPid){
			printf("Catch a Child process:pid=%d\n", tempW);
		}else{
			printf("waitpid error\n");
		}//of if
	}//of if
	return 0;
}//of main

结果:因为使用sleep()沉睡,所以父进程一开始无法捕获到子进程输出没有子进程,三秒后子进程终止,父进程捕获到子进程退出信息。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值