Linux 2.进程(exec族函数 和 fork、system、popen函数)
作者:互联网
Linux 2.进程(exec族函数 和 fork、system、popen函数)
exec 族函数 和 fork 函数的结合
做一个类似有客户端连接了服务器,服务端创建一个子进程,然后去修改 test.config 文件中的 LENGTH 的值把它改成 8 。
修改文件值主要代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc,char** argv)
{
if(argc!=2)
{
printf("The param is not good!\n");
exit(-1);
}
int fdSrc;
char* readBuf=NULL;
fdSrc=open(argv[1],O_RDWR);
int size=lseek(fdSrc,0,SEEK_END);
printf("size=%d\n",size);
lseek(fdSrc,0,SEEK_SET);
readBuf=(char*)malloc(sizeof(char)*size+8);
memset(readBuf,0,sizeof(readBuf));
int n_read=read(fdSrc,readBuf,size);
printf("----------------------n_read=%d-------------------------\n",n_read);
char* p=strstr(readBuf,"LENGTH=");
if(p==NULL)
{
printf("not found\n");
exit(-1);
}
p=p+strlen("LENGTH=");
*p='8';
lseek(fdSrc,0,SEEK_SET);
write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}
利用 exec 和 fork 组合
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
pid_t retpid;
int data=0;
while(1)
{
printf("please input a num:\n");
scanf("%d",&data);
if(data==1)
{
retpid=fork();
if(retpid>0)
{
wait(NULL);
}
else if(retpid==0)
{
while(1)
{
if(execl("./xiuGaiPeiZhiWenJian.out","xiuGaiPeiZhiWenJian.out","test.config",NULL)==-1)
//xiuGaiPeiZhiWenJian.out test.config
{
perror("why");
}
printf("do request......retpid=%d\n",getpid());
sleep(3);
}
}
else
{
printf("connect fail!\n");
exit(-1);
}
}
else
{
printf("do nothing......\n");
}
}
return 0;
}
运行结果:
成功把配置文件 test.config 中的 LENGTH=6 改为 LENGTH=8 。
注意:exec 运行成功后会去运行 xiuGaiPeiZhiWenJian.out 这个程序,不会再往下面的代码执行了。
system 函数
system 函数作用
system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。
system 函数头文件及原型
#include<stdlib.h>
int system(const char *command)
system 函数返回值
1. 成功则返回进程的状态值
2. 当 sh 不能执行时。返回127
3. 其他原因失败返回-1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
printf("before execl\n");
if(system("date")==-1||system("date")==-127)
{
printf("execl fail!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
运行结果:
system 和 exec 相比:
- system 更加简单粗暴。
- system 执行后还会继续往原程序走下去。而 exec 是不会的。
popen 函数
popen 作用
popen() 会调用 fork() 产生子进程,然后从子进程中调用 /bin/sh -c 来执行参数command 的指令。
popen 头文件及原型
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
popen 参数
command:是一个指向以NULL结束的shell 命令字符串的指针。这行命令将传到bin/sh 并 使用 -c 标志,shell将执行这个命令。
type:
"r":文件指针连接到 command 的标准输出
"w":文件指针连接到 command 的标准输入
popen 相对比system 好处:
popen 可以获取运行的输出结果。
比如像用 system 打开 “ps” 命令 我们想把 ps 命令显示出的内容存放到一个文件叫 file 里面去。
popen 示例
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
FILE* fp;
char ret[1024]={'\0'};
fp=popen("ps","r");
int n_fread=fread(ret,1,1024,fp);
printf("%s\n",ret);
int fd=open("file",O_RDWR|O_CREAT,0600);
// int open(const char *pathname, int flags);
int n_write=write(fd,ret,sizeof(char)*1024);
printf("n_write=%d\n",n_write);
pclose(fp);
close(fd);
return 0;
}
运行结果:
标签:fork,函数,exec,int,system,popen,char,printf,include 来源: https://blog.csdn.net/weixin_46105931/article/details/120191228