Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:25:13+00:00 2026-06-10T09:25:13+00:00

I was trying this program from Advance Programming in Unix Environment. #include<stdio.h> #include<signal.h> static

  • 0

I was trying this program from Advance Programming in Unix Environment.

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

static void handler(int sig){
    if(sig == SIGUSR1)
        printf("handled user1 signal");
    else if(sig == SIGUSR2)
        printf("handles user2 signal");
    else
        printf("unkown signal");
}

int main(){

    if(signal(SIGUSR1, handler) == SIG_ERR)
        printf("can't handle signal SIGUSR1");
    if(signal(SIGUSR2, handler) == SIG_ERR)
        printf("can't handle signal SIGUSR2");
    for(;;)
        pause();
    return 0;
}

I am using Ubuntu 11.10. I compile the program with gcc and then run a.out as indicated in the book.

$./a.out&
[1]+ 1345

$ kill -USR1 1345

But there is no output printed. The program keeps running in backgound and I have to kill it.

Other things I have tried:

  1. Tried handling SIGINT to see if running program in background is causing problems. Still no output.

  2. Downloaded latest release of FreeBSD and tried the same program on it, but with same problem.

  3. I put a printf statement before setting signal handler:

    int main(){
        printf("printf is working...");
        //exit(0);
        if(signal(SIGUSR1, handler) == SIG_ERR)
        ...
    

when exit() is commented, there is no output. When I uncomment it, the output is printed.

Please tell me what am I doing wrong in this?

PS: Don’t suggest using sigaction(). I am learning Unix Programming, not building any practical application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:25:15+00:00Added an answer on June 10, 2026 at 9:25 am

    The output from printf is buffered. That means it’s stored in memory until flushed to the output. The best way to flush text in printf is to end the text with a newline. You can also flush manually with the fflush function.

    However, you should be cautioned that using output functions like printf and fflush is not considered safe in signal handlers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new kernel programming.i have been trying to load this driver program for
I'm having a problem when trying to compile the program in this tutorial from
i'm trying to get this program to break down a user defined amount of
Hi iam new to c++ and iam trying out this vector program and i
Well, I was trying to fix this program, and I keep getting the errors
In this program I am trying to make, I have an expression (such as
I am trying to make this sample program work (defn foo ([x] (foo x
I'm trying to get this simple program to work on windows, but it crashes:
I'm trying to compile this simple program to start learning how to use timers:
I am trying to solve a problem. This program contains all the edge in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.