Find the answer to your Linux question:
Results 1 to 8 of 8
Is SIGFPE signal automatically raised and sent when a division by 0 occurs(just for example).Because , I thought that it would as I wrote in the following program , but ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12

    Signal - SIGFPE

    Is SIGFPE signal automatically raised and sent when a division by 0 occurs(just for example).Because , I thought that it would as I wrote in the following program , but when I run the program , nothing happens ; my handler is not called :
    Code:
    void sigfpe_handl(int sig)
    {
         printf("Argggh..Division by zero !\n");
         //..some more
    }
    void sigint_handl(int sig)
    {
         printf("Program is exiting..");
         exit(0);
    }
    int main()
    {
         signal(SIGINT,sigint_handl);
         signal(SIGFPE,sigfpe_handl);
         float x,y;
         while(1)
         {
              scanf("%f %f",&a,&b);
              if(b)
                   printf("%g",a/b);
          }
         return 0;
    }
    when run , if I enter 2 0 for example , it just does nothing . No "Argggh.." .
    So why ?
    I just read a bit about signal in book , where there are examples of SIGINT and SIGALRM , and then I try the above program . Please help me . Thanks in advance .

  2. #2
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12
    Any hint ?

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    try integers instead of floats

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

    void sigfpe_handl(int sig)
    {
    printf("Argggh..Division by zero !\n");
    }

    int main()
    {
    signal(SIGFPE,sigfpe_handl);
    int x,y;

    fscanf(stdin, "&#37;d", &x);
    fscanf(stdin, "%d", &y);
    fprintf(stdout, "x->%d\n", x);
    fprintf(stdout, "y->%d\n", y);

    printf("%d",x/y);
    fputs("\n",stdout);
    return 0;
    }

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Oh, good grief!

    Here's your problem. Take a look at the line I've highlighted in red in your program:
    Code:
    void sigfpe_handl(int sig)
    {
         printf("Argggh..Division by zero !\n");
         //..some more
    }
    void sigint_handl(int sig)
    {
         printf("Program is exiting..");
         exit(0);
    }
    int main()
    {
         signal(SIGINT,sigint_handl);
         signal(SIGFPE,sigfpe_handl);
         float x,y;
         while(1)
         {
              scanf("%f %f",&a,&b);
              if(b)
                   printf("%g",a/b);
          }
         return 0;
    }
    Your program is never going to divide by zero, now, is it?

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12
    Oh , in the 1st post , I miss this line :
    Code:
    float temp = a/b
    . Sorry . Without this , of course , there would not be anthing to ask . But with this line , there's nothing happen , as I mentioned !
    And if I change float to int , as gerard said , there are a lot of "Arrggh.." (infinite!) if I enter 0 for b
    To summarize :
    - With float , there's nothing
    - With int , sigfpe_handl wil be repeated infinitely

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Ok, this is progress.

    But please, instead of posting English descriptions of changes to your code, could you please please please post the complete, latest-version code which has the problem? (The float version, of course, because the int one acts as you expect it to.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12
    Here it is for the floats :
    Code:
    void sigfpe_handl(int sig)
    {
         printf("Argggh..Division by zero !\n");
         //..some more
    }
    void sigint_handl(int sig)
    {
         printf("Program is exiting..");
         exit(0);
    }
    int main()
    {
         signal(SIGINT,sigint_handl);
         signal(SIGFPE,sigfpe_handl);
         float x,y,temp;
         while(1)
         {
              scanf("%f %f",&a,&b);
              temp = a / b ; 
              if(b)
                   printf("%g",a/b);
          }
         return 0;
    }
    And for the ints , it doesn't work as I expect also . Because , it should print only one error message - if there is error - and process next I/O , so on until Ctrl-C . But with one error , it prints many error messages - that's the problem !

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This can't be the complete program, because you don't declare a or b anywhere.

    Please do this:
    1. Edit the program to the way you want it. THEN
    2. Compile it. If it doesn't compile, go back to the first step. If it does compile, THEN
    3. Run it. If it doesn't give the results you wish to discuss, go back to the first step. If it does, THEN
    4. Post the complete program, #includes and all.

    Then let's take a look at it. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...