int count;
for( count=0; ((count<LOOP_LIMIT) && (sigcount<40)); count++ ){
if (count%5000000 == 0)
printf("Time goes by...count=%d\n", count);
if (count==0)
printf("An alarm just went off!", sigcount++);
}
printf( "AT the end, count=%d and alarms+%d\n", count, sigcount );
return 0;
}
for homework im supposed to modify it that every time the registered timer expires,the program prints "An alarm went off!" and then increments the value of the variable sigcount.
then after that im supposed to use setitimer(ITIMER_REAL,0,)); which will disable the real timer(if set) to modify the program after the 5 alarms have been recieved.
I know my codes not correct and i know getitimer() is involved in this somehow,if someone could help me and tell me what i have to do i would appreciate it.
There are a couple of ways to accomplish this, some of which are more academic and some of which are more safe. Which do you want?
The way you are approaching the problem, you'll need to write a signal handler function to handle SIGALRM. When your handler runs, it needs to set a flag indicating the timer expired which your main function then notices.
Like I said -- there are many ways to tackle this; the right way for your class is to use whatever has been taught so far. I could say how I'd do it, but that might not be what your professor is expecting.
The while() is right; the body isn't. I suggested above to call sleep(). You want your process basically to do nothing except receive 5 signals. Your code will print out the message "An alarm went off" as fast as it can until it receives 5 signals.
The best way to do nothing is to make your process go to sleep. If you literally do nothing, meaning
1 2 3
while( sigcount < 5 ) {
// Do nothing!
}
then you will use all of the CPU as your program continuously checks sigcount against 5 as fast as it can.