鍍金池/ 教程/ Linux/ 覆蓋進程映像
命名管道
消息隊列
進程創(chuàng)建與終止
信號量
進程組,會話和作業(yè)控制
共享內(nèi)存
進程間通信簡介
子進程監(jiān)視
其他進程
覆蓋進程映像
進程信息
進程映像
內(nèi)存映射
相關(guān)系統(tǒng)調(diào)用(System V)
進程資源
System V & Posix
信號
進程間通信教程
管道

覆蓋進程映像

假設(shè)我們正在運行一個程序,想從當前程序運行另一個程序。 這可能嗎? 如果我們實現(xiàn)覆蓋進程映像的概念。 當前正在運行的程序呢,也可以運行的。 當前的程序與新程序疊加,如果想運行兩個程序,而不會丟失當前正在運行的程序,有可能嗎?這是可能做到的。

創(chuàng)建一個子進程,以便有一個父進程和一個新創(chuàng)建的子進程。 我們已經(jīng)在父進程中運行當前程序,所以在子進程中運行新創(chuàng)建的進程。 這樣可以從當前程序運行另一個程序。 不僅是一個程序,而且可以通過創(chuàng)建許多子進程來運行當前程序中的任意數(shù)量的程序。

看看以下面的程序示例。文件: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程序?qū)⒏采wexecl_test的進程映像。 這就是執(zhí)行execl_test(printf())的過程映像代碼的原因。

編譯和執(zhí)行的結(jié)果如下 -

Hello World

現(xiàn)在,我們將從一個程序運行以下兩個程序,即execl_run_two_prgms.c。

  • Hello World程序(helloworld.c)
  • while循環(huán)程序從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;
}

- 放置sleep()調(diào)用以確保子進程和父進程順序運行(不重疊結(jié)果)。

執(zhí)行上面示例代碼,得到以下結(jié)果 -

Child process: Running Hello World Program
This wouldn't print
Parent process: Running While loop Program
Won't reach here

現(xiàn)在從一個程序運行兩個程序,即execl_run_two_prgms.c,與上面相同的程序,但帶有命令行參數(shù)。 所以運行了兩個程序,即子進程中的helloworld.c和父進程中的while_loop.c程序。 這是如下 -

  • Hello World程序(helloworld.c)
  • while循環(huán)程序根據(jù)命令行參數(shù)(while_loop.c)從1打印到num_times_str次數(shù)。

這個程序大致執(zhí)行以下操作 -

  • 創(chuàng)建一個子進程
  • 子進程執(zhí)行helloworld.c程序
  • 父進程執(zhí)行while_loop.c程序?qū)⒚钚袇?shù)值作為參數(shù)傳遞給程序。 如果命令行參數(shù)沒有被傳遞,那么默認值為10。否則,它取得給定的參數(shù)值。 參數(shù)值應(yīng)該是數(shù)字; 代碼不會驗證。

文件: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程序的子進程調(diào)用helloworld.c程序。

文件:helloworld.c -

#include<stdio.h>

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

以下是從execl_run_two_prgms.c程序的父進程調(diào)用的while_loop.c程序。 這個程序的參數(shù)是從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;
}

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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

現(xiàn)在讓看看覆蓋映像相關(guān)的庫函數(shù)。

#include<unistd.h>

int execl(const char *path, const char *arg, ...);

這個函數(shù)會覆蓋當前正在運行的進程映像和參數(shù)patharg中提到的新進程。 如果任何參數(shù)需要傳遞給新的進程映像,那么將通過“arg”參數(shù)發(fā)送,最后一個參數(shù)應(yīng)該是NULL。

這個函數(shù)只會在錯誤的情況下返回一個值。 覆蓋圖像相關(guān)調(diào)用的過程如下所述 -

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[]);

這些調(diào)用將處理傳遞命令行參數(shù)(argv []),環(huán)境變量(envp [])和其他參數(shù)。


上一篇:System V &amp; Posix下一篇:其他進程