Thread: wait() not working

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    wait() not working

    Hey guys,

    I'm trying to start a process which executes the uniq command to every string in a string array. this already works good. But I have to wait for execlp in parent, because the main program exits before uniq command finishes. How can I achieve this?

    Code:
    static void uniq_process(void) {    pid_t pid;
        int fd[2];
    
    
        if (pipe(fd) < 0) {
            bail_out("Error creating pipe");
        }
    
    
        pid = fork();
    
    
        /* child process */
        if (pid == 0) {
            close(fd[1]);
            close(STDIN_FILENO);
            dup(fd[0]);
            (void) execlp("uniq", "uniq", "-d",  (char*) 0 );
    
    
            bail_out("execlp failed");
        }     
        /* parent process */
        else if (pid > 0) {
        
            FILE *file = fdopen(fd[1] , "w");
            close(fd[0]);
    
    
            for (int i = 0; i < array_size; i++) {
                (void) fprintf(file, "%s", output_array[i]);
            }
            (void) fclose(file);
    
    
        } else  {
            bail_out("Fork error");
        }
    }
    Thanks for your help and time.
    Last edited by Juloor; 11-22-2015 at 08:11 AM.

  2. #2
    Registered User
    Join Date
    Oct 2015
    Location
    Turkey
    Posts
    10
    You can use flag algorithms instead of wait.With these type algorithms program need to wait result until the flag is 1 and flag will be 1 after the process done. I hope it helps to you.


    For example:
    Code:
    int theprocess(int *flag ,(enter your variables)){
       (process);
    
    
    flag=1;
    return (variable);
    }
    
    int main(void){
    int flag=0,result;
    
    .
    .
    .
    
    while(flag !=1)
    result=theprocess(&flag,(variables));
    .
    if(flag==1)
     (do the things);
    .
    .
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wait
    By Wipe in forum C Programming
    Replies: 1
    Last Post: 01-18-2005, 03:45 PM
  2. how do u say like H (wait 1 second) I
    By Prodigy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2002, 05:05 AM
  3. Wow, I can't wait to see what this will be like.
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-13-2002, 12:44 PM
  4. Wait
    By drdroid in forum C++ Programming
    Replies: 8
    Last Post: 02-12-2002, 03:55 PM
  5. wait!
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 10:06 PM