第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用shell中的管道連接n個命令?

用shell中的管道連接n個命令?

C
幕布斯6054654 2019-08-27 10:36:25
用shell中的管道連接n個命令?我試圖在C中實現(xiàn)一個shell。我可以用一個簡單的execvp()來執(zhí)行簡單的命令,但其中一個要求是管理這樣的命令:“l(fā)s -l | head | tail -4”with a for '循環(huán)并且只有一個'pipe()'語句重定向stdin和stdout。幾天后,我有點迷失了。N =簡單命令的數(shù)量(示例中為3:ls,head,tail)命令=帶有命令的結(jié)構(gòu)列表,如下所示:commands[0].argv[0]: ls commands[0].argv[1]: -l commands[1].argv[0]: head commands[2].argv[0]: tail commands[2].argv[1]: -4所以,我做了for循環(huán),并開始重定向stdin和stdout以便用管道連接所有命令,但是...我只是無能為力它為什么不起作用。for (i=0; i < n; i++){pipe(pipe);if(fork()==0){  // CHILD     close(pipe[0]);     close(1);     dup(pipe[1]);     close(pipe[1]);     execvp(commands[i].argv[0], &commands[i].argv[0]);     perror("ERROR: ");     exit(-1);}else{      // FATHER     close(pipe[1]);     close(0);     dup(pipe[0]);     close(pipe[0]);}}我想要創(chuàng)建的是一系列childed進程:[ls -l] ---- pipe ----> [head] ---- pipe ----> [tail -4]所有這些進程都有一個root(運行我的shell的進程)所以,第一個父親也是shell進程的一個孩子,我已經(jīng)有點筋疲力盡了,有人可以幫助我嗎?我甚至不確定孩子是否應該是執(zhí)行命令的孩子。多謝你們 ??!
查看完整描述

2 回答

?
Cats萌萌

TA貢獻1805條經(jīng)驗 獲得超9個贊

這里沒有什么復雜的,只要記住最后一個命令應該輸出到原始進程'文件描述符1,第一個應該從原始進程文件描述符0讀取。你只是按順序生成進程,沿著輸入端傳輸previois pipe電話。

所以,以下是類型:

#include <unistd.h>struct command{
  const char **argv;};

使用簡單明確定義的語義創(chuàng)建一個輔助函數(shù):

intspawn_proc (int in, int out, struct command *cmd){
  pid_t pid;

  if ((pid = fork ()) == 0)
    {
      if (in != 0)
        {
          dup2 (in, 0);
          close (in);
        }

      if (out != 1)
        {
          dup2 (out, 1);
          close (out);
        }

      return execvp (cmd->argv [0], (char * const *)cmd->argv);
    }

  return pid;}

這是主叉例程:

intfork_pipes (int n, struct command *cmd){
  int i;
  pid_t pid;
  int in, fd [2];

  /* The first process should get its input from the original file descriptor 0.  */
  in = 0;

  /* Note the loop bound, we spawn here all, but the last stage of the pipeline.  */
  for (i = 0; i < n - 1; ++i)
    {
      pipe (fd);

      /* f [1] is the write end of the pipe, we carry `in` from the prev iteration.  */
      spawn_proc (in, fd [1], cmd + i);

      /* No need for the write end of the pipe, the child will write here.  */
      close (fd [1]);

      /* Keep the read end of the pipe, the next child will read from there.  */
      in = fd [0];
    }

  /* Last stage of the pipeline - set stdin be the read end of the previous pipe
     and output to the original file descriptor 1. */  
  if (in != 0)
    dup2 (in, 0);

  /* Execute the last stage with the current process. */
  return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);}

還有一個小測試:

intmain (){
  const char *ls[] = { "ls", "-l", 0 };
  const char *awk[] = { "awk", "{print $1}", 0 };
  const char *sort[] = { "sort", 0 };
  const char *uniq[] = { "uniq", 0 };

  struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} };

  return fork_pipes (4, cmd);}

似乎工作。:)


查看完整回答
反對 回復 2019-08-27
?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

首先,你過早地關(guān)閉管道。僅關(guān)閉當前進程中不需要的結(jié)尾,并記住關(guān)閉子進程中的stdin / stdout。

其次,您需要記住上一個命令中的fd。因此,對于兩個進程,這看起來像:

int pipe[2];pipe(pipe);if ( fork() == 0 ) {
     /* Redirect output of process into pipe */
     close(stdout);
     close(pipe[0]);
     dup2( pipe[1], stdout );
     execvp(commands[0].argv[0], &commands[0].argv[0]);} if ( fork() == 0 ) {
     /* Redirect input of process out of pipe */
     close(stdin);
     close(pipe[1]);
     dup2( pipe[0], stdin );
     execvp(commands[1].argv[0], &commands[1].argv[0]);}/* Main process */close( pipe[0] );close( pipe[1] );waitpid();

現(xiàn)在你的工作是為此添加錯誤處理并為n個進程生成n-1個管道。第一個fork()塊中的代碼需要為進程1..n-1的相應管道運行,而第二個fork()塊中的代碼用于進程2..n。


查看完整回答
反對 回復 2019-08-27
  • 2 回答
  • 0 關(guān)注
  • 543 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號