13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define BUFSIZE 200
int main()
{
FILE *read_fp;
char buffer[BUFSIZE+];/*用于存放读取的内容*/
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp=popen("uname -a","r");//用于显示内核版本 及现在时间
/*返回的是文件指针,因此-文件指针是要定义的*/
if(read_fp != NULL)
{
chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);//返回读取字符的个数,错误则为-1
/*并读取传回的内容是什么*/
if(chars_read > )
{
printf("Output was:-\n%s\n",buffer);
} pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
/*因些,使用popen到少要定义:
文件指针,
字符串
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define BUFSIZE 50
int main()
{
FILE *read_fp;
char buffer[BUFSIZE+];/*用于存放读取的内容*/
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp=popen("ps ax","r");//用于显示内核版本 及现在时间
/*返回的是文件指针,因此-文件指针是要定义的*/
if(read_fp != NULL)
{
while(chars_read > )
{ //返回读取字符的个数,错误则为-1
/*并读取传回的内容是什么*/
chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);
buffer[chars_read-]='\0';
printf("Reading %d:-\n %s \n",BUFSIZE,buffer); }
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
return ;
}
/*因些,使用popen到少要定义:
文件指针,
字符串
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define BUFSIZE 50
int main()
{
FILE *read_fp;
char buffer[BUFSIZE+];/*用于存放读取的内容*/
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp=popen("cat 13-3-*.cpp | wc -l","r");//用于显示内核版本 及现在时间
/*返回的是文件指针,因此-文件指针是要定义的*/
if(read_fp != NULL)
{
while(chars_read > )
{ //返回读取字符的个数,错误则为-1
/*并读取传回的内容是什么*/
chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);
buffer[chars_read-]='\0';
printf("Reading %d:-\n %s \n",BUFSIZE,buffer); }
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
return ;
}
/*因些,使用popen到少要定义:
文件指针,
字符串
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h> #define Buffer 100 int main()
{
FILE *write_fp;
char buffer[Buffer + ];
//把格式化的数据写入某个字符串缓冲区。
//返回值:字符串长度(strlen)
sprintf(buffer,"Once upon a time, there was...\n");
//********格式化输出文件中的数据***********///
//-c:等价于-t c,选择ASCII码字符或者是转义字符
write_fp=popen("od -c","w");
if(write_fp != NULL)
{
fwrite(buffer,sizeof(char),strlen(buffer),write_fp);
pclose(write_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> int main()
{
int data_processed;
int file_pies[];
const char some_data[]="";
char buffer[BUFSIZ+]; memset(buffer,'\0',sizeof(buffer)); if(pipe(file_pies)==)
{
data_processed=write(file_pies[],some_data,strlen(some_data));
printf("Wrote %d bytes\n",data_processed);
data_processed=read(file_pies[],buffer,BUFSIZ);
printf("Read %d bytes: %s\n",data_processed,buffer);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例的更多相关文章
- Python和其他进程的管道通信方式--popen和popen2的比较
目前有一个查询程序 get_user_id 是用C写的,python需要调用这个程序:使用 get_user_id "用户名" 可以得到输出: "ID0002451&q ...
- Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...
- 管道与popen函数与重定向
转自:http://www.tldp.org/LDP/lpg/node12.html Pipes the Easy Way! LIBRARY FUNCTION: popen(); PROTOTYPE: ...
- 《Unix 网络编程》13:守护进程和 inetd 超级服务器
守护进程和 inetd 超级服务器 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
- APUE读书笔记-第13章-守护进程
第13章 守护进程 13.1 引言 *守护进程也称精灵进程(daemon)是生存期较长的一种进程.它们常常在系统自举时启动,仅在系统关闭时才终止.因为它们没有控制终端,所以说它们是在后台运行的.UNI ...
- 13.2、进程的通信:Queue、Pipe、
内容相关: 概念:进程的通信 Queue:创建与使用 Pipe:创建与使用 进程通信的概念 进程的资源空间是相互独立的,一般而言是不能相互访问的.但很多情况下进程间需要互相通信,来完成系统的某项功能. ...
- UNP第13章——守护进程
1. 守护进程的启动方法 (1)系统初始化脚本启动,在系统启动阶段,按照如/etc目录或/etc/rc开头的目录中的某些脚本启动,这些守护进程一开始就有超级用户权限.如inetd,cron,Web服务 ...
- SmartSql = Dapper + MyBatis + Cache(Memory | Redis) + ZooKeeper + R/W Splitting + ......
SmartSql Why 拥抱 跨平台 DotNet Core,是时候了. 高性能.高生产力,超轻量级的ORM.156kb (Dapper:168kb) So SmartSql TargetFrame ...
- os.popen('python hello_out.py')中Python程序执行时默认的当前路径为MS-DOS CMD的默认路径
>>> import os >>> os.getcwd() 'D:\\pythonCode\\pp4e' >>> os.chdir('Stream ...
随机推荐
- MouseOver/MouseOut vs MouseEnter/MouseLeave
参考 http://www.oschina.net/question/234345_45280 这是jQuery提供的函数 要注意MouseOut 和 MouseLeave的区别 比如对元素A绑定Mo ...
- c++ 实现将数字转换为中文数字输出
实现如下函数: void printInChinese(int num); 这个函数输入一个小于100000000(一亿)的正整数,并在屏幕上打印这个数字的中文写法. 例如: 17 -> 一十七 ...
- ORA-00845 Oracle 启不来修改 MEMORY_TARGET
1.内存减小导致Oracle启动不了 Last login: Sun Nov 4 15:09:06 2012 from 192.168.5.222 [oracle@h1 ~]$ sqlplus ...
- nginx日志每日定时写入Hdfs
#!/bin/bash hadoop_home=/opt/hadoop-2.4.0 tw_nginx_log_file=/home/chiline.com.all/access_com_tw.log ...
- [转]组合数取模 Lucas定理
对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...
- Python高级之Socket 探索(五)
目录: 面向对象 反射 socket 一.面向对象 方法 方法包括:普通方法.静态方法和类方法,三种方法在内存中都归属于类,区别在于调用方式不同. 普通方法:由对象调用:至少一个self参数:执行普通 ...
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- IEEE浮点数float、double的存储结构
众所周知,C的float.VB的Single都是32位浮点数变量类型(也叫单精度浮点数),C的double和VB的Double则都是64位的浮点数变量类型(也叫双精度浮点数).有些编译器还支持更屌的l ...
- python 拼写检查代码(怎样写一个拼写检查器)
原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...
- northern truck 是什么牌子?具体_百度知道
northern truck 是什么牌子?具体_百度知道 northern truck 是什么牌子?具体