linux下一切设备皆文件,对于一些驱动函数的操作,无外乎open、read、write、ioctl、close;日常熟悉linux内核驱动模型,调用驱动函数接口,可以避免编程中多数情况下直接调用shell命令;
对于一些想实现的功能与现有linux shell命令功能相同的情况下,编程调用shell命令无外乎是最简便快速的方法了;
下面附上Linux的system()和popen()差异_liuxingen的专栏-CSDN博客_popen和system区别
总结:
1、在linux中我们可以通过system()来执行一个shell命令,popen()也是执行shell命令并且通过管道和shell命令进行通信。
2、system()、popen()给我们处理了fork、exec、waitpid等一系列的处理流程,让我们只需要关注最后的返回结果(函数的返回值)即可;
3、system()是串行执行的,popen()是并行执行的;
4、在特权(setuid、setgid)进程中千万注意不要使用system和popen。
为什么建议使用posix_spawn?
工作中发现system调用某些shell指令会失败,拿udhcpc为例;
这个问题的表面现象是用system调用的方式执行udhcpc会失败。
解释:由于system是通过fork实现的,而子进程会复制父进程的VM空间,当父进程占用较多VM空间,很容易导致system调用失败。其本质是子进程分配VM空间失败导致的。
解决方法:执行:echo 1 > /proc/sys/vm/overcommit_memory即可。
更好的解决办法是不使用system调用方法,而是使用posix_spawn调用,简单的示例如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
pid_t pid;
int err;
char *spawnedArgs[] = {"/bin/ls","-l","/home ",NULL};/* posix_spawn需要指定子进程的命令的全路径(绝对路径) */
char *spawnedEnv[] = {NULL};
printf("Parent process id=%ld\n", getpid());
if( (err=posix_spawn(&pid, spawnedArgs[0], NULL, NULL, spawnedArgs, spawnedEnv)) !=0 )
{
fprintf(stderr,"posix_spawn() error=%d\n",err), exit(-1);
}
printf("Child process id=%ld\n", pid);
/* Wait for the spawned process to exit */
(void)wait(NULL);
return 0;
}
posix_spawn的更多用法,请自行上网搜索。