-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculatepi.c
69 lines (49 loc) · 1.34 KB
/
calculatepi.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include "warmer.h"
void calculate_pi ()
{
}
void * control_cpu_usage ( mt_ctrl_t * c )
{
while ( 1 )
{
pthread_mutex_lock ( &c->m );
int cup = c->cpu_usage_percentage;
pthread_mutex_unlock ( &c->m );
int procs = ( ( ( cup + 100 ) / 100 ) );
pid_t * pids = ( pid_t * ) malloc ( sizeof ( pid_t ) * procs );
for ( int i = 0; i < procs; i++ )
{
unsigned long busy_time = i == procs - 1 ? cup % 100 : 100,
idle_time = 100 - busy_time;
// printf("\t%lu", busy_time);
pid_t pid = fork();
if ( pid < 0 )
{
exit ( EXIT_FAILURE );
}
if ( pid == 0 )
{
clock_t st_time = clock();
while ( ( clock() - st_time ) * 100 / CLOCKS_PER_SEC < busy_time );
usleep ( idle_time * 10 * 1000 );
exit ( EXIT_SUCCESS );
}
if ( pid > 0 )
{
pids[i] = pid;
}
}
int status;
for ( int i = 0; i < procs; i++ )
{
waitpid ( pids[i], &status, ( int ) NULL );
}
}
return NULL;
}