Created
May 19, 2014 18:53
-
-
Save johno/b828d0392aa61fc0a429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: John Otander | |
For: BSU CS 297 | |
Date: 3/12/2014 | |
Run Program: $ gcc godot.c -ogodot | |
$ ./godot <ALARM_INTERVAL> | |
*/ | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <pthread.h> | |
#include <sysexits.h> | |
#include <signal.h> | |
#define MAX_COUNT 1000000000 | |
// Alarm handler. | |
static void onalarm(int signo); | |
pid_t pid; | |
int main(int argc, char **argv) | |
{ | |
int i, num_cpus, random_number, between_nums, status, alarm_interval; | |
// Argument error checking. | |
if (argc != 2) { | |
fprintf(stderr, "Usage: godot <ALARM_INTERVAL>\n"); | |
exit(EXIT_FAILURE); | |
} | |
// Set alarm interval. | |
alarm_interval = atoi(argv[1]); | |
// Set number count for numbers between 90-110 to 0. | |
between_nums = 0; | |
// Get the number of CPUs. | |
num_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
// Set alarm handler, and the interval. | |
signal(SIGALRM, onalarm); | |
alarm(alarm_interval); | |
// Fork a new child for every CPU. | |
for (i = 0; i < num_cpus; i++) { | |
if(pid != 0) { | |
pid = fork(); | |
} | |
} | |
// Loop 4eva, or until the alarm sounds. | |
while(1) { | |
// Seed with srandom with pid. | |
srandom(getpid()); | |
// Generate random numbers. | |
for (i = 0; i < MAX_COUNT; i++) { | |
random_number = random(); | |
// Increment between_numbers if appropriate. | |
if(random_number < 110 && random_number > 90) | |
between_nums++; | |
} | |
printf("Generated %d special random numbers.\n", between_nums); | |
sleep(1); | |
} | |
exit(EXIT_SUCCESS); | |
} | |
// Kill child process. | |
static void onalarm(int signo) | |
{ | |
printf("ALARM INTERVAL REACHED\n"); | |
kill(pid, SIGKILL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment