目录

覆盖过程图像(Overlaying Process Image)

假设我们正在运行一个程序,我们想从当前程序运行另一个程序。 这可能吗? 如果我们实施覆盖过程映像的概念,为什么不呢。 那很好但是当前正在运行的程序呢,也可以运行。 怎么可能,因为我们将当前的计划与新计划重叠。 该怎么做,如果我想在不丢失当前正在运行的程序的情况下运行这两个程序,是否可能? 对的,这是可能的。

创建子进程,以便我们有一个父进程和一个新创建的子进程。 我们已经在父进程中运行当前程序,因此在子进程中运行新创建的进程。 通过这种方式,我们可以从当前程序运行另一个程序。 不仅是单个程序,而且我们可以通过创建许多子进程来从当前程序运行任意数量的程序。

让我们以下面的程序为例。

/*文件名: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())的过程映像代码。

编译和执行步骤 (Compilation and Execution Steps)

Hello World

现在,我们将从一个程序运行以下两个程序,即execl_run_two_prgms.c。

  • Hello World程序(helloworld.c)

  • while循环程序从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 */

#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;
}

Note - 放置sleep()调用以确保子进程和父进程按顺序运行(不与结果重叠)。

编译和执行步骤 (Compilation and Execution Steps)

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循环程序按照命令行参数从1到num_times_str打印(while_loop.c)

该计划广泛执行以下行动 -

  • 创建子进程

  • 子进程执行helloworld.c程序

  • 父进程执行while_loop.c程序,将命令行参数值作为参数传递给程序。 如果未传递命令行参数,则默认值为10.否则,它将采用给定的参数值。 参数值应为数字; 如果在字母表中给出,代码将无法验证。

/*文件名:execl_run_two_prgms.c */

#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;
}

编译和执行步骤 (Compilation and Execution Steps)

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 [])和其他参数。

↑回到顶部↑
WIKI教程 @2018