-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from Tarsnap/fork-func
Add fork_func
- Loading branch information
Showing
22 changed files
with
799 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/bin/sh | ||
|
||
### Constants | ||
c_valgrind_min=1 | ||
spawning="${s_basename}-spawning.txt" | ||
waited="${s_basename}-waited.txt" | ||
|
||
### Actual command | ||
scenario_cmd() { | ||
cd "${scriptdir}/fork_func" || exit | ||
|
||
setup_check "test_fork_func -x" | ||
# Special handling for multiple forks. | ||
c_valgrind_cmd=$(valgrind_setup "valgrind-parent") | ||
${c_valgrind_cmd} ./test_fork_func -x | ||
echo "$?" > "${c_exitfile}" | ||
|
||
setup_check "test_fork_func -e" | ||
# Special handling for multiple forks. | ||
c_valgrind_cmd=$(valgrind_setup "valgrind-parent") | ||
${c_valgrind_cmd} ./test_fork_func -e | ||
echo "$?" > "${c_exitfile}" | ||
|
||
setup_check "test_fork_func -c" | ||
# Special handling for multiple forks. | ||
c_valgrind_cmd=$(valgrind_setup "valgrind-parent") | ||
${c_valgrind_cmd} ./test_fork_func -c >"${spawning}" 2>"${waited}" | ||
echo "$?" > "${c_exitfile}" | ||
|
||
setup_check "test_fork_func -c spawning" | ||
cmp "${spawning}" "${scriptdir}/fork_func/check_order_spawning.good" | ||
echo "$?" > "${c_exitfile}" | ||
|
||
setup_check "test_fork_func -c waited" | ||
cmp "${waited}" "${scriptdir}/fork_func/check_order_waited.good" | ||
echo "$?" > "${c_exitfile}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.POSIX: | ||
# AUTOGENERATED FILE, DO NOT EDIT | ||
PROG=test_fork_func | ||
SRCS=main.c check_exec.c check_exit.c check_order.c check_perftest.c | ||
IDIRS=-I../../util | ||
LDADD_REQ=-lpthread | ||
SUBDIR_DEPTH=../.. | ||
RELATIVE_DIR=tests/fork_func | ||
LIBALL=../../liball/liball.a ../../liball/optional_mutex_pthread/liball_optional_mutex_pthread.a | ||
|
||
all: | ||
if [ -z "$${HAVE_BUILD_FLAGS}" ]; then \ | ||
cd ${SUBDIR_DEPTH}; \ | ||
${MAKE} BUILD_SUBDIR=${RELATIVE_DIR} \ | ||
BUILD_TARGET=${PROG} buildsubdir; \ | ||
else \ | ||
${MAKE} ${PROG}; \ | ||
fi | ||
|
||
clean: | ||
rm -f ${PROG} ${SRCS:.c=.o} | ||
|
||
${PROG}:${SRCS:.c=.o} ${LIBALL} | ||
${CC} -o ${PROG} ${SRCS:.c=.o} ${LIBALL} ${LDFLAGS} ${LDADD_EXTRA} ${LDADD_REQ} ${LDADD_POSIX} | ||
|
||
main.o: main.c ../../util/getopt.h ../../util/parsenum.h ../../util/warnp.h check.h | ||
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c main.c -o main.o | ||
check_exec.o: check_exec.c ../../util/fork_func.h ../../util/warnp.h check.h | ||
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c check_exec.c -o check_exec.o | ||
check_exit.o: check_exit.c ../../util/fork_func.h check.h | ||
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c check_exit.c -o check_exit.o | ||
check_order.o: check_order.c ../../util/fork_func.h ../../util/millisleep.h ../../util/warnp.h check.h | ||
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c check_order.c -o check_order.o | ||
check_perftest.o: check_perftest.c ../../util/fork_func.h ../../util/millisleep.h ../../util/monoclock.h ../../util/warnp.h check.h | ||
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c check_perftest.c -o check_perftest.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Program name. | ||
PROG = test_fork_func | ||
|
||
# Don't install it. | ||
NOINST = 1 | ||
|
||
# Library code required | ||
LDADD_REQ = -lpthread | ||
|
||
# Useful relative directories | ||
LIBCPERCIVA_DIR = ../.. | ||
|
||
# Main test code | ||
SRCS = main.c | ||
SRCS += check_exec.c | ||
SRCS += check_exit.c | ||
SRCS += check_order.c | ||
SRCS += check_perftest.c | ||
|
||
# libcperciva includes | ||
IDIRS += -I${LIBCPERCIVA_DIR}/util | ||
|
||
.include <bsd.prog.mk> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef CHECK_H_ | ||
#define CHECK_H_ | ||
|
||
/** | ||
* check_exit(void): | ||
* Check that fork_func() works with exit codes. | ||
*/ | ||
int check_exit(void); | ||
|
||
/** | ||
* check_exec(void): | ||
* Check that fork_func() works with an exec* function. | ||
*/ | ||
int check_exec(void); | ||
|
||
/** | ||
* check_order(void): | ||
* Check that fork_func() doesn't impose any order on multiple functions. | ||
*/ | ||
int check_order(void); | ||
|
||
/** | ||
* check_perftest(void): | ||
* Check the amount of delay added by func_fork() and func_fork_wait(). | ||
*/ | ||
int check_perftest(int child_ms, int num_reps); | ||
|
||
#endif /* !CHECK_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <unistd.h> | ||
|
||
#include "fork_func.h" | ||
#include "warnp.h" | ||
|
||
#include "check.h" | ||
|
||
static int | ||
func_exec(void * cookie) | ||
{ | ||
|
||
(void)cookie; /* UNUSED */ | ||
|
||
/* Execute the "true" binary found in the $PATH. */ | ||
if (execlp("true", "true", NULL)) | ||
warnp("execvp"); | ||
|
||
/* We should never reach this. */ | ||
return (127); | ||
} | ||
|
||
/** | ||
* check_exec(void): | ||
* Check that fork_func() works with an exec* function. | ||
*/ | ||
int | ||
check_exec(void) | ||
{ | ||
pid_t pid; | ||
|
||
/* Fork. */ | ||
if ((pid = fork_func(func_exec, NULL) == -1)) | ||
goto err0; | ||
|
||
/* Check that it didn't fail. */ | ||
if (fork_func_wait(pid) != 0) | ||
goto err0; | ||
|
||
/* Success! */ | ||
return (0); | ||
|
||
err0: | ||
/* Failure! */ | ||
return (-1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <sys/types.h> | ||
|
||
#include "fork_func.h" | ||
|
||
#include "check.h" | ||
|
||
static int | ||
func_exit(void * cookie) | ||
{ | ||
int * exitcode_p = (int *)cookie; | ||
|
||
/* Return the specified exit code. */ | ||
return (*exitcode_p); | ||
} | ||
|
||
/** | ||
* check_exit(void): | ||
* Check that fork_func() works with exit codes. | ||
*/ | ||
int | ||
check_exit(void) | ||
{ | ||
pid_t pid; | ||
int i; | ||
int rc; | ||
|
||
for (i = 0; i < 4; i++) { | ||
/* Fork. */ | ||
if ((pid = fork_func(func_exit, &i)) == -1) | ||
goto err0; | ||
|
||
/* Check the result. */ | ||
if ((rc = fork_func_wait(pid)) == -1) | ||
goto err0; | ||
if (rc != i) | ||
goto err0; | ||
} | ||
|
||
/* Success! */ | ||
return (0); | ||
|
||
err0: | ||
/* Failure! */ | ||
return (-1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <sys/types.h> | ||
|
||
#include <stdio.h> | ||
|
||
#include "fork_func.h" | ||
#include "millisleep.h" | ||
#include "warnp.h" | ||
|
||
#include "check.h" | ||
|
||
/** | ||
* Do a "sleep sort". This isn't guaranteed to sort successfully, but it's | ||
* very unlikely that any processes will be delayed by enough for it to fail. | ||
* | ||
* We want to output two different lists: the "spawning" messages, and the | ||
* "waited" messages. This allows the test to be more resistant to systems | ||
* having different overheads for fork(). | ||
* | ||
* To achieve this, the "spawning" messages go to stdout, while the "waited" | ||
* messages go to stderr. | ||
*/ | ||
|
||
#define NUM_PROCESSES 5 | ||
static int SLEEP_SORT_MS[5] = {200, 800, 0, 600, 400}; | ||
|
||
/* Wait X milliseconds, then print to stderr. */ | ||
static int | ||
sleep_print_func(void * cookie) | ||
{ | ||
int ms = *((int *)cookie); | ||
|
||
/* Wait. */ | ||
millisleep((size_t)ms); | ||
|
||
/* Write the "waited" messages to stderr. */ | ||
if (fprintf(stderr, "waited %i ms\n", ms) < 0) { | ||
warnp("fprintf"); | ||
goto err0; | ||
} | ||
|
||
/* Success! */ | ||
return (0); | ||
|
||
err0: | ||
/* Failure! */ | ||
return (-1); | ||
} | ||
|
||
/** | ||
* check_order(void): | ||
* Check that fork_func() doesn't impose any order on multiple functions. | ||
*/ | ||
int | ||
check_order(void) | ||
{ | ||
pid_t pids[NUM_PROCESSES]; | ||
int i; | ||
|
||
/*- | ||
* We want to output two different lists: the "spawning" | ||
* messages, and the "waited" messages. This allows the test | ||
* to be more resistant to systems having different overheads | ||
* for fork(). | ||
* | ||
* To achieve this, the "spawning" messages go to stdout, | ||
* while the "waited" messages go to stderr. | ||
*/ | ||
|
||
/* Write the "spawning" messages to stdout. */ | ||
for (i = 0; i < NUM_PROCESSES; i++) { | ||
if (fprintf(stdout, "spawning a process to wait %i ms\n", | ||
SLEEP_SORT_MS[i]) < 0) { | ||
warnp("fprintf"); | ||
goto err0; | ||
} | ||
} | ||
|
||
/* Flush stdout so that its buffer isn't duplicated in the forks. */ | ||
if (fflush(stdout)) { | ||
warnp("fflush"); | ||
goto err0; | ||
} | ||
|
||
/* Spawn the processes. */ | ||
for (i = 0; i < NUM_PROCESSES; i++) { | ||
if ((pids[i] = fork_func(sleep_print_func, | ||
&SLEEP_SORT_MS[i])) == -1) | ||
goto err0; | ||
} | ||
|
||
/* Wait for the processes to finish. */ | ||
for (i = 0; i < NUM_PROCESSES; i++) { | ||
if (fork_func_wait(pids[i])) | ||
goto err0; | ||
} | ||
|
||
/* Success! */ | ||
return (0); | ||
|
||
err0: | ||
/* Failure! */ | ||
return (-1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spawning a process to wait 200 ms | ||
spawning a process to wait 800 ms | ||
spawning a process to wait 0 ms | ||
spawning a process to wait 600 ms | ||
spawning a process to wait 400 ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
waited 0 ms | ||
waited 200 ms | ||
waited 400 ms | ||
waited 600 ms | ||
waited 800 ms |
Oops, something went wrong.