sl@0: /* Portions copyright (c) 2009 Nokia Corporation. All rights reserved.*/ sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #define TRUE 1 sl@0: #define FALSE 0 sl@0: sl@0: static int n_children = 3; sl@0: static int n_active_children; sl@0: static int n_iters = 10000; sl@0: sl@0: static int write_fds[1024]; sl@0: static struct pollfd poll_fds[1024]; sl@0: sl@0: void sl@0: my_pipe (int *fds) sl@0: { sl@0: if (pipe(fds) < 0) sl@0: { sl@0: fprintf (stderr, "Cannot create pipe %s\n", strerror (errno)); sl@0: exit (1); sl@0: } sl@0: } sl@0: sl@0: int sl@0: read_all (int fd, char *buf, int len) sl@0: { sl@0: size_t bytes_read = 0; sl@0: ssize_t count; sl@0: sl@0: while (bytes_read < len) sl@0: { sl@0: count = read (fd, buf + bytes_read, len - bytes_read); sl@0: if (count < 0) sl@0: { sl@0: if (errno != EAGAIN) sl@0: return FALSE; sl@0: } sl@0: else if (count == 0) sl@0: return FALSE; sl@0: sl@0: bytes_read += count; sl@0: } sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: int sl@0: write_all (int fd, char *buf, int len) sl@0: { sl@0: size_t bytes_written = 0; sl@0: ssize_t count; sl@0: sl@0: while (bytes_written < len) sl@0: { sl@0: count = write (fd, buf + bytes_written, len - bytes_written); sl@0: if (count < 0) sl@0: { sl@0: if (errno != EAGAIN) sl@0: return FALSE; sl@0: } sl@0: sl@0: bytes_written += count; sl@0: } sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: void sl@0: run_child (int in_fd, int out_fd) sl@0: { sl@0: int i; sl@0: int val = 1; sl@0: sl@0: for (i = 0; i < n_iters; i++) sl@0: { sl@0: write_all (out_fd, (char *)&val, sizeof (val)); sl@0: read_all (in_fd, (char *)&val, sizeof (val)); sl@0: } sl@0: sl@0: val = 0; sl@0: write_all (out_fd, (char *)&val, sizeof (val)); sl@0: sl@0: exit (0); sl@0: } sl@0: sl@0: int sl@0: input_callback (int source, int dest) sl@0: { sl@0: int val; sl@0: sl@0: if (!read_all (source, (char *)&val, sizeof(val))) sl@0: { sl@0: fprintf (stderr,"Unexpected EOF\n"); sl@0: exit (1); sl@0: } sl@0: sl@0: if (val) sl@0: { sl@0: write_all (dest, (char *)&val, sizeof(val)); sl@0: return TRUE; sl@0: } sl@0: else sl@0: { sl@0: close (source); sl@0: close (dest); sl@0: sl@0: n_active_children--; sl@0: return FALSE; sl@0: } sl@0: } sl@0: sl@0: void sl@0: create_child (int pos) sl@0: { sl@0: int pid; sl@0: int in_fds[2]; sl@0: int out_fds[2]; sl@0: sl@0: my_pipe (in_fds); sl@0: my_pipe (out_fds); sl@0: sl@0: pid = fork (); sl@0: sl@0: if (pid > 0) /* Parent */ sl@0: { sl@0: close (in_fds[0]); sl@0: close (out_fds[1]); sl@0: sl@0: write_fds[pos] = in_fds[1]; sl@0: poll_fds[pos].fd = out_fds[0]; sl@0: poll_fds[pos].events = POLLIN; sl@0: } sl@0: else if (pid == 0) /* Child */ sl@0: { sl@0: close (in_fds[1]); sl@0: close (out_fds[0]); sl@0: sl@0: setsid (); sl@0: sl@0: run_child (in_fds[0], out_fds[1]); sl@0: } sl@0: else /* Error */ sl@0: { sl@0: fprintf (stderr,"Cannot fork: %s\n", strerror (errno)); sl@0: exit (1); sl@0: } sl@0: } sl@0: sl@0: static double sl@0: difftimeval (struct timeval *old, struct timeval *new) sl@0: { sl@0: return sl@0: (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000; sl@0: } sl@0: sl@0: int sl@0: main (int argc, char **argv) sl@0: { sl@0: int i, j; sl@0: struct rusage old_usage; sl@0: struct rusage new_usage; sl@0: sl@0: if (argc > 1) sl@0: n_children = atoi(argv[1]); sl@0: sl@0: if (argc > 2) sl@0: n_iters = atoi(argv[2]); sl@0: sl@0: printf ("Children: %d Iters: %d\n", n_children, n_iters); sl@0: sl@0: n_active_children = n_children; sl@0: for (i = 0; i < n_children; i++) sl@0: create_child (i); sl@0: sl@0: getrusage (RUSAGE_SELF, &old_usage); sl@0: sl@0: while (n_active_children > 0) sl@0: { sl@0: int old_n_active_children = n_active_children; sl@0: sl@0: poll (poll_fds, n_active_children, -1); sl@0: sl@0: for (i=0; i n_active_children) sl@0: { sl@0: j = 0; sl@0: for (i=0; i