clang-format
This commit is contained in:
@@ -51,16 +51,13 @@ static volatile int mypid;
|
||||
/*
|
||||
* Helper function for fork that prints a warning on error.
|
||||
*/
|
||||
static
|
||||
int
|
||||
dofork(void)
|
||||
{
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
warn("fork");
|
||||
}
|
||||
return pid;
|
||||
static int dofork(void) {
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
warn("fork");
|
||||
}
|
||||
return pid;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -68,24 +65,22 @@ dofork(void)
|
||||
* the pid into the data segment and read it back repeatedly, making
|
||||
* sure it's correct every time.
|
||||
*/
|
||||
static
|
||||
void
|
||||
check(void)
|
||||
{
|
||||
int i;
|
||||
static void check(void) {
|
||||
int i;
|
||||
|
||||
mypid = getpid();
|
||||
mypid = getpid();
|
||||
|
||||
/* Make sure each fork has its own address space. */
|
||||
for (i=0; i<800; i++) {
|
||||
volatile int seenpid;
|
||||
seenpid = mypid;
|
||||
if (seenpid != getpid()) {
|
||||
errx(1, "pid mismatch (%d, should be %d) "
|
||||
"- your vm is broken!",
|
||||
seenpid, getpid());
|
||||
}
|
||||
}
|
||||
/* Make sure each fork has its own address space. */
|
||||
for (i = 0; i < 800; i++) {
|
||||
volatile int seenpid;
|
||||
seenpid = mypid;
|
||||
if (seenpid != getpid()) {
|
||||
errx(1,
|
||||
"pid mismatch (%d, should be %d) "
|
||||
"- your vm is broken!",
|
||||
seenpid, getpid());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -97,121 +92,109 @@ check(void)
|
||||
* generated the current process; that means it's time to exit. Only
|
||||
* the parent of all the processes returns from the chain of dowaits.
|
||||
*/
|
||||
static
|
||||
void
|
||||
dowait(int nowait, int pid)
|
||||
{
|
||||
int x;
|
||||
static void dowait(int nowait, int pid) {
|
||||
int x;
|
||||
|
||||
if (pid<0) {
|
||||
/* fork in question failed; just return */
|
||||
return;
|
||||
}
|
||||
if (pid==0) {
|
||||
/* in the fork in question we were the child; exit */
|
||||
exit(0);
|
||||
}
|
||||
if (pid < 0) {
|
||||
/* fork in question failed; just return */
|
||||
return;
|
||||
}
|
||||
if (pid == 0) {
|
||||
/* in the fork in question we were the child; exit */
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (!nowait) {
|
||||
if (waitpid(pid, &x, 0)<0) {
|
||||
warn("waitpid");
|
||||
}
|
||||
else if (WIFSIGNALED(x)) {
|
||||
warnx("pid %d: signal %d", pid, WTERMSIG(x));
|
||||
}
|
||||
else if (WEXITSTATUS(x) != 0) {
|
||||
warnx("pid %d: exit %d", pid, WEXITSTATUS(x));
|
||||
}
|
||||
}
|
||||
if (!nowait) {
|
||||
if (waitpid(pid, &x, 0) < 0) {
|
||||
warn("waitpid");
|
||||
} else if (WIFSIGNALED(x)) {
|
||||
warnx("pid %d: signal %d", pid, WTERMSIG(x));
|
||||
} else if (WEXITSTATUS(x) != 0) {
|
||||
warnx("pid %d: exit %d", pid, WEXITSTATUS(x));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Actually run the test.
|
||||
*/
|
||||
static
|
||||
void
|
||||
test(int nowait)
|
||||
{
|
||||
int pid0, pid1, pid2, pid3;
|
||||
int depth = 0;
|
||||
static void test(int nowait) {
|
||||
int pid0, pid1, pid2, pid3;
|
||||
int depth = 0;
|
||||
|
||||
/*
|
||||
* Caution: This generates processes geometrically.
|
||||
*
|
||||
* It is unrolled to encourage gcc to registerize the pids,
|
||||
* to prevent wait/exit problems if fork corrupts memory.
|
||||
*
|
||||
* Note: if the depth prints trigger and show that the depth
|
||||
* is too small, the most likely explanation is that the fork
|
||||
* child is returning from the write() inside putchar()
|
||||
* instead of from fork() and thus skipping the depth++. This
|
||||
* is a fairly common problem caused by races in the kernel
|
||||
* fork code.
|
||||
*/
|
||||
/*
|
||||
* Caution: This generates processes geometrically.
|
||||
*
|
||||
* It is unrolled to encourage gcc to registerize the pids,
|
||||
* to prevent wait/exit problems if fork corrupts memory.
|
||||
*
|
||||
* Note: if the depth prints trigger and show that the depth
|
||||
* is too small, the most likely explanation is that the fork
|
||||
* child is returning from the write() inside putchar()
|
||||
* instead of from fork() and thus skipping the depth++. This
|
||||
* is a fairly common problem caused by races in the kernel
|
||||
* fork code.
|
||||
*/
|
||||
|
||||
pid0 = dofork();
|
||||
depth++;
|
||||
putchar('A');
|
||||
if (depth != 1) {
|
||||
warnx("depth %d, should be 1", depth);
|
||||
}
|
||||
check();
|
||||
pid0 = dofork();
|
||||
depth++;
|
||||
putchar('A');
|
||||
if (depth != 1) {
|
||||
warnx("depth %d, should be 1", depth);
|
||||
}
|
||||
check();
|
||||
|
||||
pid1 = dofork();
|
||||
depth++;
|
||||
putchar('B');
|
||||
if (depth != 2) {
|
||||
warnx("depth %d, should be 2", depth);
|
||||
}
|
||||
check();
|
||||
pid1 = dofork();
|
||||
depth++;
|
||||
putchar('B');
|
||||
if (depth != 2) {
|
||||
warnx("depth %d, should be 2", depth);
|
||||
}
|
||||
check();
|
||||
|
||||
pid2 = dofork();
|
||||
depth++;
|
||||
putchar('C');
|
||||
if (depth != 3) {
|
||||
warnx("depth %d, should be 3", depth);
|
||||
}
|
||||
check();
|
||||
pid2 = dofork();
|
||||
depth++;
|
||||
putchar('C');
|
||||
if (depth != 3) {
|
||||
warnx("depth %d, should be 3", depth);
|
||||
}
|
||||
check();
|
||||
|
||||
pid3 = dofork();
|
||||
depth++;
|
||||
putchar('D');
|
||||
if (depth != 4) {
|
||||
warnx("depth %d, should be 4", depth);
|
||||
}
|
||||
check();
|
||||
pid3 = dofork();
|
||||
depth++;
|
||||
putchar('D');
|
||||
if (depth != 4) {
|
||||
warnx("depth %d, should be 4", depth);
|
||||
}
|
||||
check();
|
||||
|
||||
/*
|
||||
* These must be called in reverse order to avoid waiting
|
||||
* improperly.
|
||||
*/
|
||||
dowait(nowait, pid3);
|
||||
dowait(nowait, pid2);
|
||||
dowait(nowait, pid1);
|
||||
dowait(nowait, pid0);
|
||||
/*
|
||||
* These must be called in reverse order to avoid waiting
|
||||
* improperly.
|
||||
*/
|
||||
dowait(nowait, pid3);
|
||||
dowait(nowait, pid2);
|
||||
dowait(nowait, pid1);
|
||||
dowait(nowait, pid0);
|
||||
|
||||
putchar('\n');
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
static const char expected[] =
|
||||
"|----------------------------|\n";
|
||||
int nowait=0;
|
||||
int main(int argc, char *argv[]) {
|
||||
static const char expected[] = "|----------------------------|\n";
|
||||
int nowait = 0;
|
||||
|
||||
if (argc==2 && !strcmp(argv[1], "-w")) {
|
||||
nowait=1;
|
||||
}
|
||||
else if (argc!=1 && argc!=0) {
|
||||
warnx("usage: forktest [-w]");
|
||||
return 1;
|
||||
}
|
||||
warnx("Starting. Expect this many:");
|
||||
write(STDERR_FILENO, expected, strlen(expected));
|
||||
if (argc == 2 && !strcmp(argv[1], "-w")) {
|
||||
nowait = 1;
|
||||
} else if (argc != 1 && argc != 0) {
|
||||
warnx("usage: forktest [-w]");
|
||||
return 1;
|
||||
}
|
||||
warnx("Starting. Expect this many:");
|
||||
write(STDERR_FILENO, expected, strlen(expected));
|
||||
|
||||
test(nowait);
|
||||
test(nowait);
|
||||
|
||||
warnx("Complete.");
|
||||
return 0;
|
||||
warnx("Complete.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user