Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SIGALARM

Status
Not open for further replies.

mmque

Programmer
Nov 14, 2003
5
US
When we put alarm() in the program. Do we know we get signal SIGALARM or not, if we ignore this signal?
 
Try running this little program and determine for yourself.

Code:
#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(&quot;Set alarm with SIG_IGN\n&quot;,v);
                } 
                if (v % 2 == 0) {
                    printf(&quot;Installing new handler.\n&quot;,v);
                    alarm(1);
                    signal(SIG,catchCall);
                    COUNTER++;
                }
                printArray(pp,v);
                sleep(1);
                free(pp);
                p++;    
            }
printf(&quot;Saw %d user handled alarms from %d possible\n&quot;, COUNTER,MAX);
return 0;
}

                     
void catchCall(int sig) {
       printf(&quot;Alarm() popped\n&quot;);
}      
        

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(&quot;%d at element %d\n&quot;,parr[y],y);
                  ++y;
            }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top