aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/generic_parent.c
blob: dd8f11d272364b52b1b167c659d8a79591dac199 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* klaar@ida

   Will try to start N child-processes, giving each an increasing
   number starting on S as argument to main. Will return S+C where C
   is the number of sucessfully started children.

   Normally used from some parent program.
 */

#include <syscall.h>
#include <stdlib.h>
#include <stdio.h>

#define BUF_SIZE 64

int main(int argc, char* argv[])
{
  int i;
  char cmd[BUF_SIZE];
  char* child;
  int start;
  int count;

  if (argc != 4)
  {
     printf("Usage: %s CHILD S N\n", argv[0]);
     printf("%s starts N copies of CHILD.\n", argv[0]);
     printf("Each child receives an unique integer I as first parameter in the range S..(S+N-1).\n");
     printf("Returns S + N on success, S + I if only I children could be started.\n");
     return -1;
  }
  
  child = argv[1];
  start = atoi(argv[2]);
  count = atoi(argv[3]);
  
  for(i = 0; i < count; i++)
  {
     snprintf(cmd, BUF_SIZE, "%s %i", child, start + i);
     if (exec(cmd) == -1)
     {
	printf("!! ERROR !!\n");
	printf("Could not start '%s'\n", cmd);
	break;
     }
  }
  exit(start + i);
}