1 /*******************************************************************************
2 * The BYTE UNIX Benchmarks - Release 3
3 * Module: hanoi.c SID: 3.3 5/15/91 19:30:20
4 *
5 *******************************************************************************
6 * Bug reports, patches, comments, suggestions should be sent to:
7 *
8 * Ben Smith, Rick Grehan or Tom Yager
9 * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
10 *
11 *******************************************************************************
12 * Modification Log:
13 * $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $
14 * August 28, 1990 - Modified timing routines (ty)
15 * October 22, 1997 - code cleanup to remove ANSI C compiler warnings
16 * Andy Kahn <kahn@zk3.dec.com>
17 *
18 ******************************************************************************/
19 char SCCSid[] = "@(#) @(#)hanoi.c:3.3 -- 5/15/91 19:30:20";
20
21 #define other(i,j) (6-(i+j))
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "timeit.c"
26
27 void mov(int n, int f, int t);
28
29 unsigned long iter = 0;
30 int num[4];
31 long cnt;
32
report()33 void report()
34 {
35 fprintf(stderr,"COUNT|%ld|1|lps\n", iter);
36 exit(0);
37 }
38
39
main(argc,argv)40 int main(argc, argv)
41 int argc;
42 char *argv[];
43 {
44 int disk=10, /* default number of disks */
45 duration;
46
47 if (argc < 2) {
48 fprintf(stderr,"Usage: %s duration [disks]\n", argv[0]);
49 exit(1);
50 }
51 duration = atoi(argv[1]);
52 if(argc > 2) disk = atoi(argv[2]);
53 num[1] = disk;
54
55 wake_me(duration, report);
56
57 while(1) {
58 mov(disk,1,3);
59 iter++;
60 }
61
62 exit(0);
63 }
64
mov(int n,int f,int t)65 void mov(int n, int f, int t)
66 {
67 int o;
68 if(n == 1) {
69 num[f]--;
70 num[t]++;
71 return;
72 }
73 o = other(f,t);
74 mov(n-1,f,o);
75 mov(1,f,t);
76 mov(n-1,o,t);
77 }
78