Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Interprocess communication

    I'm trying to implement a program which take a sequence of processes where each process have a command and an arguments (argument can be 0 or n ) .
    the program has to pass the output of each child process to the parent until it reach the final parent where it will execute all the processes .

    an example is : ./prog 'ps ufax' 'grep stackex' 'sort'
    the output have to be the excution of : ps ufax|grep stack|sort

    thank you fro helping
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    
    
    #define MAX_CMD_NUMBER 10 // maximum number of commands that the program can take as parameters
    #define MAX_ARG_NUMBER 10 // maximum number of arguments that each command can take
    
    
    void parse_commands( int argc,char *argv[], char * params[] );
    void parse_arguments( char *arg, char *arguments[] );
    
    int main( int argc, char *argv[] ){
      
      pid_t child_pid;
      char * commands[MAX_CMD_NUMBER]; // buffer containing parsed argument for the first command
      int pipe_number = argc*2;
      int pipes[pipe_number];
      int depth = 0;
      int count = 0;
      int children = 0;
      int process_number = argc-1;
    
      if ( argc < 2 ){
        printf( "Usage : %s 'cmd_1 args_list_1' 'cmd_2 args_list_2' ..\n", argv[0] );
        perror( "argc" );
    
        exit(1);
      }
    
      
      parse_commands( argc, argv, commands ); // method defined in library.h
      argc--;
      while( (depth < process_number) && (children < 1) ){
        pipe(pipes+count);
        if( (child_pid = fork()) == 0 ){
          printf("ho create un figlio");
          close( pipes[count] ); // closes the read port of the parch pipe
          dup2( pipes[count+1], 1 ); // closes the standard output port and opens the write pipe's port
          count +=2;
          depth++;
           //children++;
          argc--;
        }
        else{
    printf("ho create un padre");
          children++;
          close( pipes[count+1] );
          dup2( pipes[count], 0 );
          wait(child_pid); // parent process waits for the completion of child process
          char *arg = strtok(commands[argc-1], " " );
          char * pathname = arg; // pathname contains the command that will be executed by the current process
          char *arguments[MAX_ARG_NUMBER];
          arguments[0] = pathname;
          parse_arguments( arg, arguments ); // method defined in header library.h
    
         execv( pathname, arguments ); // executes the command with the parameter list
         //execlp(pathname,pathname,NULL);
        }
      }
      
      
      return 0;
    }
    void parse_commands( int argc,char *argv[], char * params[] ){
    int i;  
    for( i = 1; i < argc; i++ ){
        params[i-1] = argv[i];
      }
      params[argc] = NULL;
    }
    void parse_arguments( char *arg, char *arguments[] ){
      int index = 1;
      while( arg != NULL && index < MAX_ARG_NUMBER ){
        arg = strtok( NULL, " " );
        arguments[index] = arg;
        index ++;
      }
    
    }

  • #2
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool

    The program it runs .. but the problem is :execv( pathname, arguments );

    probably it doesnt escute !

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    how about having parent process to create FIFO and instruct child processes to redirect output there?
    if you do that, then all you need to do is read from FIFO

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • Users who have thanked patryk for this post:

    zabsa90 (04-13-2013)


  •  

    Tags for this Thread

    Posting Permissions

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