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

问题描述

这是使用UNIX管道叉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的窗口写的一样。我应该使用什么功能呢?任何想法?

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

推荐答案

叉() execvp()在Windows中没有直接等同的。 fork和exec的结合将映射给CreateProcess(或如果您使用MSVC _spawnvp)。对于重定向,你需要CreatePipe和DuplicateHandle,这是体面覆盖在

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类似物窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 16:55