当前位置: 技术问答>linux和unix
UNIX下利用管道重定向实现对程序的输出进行操作
来源: 互联网 发布时间:2016-03-14
本文导语: 如题,在WINDOWS下可能好点,但在UNIX下我就不知如何实现了,请各位高人帮一下忙,能给思路已经很感激了,如果有源代码将更好.我先谢过大家了. | 用pipe 和 dup2. man 2 pipe dup2 #include #include #...
如题,在WINDOWS下可能好点,但在UNIX下我就不知如何实现了,请各位高人帮一下忙,能给思路已经很感激了,如果有源代码将更好.我先谢过大家了.
|
用pipe 和 dup2. man 2 pipe dup2
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int pfd[2];
pid_t cpid;
char buf;
assert(argc == 2);
if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
cpid = fork();
if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if (cpid == 0) { /* Child reads from pipe */
close(pfd[1]); /* Close unused write end */
if (dup2(pfd[0], STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
while (read(STDIN_FILENO, &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
close(STDIN_FILENO);
close(pfd[0]);
_exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */
close(pfd[0]); /* Close unused read end */
if (dup2(pfd[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
printf("%sn", argv[1]);
fflush(stdout);
close(STDOUT_FILENO);
close(pfd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int pfd[2];
pid_t cpid;
char buf;
assert(argc == 2);
if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
cpid = fork();
if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if (cpid == 0) { /* Child reads from pipe */
close(pfd[1]); /* Close unused write end */
if (dup2(pfd[0], STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
while (read(STDIN_FILENO, &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
close(STDIN_FILENO);
close(pfd[0]);
_exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */
close(pfd[0]); /* Close unused read end */
if (dup2(pfd[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
printf("%sn", argv[1]);
fflush(stdout);
close(STDOUT_FILENO);
close(pfd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}
|
如果是要在你的程序里执行其他程序,直接用popen也挺方便的
stream=popen("ps -ef|grep mplayer","r");
if(stream!=NULL)
{
memset(buff,0,sizeof(buff));
while(fgets(buff,512,stream))
{
p=strtok(buff," ");
if(p==NULL)break ;
if(strlen(p)==0)
break;
if((pidnum=atoi(p))==0)
break;
splaypid=(pid_t)pidnum;
kill(splaypid,SIGKILL);
}
pclose(stream);
}
stream=popen("ps -ef|grep mplayer","r");
if(stream!=NULL)
{
memset(buff,0,sizeof(buff));
while(fgets(buff,512,stream))
{
p=strtok(buff," ");
if(p==NULL)break ;
if(strlen(p)==0)
break;
if((pidnum=atoi(p))==0)
break;
splaypid=(pid_t)pidnum;
kill(splaypid,SIGKILL);
}
pclose(stream);
}
|
#include
int main(int argc, char *argv[])
{
char buf[128];
FILE *pp;
int i=0;
if( (pp = popen("test.sh", "r")) == NULL )
{
printf("popen() error!n");
exit(1);
}
while(fgets(buf, sizeof buf, pp))
{
i++;
printf("%s", buf);
}
pclose(pp);
return 0;
}
int main(int argc, char *argv[])
{
char buf[128];
FILE *pp;
int i=0;
if( (pp = popen("test.sh", "r")) == NULL )
{
printf("popen() error!n");
exit(1);
}
while(fgets(buf, sizeof buf, pp))
{
i++;
printf("%s", buf);
}
pclose(pp);
return 0;
}