Closing The Connection To The Spawned Process

The Expect library provides no special functions to close the connection to a spawned process. Generally, it is sufficient to call close. If you have converted the file descriptor to a stream (or used exp_popen which returns a stream), call fclose instead.

Once the process exits, it should be waited upon in order to free up the process slot. When convenient, you can wait for the process using any of the wait family of calls. You can also catch SIGCHLD before waiting, or you can ignore SIGCHLD entirely. Further discussion on this can be found in any UNIX system programming text.

As described in Chapter 4 (p. 103), some processes do not automatically terminate when their standard input is closed. You may have to send them explicit commands to exit, or alternatively you can kill them outright. As described above, the exp_pid variable provides the process id of the most recently spawned process.

There is no matching exp_pclose to exp_popen (unlike popen and pclose). It only takes two functions to close down a connection (fclose followed by waiting on the process id), but it is not uncommon to separate these two actions by large time intervals, so providing a new function for this purpose is of little value. Just close the stream using fclose and wait for the process.

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.