本文共 6357 字,大约阅读时间需要 21 分钟。
#include// 运行以字符串参数的形式传递给它的命令并等待该命令的完成。// 命令的执行情况如同在 shell 中执行如下的命令// $ sh -c stringint system(const char *string);
/* test1.c */#include#include void main(){ int res; printf("Running test2 with system\n"); // 启动 test2 并等待 test2 执行结束 res = system("./test2"); printf("Done. res=%d\n", res);}
test2 代码如下:
/* test2.c,睡眠 3 秒,模拟耗时操作 */#include#include #include int main(){ printf("test2 running\n"); sleep(3); printf("test2 done.\n"); exit(0);}
运行 test1,执行结果如下(可以看到 test1 等待 test2 执行返回后,才继续执行):
ubuntu@cuname:~/dev/beginning-linux-programming/test$ gcc -o test1 test1.cubuntu@cuname:~/dev/beginning-linux-programming/test$ gcc -o test2 test2.cubuntu@cuname:~/dev/beginning-linux-programming/test$ ./test1Running test2 with system // test1 运行test2 running // test2 开始执行test2 done. // test2 执行终止Done. res=0 // test1 运行终止
查看进程信息,查看‘COMMAND’列,可以发现 test2 是通过 命令 ‘sh -c ./test2’ 启动。这里有3个进程,它们依次启动,pid 数值依次递增:
ubuntu@cuname:~$ ps uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDubuntu 9204 0.0 0.0 4352 636 pts/18 S+ 15:58 0:00 ./test1ubuntu 9205 0.0 0.0 4504 752 pts/18 S+ 15:58 0:00 sh -c ./test2ubuntu 9206 0.0 0.0 4352 624 pts/18 S+ 15:58 0:00 ./test2
当然也可以让 shell 在后台执行 test2 程序,使得 system 函数在 shell 命令结束后立即返回,将 test1 程序中
res = system("./test2");
修改为
res = system("./test2 &");
再次运行 test1,执行结果如下(可以看到 test1 先执行完毕,随后 test2 才执行结束):
ubuntu@cuname:~/dev/beginning-linux-programming/test$ gcc -o test1 test1.cubuntu@cuname:~/dev/beginning-linux-programming/test$ ./test1Running test2 with systemDone. res=0 // test1 执行结束test2 runningubuntu@cuname:~/dev/beginning-linux-programming/test$ test2 done. // test2 执行结束
#include// 其参数个数可变,且以一个空指针结束int execlp(const char *file, const char *arg0, ..., (char *)0);
/* test1.c */#include#include #include void main(){ int res; printf("Running test2 with exec\n"); execlp("./test2", "aa", "bb", "cc", (char *)0); printf("Done. res=%d\n", res); //}
test2 代码如下:
/* test2.c,首先打印 main 函数接收到的参数 */#include#include #include int main(int argc, char *argv[]){ printf("test2 running\n"); // 打印参数 if(argc > 1){ for(int i = 0;i < argc;i++){ printf("arg: %s\n", argv[i]); } } // 模拟耗时操作 sleep(5); printf("test2 done.\n"); exit(0);}
运行 test1,执行结果如下(可以看到 test1 的最后一个 printf 语句并没有被执行,而 test2 正确执行完毕):
ubuntu@cuname:~/dev/beginning-linux-programming/test$ gcc -o test1 test1.cubuntu@cuname:~/dev/beginning-linux-programming/test$ gcc -o test2 test2.cubuntu@cuname:~/dev/beginning-linux-programming/test$ ./test1Running test2 with exec // test1 开始运行test2 running // test2 的代码开始被执行arg: aaarg: bbarg: cctest2 done. // test2 的代码执行结束,进程终止
test1 执行期间,查看进程信息(其中并没有 test1 进程,而只有 test2 进程如下):
ubuntu@cuname:~$ ps uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDubuntu 9389 0.0 0.0 4352 636 pts/18 S+ 16:40 0:00 aa bb cc
原因:在 test1 中调用 exec 函数后,运行中的程序开始执行 exec 调用中指定的新的可执行文件的代码(即:test2),程序并没有再返回到 test1 程序中。新的进程的pid / ppid 和 nice 值与原先的完全一样,只是执行代码做了替换。
#include#include pid_t fork(void);
一个典型的使用 fork 的代码片段如下:
pid_t new_pid;new_pid = fork();switch(new_pid){ case -1: // error break;case 0; // 子进程 break;default: // 父进程 break;}
/* test1.c */#include#include #include #include void main(){ pid_t pid; printf("parent process: Running test2 with fork/exec\n"); pid = fork(); switch (pid){ case -1: // error printf("error: fork failed.\n"); break; case 0: // 子进程 // 将子进程映像替换为 test2 程序 execlp("./test2", "aa", "bb", "cc", (char *)0); // 子进程终止,下面的 'break;' 语句不会被子进程执行, // 因为子进程的执行代码已经被 test2 程序代码所替换, // 且 test2 执行完毕后,不会返回到原程序中。 break; default: // 父进程 sleep(5); break; } printf("parent process over.\n");}
test2 程序代码如下:
/* test2.c */#include#include #include int main(int argc, char *argv[]){ printf("test2 running\n"); if(argc > 1){ for(int i = 0;i < argc;i++){ printf("arg: %s\n", argv[i]); } } sleep(5); printf("test2 done.\n"); exit(0);}
执行 test1,运行结果如下:
ubuntu@cuname:~/dev/beginning-linux-programming/test$ ./test1parent process: Running test2 with fork/exectest2 running // 子进程启动并替换映像为 test2 程序arg: aaarg: bbarg: ccparent process over. // 父进程终止test2 done. // 子进程终止
test1 执行期间,进程信息如下(可以看到 父子进程分别为 test1 和 test2,不同于 system 函数,没有使用 shell 进程创建新进程):
ubuntu@cuname:~$ ps uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDubuntu 9617 0.0 0.0 4352 784 pts/18 S+ 17:07 0:00 ./test1ubuntu 9618 0.0 0.0 4352 624 pts/18 S+ 17:07 0:00 aa bb cc
#include#include #include #include void main(){ printf("1 running.\n"); fork(); fork(); printf("222\n"); sleep(6);}
程序执行流程如下图:
输出结果为(printf 语句执行 4次):ubuntu@cuname:~/dev/beginning-linux-programming/test$ gcc -o test3 test3.cubuntu@cuname:~/dev/beginning-linux-programming/test$ ./test31 running.222222222222
转载地址:http://bclsi.baihongyu.com/