十六. exec函数族函数的使用
先是execl.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, const char* argv[])
{
printf("hello, world\n");
for(int i=0; i<3; ++i)
{
printf("parent i = %d\n", i);
}
pid_t pid = fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
// 子进程执行程序
if(pid == 0)
{
// execl("hello", "xxxx", NULL);
//execl("/home/kevin/hello", "xxxx", NULL);
//NULL这里是哨兵,表示可变变量的输入已经终止
//ls程序是用的子进程的地址空间
execl("/bin/ls","666","-lah",NULL);
perror("execl");
exit(1);
}
for(int i=0; i<3; ++i)
{
printf(" i = %d\n", i);
}
return 0;
}
运行结果是:
hello, world
parent i = 0
parent i = 1
parent i = 2
i = 0
i = 1
i = 2
[root@VM_0_15_centos 5Day]# total 144K
drwxr-xr-x 4 root root 4.0K Dec 25 05:06 .
dr-xr-x---. 15 root root 4.0K Dec 25 05:06 ..
-rwxr-xr-x 1 root root 8.5K Dec 23 01:33 a.out
-rw-r--r-- 1 root root 3.2K Dec 23 00:56 demo_exec.c
-rw-r--r-- 1 root root 815 Dec 25 05:06 execl.c
-rw-r--r-- 1 root root 539 Dec 23 00:56 execlp.c
-rwxr-xr-x 1 root root 8.6K Dec 25 05:06 execl_res
-rw-r--r-- 1 root root 921 Dec 23 01:33 fork.c
-rw-r--r-- 1 root root 12K Dec 23 01:29 .fork.c.swp
-rw-r--r-- 1 root root 255 Dec 24 22:11 heelo.c
-rw-r--r-- 1 root root 8.4K Dec 23 00:56 hello
drwxr-xr-x 2 root root 4.0K Dec 23 00:56 homework
-rwxr-xr-x 1 root root 8.5K Dec 23 01:49 loop_f
-rw-r--r-- 1 root root 1.1K Dec 25 02:46 loop_fork.c
-rwxr-xr-x 1 root root 8.4K Dec 24 22:12 myhello
-rw-r--r-- 1 root root 570 Dec 23 00:56 orphan.c
drwxr-xr-x 2 root root 4.0K Dec 23 00:56 temp
-rwxr-xr-x 1 root root 8.4K Dec 23 01:56 test_f
-rw-r--r-- 1 root root 341 Dec 23 02:04 test_fork.c
-rw-r--r-- 1 root root 1.1K Dec 23 00:56 wait.c
-rw-r--r-- 1 root root 1.2K Dec 23 00:56 waitpid.c
-rw-r--r-- 1 root root 627 Dec 23 00:56 zombie.c
显然,在上述中,子程序只执行了execl中的。主程序执行了子程序的if之外的内容
不过一般说来,execl的变参那里,并不是填666,而是与文件名相关的内容
接下来我们来处理execlp.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, const char* argv[])
{
printf("hello, world\n");
pid_t pid = fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
// 子进程执行程序
if(pid == 0)
{
execlp("ps", "pssdfsdf", "aux", NULL);
perror("execlp");
exit(1);
}
for(int i=0; i<3; ++i)
{
printf(" i = %d\n", i);
}
return 0;
}
十七. 孤儿进程和僵尸进程
接下来用程序验证一下
首先是orphan.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, const char* argv[])
{
pid_t pid = fork();
// 父进程
if(pid > 0)
{
printf("parent process, pid = %d, ppid = %d\n", getpid(), getppid());
// sleep(1);
}
// 子进程
else if(pid == 0)
{
printf("child process, pid = %d, ppid = %d\n", getpid(), getppid());
sleep(2);
}
for(int i=0; i<5; ++i)
{
printf(" i = %d\n", i);
}
return 0;
}
然后是zombie.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, const char* argv[])
{
pid_t pid = fork();
// 父进程
if(pid > 0)
{
printf("parent process, pid = %d, ppid = %d\n", getpid(), getppid());
while(1)
{
printf(" i am live\n");
sleep(1);
}
}
// 子进程
else if(pid == 0)
{
printf("child process, pid = %d, ppid = %d\n", getpid(), getppid());
}
for(int i=0; i<5; ++i)
{
printf(" i = %d\n", i);
}
return 0;
}
十八. wait函数回收子进程资源
wait.c的程序
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, const char* argv[])
{
pid_t pid = fork();
// 父进程
if(pid > 0)
{
printf("parent process, pid = %d, ppid = %d\n", getpid(), getppid());
// 回收资源
int status;
pid_t wpid = wait(&status);
printf("child died pid = %d\n", wpid);
// 通过返回值退出
if(WIFEXITED(status))
{
printf("child process exit value: %d\n", WEXITSTATUS(status));
}
// 子进程被信号杀死
else if(WIFSIGNALED(status))
{
printf("child process died by signal: %d\n", WTERMSIG(status));
}
}
// 子进程
else if(pid == 0)
{
printf("child process, pid = %d, ppid = %d\n", getpid(), getppid());
sleep(200);
}
for(int i=0; i<5; ++i)
{
printf(" i = %d\n", i);
}
// return 10;
exit(10);
}
十九. waitpid函数简介
二十. 学习目标