modifying a program

I have this program:
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <stdlib.h>

#define LOOP_LIMIT 1E12

volatile int sigcount=0;

int main(int argc, char** argv) {
struct itimerval value;

value.it_interval.tv_sec = 0;
value.it_interval.tv_usec = 200000;
value.it_value.tv_sec = 5;
value.it_value.tv_usec = 0;

setitimer(ITIMER_REAL, &value, 0);

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.
ill take the most basic academic way,whatever will make the program work.
Are you supposed to use signal handlers?
i believe so,that what weve been learning about.
Ok, so write a signal handler. If you are using signal() to register the handler, then your signal handler must have the signature:

void (*)( int );

Inside the handler, increment your sigcount variable and do nothing else.

In main(), register your handler function to handle SIGALRM using signal().

Then, make a loop that looks like this pseudo-code:

1
2
3
while( haven't got 5 signals yet ) {
    sleep-for-very-long-time (use sleep())
} 


As you should know from class, sleep() will return -1 with errno set to EINTR because your program received SIGALRM.
ok........

I wrote my loop as

while(sigcount!=5){
printf("An alarm went off"):
}

is that right?
Last edited on
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.
so then it would be

while( sigcount < 5 ) {
sleep()
}


where would i print an alarm went off?
can somebody help me please?
sleep takes a parameter.

To answer your question:

As you should know from class, sleep() will return -1 with errno set to EINTR because your program received SIGALRM.


EDIT: At least I am assuming your professor covered this...
Last edited on
so then it would be....

while(sigcount <5){
sleep(printf "An Alarm just went off", sigaction(SIGALRM, &sa, NULL));
}
dude if your still there,help would be appreciated

also where exactly do i put this loop?
Last edited on
Here is the prototype for sleep:
 
unsigned int sleep( unsigned int seconds );


Pass it the number of seconds you want to sleep. I recommended using a large value.

The sleep() function returns only when:
a) The amount of time specified elapses; OR
b) It was interrupted, for example, by receiving a signal.

Put the loop in your main() after you register the signal handler and after you start the ITimer.
Topic archived. No new replies allowed.