|
创建子进程一,在子进程中递归打印/home目录中的内容(用exec系列函数调用第二次实验中的代码完成此功能);子进程结束的时候完成以下功能:打印字符串“Child process exited!”。打印子进程标识符,打印父进程标识符。创建子进程二, 打印子进程运行环境中环境变量“USER”的值,通过exec系列中的某个函数设置子进程”USER”环境变量值为“zhangsan”,并且让该子进程完成以下命令:“ls –li /home”.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void fun() {
printf("\n");
printf("Child process exited!!!\n");
printf("The child's pid is: %d\n",getpid());
printf("The father's pid is %d\n",getppid());
printf("\n");
}
int main() {
pid_t pid;
pid = vfork();
if(pid <0)
perror("vfork");
else if(pid == 0) {
printf("This is the child1 !!!\n");
atexit(fun);
if((execl("/home/wang/test/file/test6/test","test",NULL)) < 0) {
perror("execl");
exit(0);
}
} else {
printf("This is the father !!!\n");
if(vfork() == 0) {
printf("This is the child2 !!!\n");
printf("The child2's father's pid is: %d\n",getppid());
char * env[] = {"USER=zhangsan",NULL};
char *p;
p = getenv("USER");
if(p) {
printf("The user is: %s\n",p);
}
system("ls -li /home");
if((execle("/bin/env","env",NULL,env)) < 0)
perror("execle");
exit(1);
}
}
return 0;
}
————————————————
版权声明:本文为CSDN博主「ア灬格子衬衫々」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43573663/article/details/108814671
|
|