clang-format
This commit is contained in:
@@ -81,74 +81,52 @@
|
||||
*/
|
||||
|
||||
struct usem {
|
||||
char name[32];
|
||||
int fd;
|
||||
char name[32];
|
||||
int fd;
|
||||
};
|
||||
|
||||
static
|
||||
void
|
||||
semcreate(const char *tag, struct usem *sem)
|
||||
{
|
||||
int fd;
|
||||
static void semcreate(const char *tag, struct usem *sem) {
|
||||
int fd;
|
||||
|
||||
snprintf(sem->name, sizeof(sem->name), "sem:multiexec.%s.%d",
|
||||
tag, (int)getpid());
|
||||
snprintf(sem->name, sizeof(sem->name), "sem:multiexec.%s.%d", tag,
|
||||
(int)getpid());
|
||||
|
||||
fd = open(sem->name, O_WRONLY|O_CREAT|O_TRUNC, 0664);
|
||||
if (fd < 0) {
|
||||
err(1, "%s: create", sem->name);
|
||||
}
|
||||
close(fd);
|
||||
fd = open(sem->name, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
||||
if (fd < 0) {
|
||||
err(1, "%s: create", sem->name);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
semopen(struct usem *sem)
|
||||
{
|
||||
sem->fd = open(sem->name, O_RDWR, 0664);
|
||||
if (sem->fd < 0) {
|
||||
err(1, "%s: open", sem->name);
|
||||
}
|
||||
static void semopen(struct usem *sem) {
|
||||
sem->fd = open(sem->name, O_RDWR, 0664);
|
||||
if (sem->fd < 0) {
|
||||
err(1, "%s: open", sem->name);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
semclose(struct usem *sem)
|
||||
{
|
||||
close(sem->fd);
|
||||
static void semclose(struct usem *sem) { close(sem->fd); }
|
||||
|
||||
static void semdestroy(struct usem *sem) { remove(sem->name); }
|
||||
|
||||
static void semP(struct usem *sem, size_t num) {
|
||||
char c[num];
|
||||
|
||||
if (read(sem->fd, c, num) < 0) {
|
||||
err(1, "%s: read", sem->name);
|
||||
}
|
||||
(void)c;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
semdestroy(struct usem *sem)
|
||||
{
|
||||
remove(sem->name);
|
||||
}
|
||||
static void semV(struct usem *sem, size_t num) {
|
||||
char c[num];
|
||||
|
||||
static
|
||||
void
|
||||
semP(struct usem *sem, size_t num)
|
||||
{
|
||||
char c[num];
|
||||
/* semfs does not use these values, but be conservative */
|
||||
memset(c, 0, num);
|
||||
|
||||
if (read(sem->fd, c, num) < 0) {
|
||||
err(1, "%s: read", sem->name);
|
||||
}
|
||||
(void)c;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
semV(struct usem *sem, size_t num)
|
||||
{
|
||||
char c[num];
|
||||
|
||||
/* semfs does not use these values, but be conservative */
|
||||
memset(c, 0, num);
|
||||
|
||||
if (write(sem->fd, c, num) < 0) {
|
||||
err(1, "%s: write", sem->name);
|
||||
}
|
||||
if (write(sem->fd, c, num) < 0) {
|
||||
err(1, "%s: write", sem->name);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
@@ -158,115 +136,105 @@ semV(struct usem *sem, size_t num)
|
||||
static char *subargv[SUBARGC_MAX];
|
||||
static int subargc = 0;
|
||||
|
||||
static
|
||||
void
|
||||
spawn(int njobs)
|
||||
{
|
||||
struct usem s1, s2;
|
||||
pid_t pids[njobs];
|
||||
int failed, status;
|
||||
int i;
|
||||
static void spawn(int njobs) {
|
||||
struct usem s1, s2;
|
||||
pid_t pids[njobs];
|
||||
int failed, status;
|
||||
int i;
|
||||
|
||||
semcreate("1", &s1);
|
||||
semcreate("2", &s2);
|
||||
semcreate("1", &s1);
|
||||
semcreate("2", &s2);
|
||||
|
||||
printf("Forking %d child processes...\n", njobs);
|
||||
printf("Forking %d child processes...\n", njobs);
|
||||
|
||||
for (i=0; i<njobs; i++) {
|
||||
pids[i] = fork();
|
||||
if (pids[i] == -1) {
|
||||
/* continue with the procs we have; cannot kill them */
|
||||
warn("fork");
|
||||
warnx("*** Only started %u processes ***", i);
|
||||
njobs = i;
|
||||
break;
|
||||
}
|
||||
if (pids[i] == 0) {
|
||||
/* child */
|
||||
semopen(&s1);
|
||||
semopen(&s2);
|
||||
semV(&s1, 1);
|
||||
semP(&s2, 1);
|
||||
semclose(&s1);
|
||||
semclose(&s2);
|
||||
execv(subargv[0], subargv);
|
||||
warn("execv: %s", subargv[0]);
|
||||
_exit(1);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < njobs; i++) {
|
||||
pids[i] = fork();
|
||||
if (pids[i] == -1) {
|
||||
/* continue with the procs we have; cannot kill them */
|
||||
warn("fork");
|
||||
warnx("*** Only started %u processes ***", i);
|
||||
njobs = i;
|
||||
break;
|
||||
}
|
||||
if (pids[i] == 0) {
|
||||
/* child */
|
||||
semopen(&s1);
|
||||
semopen(&s2);
|
||||
semV(&s1, 1);
|
||||
semP(&s2, 1);
|
||||
semclose(&s1);
|
||||
semclose(&s2);
|
||||
execv(subargv[0], subargv);
|
||||
warn("execv: %s", subargv[0]);
|
||||
_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
semopen(&s1);
|
||||
semopen(&s2);
|
||||
printf("Waiting for fork...\n");
|
||||
semP(&s1, njobs);
|
||||
printf("Starting the execs...\n");
|
||||
semV(&s2, njobs);
|
||||
semopen(&s1);
|
||||
semopen(&s2);
|
||||
printf("Waiting for fork...\n");
|
||||
semP(&s1, njobs);
|
||||
printf("Starting the execs...\n");
|
||||
semV(&s2, njobs);
|
||||
|
||||
failed = 0;
|
||||
for (i=0; i<njobs; i++) {
|
||||
if (waitpid(pids[i], &status, 0) < 0) {
|
||||
warn("waitpid");
|
||||
failed++;
|
||||
}
|
||||
else if (WIFSIGNALED(status)) {
|
||||
warnx("pid %d (child %d): Signal %d",
|
||||
(int)pids[i], i, WTERMSIG(status));
|
||||
failed++;
|
||||
}
|
||||
else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
|
||||
warnx("pid %d (child %d): Exit %d",
|
||||
(int)pids[i], i, WEXITSTATUS(status));
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
if (failed > 0) {
|
||||
warnx("%d children failed", failed);
|
||||
}
|
||||
else {
|
||||
printf("Succeeded\n");
|
||||
}
|
||||
failed = 0;
|
||||
for (i = 0; i < njobs; i++) {
|
||||
if (waitpid(pids[i], &status, 0) < 0) {
|
||||
warn("waitpid");
|
||||
failed++;
|
||||
} else if (WIFSIGNALED(status)) {
|
||||
warnx("pid %d (child %d): Signal %d", (int)pids[i], i, WTERMSIG(status));
|
||||
failed++;
|
||||
} else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
|
||||
warnx("pid %d (child %d): Exit %d", (int)pids[i], i, WEXITSTATUS(status));
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
if (failed > 0) {
|
||||
warnx("%d children failed", failed);
|
||||
} else {
|
||||
printf("Succeeded\n");
|
||||
}
|
||||
|
||||
semclose(&s1);
|
||||
semclose(&s2);
|
||||
semdestroy(&s1);
|
||||
semdestroy(&s2);
|
||||
semclose(&s1);
|
||||
semclose(&s2);
|
||||
semdestroy(&s1);
|
||||
semdestroy(&s2);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
static char default_prog[] = "/bin/pwd";
|
||||
int main(int argc, char *argv[]) {
|
||||
static char default_prog[] = "/bin/pwd";
|
||||
|
||||
int njobs = 12;
|
||||
int i;
|
||||
int njobs = 12;
|
||||
int i;
|
||||
|
||||
for (i=1; i<argc; i++) {
|
||||
if (!strcmp(argv[i], "-j")) {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
errx(1, "Option -j requires an argument");
|
||||
}
|
||||
njobs = atoi(argv[i]);
|
||||
}
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "-j")) {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
errx(1, "Option -j requires an argument");
|
||||
}
|
||||
njobs = atoi(argv[i]);
|
||||
}
|
||||
#if 0 /* XXX we apparently don't have strncmp? */
|
||||
else if (!strncmp(argv[i], "-j", 2)) {
|
||||
njobs = atoi(argv[i] + 2);
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
subargv[subargc++] = argv[i];
|
||||
if (subargc >= SUBARGC_MAX) {
|
||||
errx(1, "Too many arguments");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
subargv[subargc++] = argv[i];
|
||||
if (subargc >= SUBARGC_MAX) {
|
||||
errx(1, "Too many arguments");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (subargc == 0) {
|
||||
subargv[subargc++] = default_prog;
|
||||
}
|
||||
subargv[subargc] = NULL;
|
||||
if (subargc == 0) {
|
||||
subargv[subargc++] = default_prog;
|
||||
}
|
||||
subargv[subargc] = NULL;
|
||||
|
||||
spawn(njobs);
|
||||
spawn(njobs);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user