popen() in Russian dolls

This artical gave a way to learn and understand popen() and thus the following pclose() in a Russian nesting doll-like practice.
As described in The GNU C Library Reference Manual, popen and pclose is a combination of pipe (to create the pipe), fork (to create the subprocess), dup2 (to force the subprocess to use the pipe as its standard input or output channel), and exec (to execute the new program).

#include <stdlib.h>


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

int pclose (FILE* stream);


popen() executes the shell command command as a subprocess, and creates a pipe to the subprocess and returns a stream that corresponds to that pipe. If a mode argument of “r” is specified, then one can get the output of the well-executed command line program by using fread() on the returned stream(FILE*). Otherwise if “w” is specified, one can write to the stream to send data to the standard input channel of the subprocess. The subprocess inherits its standard input or output channel from the parent process.
pclose() is used to close a stream created by popen. It waits for the child process to terminate and returns 0 if no errors occurs.

Now, let’s see the Russian dolls practice:
First of all, let’s take a look at the overview of the practice.

[root@localhost RussianDolls]# ll -t
total 40
-rwxr-xr-x. 1 root root 16120 Oct 20 23:59 innerDoll
-rwxr-xr-x. 1 root root 16128 Oct 20 23:59 outterDoll
-rw-r--r--. 1 root root   716 Oct 20 23:59 src_outter_doll.c
-rw-r--r--. 1 root root   718 Oct 20 23:55 src_inner_doll.c

And, the following shows the output of the program outterDoll, which is the final target of this practice.

[root@localhost RussianDolls]# ./outterDoll 
+++++++++++++++++outter doll output+++++++++++++++++++++++
221 bytes of output have been read by outter doll:
-----------------inner doll output-----------------------
56 bytes of output have been read by inner doll:
innerDoll
outterDoll
src_outter_doll.c
src_inner_doll.c
-----------------inner doll output-----------------------
+++++++++++++++++outter doll output+++++++++++++++++++++++

Then, let’s have a look at the realization of the above.
src_inner_doll.c

#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>

#define BUFF_SIZE   1024


int main(int argc, char* argv[])
{
    printf("-----------------inner doll output-----------------------\n");

    char buffer[BUFF_SIZE] = { 0 };
    FILE* stream;
    size_t byteNumRead;

    stream = popen("ls -t", "r");
    if(stream){
        byteNumRead = fread(buffer, sizeof(char), BUFF_SIZE, stream);
        printf("%d bytes of output have been read by inner doll:\n%s",
                byteNumRead, buffer);
    }
    if(pclose(stream) != 0){
        fprintf (stderr, "pclose() error.\n");
    }

    printf("-----------------inner doll output-----------------------\n");

    return EXIT_SUCCESS;
}


src_outter_doll.c

define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>

#define BUFF_SIZE   1024


int main(int argc, char* argv[])
{
    printf("+++++++++++++++++outter doll output+++++++++++++++++++++++\n");

    char buffer[BUFF_SIZE] = { 0 };
    FILE* stream;
    size_t byteNumRead;

    stream = popen("./innerDoll", "r");
    if(stream){
        byteNumRead = fread(buffer, sizeof(char), BUFF_SIZE, stream);
        printf("%d bytes of output have been read by outter doll:\n%s",
                byteNumRead, buffer);
    }
    if(pclose(stream) != 0){
        fprintf (stderr, "pclose() error.\n");
    }

    printf("+++++++++++++++++outter doll output+++++++++++++++++++++++\n");

    return EXIT_SUCCESS;
}

Tips: there is a easy way to prepare the frame of the source code of the outterDoll program, that is, command line " cp src_inner_doll.c src_outter_doll.c "

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值