Results 1 to 4 of 4

Thread: Help with Mkfifo and exec

  1. #1
    Join Date
    Apr 2009
    Beans
    3

    Help with Mkfifo and exec

    Hello guys!

    I am doing a project for the university and I have to do that a process has to create several children through fork(). The father process sends a pathname to each one through exec and the children must send to the father a list with the files from each directory.

    The father is waiting and the children don't send anything. I must to do the pipe with mkfifo. I am trying to send text from the children but nothing happens. This is the code that I have wroten:


    Code:
    struct stat buf;
     mkfifo(NOMBREFIFO, S_IRWXU);
        while (--argc>0){
            stat (argv[argc], &buf);
            if (S_ISDIR(buf.st_mode)){
                pid = fork();
                
                if (pid<0) fprintf (stderr, "error %d", errno);
                if (pid==0) printf ("hhhhhhhhh");
                if (pid==0 && (exec=execl ("./listarordenado", argv[argc], NULL))<0) fprintf (stderr, "error %d", errno);
                if (pid>0 && wait(&status) && waitpid (pid, &status, 0)){
                    printf ("ssssssssss");
                     while(TRUE) {
                          fp=open(NOMBREFIFO,O_RDONLY);
                          nbytes=read(fp,buffer,TAM_BUF-1);
                          buffer[nbytes]='\0';
                          printf("%d Cadena recibida: %s \n",i, buffer);
                          close(fp);
                          i++;
                    }
                     }
             }
          }
    
    
    /////***code of listarordenado*//
    
    
    for (i=0; i<50; i++){
            if ((fp=open(NOMBREFIFO,O_WRONLY))==-1)
                perror("fopen");
              else {    
                    descr=write(fp,argv[1],strlen(argv[1]));
            }
            
          close(fp);
        }
    thanks,
    daniel
    Last edited by dano88tole; November 6th, 2010 at 10:34 PM.

  2. #2
    Join Date
    Apr 2007
    Location
    Glasgow
    Beans
    308
    Distro
    Ubuntu

    Re: Help with Mkfifo and exec

    Yuk yuk yuk! You need a role model, this is not good quality code! Lazy code.

    Anyways, lecture over

    I suspect your code just hangs, right? Absolutely nothing happens?

    Parent - forks a child, then immediately blocks, waiting for the child

    Child - writes to fifo, blocks waiting for write(2) to complete. This only happens when parent read(2)'s from the fifo, but the parent is waiting, not reading.

    Hope this helps

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,276
    Distro
    Kubuntu

    Re: Help with Mkfifo and exec

    I would suspect that the child is not being supplied the correct args; consider this app:
    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/wait.h>
    
    int main(void)
    {
       char* const appargv[] = { "/bin/ls", "-1", NULL };
    
       pid_t pid = fork();
    
       if (pid == 0)
       {
          /* child process */
          execl(appargv[0], appargv[0], appargv[1], appargv[2]);
    
          perror("execl() failed.");
       }
       else if (pid > 0)
       {
          /* parent process */
          int status = -1;
          waitpid(pid, &status, 0);
          printf("child exited with status = %d\n", status);
       }
       else
       {
          perror("Fork failed.");
       }
    
       return 0;
    }
    Pay close attention to the parameters passed to execl().

    P.S. execv() would be an easier function to use.
    Last edited by dwhitney67; November 7th, 2010 at 04:08 PM.

  4. #4
    Join Date
    Apr 2009
    Beans
    3

    Re: Help with Mkfifo and exec

    Hello!! Thank you for your answers!

Tags for this Thread

Bookmarks

Posting Permissions

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