Linux 中的 fork、execlp

发布于 2024-12-11 18:38:43 字数 845 浏览 0 评论 0原文

#include <unistd.h>
#include <stdio.h>


int main(int argc, char* argv[])
{

  int f1[2], f2[2];
  char buff;

  if(pipe(f1) != -1);
   printf("Pipe1 allright! \n");

  if(pipe(f2) != -1);
   printf("Pipe2 allright \n");



  if(fork()==0)
  {
    close(1);
    dup(f1[1]);
    close(0);
    execlp("ls", "ls", "-l", NULL);  
  }
  else
  {
    if(fork()==0)
    {  
    close(0);
    dup(f1[0]);
    close(1);
    dup(f2[1]);
     execlp("grep", "grep", "^d", NULL); 
    }
    else 
    {
        if(fork()==0)
        {
            close(0);
            dup(f2[0]);
            execlp("wc", "wc", "-l", NULL); 
      } 
    }

  }

  return 0;

}

我正在尝试执行 ls -l | grep ^d | C 中的 wc -l 。

我尝试了一切...

出了什么问题? :(

输出:Pipe1 好吧!,Pipe2 好吧!

Ps。您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的场景。

#include <unistd.h>
#include <stdio.h>


int main(int argc, char* argv[])
{

  int f1[2], f2[2];
  char buff;

  if(pipe(f1) != -1);
   printf("Pipe1 allright! \n");

  if(pipe(f2) != -1);
   printf("Pipe2 allright \n");



  if(fork()==0)
  {
    close(1);
    dup(f1[1]);
    close(0);
    execlp("ls", "ls", "-l", NULL);  
  }
  else
  {
    if(fork()==0)
    {  
    close(0);
    dup(f1[0]);
    close(1);
    dup(f2[1]);
     execlp("grep", "grep", "^d", NULL); 
    }
    else 
    {
        if(fork()==0)
        {
            close(0);
            dup(f2[0]);
            execlp("wc", "wc", "-l", NULL); 
      } 
    }

  }

  return 0;

}

I am trying to do ls -l | grep ^d | wc -l in C.

I tried everythig...

What is wrong? :(

Output: Pipe1 allright!, Pipe2 allright!

Ps. Your post does not have much context to explain the code sections; please explain your scenario more clearly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久夏青 2024-12-18 18:38:43

您的代码存在几个问题:

if(pipe(f1) != -1);
  printf("Pipe1 allright! \n");

我认为这应该是真正的错误检查,因此请删除 if 行中的 ;

之后运行您的程序,您会注意到 grepwc 命令仍然存在,它们不会终止。使用 ps(1) 命令检查这一点。 ls 命令似乎已终止。

假设,四个进程的 pid 是:

  • 9000 (main)
  • 9001 (ls)
  • 9002 (grep)
  • 9003 (wc)

查看 /proc/9002/fd 你会看到,文件句柄 0 ( stdin) 仍然开放供阅读:

> ll /proc/9002/fd/0
lr-x------ 1 as as 64 2011-10-22 20:10 0 -> pipe:[221916]

环顾四周,谁还拥有这个句柄,

> ll /proc/*/fd/* 2>/dev/null | grep 221916

您会发现,该管道的许多句柄都已打开:grepwc 中的两个都已打开。其他管道句柄也是如此。

解决方案:

您必须在dup之后严格关闭管道句柄。看这里:

#include <unistd.h>
#include <stdio.h>


int main(int argc, char* argv[])
{
     int f1[2];
     char buff;

     if(pipe(f1) != -1)
          printf("Pipe1 allright! \n");

     int pid = fork();
     if(pid==0)
     {
          close(1);
          dup(f1[1]);

          close(f1[0]);
          close(f1[1]);

          close(0);
          execlp("ls", "ls", "-l", NULL);  
     }
     printf("ls-pid = %d\n", pid);

     int f2[2];
     if(pipe(f2) != -1)
          printf("Pipe2 allright \n");

     pid = fork();
     if(pid==0)
     {  
          close(0);
          dup(f1[0]);

          close(f1[0]);
          close(f1[1]);

          close(1);
          dup(f2[1]);

          close(f2[0]);
          close(f2[1]);

          execlp("grep", "grep", "^d", NULL);
          // system("strace grep '^d'"); exit(0);
     }
     printf("grep-pid = %d\n", pid);

     close(f1[0]);
     close(f1[1]);

     pid = fork();
     if(pid==0)
     {
          close(0);
          dup(f2[0]);
          close(f2[0]);
          close(f2[1]);

          execlp("wc", "wc", "-l", NULL); 
     }
     printf("wc-pid = %d\n", pid);

     close(f2[0]);
     close(f2[1]);
}

There are several problems with your code:

if(pipe(f1) != -1);
  printf("Pipe1 allright! \n");

I assume this should be a real error check, so please remove the ; in the if line.

Running your program after that you will notice, that the grep and wc commands are still there, they don't terminate. Check this with the ps(1) command. The ls command seems to have terminated.

Let's assume, the pids of the four processes are :

  • 9000 (main)
  • 9001 (ls)
  • 9002 (grep)
  • 9003 (wc)

Looking into /proc/9002/fd you will see, that filehandle 0 (stdin) is still open for reading:

> ll /proc/9002/fd/0
lr-x------ 1 as as 64 2011-10-22 20:10 0 -> pipe:[221916]

And looking around, who has this handle still open with

> ll /proc/*/fd/* 2>/dev/null | grep 221916

you will see, that many handles to this pipe are open: both grep and wc have two of them open. Same is true for the other pipe handles.

Solution:

You have to close the pipe handles after dup rigorously. Look here:

#include <unistd.h>
#include <stdio.h>


int main(int argc, char* argv[])
{
     int f1[2];
     char buff;

     if(pipe(f1) != -1)
          printf("Pipe1 allright! \n");

     int pid = fork();
     if(pid==0)
     {
          close(1);
          dup(f1[1]);

          close(f1[0]);
          close(f1[1]);

          close(0);
          execlp("ls", "ls", "-l", NULL);  
     }
     printf("ls-pid = %d\n", pid);

     int f2[2];
     if(pipe(f2) != -1)
          printf("Pipe2 allright \n");

     pid = fork();
     if(pid==0)
     {  
          close(0);
          dup(f1[0]);

          close(f1[0]);
          close(f1[1]);

          close(1);
          dup(f2[1]);

          close(f2[0]);
          close(f2[1]);

          execlp("grep", "grep", "^d", NULL);
          // system("strace grep '^d'"); exit(0);
     }
     printf("grep-pid = %d\n", pid);

     close(f1[0]);
     close(f1[1]);

     pid = fork();
     if(pid==0)
     {
          close(0);
          dup(f2[0]);
          close(f2[0]);
          close(f2[1]);

          execlp("wc", "wc", "-l", NULL); 
     }
     printf("wc-pid = %d\n", pid);

     close(f2[0]);
     close(f2[1]);
}
迷路的信 2024-12-18 18:38:43

您可能想使用 dup2 而不是 dup,也就是说,对于其他情况,而不是

close(1);
dup(f1[1]);

do

dup2(f1[1], 1);

等等

you probably want to use dup2 instead of dup, that is, instead of

close(1);
dup(f1[1]);

do

dup2(f1[1], 1);

and so on for other occurencies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文