创建一个子进程,以便我们有一个父进程和一个新创建的子进程,我们已经在父进程中运行当前程序,因此在子进程中运行新创建的进程,这样我们可以从当前程序中运行另一个程序,通过创建那么多的子进程,不仅单个程序,而且我们可以运行当前程序中的任意数量的程序。
让我们以下面的程序为例。
/*文件名:helloworld.c * /
#include<stdio.h> void main() { printf("Hello World\n"); return; }
/*文件名:execl_test.c * /
#include<stdio.h> #include<unistd.h> void main() { execl("./helloworld", "./helloworld", (char *)0); printf("This wouldn't print\n"); return; }
上面的程序将用helloworld覆盖execl_test的进程镜像,因此,不执行execl_test(printf())的进程镜像代码。
编译和执行步骤
Hello World
现在,我们将从一个程序(即execl_run_two_prgms.c)运行以下两个程序。
Hello World程序(helloworld.c)
从1到10的循环程序打印(while_loop.c)
/*文件名:while_loop.c * /
/* Prints numbers from 1 to 10 using while loop */ #include<stdio.h> void main() { int value=1; while (value <= 10) { printf("%d\t", value); value++; } printf("\n"); return; }
以下是运行两个程序的程序(一个程序来自子程序,另一个程序来自父进程)。
/*文件名:execl_run_two_prgms.c * /
来源:LearnFk无涯教程网
#include<stdio.h> #include<unistd.h> void main() { int pid; pid=fork(); /* Child process */ if (pid == 0) { printf("Child process: Running Hello World Program\n"); execl("./helloworld", "./helloworld", (char *)0); printf("This wouldn't print\n"); } else { /* Parent process */ sleep(3); printf("Parent process: Running While loop Program\n"); execl("./while_loop", "./while_loop", (char *)0); printf("Won't reach here\n"); } return; }
注意-进行sleep()调用以确保子进程和父进程按顺序运行。
编译和执行步骤
Child process: Running Hello World Program This wouldn't print Parent process: Running While loop Program Won't reach here
现在我们将从一个程序(即execl_run_two_prgms.c)运行两个程序,该程序与上述相同,但带有命令行参数。因此,我们正在运行两个程序,即子进程中的helloworld.c和父进程中的while_loop.c程序。这是如下-
Hello World程序(helloworld.c)
当循环程序根据命令行参数(while_loop.c)从1打印到num_times_str
该程序大致执行以下操作-
创建一个子进程
子进程执行helloworld.c程序
父进程执行while_loop.c程序,将命令行参数值作为参数传递给程序,如果未传递命令行参数,则默认值为10。否则,它将采用给定的参数值。参数值应为数字;如果使用字母给出的代码将无法验证。
/*文件名:execl_run_two_prgms.c * /
来源:LearnFk无涯教程网
#include<stdio.h> #include<string.h> #include<unistd.h> void main(int argc, char *argv[0]) { int pid; int err; int num_times; char num_times_str[5]; /* In no command line arguments are passed, then loop maximum count taken as 10 */ if (argc == 1) { printf("Taken loop maximum as 10\n"); num_times=10; sprintf(num_times_str, "%d", num_times); } else { strcpy(num_times_str, argv[1]); printf("num_times_str is %s\n", num_times_str); pid=fork(); } /* Child process */ if (pid == 0) { printf("Child process: Running Hello World Program\n"); err=execl("./helloworld", "./helloworld", (char *)0); printf("Error %d\n", err); perror("Execl error: "); printf("This wouldn't print\n"); } else { /* Parent process */ sleep(3); printf("Parent process: Running While loop Program\n"); execl("./while_loop", "./while_loop", (char *)num_times_str, (char *)0); printf("Won't reach here\n"); } return; }
以下是从程序的子进程execl_run_two_prgms.c调用的helloworld.c程序。
/*文件名:helloworld.c * /
#include<stdio.h> void main() { printf("Hello World\n"); return; }
以下是从程序的父进程execl_run_two_prgms.c调用的while_loop.c程序,该程序的参数是从运行该程序的程序传递的,即execl_run_two_prgms.c。
/*文件名:while_loop.c * /
#include<stdio.h> void main(int argc, char *argv[]) { int start_value=1; int end_value; if (argc == 1) end_value=10; else end_value=atoi(argv[1]); printf("Argv[1] is %s\n", argv[1]); while (start_value <= end_value) { printf("%d\t", start_value); start_value++; } printf("\n"); return; }
编译和执行步骤
Taken loop maximum as 10 num_times_str is 10 Child process: Running Hello World Program Hello World Parent process: Running While loop Program Argv[1] is 10 1 2 3 4 5 6 7 8 9 10 Taken loop maximum as 15 num_times_str is 15 Child process: Running Hello World Program Hello World Parent process: Running While loop Program Argv[1] is 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
现在让我们看一下与叠加图像相关的库函数。
#include<unistd.h> int execl(const char *path, const char *arg, ...);
此函数将使用参数,路径和arg中提到的新进程覆盖当前正在运行的进程镜像,如果需要将任何参数传递给新的进程镜像,则将通过" arg"参数发送该参数,最后一个参数应为NULL。
仅在出现错误的情况下,此函数才返回值。覆盖图像相关调用的进程如下所述-
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[]);
这些调用将解决传递命令行参数(argv []),环境变量(envp [])和其他参数的问题。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)