-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.c
65 lines (57 loc) · 1.69 KB
/
Main.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
/** @file Main.c
* Stress the specified amount of processors to 100% of their computing power.
* @author Adrien RICCIARDI
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
//-------------------------------------------------------------------------------------------------
// Entry point
//-------------------------------------------------------------------------------------------------
int main(void)
{
int Processes_Count, i;
volatile int Dummy_Value = 0;
// Display banner
puts("+--------------------------------------+");
puts("| CPU stress (C) 2018 Adrien RICCIARDI |");
puts("+--------------------------------------+");
// Get the amount of processes to create
printf("Amount of CPUs to stress : ");
if ((scanf("%d", &Processes_Count) != 1) || (Processes_Count < 1)) // Try to make sure it is a valid number
{
printf("Please enter an integer number starting from 1.\n");
return EXIT_FAILURE;
}
// Fork as many processes as requested
for (i = 0; i < Processes_Count; i++)
{
switch (fork())
{
// An error occurred
case -1:
printf("Failed to create process %d (%s).\n", i, strerror(errno));
return EXIT_FAILURE;
// Child starts executing here
case 0:
while (1)
{
Dummy_Value += 6;
Dummy_Value *= 3;
Dummy_Value -= 7;
Dummy_Value /= 9;
}
// Parent continues execution here
default:
break;
}
}
// Block parent process without using processor resource
printf("Stressing %d processor(s)... Use Ctrl+C to stop the program.\n", Processes_Count);
wait(NULL);
return EXIT_SUCCESS; // Quit if one of the children has been killed
}