-
Notifications
You must be signed in to change notification settings - Fork 0
/
memhog.c
55 lines (45 loc) · 945 Bytes
/
memhog.c
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
/*
Author: Zach Nafziger and Aaron Rosenberger
Course: COMP 340, Operating Systems
Date: 10 March 2015
Description:
Compile with: gcc -o memhog memhog.c -lm
Run with: ./memhog
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
void expensiveFunction(){
int i;
float sum = 0;
for(i = 0; i < 9000; i++)
{
int j;
for(j = 0; j < 9000; j++)
{
sum += cos((float)(i))/sin((float)(j));//do some expensive calculations
malloc(i*j*1000);//allocate to memory
}
sum /= cos(sum);
}
printf("finished expensive function.\n");
}
int main(int argc, char** argv) {
int pid;
int numChildren = 2; //change this to change number of forked children
int procNum;
for(procNum = 0; procNum < numChildren; procNum++) {
pid = fork();
if (pid == 0) {
break;
}
}
if (pid == 0) {
expensiveFunction();
}
else {
exit(1);
}
/*expensiveFunction();*/
return 0;
}