Score:0

C pipes with execlp(). Code works for "head" command but not for "sort"

us flag

So my job is to write a function that will work like:

ps -ef| tr -s ' '  :| cut -d: -f1 | sort | uniq -c |sort -n 

Here is my code:

void pipe4()
{
  int pdesk1[2];
  int pdesk2[2];
  int pdesk3[2];
  int pdesk4[2];
  int pdesk5[2];

  pipe(pdesk1);
  pipe(pdesk2);
  pipe(pdesk3);
  pipe(pdesk4);
  pipe(pdesk5);

  if (pdesk1 < 0 || pdesk2 < 0 || pdesk3 < 0 || pdesk4 < 0 || pdesk5 < 0)
  {
    perror("pipe");
    exit(1);
  }

  if (fork() == 0)
  {
    dup2(pdesk1[1], 1);
    close(pdesk1[0]);
    close(pdesk1[1]);
    execlp("ps", "ps", "-e", "-f", NULL);
    perror("ps");
    exit(1);
  }

  if (fork() == 0)
  {
    dup2(pdesk1[0], 0);
    dup2(pdesk2[1], 1);
    close(pdesk2[0]);
    close(pdesk2[1]);
    close(pdesk1[0]);
    close(pdesk1[1]);
    execlp("tr", "tr", "-s", " ", ":", NULL);
    perror("tr");
    exit(1);
  }
  if (fork() == 0)
  {
    dup2(pdesk2[0], 0);
    dup2(pdesk3[1], 1);
    close(pdesk3[0]);
    close(pdesk3[1]);
    close(pdesk2[0]);
    close(pdesk2[1]);
    execlp("cut", "cut", "-d", ":", "-f", "1", NULL);
    perror("cut");
    exit(1);
  }
  if (fork() == 0)
  {
    dup2(pdesk3[0], 0);
    // commented to see result at this moment
    // dup2(pdesk4[1], 1);
    close(pdesk4[0]);
    close(pdesk4[1]);
    close(pdesk3[0]);
    close(pdesk3[1]);
    //this prints nothing 
    execlp("sort", "sort", NULL);
    // but this works
    // execlp("head", "head", NULL);
    perror("sort");
    exit(1);
  }

  if (fork() == 0)
  {
    dup2(pdesk4[0], 0);
    dup2(pdesk5[1], 1);
    close(pdesk5[0]);
    close(pdesk5[1]);
    close(pdesk4[0]);
    close(pdesk4[1]);
    execlp("uniq", "uniq", "-c", NULL);
    perror("uniq");
    exit(1);
  }

  if (fork() == 0)
  {
    dup2(pdesk5[0], 0);
    close(pdesk5[0]);
    close(pdesk5[1]);
    execlp("sort", "sort", "-n", NULL);
    perror("sort -n");
    exit(1);
  }
  wait(NULL);
  exit(1);
}

So as I wrote in comments, up until sort part everything works fine, I checked outputs in each process and compared it with results I get by writing command in terminal.

I'm even more confused since I found out that when I replace sort with head (or cat for example), code works, uniq seems to work fine and then again last sort doesn't work.

bw flag
regazrding: `if (pdesk1 < 0 || pdesk2 < 0 || pdesk3 < 0 || pdesk4 < 0 || pdesk5 < 0) { perror("pipe"); exit(1); }` *On success, zero is returned. On error, -1 is returned, errno is set appropriately, and pipefd is left unchanged.* so the returned value for each call needs to be checked, NOT the array of FDs. Also, each cal to `pipe()` updates `errno` so cannot (accurately) just use the final value of `errno` to obtain the error cause
bw flag
Please read the MAN page for `sort` including the statement: "With no FILE, or when FILE is -, read standard input."
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.