Skip to content

Commit 3e6d32c

Browse files
committedJul 7, 2012
It mostly works
1 parent b77358b commit 3e6d32c

File tree

5 files changed

+128
-4
lines changed

5 files changed

+128
-4
lines changed
 

‎Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ DEPDIR=$(DEPROOT)
1414
TARGET=job-server
1515

1616
#The BUILDROOT folder is included for config.h
17+
WARNINGS = -Wall -Wno-unused-but-set-variable
1718
CFLAGS = ${DEFAULT_CFLAGS} -I $(BUILDROOT) $(INCLUDE_FLAGS)
1819
CPPFLAGS = ${CFLAGS}
1920

‎configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ echo '#define DEBUG ' $DEBUG >> $CONFIG_H
6363
echo DEBUG=$DEBUG >> $CONFIG_MK;
6464

6565
if [ "$DEBUG" != "0" ]; then
66-
DEFAULT_CFLAGS="${DEFAULT_CFLAGS} -Werror -Wall -g"
66+
DEFAULT_CFLAGS="${DEFAULT_CFLAGS} -Werror -g"
6767
else
6868
if [ "$DEFAULT_CFLAGS" == "" ]; then
6969
DEFAULT_CFLAGS="-march=native -O2 -fomit-frame-pointer"

‎main.cpp

+100-3
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,118 @@
66

77
#include <stdio.h>
88
#include <iostream>
9+
#include <sys/sysinfo.h>
10+
#include <sys/types.h>
11+
#include <sys/wait.h>
912

1013
#include "MBString.h"
14+
#include "MBVector.h"
1115

1216
using namespace std;
1317

18+
#define PID_INVALID (-1)
19+
20+
typedef struct MainData {
21+
MBVector<pid_t> children;
22+
int numJobs;
23+
} MainData;
24+
25+
static MainData mainData;
26+
27+
static void
28+
InsertChild(pid_t pid)
29+
{
30+
for (int x = 0; x < mainData.children.size(); x++) {
31+
if (mainData.children[x] == PID_INVALID) {
32+
mainData.children[x] = pid;
33+
return;
34+
}
35+
}
36+
37+
NOT_REACHED();
38+
}
39+
40+
static void
41+
WaitForChild(void)
42+
{
43+
int status;
44+
int result;
45+
pid_t pid;
46+
47+
for (int x = 0; x < mainData.children.size(); x++) {
48+
if (mainData.children[x] == PID_INVALID) {
49+
return;
50+
}
51+
}
52+
53+
//No free slot
54+
for (int x = 0; x < mainData.children.size(); x++) {
55+
pid = mainData.children[x];
56+
ASSERT(pid != PID_INVALID);
57+
58+
result = waitpid(pid, &status, WNOHANG);
59+
if (result == -1) {
60+
Warning("%s: Error occured while waiting for child.\n");
61+
} else if (WIFEXITED(status)) {
62+
mainData.children[x] = PID_INVALID;
63+
return;
64+
}
65+
}
66+
67+
// All children are still running...
68+
Warning("%s: Waiting for any child...\n", __FUNCTION__);
69+
pid = wait(&status);
70+
if (pid != -1) {
71+
for (int x = 0; x < mainData.children.size(); x++) {
72+
if (mainData.children[x] == pid) {
73+
mainData.children[x] = PID_INVALID;
74+
return;
75+
}
76+
}
77+
ASSERT(FALSE); // We didn't find our child??
78+
}
79+
80+
Warning("%s", "Didn't find a free child... Trying again.\n");
81+
WaitForChild();
82+
}
83+
1484
int main()
1585
{
1686
MBString line;
87+
pid_t pid;
88+
89+
mainData.numJobs = get_nprocs();
90+
mainData.children.resize(mainData.numJobs);
91+
92+
for (int x = 0; x < mainData.numJobs; x++) {
93+
mainData.children[x] = PID_INVALID;
94+
}
1795

1896
while (!cin.eof()) {
1997
printf("> ");
2098
MBString_GetLine(cin, line);
21-
if (!line.isEmpty()) {
22-
printf("Executing: %s\n", line.cstr());
23-
system(line.cstr());
99+
if (!line.isEmpty()) {
100+
WaitForChild();
101+
102+
pid = fork();
103+
if (!pid) {
104+
// We are the child
105+
system(line.cstr());
106+
exit(0);
107+
} else {
108+
// We are the parent
109+
Warning("%s: Launched [%d] %s\n", __FUNCTION__, pid, line.cstr());//banackm
110+
InsertChild(pid);
111+
}
112+
}
113+
}
114+
115+
// Wait for any remaining children.
116+
for (int x = 0; x < mainData.children.size(); x++) {
117+
pid = mainData.children[x];
118+
if (pid != PID_INVALID) {
119+
printf("%s: Waiting for %d\n", __FUNCTION__, pid);//banackm
120+
waitpid(pid, NULL, 0);
24121
}
25122
}
26123

‎test.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/perl
2+
3+
4+
open JOBSERVER, "| build/job-server";
5+
6+
print JOBSERVER "./work.sh 5\n";
7+
print JOBSERVER "./work.sh 3\n";
8+
print JOBSERVER "./work.sh 6\n";
9+
print JOBSERVER "./work.sh 7\n";
10+
print JOBSERVER "./work.sh 8\n";
11+
print JOBSERVER "./work.sh 9\n";
12+
print JOBSERVER "./work.sh 10\n";
13+
print JOBSERVER "./work.sh 11\n";
14+
15+
close JOBSERVER
16+
17+
18+

‎work.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
echo Starting $*
4+
sleep $1
5+
echo Ending $*
6+
7+
exit 0
8+

0 commit comments

Comments
 (0)
Please sign in to comment.