Thread: Process and Pipes

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Process and Pipes

    I have to create a pipe which simulates the shell command " ps -aux | sort -n | head -10 "

    For one pipe the code would be like this:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main() {
    
    
    	int pid, fd[2], ret;
    
    	pipe(fd);
    	pid = fork();
    
    	if (pid==0) {
    	
    		dup2(fd[1], STDOUT_FILENO);
    		close(fd[0]);
    		close(fd[1]);
    		ret = execlp ("ps", "ps", "-aux", NULL);
    
    	} else {
    		dup2(fd[0], STDIN_FILENO);
    		close(fd[0]); 
                    close(fd[1]);
    		ret=execlp("sort", "sort", "-n", NULL);
    
    	} 
    
    	return 0;
    
    }

    But don't know how to do that with two pipes??
    Can anybody help me please?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You might want to look at popen. It will probably do just what you want a lot easier.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    We are not allowed to use popen or system...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you had two pipes, say called p1 and p2 (and not something like fd), then the sort command would dup2 the write end of one of them, and dup2 the read end of the other one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    But were do I have to write the second pipe into the "else" branch?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the else (the parent) needs another fork() because it has another child process to spawn (head -10)

    Basically, same again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Ok but after I created another fork() do I still have just 2 branches or do I have to make four now?
    Like:

    Code:
    pid1 = fork();
    pid2 = fork();
    
    if (pid1==0) {
    	
    		dup2(fd[1], STDOUT_FILENO);
    		close(fd[0]);
    		close(fd[1]);
    		ret = execlp ("ps", "ps", "-aux", NULL);
    
    	} else {
    		dup2(fd[0], STDIN_FILENO);
    		close(fd[0]); 
                    close(fd[1]);
    		ret=execlp("sort", "sort", "-n", NULL);
    
    	} 
    
    if (pid2 = 0) {
    
               ...
              ...
    
    } else {
    ...
    ...
    
    }
    ???

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    No, the else you have at present needs a copy of everything you have at present
    Code:
    pipe()
    fork()
    if ( p == 0 ) {
      // dup(), and run a process
    } else {
      pipe()
      fork()
      if ( p == 0 ) {
        // dup(), and run a process
      } else {
        // dup(), and run another
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Ok changed my code no, looks like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    
    
    int main() {
    
    	int ret;
    	int pid, pid2;
    	int fd1[2];
    	int fd2[2];
    
    	pipe(fd1);
    	pid = fork();
    
    	if (pid == 0) {
    
    				dup2(fd1[1], STDOUT_FILENO);
    				close(fd2[0]);
    				close(fd1[0]);
    				close(fd2[1]);
    				ret = execlp("ps", "ps", "-aux", NULL);
    	}
    	else {
    
    		pipe(fd2);
    		pid2 = fork();
    
    		if (pid2 == 0) {
    				dup2(fd1[0], STDIN_FILENO);
    				dup2(fd2[1], STDOUT_FILENO);
    				close(fd1[1]);
    				close(fd2[0]);
    				ret = execlp("head", "head", "-10", NULL);
    
    		}
    		else {
    				dup2(fd2[0], STDIN_FILENO);
    				close(fd1[1]);
    				close(fd2[0]);
    				close(fd1[0]);
    				ret = execlp("sort", "sort", "-n", NULL);
    		}
    	}
    	return 0;
    
    }
    But it doesn't work what's my mistake??

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Look carefully at what you're closing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-01-2011, 11:06 AM
  2. 2 way pipes
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-25-2010, 04:21 AM
  3. pipes in c
    By ajal1 in forum C Programming
    Replies: 6
    Last Post: 10-29-2005, 03:29 PM
  4. Redirecting STDOUT to STDIN of a child process
    By 0xception in forum Linux Programming
    Replies: 4
    Last Post: 09-13-2005, 11:58 PM
  5. process ring
    By gregulator in forum C++ Programming
    Replies: 0
    Last Post: 02-28-2005, 08:21 PM

Tags for this Thread