diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-09-13 13:36:09 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-09-18 23:07:12 -0400 |
commit | 17d3d0517123f5f07b4ac6bb9f1dec73c1c8ce4c (patch) | |
tree | d5317f2cb8c3c13f831fb37d1cd28d7d56c45bac | |
parent | 4e0e40bf54f68a98b2cfbb419c8d1cbabf7986ba (diff) | |
download | stasis-17d3d0517123f5f07b4ac6bb9f1dec73c1c8ce4c.tar.gz |
Fix sem_open initial state
* Move slot->gate assignment to mp_pool_task()
* Remove mmap() to slot->gate.
* Change type of ident and log_root variables for the sake of easy (fewer maps)
-rw-r--r-- | include/multiprocessing.h | 4 | ||||
-rw-r--r-- | src/multiprocessing.c | 41 |
2 files changed, 16 insertions, 29 deletions
diff --git a/include/multiprocessing.h b/include/multiprocessing.h index 648bd80..f0e6e5c 100644 --- a/include/multiprocessing.h +++ b/include/multiprocessing.h @@ -24,8 +24,8 @@ struct MultiProcessingPool { struct MultiProcessingTask *task; ///< Array of tasks to execute size_t num_used; ///< Number of tasks populated in the task array size_t num_alloc; ///< Number of tasks allocated by the task array - const char *ident; ///< Identity of task pool - const char *log_root; ///< Base directory to store stderr/stdout log files + char ident[255]; ///< Identity of task pool + char log_root[PATH_MAX]; ///< Base directory to store stderr/stdout log files }; ///!< Maximum number of multiprocessing tasks STASIS can execute diff --git a/src/multiprocessing.c b/src/multiprocessing.c index 76bd241..ad6dec5 100644 --- a/src/multiprocessing.c +++ b/src/multiprocessing.c @@ -35,6 +35,15 @@ struct MultiProcessingTask *mp_task(struct MultiProcessingPool *pool, const char fflush(tp); fclose(tp); + char sema_name[PATH_MAX] = {0}; + sprintf(sema_name, "/sem-%zu-%s-%s", mp_global_task_count, pool->ident, slot->ident); + sem_unlink(sema_name); + slot->gate = sem_open(sema_name, O_CREAT, 0644, 0); + if (slot->gate == SEM_FAILED) { + perror("sem_open failed"); + exit(1); + } + pid_t pid = fork(); int child_status = 0; if (pid == -1) { @@ -275,10 +284,12 @@ struct MultiProcessingPool *mp_pool_init(const char *ident, const char *log_root return NULL; } - //pool = malloc(1 * sizeof(*pool)); pool = mmap(NULL, sizeof(*pool), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); - pool->ident = ident; - pool->log_root = log_root; + memset(pool->ident, 0, sizeof(pool->ident)); + strncpy(pool->ident, ident, sizeof(pool->ident) - 1); + + memset(pool->log_root, 0, sizeof(pool->log_root)); + strncpy(pool->log_root, log_root, sizeof(pool->log_root) - 1); pool->num_used = 0; pool->num_alloc = MP_POOL_TASK_MAX; @@ -290,7 +301,6 @@ struct MultiProcessingPool *mp_pool_init(const char *ident, const char *log_root } } - //pool->task = calloc(pool->num_alloc + 1, sizeof(*pool->task)); pool->task = mmap(NULL, (pool->num_alloc + 1) * sizeof(*pool->task), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (pool->task == MAP_FAILED) { perror("mmap"); @@ -298,25 +308,6 @@ struct MultiProcessingPool *mp_pool_init(const char *ident, const char *log_root return NULL; } - for (size_t i = 0; i < pool->num_alloc; i++) { - struct MultiProcessingTask *slot = &pool->task[i]; - slot->gate = mmap(NULL, sizeof(*slot->gate), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); - if (slot->gate == MAP_FAILED) { - perror("mmap failed"); - exit(1); - } - - char sema_name[PATH_MAX] = {0}; - sprintf(sema_name, "/sem-%zu-%s-%s", mp_global_task_count, pool->ident, slot->ident); - sem_unlink(sema_name); - slot->gate = sem_open(sema_name, O_CREAT, 0600, sizeof(*slot->gate)); - if (slot->gate == SEM_FAILED) { - perror("sem_init failed"); - exit(1); - } - } - - return pool; } @@ -327,10 +318,6 @@ void mp_pool_free(struct MultiProcessingPool **pool) { perror("sem_close failed"); exit(1); } - if (munmap((*pool)->task[i].gate, sizeof(*(*pool)->task[i].gate)) < 0) { - perror("munmap"); - exit(1); - } } } if ((*pool)->task) { |