user4102404
user4102404

Reputation: 33

Creating multiple children of a process and maintaining a shared array of all their PIDs

I have forked a couple of times and have created a bunch of child processes in C. I want to store all their PIDs in a shared array. The ordering of the PIDs does not matter. For instance, I created 32 processes. I would like to have a 32 integer long array that would store each of their PIDs and is accessible to each of these processes. What could be the best way to do this.

Upvotes: 1

Views: 7592

Answers (1)

ThomasH
ThomasH

Reputation: 1165

Here's a program that illustrates what you want using mmap():

#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PIDS 32

volatile pid_t *pids;

// Called for each child process
void do_child(void)
{
  int c;
  printf("Child: %d - pid array: ", getpid());

  for (c=0; c<10; c++) {
    printf("%d%s", pids[c], c==9?"\n":" ");
  }
}

int main(int argc, char **argv)
{
  int c;
  pid_t pid;

  // Map space for shared array
  pids = mmap(0, MAX_PIDS*sizeof(pid_t), PROT_READ|PROT_WRITE,
              MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  if (!pids) {
    perror("mmap failed");
    exit(1);
  }
  memset((void *)pids, 0, MAX_PIDS*sizeof(pid_t));

  // Fork children
  for (c=0; c<10; c++) {
    pid = fork();
    if (pid == 0) {
      // Child process
      do_child();
      exit(0);
    } else if (pid < 0) {
      perror("fork failed");
    } else {
      // Store in global array for children to see
      pids[c] = pid;
      sleep(1);
    }
  }
  exit(0);
}

Upvotes: 3

Related Questions