Author Topic: pclose() waits for the associated process to terminate  (Read 779 times)

0 Members and 1 Guest are viewing this topic.

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
pclose() waits for the associated process to terminate
« 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
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 3719
  • Country: us
Re: pclose() waits for the associated process to terminate
« Reply #1 on: March 19, 2021, 04:15:34 pm »
 
The following users thanked this post: DiTBho

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: pclose() waits for the associated process to terminate
« Reply #2 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
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: pclose() waits for the associated process to terminate
« Reply #3 on: March 19, 2021, 04:50:14 pm »
Without "persistent" the program exits without anything plotted  :-//
Probably the GNUPlot x window is immediately terminated.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6260
  • Country: fi
    • My home page and email address
Re: pclose() waits for the associated process to terminate
« Reply #4 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).
 
The following users thanked this post: DiTBho

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: pclose() waits for the associated process to terminate
« Reply #5 on: March 19, 2021, 06:46:32 pm »
Perfect! This does the job, thank you!  :D

The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf