Find the answer to your Linux question:
Results 1 to 4 of 4
hello, first post here.. to be honest: I fail as a programmer, etc etc, I'm a n00b etc etc.. I've to solve a problem by using pipes. client gives server ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    2

    Question C pipes, forks -- it doesn't works

    hello, first post here.. to be honest:
    I fail as a programmer, etc etc, I'm a n00b etc etc..

    I've to solve a problem by using pipes.

    client gives server a username, server gives back.. well for us, the command which the client can use to view how many processes the user has open.

    get ready for something ugly. and not working.
    tell me everything I need to know, and point out all that I've done wrong, thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int pid, client[2], serv[2];
    char buf1[128], buf2[128];
    
    int main (int argc, char **argv)
    {
      if ((argc == 1) || (argc > 2))
      {
        printf ("\nspecify user.\n");
        exit(1);
      }
      else
      {
        pipe (serv);
        
        pid = fork();
    
        pipe (client);
        
        if (!pid)
        {
          close (client[1]);
        
          write (serv[1], argv[1], strlen(argv[1]));
          
          read (client[0], buf1, strlen(buf1));
          
          popen (buf1, "w");
          
          exit (0);
        }
        
        if (pid > 0)
        {
        //  char temp[128];
          close (serv[1]);
        
          read (serv[0], buf1, strlen(buf1));
          
          sprintf (buf2, "ps -u %s", buf1);
          
          write (client[1], buf2, strlen(buf2));
          
          wait (NULL);  
        }
        
        if (pid < 0)
        {
          perror ("fork");
        }
      }
    
      return 0;
    }

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I didn't go over the whole thing with a fine tooth comb, but to begin with, you want to do both pipe()s before you do the fork().
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2
    is this the entire source?
    if it is, you have not included unistd.h // for pipes and fork

  4. #4
    Just Joined!
    Join Date
    Oct 2008
    Posts
    2
    Quote Originally Posted by wje_lf View Post
    you want to do both pipe()s before you do the fork().
    roger that. I'm learning. still confused in some regards..

    Quote Originally Posted by SIGTERMer View Post
    is this the entire source?
    if it is, you have not included unistd.h // for pipes and fork
    I never said it didn't compile.. meaning that it does just fine. weird.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...