Last active
September 3, 2016 21:19
-
-
Save n-soda/831f57d73174efed33ac4f5ecf916052 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <libgen.h> | |
#include <sys/time.h> | |
#define IBUFSIZE 30 | |
static void | |
usage(const char *prog) | |
{ | |
fprintf(stderr, "Usage: %s <number>\n", prog); | |
exit(EXIT_FAILURE); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
char *prog; | |
char *argbuf[5]; | |
char sec[IBUFSIZE], usec[IBUFSIZE], iter[IBUFSIZE]; | |
int n, rv, status; | |
struct timeval start, now; | |
if (argc < 1) { | |
fprintf(stderr, "argc = %d\n", argc); | |
return EXIT_FAILURE; | |
} | |
prog = basename(argv[0]); | |
if (argc != 2 && argc != 4) | |
usage(prog); | |
n = atoi(argv[1]); | |
if (n <= 0) { | |
if (argc == 2) | |
printf("nop\n"); | |
else { | |
gettimeofday(&now, NULL); | |
printf("%g\n", | |
now.tv_sec + now.tv_usec / 1000000.0 - | |
(atoi(argv[2]) + atoi(argv[3]) / 1000000.0)); | |
} | |
return EXIT_SUCCESS; | |
} | |
if (strcmp(prog, "forkbench") == 0 || | |
strcmp(prog, "vforkbench") == 0 || | |
strcmp(prog, "forkrepeatbench") == 0 || | |
strcmp(prog, "vforkrepeatbench") == 0 || | |
strcmp(prog, "forkwaitrepeatbench") == 0 || | |
strcmp(prog, "vforkwaitrepeatbench") == 0) { | |
int test_vfork = | |
strcmp(prog, "vforkbench") == 0 || | |
strcmp(prog, "vforkrepeatbench") == 0 || | |
strcmp(prog, "vforkwaitrepeatbench") == 0; | |
int test_forkrepeat = | |
strcmp(prog, "forkrepeatbench") == 0 || | |
strcmp(prog, "vforkrepeatbench") == 0 || | |
strcmp(prog, "forkwaitrepeatbench") == 0 || | |
strcmp(prog, "vforkwaitrepeatbench") == 0; | |
int test_wait = | |
strcmp(prog, "forkwaitrepeatbench") == 0 || | |
strcmp(prog, "vforkwaitrepeatbench") == 0; | |
gettimeofday(&start, NULL); | |
while (--n >= 0) { | |
if (test_vfork) { | |
rv = vfork(); | |
} else { | |
rv = fork(); | |
} | |
if (rv == -1) { | |
perror(test_vfork ? "vfork" : "fork"); | |
_exit(EXIT_FAILURE); | |
} | |
if (test_forkrepeat) { | |
if (rv == 0) | |
_exit(EXIT_SUCCESS); | |
if (test_wait) { | |
rv = waitpid(rv, &status, 0); | |
if (rv == -1) | |
perror("waitpid"); | |
} | |
} else { | |
if (rv != 0) | |
_exit(EXIT_SUCCESS); | |
} | |
} | |
gettimeofday(&now, NULL); | |
printf("%g\n", | |
now.tv_sec + now.tv_usec / 1000000.0 - | |
(start.tv_sec + start.tv_usec / 1000000.0)); | |
_exit(EXIT_SUCCESS); | |
} | |
snprintf(iter, sizeof iter, "%d", n - 1); | |
if (argc == 2) { | |
gettimeofday(&now, NULL); | |
snprintf(sec, sizeof sec, "%ld", (long)now.tv_sec); | |
snprintf(usec, sizeof usec, "%ld", (long)now.tv_usec); | |
argbuf[0] = argv[0]; | |
argbuf[1] = iter; | |
argbuf[2] = sec; | |
argbuf[3] = usec; | |
argbuf[4] = NULL; | |
} else { | |
argbuf[0] = argv[0]; | |
argbuf[1] = iter; | |
argbuf[2] = argv[2]; | |
argbuf[3] = argv[3]; | |
argbuf[4] = NULL; | |
} | |
if (strcmp(prog, "forkexecbench") == 0 || | |
strcmp(prog, "vforkexecbench") == 0) { | |
int test_vfork = strcmp(prog, "vforkexecbench") == 0; | |
if (test_vfork) { | |
rv = vfork(); | |
} else { | |
rv = fork(); | |
} | |
if (rv == -1) { | |
perror(test_vfork ? "vfork" : "fork"); | |
} else if (rv != 0) { | |
_exit(EXIT_SUCCESS); | |
} | |
execv(prog, argbuf); | |
perror("execv"); | |
_exit(EXIT_FAILURE); | |
} else if(strcmp(prog, "execbench") == 0) { | |
execv(prog, argbuf); | |
perror("execv"); | |
return EXIT_FAILURE; | |
} else { | |
fprintf(stderr, "unknown program name: %s\n", prog); | |
} | |
/*NOTREACHED*/ | |
return EXIT_FAILURE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment