Synchronize forked child processes by task
I am very confused about how to use different sync methods.
I am working with this general structure of code:
int i, pnum;
pid_t pid;
for(i = 0; i < NUM_PROCS; ++i) {
if ((pid = fork()) < 0) {
perror("fork failed. . .\n");
exit(-1);
}
else if (pid == 0) {
pnum = i;
break;
}
}
if (pid == 0) {
for (i = 0; i < NUM_ITER; ++i) {
// DO SECTION A
// DO SECTION B
// DO SECTION C
}
exit(0);
}
else {
for (i = 0; i < NUM_ITER; ++i) {
// DO SECTION A
// DO SECTION B
// DO SECTION C
}
}
for (i = 0; i < NUM_PROCS; ++i) wait(NULL);
What is the simplest way to ensure that all processes do partition A first, then B, then C and never crash? My purpose involves using pipes in each section to transfer the data of the array, and I was under the impression that the read would block until the array was read. I have 2 channels from parent to each child and then between each child and its entanglement, since I have ALOT read and write. There must be an easier way?
source to share
I hacked the example code (on Linux and maybe some other systems, compile with a flag -pthread
):
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#define SECTIONS 3
#define NUM_PROCS 5
struct sectionguards
{
sem_t enter_clear;
sem_t leave_clear;
sem_t count_lock;
int count;
};
static struct sectionguards *guards;
static void init_guards(void)
{
int shmid, i;
void *shmaddr;
shmid = shmget(IPC_PRIVATE, SECTIONS * sizeof(struct sectionguards), 00600);
if (shmid < 0)
{
perror("shmget");
exit(EXIT_FAILURE);
}
shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (void *)-1)
{
perror("shmat");
exit(EXIT_FAILURE);
}
guards = shmaddr;
for (i = 0; i < SECTIONS; ++i)
{
if (sem_init(&(guards[i].enter_clear), 1, 1) < 0)
{
perror("sem_init");
exit(EXIT_FAILURE);
}
if (sem_init(&(guards[i].leave_clear), 1, 0) < 0)
{
perror("sem_init");
exit(EXIT_FAILURE);
}
if (sem_init(&(guards[i].count_lock), 1, 1) < 0)
{
perror("sem_init");
exit(EXIT_FAILURE);
}
guards[i].count = 0;
}
}
static void enter(int section)
{
int next_section = section + 1;
if (next_section == SECTIONS) next_section = 0;
sem_wait(&(guards[section].enter_clear));
sem_post(&(guards[section].enter_clear));
sem_wait(&(guards[section].count_lock));
if (!(guards[section].count)++)
{
sem_wait(&(guards[next_section].enter_clear));
}
if (guards[section].count == NUM_PROCS)
{
sem_post(&(guards[section].leave_clear));
}
sem_post(&(guards[section].count_lock));
}
static void leave(int section)
{
int next_section = section + 1;
if (next_section == SECTIONS) next_section = 0;
sem_wait(&(guards[section].leave_clear));
sem_post(&(guards[section].leave_clear));
sem_wait(&(guards[section].count_lock));
if (!--(guards[section].count))
{
sem_post(&(guards[next_section].enter_clear));
sem_wait(&(guards[section].leave_clear));
}
sem_post(&(guards[section].count_lock));
}
int main(void)
{
int i, pnum;
pid_t pid;
init_guards();
pnum = 5;
for(i = 1; i < NUM_PROCS; ++i) {
if ((pid = fork()) < 0) {
perror("fork failed. . .\n");
exit(-1);
}
else if (pid == 0) {
pnum = i;
break;
}
}
if (pid == 0) {
for (i = 0; i < 5; ++i) {
enter(0);
printf("Worker %d in a\n", pnum);
leave(0);
enter(1);
printf("Worker %d in b\n", pnum);
leave(1);
enter(2);
printf("Worker %d in c\n", pnum);
leave(2);
}
exit(0);
}
else {
for (i = 0; i < 5; ++i) {
enter(0);
printf("Worker %d in a\n", pnum);
leave(0);
enter(1);
printf("Worker %d in b\n", pnum);
leave(1);
enter(2);
printf("Worker %d in c\n", pnum);
leave(2);
}
}
for (i = 1; i < NUM_PROCS; ++i) wait(NULL);
return EXIT_SUCCESS;
}
It is not completely bulletproof because a process can enter and leave a section in a package, thus marking the next section "clear". But this will not happen once you have a blocking call inside each section.
Now he bulletproof by introducing a second semaphore which ensures that no process can leave the partition before everyone enters it.
Further edit: Now only use semaphores. The security that only the locking thread can unlock is lost, but this is not a big issue here. Advantage of not using any pthread functions for non-threaded code. On Linux you still need libpthread, some other systems (like FreeBSD) have semaphore functions in the C standard library.
source to share