From: Thierry FOURNIER Date: Fri, 23 Feb 2018 12:50:26 +0000 (+0100) Subject: MINOR: spoa-server: Replace the thread init system by processes X-Git-Tag: v2.0-dev3~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7de6fc6ae17ec03c97c90a87850fa66bec0b437e;p=thirdparty%2Fhaproxy.git MINOR: spoa-server: Replace the thread init system by processes I will replace thread by processes. Note that, I keep the pthread_key system for identifiying process in the same way that threads. Note also that I keep commented out the original thread code because I hope to reactivate it. --- diff --git a/contrib/spoa_server/spoa.c b/contrib/spoa_server/spoa.c index b08c1d6967..c7625414e5 100644 --- a/contrib/spoa_server/spoa.c +++ b/contrib/spoa_server/spoa.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -973,7 +974,21 @@ spoa_worker(void *data) out: free(info); +#if 0 pthread_exit(NULL); +#endif + return NULL; +} + +int process_create(pid_t *pid, void *(*ps)(void *), void *data) +{ + *pid = fork(); + if (*pid == -1) + return -1; + if (*pid > 0) + return 0; + ps(data); + return 0; } static void @@ -989,9 +1004,13 @@ usage(char *prog) int main(int argc, char **argv) { +#if 0 pthread_t *ts = NULL; +#endif + pid_t *pids; struct sockaddr_in server; int i, sock, opt, nbworkers, port; + int status; nbworkers = NUM_WORKERS; port = DEFAULT_PORT; @@ -1049,13 +1068,20 @@ main(int argc, char **argv) } fprintf(stderr, "SPOA is listening on port %d\n", port); - ts = calloc(nbworkers, sizeof(*ts)); pthread_key_create(&worker_id, NULL); + + /* Initialise the server in thread mode. This code is commented + * out and not deleted, because later I expect to work with + * process ansd threads. This first version just support processes. + */ +#if 0 + ts = calloc(nbworkers, sizeof(*ts)); for (i = 0; i < nbworkers; i++) { int *info = calloc(2, sizeof(*info)); info[0] = sock; info[1] = i+1; + if (pthread_create(&ts[i], NULL, spoa_worker, info) < 0) { fprintf(stderr, "Failed to create thread %d: %m\n", i+1); goto error; @@ -1067,11 +1093,35 @@ main(int argc, char **argv) pthread_join(ts[i], NULL); fprintf(stderr, "SPOA worker %02d stopped\n", i+1); } - pthread_key_delete(worker_id); free(ts); +#endif + + /* Start processes */ + pids = calloc(nbworkers, sizeof(*pids)); + if (!pids) { + fprintf(stderr, "Out of memory error\n"); + goto error; + } + for (i = 0; i < nbworkers; i++) { + int *info = calloc(2, sizeof(*info)); + + info[0] = sock; + info[1] = i+1; + + if (process_create(&pids[i], spoa_worker, info) == -1) { + fprintf(stderr, "SPOA worker %02d started\n", i+1); + goto error; + } + fprintf(stderr, "SPOA worker %02d started\n", i+1); + } + for (i = 0; i < nbworkers; i++) { + waitpid(pids[0], &status, 0); + fprintf(stderr, "SPOA worker %02d stopped\n", i+1); + } + close(sock); + pthread_key_delete(worker_id); return EXIT_SUCCESS; error: - free(ts); return EXIT_FAILURE; }