System函数

来源:互联网 发布:淘宝拒收快递运费谁出 编辑:程序博客网 时间:2024/06/05 12:08

system函数---可使用system在系统中调用系统提供的各种命令。

system的实现是通过调用fork/exec/waitpid等来实现的。

函数声明:

#include<stdlib.h>

int system(const char *cmdstring);

cmdstring---字符串指针,用于指向表示命令行的字符串。

调用fork函数失败,返回值为-1;

调用exec失败,返回值为shell操作的返回值(表示shell无法执行设定的命令);

调用waitpid失败,返回值为-1。


system函数示例(函数的不同返回情况)

#incldue<sys/types.h>

#include<stdlib.h>

#include<stdio.h>

#include<sys/wait.h>

int main(void)

{

int status;

if(status=system(NULL)<0)

{

printf("system error!\n");

exit(0);

}

printf("exit status=%d\n",status);

if((status=system("date"))<0)

{

printf("system error!\n");

exit(0);

}

if((status=system("invalidcommand"))<0)

{

printf("system error!\n");

exit(0);

}

printf("exit status=%d.\n".status);

if((status=system("eho;exit 33"))<0)

{

printf("system error!\n");

exit(0);

}

printf("exit status=%d.\n",status);

exit(0);

}

0 0
原创粉丝点击