system()函数,作用和execl一样
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int data = 10;
while(1){
printf("please input a data:\n");
scanf("%d",&data);
if(data==1){
int fdSrc;
pid = fork();
if(pid>0){
wait(NULL);
}
if(pid==0){
// execl("./changeData","changeData","congfig.txt",NULL);
system("./changeData congfig.txt");
exit(0);
}
}
else{
printf("wait,do nothing\n");
}
}
return 0;
}
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//int execl(const char *path,const char *arg ...);
int main()
{
printf("before execl\n");
if(system("ps") == -1){ //system代替execl函数
printf("execl failed!\n");
perror("why");
}
printf("after execl!\n");
return 0;
}
使用popen函数调用ps指令,将内容存到ret中并且打印,popen函数能将运行结果返回
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
FILE *fd;
char ret[1024]={0};
fd = popen("ps","r");
int nread = fread(ret,1,1024,fd);
printf("read ret %d byte,ret = %s\n",nread,ret);
return 0;
}