Sponsored Content
Full Discussion: Pipes in C
Top Forums Programming Pipes in C Post 302392587 by petrca on Thursday 4th of February 2010 11:53:16 PM
Old 02-05-2010
Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C.
I have created a pipe that does od -bc < myfile | head
Now, I am trying to create od -bc < myfile | head | wc
Here is my code, and I know I might be off, thats why I am here so I can get some clarification.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/file.h>

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

   int i, filefd, pipefd[2], od_pid, head_pid, pid, wc_pid;

   for (i = 1; i < argc; i++) {
      filefd = open(argv[i], O_RDONLY);
      if (filefd < 0){
         printf("%s could not be opened\n", argv[i]);
         continue;
      }

      dup2(filefd, STDIN_FILENO);
      close(filefd);
      pipe(pipefd);

      if ((od_pid = fork()) == 0) {
         dup2(pipefd[1], STDOUT_FILENO);
         close(pipefd[0]);
         close(pipefd[1]);
         execl("/usr/bin/od", "od", "-bc", NULL);
         exit(EXIT_FAILURE);
      }

      if ((head_pid = fork()) == 0) {
         dup2(pipefd[1], STDOUT_FILENO);
         close(pipefd[0]);
         close(pipefd[1]);
         execl("/usr/bin/head","head", NULL);
         exit(EXIT_FAILURE);
      }

     if ((wc_pid = fork()) == 0) {
        dup2(pipefd[0], STDIN_FILENO);
        close(pipefd[0]);
        close(pipefd[1]);
        execl("/usr/bin/wc","wc", NULL);
        exit(EXIT_FAILURE);
     }

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

      do {
         pid = wait(NULL);
         if (pid == od_pid)
            od_pid = 0;
         if (pid == head_pid)
            head_pid = 0;
        if (pid == wc_pid)
            head_pid = 0;
      } while ((od_pid != 0 || head_pid != 0 || wc_pid !=0) && pid != -1);

     } /* end of for loop*/

   exit(EXIT_SUCCESS);
}

Any direction would be appreciated. Cheers.

---------- Post updated at 11:53 PM ---------- Previous update was at 11:01 PM ----------

oh ok...figured it out. I had to create another pipe to take the input from "head".

Cheers!
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

2. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

3. Programming

pipes inside

Extremely need to understand some aspects of pipes realization. The main question is in which memory are pipes placed? (13 Replies)
Discussion started by: pranki
13 Replies

4. UNIX for Advanced & Expert Users

broken pipes

A broken pipe means that you are writing onto a pipe that has been closed by the other end. i have unix batch which forks some process and these processes are communicating each other via pipes. but sometimes i got too many broken pipe error. how can i search for the cause of these errors,... (1 Reply)
Discussion started by: yakari
1 Replies

5. UNIX for Advanced & Expert Users

Consolidating Pipes

This is something I've given a lot of thought to and come up with no answer. Say you have a data stream passing from a file, through process A, into process B. Process A only modifies a few bytes of the stream, then prints the rest of the stream unmodified. Is there any way to stream the file... (4 Replies)
Discussion started by: Corona688
4 Replies

6. Shell Programming and Scripting

Help with pipes in a file

I have to add pipes for particualr number of records in a file. For eg the file has 10 records and i need to add the "|" for records numbers 7 to 10 at particular positons as shown. I need to add pipes in each of these records at positions 9, 11,,21 as like below. Can some body... (7 Replies)
Discussion started by: dsravan
7 Replies

7. Shell Programming and Scripting

named pipes

How to have a conversation between 2 processes using named pipes? (5 Replies)
Discussion started by: kanchan_agr
5 Replies

8. UNIX for Dummies Questions & Answers

Named Pipes

hi, i am working on a script for oracle export, m using a parameter file... i want to compress the dump file that is generated.. in my script following is the code i have written. i am not able to generata .gz file mknod /tmp/exp_tesd1_pipe p gzip -cNf... (4 Replies)
Discussion started by: saharookiedba
4 Replies

9. Homework & Coursework Questions

Using Pipes and Redirection

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a pipe to show the number of people who are logged into the system right now. Create a pipe to show... (2 Replies)
Discussion started by: lakers34kb
2 Replies

10. Shell Programming and Scripting

Pipes with the < and >> meanings.

Hey, Well, we're starting scripting next week for my class, and I have my command done, but I don't actually understand what the meaning is, so I was just wondering if someone could "translate" this in to words so that I might be able to do this better... command1 | command2 | command3... (5 Replies)
Discussion started by: sso
5 Replies
PIPE(2) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for reasons why this may be useful. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE Too many file descriptors are in use by the process. ENFILE The system limit on the total number of open files has been reached. VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2009-09-15 PIPE(2)
All times are GMT -4. The time now is 05:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy