popen与pclose

#include <stdio.h>

函数原型如下:

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

int pclose(FILE *stream_to_close);

popen函数允许一个程序将另一个程序作为新进程来启动,并可以传递数据给它或者通过它接收数据。command字符串是要运行的程序名和相应的参数。open_mode必须是“r”或者“w”。

例子:


FILE *read_fp;

read_fp 
= popen("uname -a","r");

pclose(read_fp);

 

pipe调用

#include<unistd.h>

pipe函数的原型如下:

int pipe(int file_descriptor[2]);

数据从file_descriptor[1]写入,从 file_descriptor[0]读取。

例子:


int data_processed;

int file_pipes[2];

const char some_data[]="123";

char buffer[1024];

data_processed 
= write(file_pipes[1], some_data, strlen(some_data));

data_processed 
= read(file_pipes[0], buffer,1024);

 

#include <unistd.h>

int dup(int file_descriptor);

int dup2(int file_descriptor_one, int file_descriptor_two);