Trouble with named pipe...

This is a discussion on Trouble with named pipe... within the C Programming forums, part of the General Programming Boards category; Good evening, I am trying to use a named pipe to implement communication between two UNIX commands. It does not ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Trouble with named pipe...

    Good evening,

    I am trying to use a named pipe to implement communication between two UNIX commands. It does not compile properly. The error message in Dev-C++ is "syntax error before numeric constant". What am I missing?

    Code:
      
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/stat.h>
    
    int main(int argc, char *argv[]){
    pid_t pid;
    int fd[2];
    int val;
    
    int mkfifo(const char *myFifo, 0644);	    /* create a named pipe */				
    
    pid = fork();					/* fork */
    	if(pid < 0){
    		printf("Cannot fork");
    		exit(1);
    	}
    	if(pid > 0){
    		/* parent process */
    		dup2(fd[1], 1);			
    		close(fd[0]);			
    		execlp("ps", "ps", "-ef", (char *)0);
    	}
    	else if(pid == 0){
    		/* child process */
    		dup2(fd[0], 0);			
    		close(fd[1]);			
    		execlp("grep", "grep", "ct321j", (char *)0);
    	}
    
    }
    Thanks!

    Damien

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int mkfifo(const char *myFifo, 0644);	    /* create a named pipe */
    Did you intend this to be a function call? If so, don't use function prototype syntax for it, but make it look like every other function call in your program.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Troubles continue...It does not appear as if the pipe is working. The first part, "ps -ef" is working but not the second part.

    Ideas as to why?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc, char *argv[]){
    pid_t pid;
    int fd[2];
    int pipe;
    
    mkfifo("/p2/myFIFO", O_WRONLY);  /* create a named pipe */
        
    pid = fork();           /* fork */
     if(pid < 0){
      printf("Cannot fork");
      exit(1);
     }
     if(pid > 0){
      /* parent process */
            dup2(fd[1], 1);   
      close(fd[0]);   
      execlp("ps", "ps", "-ef", (char *)0);
     }
     else if(pid == 0){
      /* child process */
      dup2(fd[0], 0);   
      close(fd[1]);   
      execlp("grep", "grep", "cm415b", (char *)0);
     }
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You would have to define "second part" and "not working" for us to get anywhere.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Evening!

    What I mean is that the parent process executes "ps -ef" and sends it down the pipe to the child process which uses it as input for grep. I have an example of the unnamed pipe which works as it should. When I run the named pipe, it appears to only run "ps -ef".



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc, char *argv[]){
    pid_t pid;
    int fd[2];
    int pipe;
    
    mkfifo("/p2/myFIFO", O_WRONLY);  /* create a named pipe */
        
    pid = fork();           /* fork */
     if(pid < 0){
      printf("Cannot fork");
      exit(1);
     }
     if(pid > 0){
      /* parent process */
            dup2(fd[1], 1);   
      close(fd[0]);   
      execlp("ps", "ps", "-ef", (char *)0);
     }
     else if(pid == 0){
      /* child process */
      dup2(fd[0], 0);   
      close(fd[1]);   
      execlp("grep", "grep", "cm415b", (char *)0);
     }
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you expecting fd[1] to be your named pipe? If so, wouldn't you need to put that in your dup2 call instead of stdout?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  4. Named Pipe Problems.
    By Mastadex in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2006, 08:35 AM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21