管道叉和execvp类似物 [英] pipe fork and execvp analogs in windows

查看:217
本文介绍了管道叉和execvp类似物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在unix中使用管道fork exec三重奏的简单演示。

This is simple demonstration of pipe fork exec trio using in unix.

#include <stdio.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
    int outfd[2];
    if(pipe(outfd)!=0)
    {
          exit(1);
    }
    pid_t pid = fork();
    if(pid == 0)
    {
        //child
        close(outfd[0]);
        dup2(outfd[1], fileno(stdout));
        char *argv[]={"ls",NULL};
        execvp(argv[0], (char *const *)argv);
        throw;
    }
    if(pid < 0)
    {
        exit(1);
    }
    else
    {
        //parrent
        close(outfd[1]);
        dup2(outfd[0], fileno(stdin));
        FILE *fin = fdopen(outfd[0], "rt");
        char *buffer[2500];
        while(fgets(buffer, 2500, fin)!=0)
        {
            //do something with buffer
        }
    }
    return 0;
}



现在我想使用WinAPI在Windows中写入相同的内容。我应该使用什么功能?任何想法?

Now I want to write same in windows using WinAPI. What functions should I use? Any ideas?

推荐答案

fork() execvp()在Windows中没有直接等效。 fork和exec的组合将映射到CreateProcess(或_spawnvp,如果您使用MSVC)。对于重定向,您需要CreatePipe和DuplicateHandle,本文适用于此MSDN文章

fork() and execvp() have no direct equivalent in Windows. The combination of fork and exec would map to CreateProcess (or _spawnvp if you use MSVC). For the redirection, you need CreatePipe and DuplicateHandle, this is covered decently in this MSDN article

这篇关于管道叉和execvp类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆