system与popen对比
system与popen对比:
popen和system都可以执行外部命令。
popen相当于是先创建一个管道,fork,关闭管道的一端,执行exec,返回一个标准的io文件指针。system相当于是先后调用了fork, exec,wait来执行外部命令。
system
int system(const char *command);
参数:
command:命令
返回值:
错误返回-1
popen,plose
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
参数:
command:命令
type:
r:只读
w:只写
返回值:
错误返回-1
demo1:
1.创建文件,写入hello word
2.用system执行cat文件
#include <stdio.h>
#include <stdlib.h>void result()
{int ret;ret = system("cat ./file");if(ret == -1){printf("command error\n");perror("why");exit(-1);}
}int main()
{result();return 0;
}
结果示例:
demo2:
1.创建文件,写入hello word
2.用popen执行cat文件
#include <stdio.h>
#include <stdlib.h>void result()
{FILE *fp;int fp_read;char context[20] = {0}; fp = popen("cat ./file","r");if(fp == NULL){printf("command error\n");perror("why");exit(-1);}fp_read = fread(context,20,1,fp);printf("file size is %d ,file context is %s\n",fp_read,context);pclose(fp);
}int main()
{result();return 0;
}
demo3:
1.创建文件
2.用popen执行输入hello word到文件中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void result()
{FILE *fp;int fp_write;char write_buf[] = "hello word\n"; fp = popen("cat > ./file","w");if(fp == NULL){printf("command error\n");perror("why");exit(-1);}fp_write = fwrite(write_buf,strlen(write_buf),1,fp);pclose(fp);
}int main()
{result();return 0;
}
结果示例:
system与popen对比相关推荐
- Linux的system和popen的差异
1.system()和popen()简介 在linux中我们可以通过system()来执行一个shell命令,popen()也是执行shell命令并且通过管道和shell命令进行通信. system( ...
- Linux进程编程(PS: exec族函数、system、popen函数)
目录 1.进程相关概念 程序和进程 查看系统中的进程 ps指令 top指令 进程标识符 使用getpid()获取 父进程,子进程 2.创建进程fork 进程创建发生了什么--C程序的存储空间如何分配 ...
- Linux的system()和popen()差异
Linux的system()和popen()差异 1. system()和popen()简介 在Linux中我们可以通过system()来执行一个shell命令,popen()也是执行shell命令并 ...
- python os.system 512_python os.system os.popen 区别
最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或者 ...
- Python3.X subprocess、os.system/os.popen/os.spawn*、commands:与系统进行交互,如测试时常用的adb命令
如需转载请注明出处. win10 64位.Python 3.6.3.Sublime Text 3. 一.首先明白一些概念: 1.操作系统(OS,operating system). 是管理和控制计算机 ...
- python os.system os.popen 区别
python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后 者的返回值是脚本执行过程中的输出内容.实际使用时视需求情况而选择 ...
- Linux API-进程:getpid、getppid、exit、wait、fork、vfork、execl、execv、system、popen
函数说明 一.getpid--取得进程识别码 二.getppid--取得父进程的进程识别码 三.exit--正常结束进程 四.wait--等待子进程中断或结束 五.fork--建立一个新子进程 六.v ...
- os.system() 和 os.popen()
1.os.popen(command[, mode[, bufsize]]) os.system(command) 2.os.popen() 功能强于os.system() , os.popen() ...
- python os system用法_Python调用系统命令os.system()和os.popen()的实现
作为一门脚本语言,写脚本时执行系统命令可以说很常见了,python提供了相关的模块和方法. os模块提供了访问操作系统服务的功能,由于涉及到操作系统,它包含的内容比较多,这里只说system和pope ...
最新文章
- 用Ghost几秒钟快速格式化120G大硬盘
- 静态时序分析的约束命令
- java工单自动化流程控件,一种工单审批的自动化处理方法及系统与流程
- 《spring揭秘》读书笔记三
- Juniper NetScreen 基于源NAT转换
- 易语言关闭指定窗口_易语言取外部程序指定窗口位置源码 _易语言源码网_易语言资源网...
- vue的 v-for 循环中图片加载路径问题
- 用strings命令查看kafka-log内容 过滤二进制编码
- Heron 数据模型,API和组件介绍
- oracle rds 运维服务_从Oracle一条新闻说起,为什么我们需要更好更开放的RDS服务?...
- 上海电力学院linux大作业,上海电力电路大作业2重点.docx
- 怎么在Guitar Pro乐谱中加入哇音
- 周题:UVa10736题。Foreign Exchange
- html页面到服务器上乱码,网页乱码问题
- Altium Designer中mm/mil单位切换
- 解决局域网共享文件时提示“没有权限访问,请与网络管理员联系请求访问权限“
- java操作word转换pdf加水印
- Easyui之datagrid修改
- win10更新后应用无法连接服务器,win10更新无法连接到更新服务怎么办_win10无法连接到更新服务的解决方法...
- windows计算机未从dhcp,windows10系统提示未启用dhcp是怎么回事
热门文章
- SQL经典50题(MySQL版)
- 新建oracle数据库定时任务,创建 Oracle 定时任务
- Qin Shi Huang's National Road System( 次小生成树 )
- FastReport.Net 版本2019.3.5 免安装--连接数据源
- 微信小程序识别图片并提取文字_微信小程序(图像识别,文字识别)
- Android使用移动智能终端补充设备标识获取OAID
- kali工具熟悉——路由器分析
- windows与linux系统ntp服务器配置
- OSChina 周三乱弹 —— 您的好友,苏菲利普亲王已经上线。
- 利用ArcGIS 的Spatial Join统计斑块中的点数量