-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu-hogging-2.c
More file actions
66 lines (54 loc) · 1.89 KB
/
Copy pathcpu-hogging-2.c
File metadata and controls
66 lines (54 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Derived from https://github.com/michilu/algo-c/blob/master/tarai.c */
/* https://github.com/michilu/algo-c
============================================================
『C言語による最新アルゴリズム事典』全ソースコード
------------------------------------------------------------
・『C言語による最新アルゴリズム事典』掲載の全ソースコード
です。
本に載せたものと違って,できるかぎりテスト用の main() を
補ってあります。本に載っていないソースコードやテストデー
タも若干ですが含まれています。
------------------------------------------------------------
ご利用についての制限はありません。ただしバグによる損害の賠
償などには応じかねますのでご了承ください。
バグを発見されたらお知らせいただければ幸いです.
------------------------------------------------------------
奥村 晴彦 Haruhiko Okumura <okumura@matsusaka-u.ac.jp>
〒515 松阪市久保町1846 松阪大学
Phone: (0598)29-1122(代) Fax: (0598)29-1014
============================================================ */
/* gcc cpu-hogging-2.c
/usr/bin/time ./a.out */
int tarai(int x, int y, int z)
{
if (x <= y) return y;
return tarai(tarai(x - 1, y, z),
tarai(y - 1, z, x),
tarai(z - 1, x, y));
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <sys/wait.h>
int main()
{
char buf[BUFSIZ];
int x = 14 , y = 1, z = 0;
int v;
getpid();
pid_t pid = fork ();
if (pid < 0)
err(EXIT_FAILURE, "fork");
(void)tarai(x, y, z);
if (pid == 0)
return EXIT_SUCCESS;
else
wait(NULL);
// snprintf(buf, sizeof(buf), "%d\n", v);
// write(1, buf, strlen(buf));
getuid();
getgid();
return EXIT_SUCCESS;
}