views:

144

answers:

1

Hello,

I seem to be having trouble with pipe and select.

Context: Have to program something which will be shell executed as such:
logn [--tick n] cmd [args] [, cmd [args]]...

Basically, it's a program that multiple programs simultanously.

Constraints: Each output line has to start with it's command number in front in format printf "%d: %s"
ie:
0: first line of first command.
0: second line of first command.
1: first line of second command.
0: third line of first command.
1: second line of second command.

If the tick has been specified, system will print a period if no output has been sent for n seconds. Must use Select() If the last output was a period, system does not print another period.

Issues:
1) It seems the output of the second command goes to the p[0] of the first pipe.
When I read into p[0][0] i get what was sent to p[1][0]. there doesn't seem to be anything in p[1][0].

2) Whenever my select encouters a timeout, it seems to get stuck there.

I asked a question earlier, so it may seems familiar. As the issue was different, I made a new post. The old post helped me alot to figure out the forking.

Here's my code:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h> 
#include "readline.h"

/* Fetches the number of commands in parameters. Number of Commas + 1 */
int getNbCmd(char ** argv)
{
    int nbCmd = 1;
    int i = 0;    
    while(argv[i] != '\0')
    {
        if(strcmp(argv[i], ",") == 0)
            nbCmd++;
        i++;
    }

    return nbCmd;
}


/* Fills the Command Array */
void getCommandes(char *** tbCmd, int argc, char ** argv)
{    
    int indexArgv = 1;
    int indexCmd = 0;
    int indexTbCmd = 0;        

    char ** cmd = (char **)malloc(argc*sizeof(char *));
    if(indexArgv < argc)
    {
        if(strcmp(argv[indexArgv], "--tick") == 0)
            indexArgv = 3;
    }

    while (indexArgv < argc)
    {

        if(strcmp(argv[indexArgv], ",") == 0)
        {    
            cmd[indexCmd] = (char *) 0;
            tbCmd[indexTbCmd] = cmd;
            free(cmd);
            cmd = (char **)malloc(argc*sizeof(char *));

            indexTbCmd++;
            indexCmd = 0;
        }
        else
        {
            char * arg;
            arg = argv[indexArgv];
            cmd[indexCmd] = arg;        

            indexCmd++;
        }
        indexArgv++;
    }

    cmd[indexCmd] = (char *) 0;
    tbCmd[indexTbCmd] = cmd;
    free(cmd);        
}


int main (int argc, char ** argv)
{
    int nbCmds = getNbCmd(argv);    
    int tick = -1;    

    char *** tbCmd = (char ***) malloc (nbCmds*sizeof(char **));

    if(argc > 3)
    {
        if(strcmp(argv[1], "--tick") == 0)
            tick = atoi(argv[2]);
    }

    getCommandes(tbCmd, argc, argv);

    int i,j;

    pid_t pidM[nbCmds];    
    int p[nbCmds][2];

    for (i = 0;i < nbCmds;i++)
    {            
        if ( pipe( p[i] ) != 0 ){ perror( "pipe()" ); exit(1); }
    }

    for (i = 0;i < nbCmds;i++)
    {    
        // fork() to get child process        
        pidM[i] = fork();    

        if ( pidM[i] < 0 ){ perror( "fork()" ); exit(1); }
        //Child Processing
        else if (pidM[i] == 0)
        {
            close(p[i][0]);
            dup2(p[i][1], STDOUT_FILENO);

            int ret;
            ret = execvp(tbCmd[i][0], tbCmd[i]);
            perror("execvp()");
        }
    }

    // Parent Processing
    for (i = 0;i < nbCmds;i++)
    {
        close(p[i][1]);
    }

    char * buffer;
    int retval = 1;
    int boolAfficher = 0;
    int nbNull = 0;        

    fd_set set;
    struct timeval timeout;

    /* Initialize the file descriptor set. */
    FD_ZERO (&set);
    for (i = 0;i < nbCmds;i++)
    {
        FD_SET (p[i][0], &set);            
    }

    while(nbNull < nbCmds)
    {
        if(tick >= 0)
        {
            timeout.tv_sec = (unsigned int)tick;
            timeout.tv_usec = 0;
            retval = select (FD_SETSIZE,&set, NULL, NULL, &timeout);                
        }
        else
            retval = select (FD_SETSIZE,&set, NULL, NULL, NULL);

        if(retval == 0)
        {
            if(boolAfficher == 0 && tick >= 0)
            {
                printf(".\n");
                boolAfficher = 1;
            }                
        }
        else if(retval > 0)
        {            
            for (i = 0;i < nbCmds;i++)
            {
                if(FD_ISSET(p[i][0], &set))
                {
                    buffer = readline(p[i][0]);
                    if(buffer[0] != '\0')
                    {
                        printf("%d: %s", i, buffer);
                    }
                    else
                    {
                        FD_CLR(p[i][0], &set);                            
                        nbNull++;
                    }
                }
                else
                    printf("Not ISSET[%d]\n", i);

                free(buffer);
            }                
        }
        else
            perror("select()");
    }

    wait(NULL);
    free(tbCmd);
    exit(0);
}
A: 

I was able to figure out!

Still buggy when recursivly calling logn

I.e: ./logn --tick 2 ./logn --tick 2 ./cmd1 0 un 6 deux , ./logn --tick 2 ./cmd1 1 trois 6 quatre

Code for ./cmd1

while test "x$#" != "x0"; do
    sleep $1
    shift
    test "x$#" != "x0" || exit
    echo $1
    shift
done

Here is my fixed code in case it interests anyone:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h> 
#include "readline.h"

/* Fetches the number of commands in parameters. Number of Commas + 1 */
int getNbCmd(char ** argv)
{
    int nbCmd = 1;
    int i = 0;    
    while(argv[i] != '\0')
    {
        if(strcmp(argv[i], ",") == 0)
            nbCmd++;
        i++;
    }

    return nbCmd;
}


/* Fills the Command Array */
void getCommandes(char *** tbCmd, int argc, char ** argv)
{    
    int indexArgv = 1;
    int indexCmd = 0;
    int indexTbCmd = 0;        

    char ** cmd = (char **)malloc(argc*sizeof(char *));
    if(indexArgv < argc)
    {
        if(strcmp(argv[indexArgv], "--tick") == 0)
            indexArgv = 3;
    }

    while (argv[indexArgv] != '\0')
    {
        if(strcmp(argv[indexArgv], ",") == 0)
        {

            cmd[indexCmd] = (char *) 0;
            tbCmd[indexTbCmd] = cmd;                
            cmd = (char **)malloc(argc*sizeof(char *));                

            indexTbCmd++;
            indexCmd = 0;
        }
        else
        {

            char * arg;
            arg = argv[indexArgv];
            cmd[indexCmd] = arg;        

            indexCmd++;
        }
        indexArgv++;
    }


    cmd[indexCmd] = (char *) 0;
    tbCmd[indexTbCmd] = cmd;
    free(cmd);        
}


int main (int argc, char ** argv)
{
    int nbCmds = getNbCmd(argv);

    int tick = -1;    

    char *** tbCmd = (char ***) malloc (nbCmds*sizeof(char **));

    if(argc > 3)
    {
        if(strcmp(argv[1], "--tick") == 0)
            tick = atoi(argv[2]);
    }

    getCommandes(tbCmd, argc, argv);

    int i,j;        

    pid_t pidM[nbCmds];    
    int p[nbCmds][2];

    for (i = 0;i < nbCmds;i++)
    {            
        if ( pipe( p[i] ) != 0 ){ perror( "pipe()" ); exit(1); }
    }

    for (i = 0;i < nbCmds;i++)
    {    
        // fork() to get child process        
        pidM[i] = fork();    

        if ( pidM[i] < 0 ){ perror( "fork()" ); exit(1); }
        //Child Processing
        else if (pidM[i] == 0)
        {
            close(p[i][0]);                            
            dup2(p[i][1], STDOUT_FILENO);                
            int ret;

            ret = execvp(tbCmd[i][0], tbCmd[i]);
            perror("execvp()");
            _exit(1);
        }
    }

    // Parent Processing

    for (i = 0;i < nbCmds;i++)
    {
        close(p[i][1]);
    }

    char * buffer = NULL;        
    int retval = 1;
    int boolAfficher = 0;

    int nbNull = 0;
    int isOk[nbCmds];

    for (i = 0;i < nbCmds;i++)
    {
        isOk[i] = 1;
    }

    fd_set set;
    struct timeval timeout;

    while(nbNull < nbCmds)
    {
        FD_ZERO (&set);
        for (i = 0;i < nbCmds;i++)
        {    
            if(isOk[i] == 1)
                FD_SET (p[i][0], &set);
        }

        if(tick > 0)
        {
            timeout.tv_sec = (unsigned int)tick;
            timeout.tv_usec = 0;
            retval = select (FD_SETSIZE,&set, NULL, NULL, &timeout);                
        }
        else
        {
            timeout.tv_sec = 0;
            timeout.tv_usec = 0;
            retval = select (FD_SETSIZE,&set, NULL, NULL, &timeout);      
        }

        if(retval == 0)
        {
            if(boolAfficher == 0 && tick >= 0)
            {
                printf(".\n");
                boolAfficher = 1;
            }                    
        }
        else
        {
            for (i = 0;i < nbCmds;i++)
            {
                if(FD_ISSET(p[i][0], &set))
                {                            
                    buffer = readline(p[i][0]);
                    if(buffer[0] != '\0')
                    {
                        printf("%d: %s", i, buffer);
                        boolAfficher = 0;
                    }
                    else
                    {
                        isOk[i] = 0;
                        nbNull++;
                    }
                    free(buffer);
                }
            }
        }
    }

    wait(NULL);
    free(tbCmd);
    exit(0);
}
I'd mark this as an answer. It removes it from the unanswered Qs list and if it gets 3 upvotes earns you a badge. (Not sure if you get the rep as well.
Edd