1 /*******************************************************************************
2  *  The BYTE UNIX Benchmarks - Release 1
3  *          Module: looper.c   SID: 1.4 5/15/91 19:30:22
4  *
5  *******************************************************************************
6  * Bug reports, patches, comments, suggestions should be sent to:
7  *
8  *	Ben Smith or Tom Yager at BYTE Magazine
9  *	ben@bytepb.byte.com   tyager@bytepb.byte.com
10  *
11  *******************************************************************************
12  *  Modification Log:
13  *
14  *  February 25, 1991 -- created (Ben S.)
15  *  October 22, 1997 - code cleanup to remove ANSI C compiler warnings
16  *                     Andy Kahn <kahn@zk3.dec.com>
17  *
18  ******************************************************************************/
19 char SCCSid[] = "@(#) @(#)looper.c:1.4 -- 5/15/91 19:30:22";
20 /*
21  *  Shell Process creation
22  *
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28 #include "timeit.c"
29 
30 unsigned long iter;
31 char *cmd_argv[28];
32 int  cmd_argc;
33 
report(void)34 void report(void)
35 {
36         fprintf(stderr,"COUNT|%lu|60|lpm\n", iter);
37 	exit(0);
38 }
39 
main(argc,argv)40 int main(argc, argv)
41 int	argc;
42 char	*argv[];
43 {
44 int	slave, count, duration;
45 int	status;
46 
47 if (argc < 2)
48 	{
49 	fprintf(stderr,"Usage: %s duration command [args..]\n", argv[0]);
50 	fprintf(stderr,"  duration in seconds\n");
51 	exit(1);
52 	}
53 
54 if((duration = atoi(argv[1])) < 1)
55 	{
56 	fprintf(stderr,"Usage: %s duration command [arg..]\n", argv[0]);
57 	fprintf(stderr,"  duration in seconds\n");
58 	exit(1);
59 	}
60 
61 /* get command  */
62 cmd_argc=argc-2;
63 for( count=2;count < argc; ++count)
64 	cmd_argv[count-2]=argv[count];
65 #ifdef DEBUG
66 printf("<<%s>>",cmd_argv[0]);
67 for(count=1;count < cmd_argc; ++count)
68 	printf(" <%s>", cmd_argv[count]);
69 putchar('\n');
70 exit(0);
71 #endif
72 
73 iter = 0;
74 wake_me(duration, report);
75 
76 while (1)
77 	{
78 	if ((slave = fork()) == 0)
79 		{ /* execute command */
80 		execvp(cmd_argv[0],cmd_argv);
81 		exit(99);
82 		}
83 	else if (slave < 0)
84 		{
85 		/* woops ... */
86 		fprintf(stderr,"Fork failed at iteration %lu\n", iter);
87 		perror("Reason");
88 		exit(2);
89 		}
90 	else
91 		/* master */
92 		wait(&status);
93         if (status == 99 << 8)
94                 {
95                 fprintf(stderr, "Command \"%s\" didn't exec\n", cmd_argv[0]);
96                 exit(2);
97                 }
98 	else if (status != 0)
99 		{
100 		fprintf(stderr,"Bad wait status: 0x%x\n", status);
101 		exit(2);
102 		}
103 	iter++;
104 	}
105 }
106