pipe - c execute external program multiple times -


i'm trying call external program code arguments. i'm trying see how different parameters change it's output have run multiple times (about 1000 times). every time external program runs i'm interested in 1 line of output although printing lot of (for purpose) useless stuff. line i'm interested in right above special identifier("some_signal") in output. thought i'll wait till line appears , read line above.

i tried following:

 pid_t pid = 0;   int pipefd[2];   file* output;   char line[256];               // pipe read buffer   char prev_line[256];          // prev. line pipe buffer   char signal[] = "some_signal\n";   int status = 0;   double obj_val;    pipe (pipefd); //create pipe   pid = fork (); //span child process   if (pid == 0)     {       // redirect child's output pipe       close (pipefd[0]);       dup2 (pipefd[1], stdout_fileno);       dup2 (pipefd[1], stderr_fileno);       execl ("/some/path",          "some/path",          "some_argument", (char*) null);     }   else if (pid < (pid_t) 0)     {       printf("fork failed \n");       return exit_failure;     }   else     {       // child output pipe       close (pipefd[1]);       output = fdopen (pipefd[0], "r");         while (fgets (line, sizeof(line), output), signal != null)         {           if(strcmp(line, signal) == 0)            {              obj_val = atof (prev_line);              kill (pid, sigterm);              waitpid (pid, &status, wnohang);              kill (pid, sigkill);              waitpid (pid, &status, 0);              break;            }//end if           strcpy (prev_line, line);         }//end while      } 

this works fine 100 runs or , 1 out of 2 errors occurs. first 1 segmentation fault. second 1 calling program printing out output of called program (without line containing wanted signal) , goes infinite loop (my guess is, since signal missing while loop won't terminate). maybe can provide hint or link , for, or preferably tell me i'm doing wrong.

your while condition broken: signal != null true, signal array; , because of comma , operator entire condition true.

and because of following, you're not checking return value of fgets, means nothing read , buffer uninitialized.

also prev_line not initialized before atof on first time.


in case, compile -wall -werror , fix remaining errors.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -