Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#define MAX 15
#define SIG 14
//function protos
void catchCall(int);
int *retnewArray(int);
void printArray(int *,int);
//global counter
int COUNTER = 0;
int main(void) {
int p = 0, v;
int *pp;
//init
srand(time(NULL));
signal(SIG,SIG_IGN);
//end init
while (p < MAX) {
v = (1 + rand() % MAX);
pp = retnewArray(v);
if (v % 2 == 1) {
alarm(1);
signal(SIG,SIG_IGN);
printf("Set alarm with SIG_IGN\n",v);
}
if (v % 2 == 0) {
printf("Installing new handler.\n",v);
alarm(1);
signal(SIG,catchCall);
COUNTER++;
}
printArray(pp,v);
sleep(1);
free(pp);
p++;
}
printf("Saw %d user handled alarms from %d possible\n", COUNTER,MAX);
return 0;
}
void catchCall(int sig) {
printf("Alarm() popped\n");
}
int *retnewArray(int num) {
int i = 0;
int *new = malloc(num * sizeof(int));
if (!new) {return NULL;}
for (i=0 ; i < num ; i++) {
new[i] = (1 + rand() % MAX);
}
return new;
}
void printArray(int *parr, int n) {
int y = 0;
while (y < n) {
printf("%d at element %d\n",parr[y],y);
++y;
}
}