New 'benchmarks' dir in test/, with first benchmark - unixbench-5.1.2, ported
[minix.git] / test / benchmarks / unixbench-5.1.2 / src / context1.c
blob8238843e2e90ade178771856367a6a250699386b
2 /*******************************************************************************
3 * The BYTE UNIX Benchmarks - Release 3
4 * Module: context1.c SID: 3.3 5/15/91 19:30:18
6 *******************************************************************************
7 * Bug reports, patches, comments, suggestions should be sent to:
9 * Ben Smith, Rick Grehan or Tom Yager
10 * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
12 *******************************************************************************
13 * Modification Log:
14 * $Header: context1.c,v 3.4 87/06/22 14:22:59 kjmcdonell Beta $
15 * August 28, 1990 - changed timing routines--now returns total number of
16 * iterations in specified time period
17 * October 22, 1997 - code cleanup to remove ANSI C compiler warnings
18 * Andy Kahn <kahn@zk3.dec.com>
20 ******************************************************************************/
21 char SCCSid[] = "@(#) @(#)context1.c:3.3 -- 5/15/91 19:30:18";
23 * Context switching via synchronized unbuffered pipe i/o
27 #ifdef MINIX
28 #include <signal.h>
29 #endif
30 #ifdef LINUX
31 #include <sys/types.h>
32 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include "timeit.c"
38 unsigned long iter;
39 #ifdef MINIX
40 pid_t child;
41 #endif
43 void report(int sig)
45 fprintf(stderr, "COUNT|%lu|1|lps\n", iter);
46 #ifdef MINIX
47 kill(child, SIGKILL);
48 #endif
49 exit(0);
52 int main(int argc, char *argv[])
54 int duration;
55 unsigned long check;
56 int p1[2], p2[2];
58 if (argc != 2) {
59 fprintf(stderr, "Usage: context duration\n");
60 exit(1);
63 duration = atoi(argv[1]);
65 /* set up alarm call */
66 iter = 0;
67 wake_me(duration, report);
69 if (pipe(p1) || pipe(p2)) {
70 perror("pipe create failed");
71 exit(1);
74 #ifndef MINIX
75 if (fork()) { /* parent process */
76 #else
77 if ((child = fork())) {
78 #endif
79 /* master, write p1 & read p2 */
80 close(p1[0]); close(p2[1]);
81 while (1) {
82 if (write(p1[1], (char *)&iter, sizeof(iter)) != sizeof(iter)) {
83 if ((errno != 0) && (errno != EINTR))
84 perror("master write failed");
85 exit(1);
87 if (read(p2[0], (char *)&check, sizeof(check)) != sizeof(check)) {
88 if ((errno != 0) && (errno != EINTR))
89 perror("master read failed");
90 exit(1);
92 if (check != iter) {
93 fprintf(stderr, "Master sync error: expect %lu, got %lu\n",
94 iter, check);
95 exit(2);
97 iter++;
100 else { /* child process */
101 unsigned long iter1;
103 iter1 = 0;
104 /* slave, read p1 & write p2 */
105 close(p1[1]); close(p2[0]);
106 while (1) {
107 if (read(p1[0], (char *)&check, sizeof(check)) != sizeof(check)) {
108 if ((errno != 0) && (errno != EINTR))
109 perror("slave read failed");
110 exit(1);
112 if (check != iter1) {
113 fprintf(stderr, "Slave sync error: expect %lu, got %lu\n",
114 iter, check);
115 exit(2);
117 if (write(p2[1], (char *)&iter1, sizeof(iter1)) != sizeof(check)) {
118 if ((errno != 0) && (errno != EINTR))
119 perror("slave write failed");
120 exit(1);
122 iter1++;