Sponsored Content
Full Discussion: Pipe problem
Top Forums Programming Pipe problem Post 302247305 by isato on Wednesday 15th of October 2008 11:11:02 AM
Old 10-15-2008
Pipe problem

Could anyone tell me whats wrong whit this piping? the commands that they execute are correct. the command I am trying is ls|wc. Both processes go to the right if statement.


Code:
for(i=0;i<argc;i++){
   
       if(i==0&&argc>1){//first command
           if(pipe(pipa1)==-1)
               perror("pipe");
           if((pid=fork())<0)    
                perror("fork");
            if(pid==0){//child
                printf("jag är fösta kommandot");
                printf("I am a 1 process--%d, my parent is--%d jag skall exikvera--%s\n ",getpid(),getppid(),com[i].argv[0]);
                dupPipe(pipa1, WRITE_END, STDOUT_FILENO);
                execvp(com[i].argv[0],com[i].argv);
            }                
       }
       if (i>0 && argc==2) {       
         pid = fork();          
         if (pid == 0) {        
            printf("jag är andra kommandot");
            printf("I am a process--%d, my parent is--%d jag skall exikvera--%s \n ",getpid(),getppid(),com[i].argv[0]);
            dupPipe(pipa1, READ_END, STDIN_FILENO);
            execvp(com[i].argv[0],com[i].argv);
             }
   
        }


Code:
/* Duplicate a pipe to a standard I/O file descriptor, and close both pipe ends
 * Arguments:    pip    the pipe
 *        end    tells which end of the pipe shold be dup'ed; it can be
 *            one of READ_END or WRITE_END
 *        destfd    the standard I/O file descriptor to be replaced
 * Returns:    -1 on error, else destfd
 */
int dupPipe(int pip[2], int end, int destfd){
 
  if (-1 == dup2 (pip[end], destfd))
    {
      perror ("dupPipe()");
      return -1;
    }
  return destfd;
}

 

10 More Discussions You Might Find Interesting

1. Programming

Wierd pipe problem

I have encountered a strange problem dealing with pipes and forking. The program basicaly does this: cat file | tbl | eqn | groff Now, I have a parent process that forks children that that exec the stuff that they should. The pipes defined in the parent are the ones used. The chain goes... (1 Reply)
Discussion started by: denoir
1 Replies

2. Shell Programming and Scripting

read after pipe problem OSX10.4

I use read often in scripts to filter the right part into a variable like: $ print "abc cde efg" | read k l ; print "k=$k, l=$l" k=, l= This works on linux and unix versions I work with. On OSX 10.4 this doesn't work. I found a workaround but would like to know why the original line... (5 Replies)
Discussion started by: relyveld
5 Replies

3. Shell Programming and Scripting

Problem with pipe into sed

Basically I am trying to write a short script to report total space used on /u0? file systems. This is what I was trying to do:df -k /u0? | grep -v kbytes | awk '{ printf $2 "+" }' | sed s/.$// | bcBut it returns no output. This works however: > A=`df -k /u0? |grep -v kbytes | awk '{ printf $2... (2 Replies)
Discussion started by: 98_1LE
2 Replies

4. Programming

Pipe Problem

Is there a way to know whether is pipe is opened in read or write mode.I mean is there any signal that is generated when a pipe is opened in read or write mode. If you have some solution .please let me know ........ (2 Replies)
Discussion started by: vivivo2000
2 Replies

5. UNIX for Dummies Questions & Answers

awk problem broken pipe

Hello everyone I got a school project that was due yesterday so i really would aprreciate some help the following is an example of the input file (bateriaTestes) 1 caeiro 2000 d 2 pessoa 100 w 3 campos 200 b 4 soares 500 w simple 4 field lines. lines separated by \n and fields by... (1 Reply)
Discussion started by: zemanel
1 Replies

6. Programming

Problem in read() from a pipe

Hi, Can any one please help me with this. Am struggling hard to get a solution. I am doing telnet through a C program and getting the stdout file descriptor of the remote machine to pipe. read() function is getting data, But whenl it receives SOH character ie. ^A ( Start of heading = Console... (2 Replies)
Discussion started by: JDS
2 Replies

7. Shell Programming and Scripting

problem using a pipe to grep

Hello ! I want to process a text file in order to extract desired data using sed and grep... Now I am facing a problem piping to grep... nothing happens.. The text consists of blocks of 3 lines that may (or not) contain the Desired data. the desired data is on each 2... (4 Replies)
Discussion started by: ShellBeginner
4 Replies

8. UNIX for Dummies Questions & Answers

problem with pipe operator

hi i am having issues with extra pipe. i have a data file and i need to remove the extra pipe in the(example 4th and 7thline) in datafile. there are many other line and filed like this which i need to remove from files. The sample data is below: 270 31|455004|24/03/2010|0001235|72 271... (3 Replies)
Discussion started by: abhi_n123
3 Replies

9. Programming

pipe() and poll() problem in C

Hi all, Im trying to do a simple program which ask the user for a unix command with the arguments. The program fork and the two process communicate with pipes. The child process call execvp with the command and the father process read the result of the execvp via the pipe. This program works... (11 Replies)
Discussion started by: blackmamba21
11 Replies

10. Shell Programming and Scripting

The problem of pipe

Hi,guys: I want to use c to implement a pipe. For example: ps auxwww | grep fred | more I forked three child processes. Each is responsible for each command, and pipe to next one. for(i=0;i<2;i++) pipe(fd) if(child==1) // child 1 { close(1) dup2(fd,1) close(fd) }... (3 Replies)
Discussion started by: tomlee
3 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 /* See feature_test_macros(7) */ #include <fcntl.h> /* Obtain O_* constant definitions */ #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_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 rea- sons why this may be useful. O_DIRECT (since Linux 3.4) Create a pipe that performs I/O in "packet" mode. Each write(2) to the pipe is dealt with as a separate packet, and read(2)s from the pipe will read one packet at a time. Note the following points: * Writes of greater than PIPE_BUF bytes (see pipe(7)) will be split into multiple packets. The constant PIPE_BUF is defined in <limits.h>. * If a read(2) specifies a buffer size that is smaller than the next packet, then the requested number of bytes are read, and the excess bytes in the packet are discarded. Specifying a buffer size of PIPE_BUF will be sufficient to read the largest possible packets (see the previous point). * Zero-length packets are not supported. (A read(2) that specifies a buffer size of zero is a no-op, and returns 0.) Older kernels that do not support this flag will indicate this via an EINVAL error. Since Linux 4.5, it is possible to change the O_DIRECT setting of a pipe file descriptor using fcntl(2). 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. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. On Linux (and other systems), pipe() does not modify pipefd on failure. A requirement standardizing this behavior was added in POSIX.1-2016. The Linux-specific pipe2() system call likewise does not modify pipefd on failure. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE The per-process limit on the number of open file descriptors has been reached. ENFILE The system-wide limit on the total number of open files has been reached. ENFILE The user hard limit on memory that can be allocated for pipes has been reached and the caller is not privileged; see pipe(7). 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, POSIX.1-2008. 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 file 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. Program source #include <sys/types.h> #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), splice(2), tee(2), vmsplice(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-11-26 PIPE(2)
All times are GMT -4. The time now is 06:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy