0

I have to write a program called BuzzOff.c. My program has to take in 3 integer arguments as such:

$ BuzzOff 10 99999 2

My program should quietly count from 0 to by 0.001 increments and keep a running total of the resulting product of and the counter, i.e. total += count * <arg1>;

I've read up signals but I'm still not sure how they work. How do I make my program print the current running total when it receives SIGUSR1?

This is what I have so far:

#include<stdio.h>
#include<signal.h>

float total;

void sig_handler(int signo)
{
    if (signo == SIGUSR1)
      printf("total: %f\n", total);
}

int main(int argc, char *argv[])
{
    if( argc!=4 ) {
        printf("need three arguments\n"); return(1);
    }
    float count;

    for (count = 0; count < argv[3]; count += 0.001)
      total += count*argv[2];

    return 0;

}
user2264035
  • 565
  • 1
  • 7
  • 13
  • Perhaps the compiler errors you (should) receive will point you in the right direction. – Kevin Nov 06 '13 at 00:01
  • You've got bigger problems than signals, in here, and SO isn't really the right place to ask someone to just teach you how signals work, but you essentially have to (1) register your signal handler, preferably with `sigaction()`; and (2) actually send the signal to your process in some way. `printf()` and friends are generally not safe to use in a signal handler, although you won't get any problems in this particular case. – Crowman Nov 06 '13 at 00:09
  • And you can run into problems using global variables like that, too. `sig_atomic_t` would be better than `float`, but `sig_atomic_t` is an integral type. – Crowman Nov 06 '13 at 00:17

1 Answers1

0

You need to specify the handler for SIGUSR1.

Something like..

signal(SIGUSR1, sig_handler);

and call that in main()

Abhilash Divakaran
  • 4,229
  • 1
  • 16
  • 16