Example: Run tasks on many machines

This example program uses ls_rtask() to run rm -f /tmp/core on user specified hosts.

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <lsf/lsf.h>
int main (int argc, char **argv)
{
    char *command[4];
    int numHosts;
    int i;
    int tid;
    if (argc <= 1) {
        printf("Usage: %s host1 [host2 ...]\n",argv[0]);
        exit(-1);
    }
    numHosts = argc - 1;
    command[0]="rm";
    command[1]="-f";
    command[2]="/tmp/core";
    command[3] = NULL;
    if (ls_initrex(numHosts, 0) < 0) {
        ls_perror("ls_initrex");
        exit(-1);
    }
    signal(SIGUSR1, SIG_IGN);
    /* Run command on the specified hosts */
    for (i=1; i<=numHosts; i++) {
        if ((tid = ls_rtask(argv[i], command, 0)) < 0) {
            fprintf(stderr, "lsrtask failed for host %s: %s\n", 
                    argv[i], ls_sysmsg());
            exit(-1);
        }
        printf("Task %d started on %s\n", tid, argv[i]);
    }
    while (numHosts) {
        LS_WAIT_T status;
        tid = ls_rwait(&status, 0, NULL);
        if (tid < 0) {
            ls_perror("ls_rwait");
            exit(-1);
        }
        
        printf("task %d finished\n", tid);
        numHosts--;
    }
    exit(0);
} 

The above program sets the signal handler for SIGUSR1 to SIG_IGN. This causes the signal to be ignored. It uses ls_rwait() to poll the status of remote tasks. You could set a signal handler so that it calls ls_rwait() inside the signal handler.

Use the task ID to preform an operation on the task. For example, you can send a signal to a remote task explicitly by calling ls_rkill().

To run the task on remote hosts one after another instead of concurrently, call ls_rwait() right after ls_rtask().

Also note the use of ls_sysmsg() instead of ls_perror(), which does not allow flexible printing format.

The above example program produces output similar to the following:

% a.out hostD hostA hostB
Task 1 started on hostD
Task 2 started on hostA
Task 3 started on hostB
Task 1 finished
Task 3 finished
Task 2 finished

Remote tasks are run concurrently, so the order in which tasks finish is not necessarily the same as the order in which tasks are started.