← Programming examples · libnex
06_coda.c
The file alone · in the libnex-dev package: /usr/share/doc/libnex-dev/examples/
/*
-------------------------------------------------------------------------------------------------
_ ____ _ _____ ___ ___ _ _ ____ _____ ____ _ _ _____ ____ ___ _
/ \ | _ \ / \|_ _/ _ \ |_ _| \ | |/ ___| ____/ ___| \ | | ____| _ \|_ _| / \
/ _ \ | |_) | / _ \ | || | | | | || \| | | _| _|| | _| \| | _| | |_) || | / _ \
/ ___ \| _ < / ___ \| || |_| | | || |\ | |_| | |__| |_| | |\ | |___| _ < | | / ___ \
/_/ \_\_| \_\/_/ \_\_| \___/ |___|_| \_|\____|_____\____|_| \_|_____|_| \_\___/_/ \_\
-------------------------------------------------------------------------------------------------
Copyright (c) 2026 Arato Ingegneria. Tutti i diritti riservati.
Name : 06_coda.c
Author : Ing. Roberto arato
Version : 1.0.0
Copyright : Ing. Roberto arato (c) 2026
Description : Esempio 6 - coda dei lavori: tante esecuzioni che partono quando c'e' CPU libera
-------------------------------------------------------------------------------------------------
*/
/*
nex_submit mette un lavoro nella coda del cluster: parte, come lavoro staccato, su
un nodo compute appena ci sono CPU libere (qui ogni lavoro prenota mezza CPU: "50").
Utile per gli studi parametrici: si accodano tutti i casi e il cluster li smaltisce.
nex_queue mostra lo stato (queued, claimed, running, done) e il codice d'uscita;
nex_queue_cancel toglie una voce ancora in attesa.
Uso: cd /nex/...; ./06_coda [N]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nex.h>
// Valore della chiave key nel JSON (la prima che compare), testo o numero, senza
// virgolette: basta per l'esempio, in un programma vero si usa una libreria JSON.
// 0 se la chiave non c'e'.
static int valore(const char *json, const char *key, char *out, size_t len) {
char pat[64];
snprintf(pat, sizeof(pat), "\"%s\"", key);
const char *p = json ? strstr(json, pat) : NULL;
out[0] = 0;
if (p == NULL) return 0;
p += strlen(pat);
while (*p == ' ' || *p == ':') p++; // "key":"v" e "key": "v"
if (*p == '"') p++;
snprintf(out, len, "%.*s", (int)strcspn(p, "\",}] "), p);
return 1;
}
int main(int argc, char **argv) {
int n = argc > 1 ? atoi(argv[1]) : 5;
char cwd[4096], qid[64][24];
if (n < 1 || n > 64 || getcwd(cwd, sizeof(cwd)) == NULL) return 2;
nex_conn *nd = nex_open(NULL);
if (nd == NULL) { fprintf(stderr, "agent non raggiungibile\n"); return 1; }
for (int i = 0; i < n; i++) {
char cmd[160], *resp = NULL;
snprintf(cmd, sizeof(cmd), "echo caso %d su $(hostname) > caso.%d; sleep 2", i, i);
const char *args[] = { "sh", "-c", cmd, NULL };
// nodo NULL = qualunque, cpu "50", memoria "256M", priorita' 5 (0..9), quiet 1
if (nex_submit(nd, NULL, args, cwd, NULL, "50", "256M", 5, 1, &resp) != NEX_OK) {
fprintf(stderr, "submit %d: %s\n", i, resp ? resp : "?");
return 1;
}
valore(resp, "queue", qid[i], sizeof(qid[i])); // {"queue":"q1a2b3c","position":N}
printf("caso %d in coda come %s\n", i, qid[i]);
nex_free(resp);
}
// Si aspetta che tutte le voci siano "done", guardando la coda ogni secondo.
for (int giro = 0; giro < 300; giro++) {
char *resp = NULL;
if (nex_queue(nd, &resp) != NEX_OK) break;
int fatti = 0;
for (int i = 0; i < n; i++) {
// la voce e' un oggetto {"qid":"...",...,"state":"..."}: lo stato e' il primo dopo il qid
const char *p = strstr(resp, qid[i]);
char stato[16];
if (p == NULL || !valore(p, "state", stato, sizeof(stato)) || strcmp(stato, "done") == 0) fatti++;
}
nex_free(resp);
if (fatti == n) { printf("tutti i %d casi finiti in %d s\n", n, giro); break; }
sleep(1);
}
nex_close(nd);
return 0;
}