EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: DiTBho on March 19, 2021, 03:54:09 pm

Title: pclose() waits for the associated process to terminate
Post by: DiTBho on March 19, 2021, 03:54:09 pm
Code: [Select]
    gnuplotPipe = popen("/usr/bin/gnuplot -persist", "w");
    if (gnuplotPipe)
    {
        fprintf(gnuplotPipe, "plot \"%s\" with lines\n", DataFileName);
        fflush(gnuplotPipe);
        DataFile = fopen(DataFileName, "w");
        for (i = 0; i < dataSize; i++)
        {
            x = data[i].x;
            y = data[i].y;
            fprintf(DataFile, "%lf %lf\n", x, y);
        }
        fclose(DataFile);
        fflush(gnuplotPipe);
        pclose(gnuplotPipe);
        remove(DataFileName);
    }

I am writing a piece of code that tries to open a pipe with gnuplot for plotting data.

According to the documentation, pclose() should wait for the associated process to terminate and returns the exit status of the command as returned by wait4().

However, there is something wrong since the program terminates with still gnuplot opened in a window.
 Doesn't pclose() wait for the gnuplot process to terminate? Where am I wrong?  :D
Title: Re: pclose() waits for the associated process to terminate
Post by: ejeffrey on March 19, 2021, 04:15:34 pm
http://www.bersch.net/gnuplot-doc/persist.html (http://www.bersch.net/gnuplot-doc/persist.html)

Will answer the question.
Title: Re: pclose() waits for the associated process to terminate
Post by: DiTBho on March 19, 2021, 04:40:00 pm
Quote
The persist option tells gnuplot to leave these windows open when the main program exits

So this is the problem. I assumed it pclose was able to force it to exit: my mistake  :D
Title: Re: pclose() waits for the associated process to terminate
Post by: DiTBho on March 19, 2021, 04:50:14 pm
Without "persistent" the program exits without anything plotted  :-//
Probably the GNUPlot x window is immediately terminated.
Title: Re: pclose() waits for the associated process to terminate
Post by: Nominal Animal on March 19, 2021, 05:53:21 pm
You'll probably want to add
    bind all "alt-End" "exit gnuplot"
    pause mouse close
after the plot command, and not use the persist option, so that the window exists (and pclose() blocks) until the user closes the window (by using the window close button, Ctrl-Q, or Alt+F4).
Title: Re: pclose() waits for the associated process to terminate
Post by: DiTBho on March 19, 2021, 06:46:32 pm
Perfect! This does the job, thank you!  :D