linux--进程--system与popen函数

news/2024/10/21 10:08:51/

1.system

#include <stdlib.h>int system(const char *command);

返回值:

        成功,则返回进程的状态值;不能源码execl函数,返回127;失败返回-1;

        不能成功运行分析文章:linux下system函数详解_linux system_遥_望的博客-CSDN博客

在linux系统下,system函数是execl函数的封装版

popen()函数较于system()函数的优势在于使用简单,popen()函数只返回两个值:成功 /失败

源码:

#include
#include
#include
#includeint system(const char * cmdstring)
{pid_t pid;int status;if(cmdstring == NULL){return (1);}if((pid = fork())<0){status = -1;}else if(pid == 0){execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);-exit(127); //子进程正常执行则不会执行此语句}else{while(waitpid(pid, &status, 0) < 0){if(errno != EINTER){status = -1;break;}}}return status;
}

在linux系统下,system函数是execl函数的封装版
文中的 "sh -c ps"和我们所使用的"ps"是完全等价的

例子代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{system("ps");printf("\n");
}

直接运行ps指令;

运行文件:

#include <stdio.h>
int main(int argc,char *argv[])
{int i;for(i=0;i<argc;i++){printf("argv[%d]:%s\n",i,argv[i]);}return 0;
}

system运行:

#include <stdio.h>
#include <stdlib.h>
int main()
{system("./test aa bb cc dd");printf("\n");
} 

结果:

    argv[0]:./test  //程序地址
    argv[1]:aa //以下为程序参数
    argv[2]:bb
    argv[3]:cc
    argv[4]:dd

还可以运行子进程中的其他程序:

在Linux文件编程中

写一个TEST.config文件:

SPEED=5
LENG=1
SCORE=90
LEVEL=95

对TEST.config文件内容进行修改,将LENG=1,改成LENG=5

代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main(int argc,char **argv)
{int fdsrc;char *readbuf=NULL;if(argc!=2){printf("pararm error\n");exit(-1);//tuichugaichengxu}//打开文件,将文件复制到readbuf中fdsrc=open(argv[1],O_RDWR);int size=lseek(fdsrc,0,SEEK_END);lseek(fdsrc,0,SEEK_SET);readbuf=(char *)malloc(sizeof(char)*size+8);int n_read=read(fdsrc,readbuf,size);//找到readbuf中LENG=中的位置,将位置移动到1,替换为5char *p=strstr(readbuf,"LENG=");if(p==NULL){printf("not found\n");exit(-1);}p=p+strlen("LENG=");*p='5';//移动光标到文件头,重新将readbuf中的内容写入到打开的文件中lseek(fdsrc,0,SEEK_SET);int n_write=write(fdsrc,readbuf,strlen(readbuf));close(fdsrc);return 0;
}


编译:gcc demo13.c

运行:./a.out TEST.config

运行结果为:

SPEED=5
LENG=5
SCORE=90
LEVEL=95

system运用:

将上述代码编译为./changedata

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>int main()
{pid_t pid;int data=10;while(1){printf("please input a data\n");scanf("%d",&data);if(data==1){pid=fork();if(pid>0){wait(NULL);}if(pid==0){
//				execl("./changedata","changedata","config.txt",NULL);system("./changedata config.txt");exit(0);}}else{printf("wait,do nothing\n");	}}return 0;
}

cat config.txt

能够发现LENG=1变成了LENG=5

要注意的是,system运行完后,父进程还会继续向下运行,这点与execl函数不同。

2.popen函数

popen的使用:

#include <stdio.h>
 
FILE *popen(const chat *command, const char *type);
int pclose(FILE *stream);

 command:是一个指向以NULL结束的shell命令字符串的指针。

type:只能是读或写的其中一种r/w

无法获得system的值,需要使用popen
 

代码:

#include <stdio.h>int main()
{char ret[500]={0};FILE *fp;fp = popen("ps","r");int n_read = fread(ret,1,1024,fp);printf("read ret = %d byte\n ret =\n %s\n",n_read,ret);return 0;
}

结果:

当使用system时,ret的值无法读出,用popen函数;


http://www.ppmy.cn/news/1097808.html

相关文章

MCAL实战二(S32K324-NXP EB tresos GPT驱动配置详解)

目录 前言 一、配置之前 第一步 找时钟源 第二步 配置MCU时钟 二、开始配置 第一步 新建时钟参考点 第二步 硬件通道使能 第三步 配置连接 <

深度学习Pytorch常用api详解记录

深度学习常用的torch函数 torch.cat()torch.Tensor.repeat()&#xff1a;持续更新中... torch.cat() 对象&#xff1a;给定的序列化张量&#xff0c;即Tensor型。 功能&#xff1a;实现两个张量在指定维度上的拼接。 输出&#xff1a;拼接后的张量。 函数以及参数&#xff1a;…

【Python程序设计】 工厂模式【07/8】

一、说明 我们探索数据工程中使用的设计模式 - 软件设计中常见问题的可重用解决方案。 以下文章是有关 Python 数据工程系列文章的一部分&#xff0c;旨在帮助数据工程师、数据科学家、数据分析师、机器学习工程师或其他刚接触 Python 的人掌握基础知识。 迄今为止&#xff0c;…

【CMake工具】工具CMake编译轻度使用(C/C++)

目录 CMake编译工具 一、CMake概述 二、CMake的使用 2.1 注释 2.1.1 注释行 2.1.2 注释块 2.2 源文件 2.1.1 共处一室 2.1.2 VIP包房 2.3 私人定制 2.2.1 定义变量 2.2.2 指定使用的C标准 2.2.3 指定输出的路径 2.4 搜索文件 2.3.1 方式1 2.3.2 方式2 2.5 包含…

【Spring面试】BeanFactory与IoC容器的加载

文章目录 Q1、BeanFactory的作用是什么&#xff1f;Q2、BeanDefinition的作用是什么&#xff1f;Q3、BeanFactory和ApplicationContext有什么区别&#xff1f;Q4、BeanFactory和FactoryBean有什么区别&#xff1f;Q5、说下Spring IoC容器的加载过程&#xff08;※&#xff09;Q…

Unity的UI面板基类

使用这个组件实现淡入淡出 public abstract class BasePanel : MonoBehaviour {//控制面板透明度 用于淡入淡出private CanvasGroup canvasGroup;//淡入淡出速度private float alphaSpeed 10;//隐藏还是显示public bool isShow false;//隐藏完毕后做的事private UnityAction …

27.方向标

题目 描述 一位木匠收到了一个木制指示牌的订单。每块木板必须与前一块垂直对齐&#xff0c;要么与前一个箭头的基部对齐&#xff0c;要么与相反的一侧对齐&#xff0c;在那里用特制的螺钉固定。两块木板必须重叠。木匠将设计师发送的草图编码成了一个整数序列&#xff0c;但…

Java基础09 —— 字符序列--String、StringBuilder、StringBuffer区别及其方法介绍

Java基础09 —— 字符序列 字符串类型 字符与字符串 字符类型(char)是Java中的基本数据类型&#xff0c;占2个字节16位&#xff0c;默认值是 ‘\u0000’ 。字符是用单引号引住的单个符号. // 字符 char c A; //单引号 char cA 65; //数字 char c1 \u8888; //Unicode码 S…