系统调用函数system()的作用相当于执行了一次fork()和exec()来获取运行shell,再由这个shell运行指定的命令.
在程序中调用函数system(),可以像在命令行中一样,执行同样的命令.
/* system.c */
#include <unistd.h>
int main(int argc, char *argv[])
{
int pid1;
int pid2;
pid1 = system("grep print *.c \n");
pid2 = system("grep include *.c > inclist &\n");
wait(pid2);
return 0;
}
说明:
第一个system()将grep命令在前台运行,并在标准输出上显示其结果;如同一般情况下所做的那样;
第二个system()调用使用了输出重定向,并使用&符号将命令作为子进程在后台运行.system()调用将等待直到命令完成,除非它是以后台方式运行.