Help on pipe

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I'm writing pipe demo:

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  pid_t pid;
  int fds[2], ret;
  const char *sort = "/usr/bin/sort";
  char *argv[2] = { "sort", NULL };

  ret = pipe(fds);
  if (ret == -1) {
    perror("pipe");
    return -1;
  }
  pid = fork();
  if (pid == -1) {
    perror("fork");
    return -1;
  }

  if (pid == 0) {
    int ret;

    close(fds[1]);
    dup2(fds[0], STDIN_FILENO);

    ret = execve(sort, argv, NULL);
    if (ret == -1) {
      perror("execve");
    }
    close(fds[0]);
    _exit(0);
  }
  else {
    FILE *stream;

    close(fds[0]);
    stream = fdopen(fds[1], "w");
    if (!stream) {
      perror("fdopen");
      return -1;
    }
    fprintf(stream, "this\n");
    fprintf(stream, "means\n");
    fprintf(stream, "war\n");
    fflush(stream);
    waitpid(pid, &ret, 0);
    close(fds[1]);
  }
  return 0;
}

I want to send parent's data to the child as "sort" input, i don't
know why sort doesn't receive the data. Did i made mistake here?

Thanks before.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux