Sourceware Bugzilla – Attachment 13362 Details for
Bug 25951
support for parallel processing?
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
Demonstator source file using seperate reaper/coordinator
pipes.c (text/x-csrc), 2.79 KB, created by
Tom de Vries
on 2021-04-12 08:22:20 UTC
(
hide
)
Description:
Demonstator source file using seperate reaper/coordinator
Filename:
MIME Type:
Creator:
Tom de Vries
Created:
2021-04-12 08:22:20 UTC
Size:
2.79 KB
patch
obsolete
>#include <unistd.h> >#include <assert.h> >#include <stdlib.h> >#include <stdio.h> >#include <sys/types.h> >#include <sys/wait.h> > >#define N 5 > >#define READ_FD 0 >#define WRITE_FD 1 >#define FDS_PER_PIPE 2 > >#define PARENT_TO_CHILD 0 >#define CHILD_TO_PARENT 1 >#define PIPES_PER_CHILD 2 > >int pipes[N][PIPES_PER_CHILD][FDS_PER_PIPE]; > >/* Get token. */ >static char >get_token (int n, int m, const char *msg) >{ > int readfd = pipes[n][m][READ_FD]; > int writefd = pipes[n][m][WRITE_FD]; > //close (writefd); > char buf; > fprintf (stderr, "%s: READING TOKEN for child %d direction %s\n", msg, n, > m == PARENT_TO_CHILD ? "P2C" : "C2P"); > read (readfd, &buf, 1); > fprintf (stderr, "%s: READ TOKEN for child %d direction %s: '%c'\n", msg, n, > m == PARENT_TO_CHILD ? "P2C" : "C2P", buf); > //close (readfd); > return buf; >} > >/* Pass token to child N. */ >static void >pass_token (int n, int m, char buf, char *msg) >{ > int readfd = pipes[n][m][READ_FD]; > int writefd = pipes[n][m][WRITE_FD]; > //close (readfd); > fprintf (stderr, "%s: WRITING TOKEN for child %d direction %s: '%c'\n", msg, n, > m == PARENT_TO_CHILD ? "P2C" : "C2P", buf); > write (writefd, &buf, 1); > fprintf (stderr, "%s: WROTE TOKEN for child %d direction %s\n", msg, n, > m == PARENT_TO_CHILD ? "P2C" : "C2P"); > //close (writefd); >} > > >int >main (void) >{ > > int i; > pid_t pids[N]; > > for (i = 0; i < N; i++) > { > int j; > for (j = 0; j < PIPES_PER_CHILD; ++j) > { > int fds[FDS_PER_PIPE]; > if (pipe (fds) != 0) > abort (); > pipes[i][j][READ_FD] = fds[READ_FD]; > pipes[i][j][WRITE_FD] = fds[WRITE_FD]; > } > } > > for (i = 0; i < N; ++i) > { > pid_t fork_res = fork (); > assert (fork_res != -1); > if (fork_res == 0) > { > int child_id = i; > sleep (1); > > /* Let's have one of the children not pass a token. */ > if (child_id == 3) > return 0; > > pass_token (child_id, CHILD_TO_PARENT, '0', "Child"); > char c = get_token (child_id, PARENT_TO_CHILD, "Child"); > assert (c == '1'); > return 0; > } > else > { > pids[i] = fork_res; > } > } > > pid_t coordinator_pid = 0; > pid_t fork_res = fork (); > assert (fork_res != -1); > if (fork_res == 0) > { > /* Coordinator. */ > for (i = 0; i < N; ++i) > { > int state; > char c = get_token (i, CHILD_TO_PARENT, "Coordinator"); > if (c == '0') > { > pass_token (i, PARENT_TO_CHILD, '1', "Coordinator"); > } > else if (c == '2') > { > /* Child terminated. */ > } > else > abort (); > } > > return 0; > } > else > { > coordinator_pid = fork_res; > } > > for (i = 0; i < N; ++i) > { > int state; > waitpid (pids[i], &state, 0); > fprintf (stderr, "Parent: CHILD TERMINATED: %d\n", i); > pass_token (i, CHILD_TO_PARENT, '2', "Parent"); > } > > { > int state; > waitpid (coordinator_pid, &state, 0); > } > > return 0; >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 25951
:
13297
| 13362