Code snippet using attach/detach calls

QNX SDP8.0High Availability Framework Developer's GuideDeveloper
...
ham_entity_t *ehdl;
int status;
ehdl = ham_attach("ptpd2", 0, -1, "/usr/sbin/ptpd2 -K", 0);
/* ptpd2 is started, running and monitored now */
...
...
status = ham_detach(ehdl,0);
...
...

Of course the attach and detach needn't necessarily be performed by the same caller:

...
ham_entity_t *ehdl;
int status;
/* starts and begins monitoring ptpd2 */
ehdl = ham_attach("ptpd2", 0, -1, "/usr/sbin/ptpd2 -K", 0);
...
...
/* disconnect from Ham (monitoring still continues) */
exit(0);

And to detach ptpd2:

...
int status;
/* stops monitoring ptpd2. */
status = ham_detach_name(0, "ptpd2", 0);
...
exit(0);

If ptpd2 were already running, say with pid 105328676, then we can write the attach/detach code as follows:

ham_entity_t *ehdl;
int status;
ehdl = ham_attach("ptpd2", 0, 105328676, NULL, 0);
...
...
status = ham_detach(ehdl,0);
/* status = ham_detach_name(0, "ptpd2",0); */
...
...
exit(0);

For convenience, the ham_attach() and ham_detach() functions connect to a HAM if such a connection doesn't already exist. We do this only to make the use of the functions easier.

The connections to a HAM persist only for the duration of the attach/detach calls; any subsequent requests to the HAM must be preceded by the appropriate ham_connect() calls.

The best way to perform a large sequence of requests to a HAM is to:

  1. Call ham_connect() before the first request.
  2. Call ham_disconnect() after the last request.

This is the most efficient method, because it guarantees that there's always the same connection open to the HAM.

Page updated: