diff options
Diffstat (limited to 'addjob.c')
-rw-r--r-- | addjob.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/addjob.c b/addjob.c new file mode 100644 index 0000000..db6a9d3 --- /dev/null +++ b/addjob.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include "shared.h" + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s <path-to-job-binary>\n", argv[0]); + return 1; + } + + int shm_fd = shm_open(SHM_NAME, O_RDWR, 0666); + if (shm_fd == -1) { + perror("shm_open"); + return 1; + } + + JobQueue *queue = mmap(NULL, sizeof(JobQueue), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (queue == MAP_FAILED) { + perror("mmap"); + return 1; + } + + pthread_mutex_lock(&queue->mutex); + if ((queue->tail + 1) % MAX_JOBS == queue->head) { + printf("[addjob] Queue full\n"); + } else { + strncpy(queue->jobs[queue->tail], argv[1], MAX_PATH); + queue->tail = (queue->tail + 1) % MAX_JOBS; + printf("[addjob] Queued job: %s\n", argv[1]); + } + pthread_mutex_unlock(&queue->mutex); + + munmap(queue, sizeof(JobQueue)); + close(shm_fd); + return 0; +} |