linux C编程中怎样调用shell命令

如题所述

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char const *argv[])
{
pid_t pid;
while(1){
printf("\n0---kill the work\n");
printf("1---use ls / \n");
int choose = 0;
scanf("%d",&choose);
switch(choose)
{
case 1:
//把当前进程复制一份来创建一个新进程
pid = fork();
//如果pid==0,说明是新创建的进程
if(pid==0)
{
close(0);
//用下面的命令替换近程的执行内容
execlp("ls","ls","/",NULL);
       exit(0);
}
break;
case 0:
if(pid!=0)
kill(pid,9);
return -1;
default:
printf("NO\n");

}
}
return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答