开发手册 欢迎您!
软件开发者资料库

覆盖过程图像

过程间通信覆盖过程映像 - 从基本概念到高级概念,从简单和简单的步骤学习过程间通信,包括概述,过程信息,过程映像,过程创建和终止,子进程监控,进程组,会话和作业控制,进程资源,其他进程,覆盖进程映像,相关系统调用(系统V),系统V和Posix,管道,命名管道,共享内存,消息队列,信号量,信号,内存映射。

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

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

让我们考虑以下程序作为示例.

/*文件名:helloworld.c */

#includevoid main() {   printf("Hello World\n");   return;}

/*文件名:execl_test.c */

#include#includevoid 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)

  • while循环程序打印从1到10(while_loop.c)

/*文件名:while_loop.c */

/* Prints numbers from 1 to 10 using while loop */#includevoid main() {   int value = 1;   while (value <= 10) {      printf("%d\t", value);      value++;   }   printf("\n");   return;}

以下是运行两个程序的程序(一个程序来自子程序,另一个程序来自父程序).

/*文件名:execl_run_two_prgms.c */

#include#includevoid 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 ProgramThis wouldn't printParent process: Running While loop ProgramWon'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#include#includevoid 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 */

#includevoid main() {   printf("Hello World\n");   return;}

以下是从程序的父进程execl_run_two_prgms.c调用的while_loop.c程序.该程序的参数从运行它的程序传递,即execl_run_two_prgms.c.

/*文件名:while_loop.c */

#includevoid 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 10num_times_str is 10Child process: Running Hello World ProgramHello WorldParent process: Running While loop ProgramArgv[1] is 101 2 3 4 5 6 7 8 9 10Taken loop maximum as 15num_times_str is 15Child process: Running Hello World ProgramHello WorldParent process: Running While loop ProgramArgv[1] is 151 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 [])和其他参数.