| 1 | /* $Id: fork.c 1454 2004-09-04 06:22:38Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * Performance testcase for fork().
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2004 knut st. osmundsen <[email protected]>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of InnoTek LIBC.
|
|---|
| 10 | *
|
|---|
| 11 | * InnoTek LIBC is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * InnoTek LIBC is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with InnoTek LIBC; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /*******************************************************************************
|
|---|
| 29 | * Header Files *
|
|---|
| 30 | *******************************************************************************/
|
|---|
| 31 | #include <string.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <unistd.h>
|
|---|
| 34 | #include <stdlib.h>
|
|---|
| 35 | #include <sys/time.h>
|
|---|
| 36 | #include <sys/types.h>
|
|---|
| 37 | #include <sys/wait.h>
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | int main(int argc, char **argv)
|
|---|
| 41 | {
|
|---|
| 42 | pid_t pid;
|
|---|
| 43 | int i;
|
|---|
| 44 | int cTimes = 10;
|
|---|
| 45 | struct timeval tvStart;
|
|---|
| 46 | struct timeval tvEnd;
|
|---|
| 47 | struct timeval tv;
|
|---|
| 48 |
|
|---|
| 49 | /* args */
|
|---|
| 50 | if (argc > 1)
|
|---|
| 51 | if (!(cTimes = atol(argv[1])))
|
|---|
| 52 | cTimes = 10;
|
|---|
| 53 |
|
|---|
| 54 | /* profiling */
|
|---|
| 55 | gettimeofday(&tvStart, 0);
|
|---|
| 56 | for (i = 0; i < cTimes; i++)
|
|---|
| 57 | {
|
|---|
| 58 | int rc;
|
|---|
| 59 | pid = fork();
|
|---|
| 60 | if (!pid)
|
|---|
| 61 | exit(0);
|
|---|
| 62 | else if (pid < 0)
|
|---|
| 63 | {
|
|---|
| 64 | printf("fork failed. errno=%d i=%d\n", errno, i);
|
|---|
| 65 | break;
|
|---|
| 66 | }
|
|---|
| 67 | waitpid(pid, &rc, 0);
|
|---|
| 68 | }
|
|---|
| 69 | gettimeofday(&tvEnd, 0);
|
|---|
| 70 |
|
|---|
| 71 | /* calc duration */
|
|---|
| 72 | tv = tvEnd;
|
|---|
| 73 | tv.tv_usec -= tvStart.tv_usec;
|
|---|
| 74 | if (tv.tv_usec < 0)
|
|---|
| 75 | {
|
|---|
| 76 | tv.tv_usec += 1000000;
|
|---|
| 77 | tv.tv_sec--;
|
|---|
| 78 | }
|
|---|
| 79 | tv.tv_sec -= tvStart.tv_sec;
|
|---|
| 80 |
|
|---|
| 81 | /* print result. */
|
|---|
| 82 | printf("%ld.%06ld - %d times.\n", tv.tv_sec, tv.tv_usec, i);
|
|---|
| 83 |
|
|---|
| 84 | return 0;
|
|---|
| 85 | }
|
|---|