Skip to content

Instantly share code, notes, and snippets.

@krackers
Forked from kazupon/Makefile
Created September 19, 2023 04:51
Show Gist options
  • Save krackers/49d2d84591be2b6d056479bdc5898601 to your computer and use it in GitHub Desktop.
Save krackers/49d2d84591be2b6d056479bdc5898601 to your computer and use it in GitHub Desktop.
semaphore multi process sample for Mac OSX
CC=gcc
all: multi-process-counter
build: multi-process-counter
multi-process-counter:
${CC} -std=c99 -o multi-process-counter multi-process-counter.c
#${CC} -std=c99 -arch x86_64 -o multi-process-counter multi-process-counter.c
clean:
rm multi-process-counter
#include <stdio.h>
#include <stdlib.h>
#include <mach/mach_init.h>
#include <mach/mach_error.h>
#include <mach/semaphore.h>
#include <mach/task.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/* global */
semaphore_t mutex;
int main() {
int fd, i, nloop, zero = 0;
int *ptr = NULL;
nloop = 50000;
fd = open("./share", O_RDWR | O_CREAT, FILE_MODE);
write(fd, &zero, sizeof(int));
ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
printf("ready mmap: %d\n", getpid());
mach_port_t self = mach_task_self();
kern_return_t ret = semaphore_create(self, &mutex, SYNC_POLICY_FIFO, 1);
if (ret != KERN_SUCCESS) {
fprintf(stderr, "semaphore create failed. Error <%d, %s>", ret, mach_error_string(ret));
return ret;
}
setbuf(stdout, NULL);
/* child */
if (fork() == 0) {
for (i = 0; i < nloop; i++) {
ret = semaphore_wait(mutex);
(*ptr)++;
(*ptr)--;
(*ptr)++;
printf("child (%d): %d\n", getpid(), *ptr);
ret = semaphore_signal(mutex);
}
exit(0);
} else {
/* parent */
for (i = 0; i < nloop; i++) {
ret = semaphore_wait(mutex);
(*ptr)++;
(*ptr)--;
(*ptr)++;
printf("parent (%d): %d\n", getpid(), *ptr);
ret = semaphore_signal(mutex);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment