标准管道(popen)

NAME
       popen, pclose - pipe stream to or from a process

SYNOPSIS
       #include <stdio.h>

       FILE *popen(const char *command, const char *type);

       int pclose(FILE *stream);

DESCRIPTION
       The popen() function opens a process by creating a pipe,  forking,  and
       invoking  the shell.  Since a pipe is by definition unidirectional, the
       type argument may specify  only  reading  or  writing,  not  both;  the
       resulting stream is correspondingly read-only or write-only.

       The  command argument is a pointer to a null-terminated string contain-
       ing a shell command line.  This command is passed to /bin/sh using  the
       -c  flag;  interpretation, if any, is performed by the shell. 

RETURN VALUE
       The popen() function returns NULL if the fork(2) or pipe(2) calls fail,
       or if it cannot allocate memory.

       The pclose() function returns -1 if wait4(2) returns an error, or  some
       other error is detected.

popen.c,如下:

/*************************************************************************
    > File Name: popen.c
    > Author: KrisChou
    > Mail:[email protected]
    > Created Time: Fri 22 Aug 2014 11:07:26 AM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char buf[1024];
    FILE *fp;

    while(memset(buf,0,1024),fgets(buf,1024,stdin) != NULL)
    {
        fp = popen(argv[1],"w");
        fputs(buf,fp);
        pclose(fp);
    }

    return 0;
}

被调用函数reverse.c,如下:

/*************************************************************************
    > File Name: reverse.c
    > Author: KrisChou
    > Mail:[email protected]
    > Created Time: Sat 23 Aug 2014 11:21:27 AM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char word[256]; //从标准输入读取字符串
    char buf[256][256],index = 0; //保存从标准输入读取的字符串
    while(memset(word,0,256), scanf("%s",word) != EOF )
    {
        strcpy(buf[index++],word);
    }
    index--;
    while(index >=0 )
    {
        printf("%s ",buf[index]);
        index--;
    }
    printf("\n");
    return 0;
}

运行程序:

[[email protected] popen]$ gcc popen.c -o main
[[email protected] popen]$ gcc reverse.c -o reverse
[[email protected] popen]$ ./main ./reverse
how are you
you are how
baby u r beautiful
beautiful r u baby
[[email protected] popen]$

按 ctrl+D 退出popen.c中的循环,从而退出程序。

时间: 2024-07-31 01:46:29

标准管道(popen)的相关文章

标准管道的使用

FILE * popen ( const char * command , const char * type ); 第一个参数是命令,不是文件名 popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程.这个进程必须由 pclose() 函数关闭,而不是 fclose() 函数.pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态.如果 shell 不能被执行,则 pclose() 返回的终止

python标准模块(下)

Python 系统标准模块(shutil.logging.shelve.configparser.subprocess.xml.yaml.自定义模块) 目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操作工具,类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好,是高级的文件.文件夹.压缩包处理模块,而且是系统的标准自带模块

Linux内核解析:进程间通信:管道

管道的定义管道的用途管道的操作管道非法read与write内核实现解析管道通信原理及其亲戚通信解析父子进程通信解析亲缘关系的进程管道通信解析管道的注意事项及其性质管道有以下三条性质shell管道的实现与shell命令进行通信system函数与popen函数区别 管道的定义 管道是第一个广泛应用的进程间通信手段.日常在终端执行shell命令时,会大量用到管道.但管道的缺陷在于只能在有亲缘关系(有共同的祖先)的进程之间使用.为了突破这个限制,后来引入了命名管道. 管道的用途 管道是最早出现的进程间通

linux 管道通信

进程通信 进程是系统分配资源的最小单位, 不同进程之间是相互隔离的, Linux常用于进程通信的几种方式有 匿名管道及有名管道 : 匿名管道用于具有亲缘关系的进程通信, 有名管道则可用于一般进程之间. 信号 : 软件层对中断机制的一种模拟. 消息队列 共享内存 : 不同进程享同一块内存区域, 不同进程可以实时查看对方对共享内存的更新. 需要借助同步机制, 如互斥锁, 信号量. 信号量 : 主要用于不同进程以及同一进程不同线程的同步和互斥. 套接字 : 广泛用于网络间进程通信. 无名管道 管道是是

Python 调用 shell 命令的模块总结

os.system('cat /proc/cpuinfo') 阻塞,返回shell执行参数命令的状态,即成功返回0 os.popen('cat /proc/cpuinfo') 阻塞,返回file read的对象,对该对象进行 read() 可以获取shell执行参数命令的结果,即标准输出 commands.getstatus('/proc/cputinfo') 阻塞,返回参数指定的系统文件的详细属性 commands.getoutput('cat /proc/cpuinfo') 阻塞,返回she

总结文件操作函数(二)-C语言

格式化读写: #include <stdio.h> int printf(const char *format, ...);                   //相当于fprintf(stdout,format,-); int scanf(const char *format, -); int fprintf(FILE *stream, const char *format, ...);      //中间的参数为写入文件的格式 int fscanf(FILE *stream, const

linux c常用函数

(1)字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符) iscntrl(测试字符是否为ASCII码的控制字符) isdigit(测试字符是否为阿拉伯数字) isgraph(测试字符是否为可打印字符) islower(测试字符是否为小写英文字母) isprint(测试字符是否为可打印字符) isspace(测试字符是否为空格字符) ispunct(测试字

Linux C函数库大全

(1)字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符) iscntrl(测试字符是否为ASCII码的控制字符) isdigit(测试字符是否为阿拉伯数字) isgraph(测试字符是否为可打印字符) islower(测试字符是否为小写英文字母) isprint(测试字符是否为可打印字符) isspace(测试字符是否为空格字符) ispunct(测试字

2014025665《嵌入式程序设计》第七周学习总结

一.管道 本周我们学习了管道  消息队列的传输和接收 1.管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信. 2.信息队列(Messge Queue):消息队列的实现包括创建或打开消息队列.添加消息.读取消息和控制消息队列这 4 种操作.其中创建 或打开消息队列使用的函数是 msgget(),这里创建的消息队列的数量会受到系统消息队列数量的限制: 添加消息使用的函数是 msgsnd()函数,