ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux 2.进程(exec族函数 和 fork、system、popen函数)

2021-09-09 12:58:51  阅读:236  来源: 互联网

标签:fork 函数 exec int system popen char printf include


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 相比:

  1. system 更加简单粗暴。
  2. 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有