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 40077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:58:01+00:00 2026-05-10T14:58:01+00:00

The following program is very simple: it outputs a single dot each half a

  • 0

The following program is very simple: it outputs a single dot each half a second. If it recieves a SIGQUIT, it proceeds to output ten Qs. If it recieves a SIGTSTP (Ctrl–Z), it outputs ten Zs.

If it recieves a SIGTSTP while printing Qs, it will print ten Zs after it’s done with the ten Qs. This is a good thing.

However, if it recieves a SIGQUIT while printing Zs, it fails to print Qs after them. Instead, it prints them out only after I manually terminate execution via a KeyboardInterrupt. I want the Qs to be printed immediately after the Zs.

This happens using Python2.3.

What am I doing wrong?

#!/usr/bin/python  from signal import * from time import sleep from sys import stdout  def write(text):     stdout.write(text)     stdout.flush()  def process_quit(signum, frame):     for i in range(10):         write("Q")         sleep(0.5)  def process_tstp(signum, frame):     for i in range(10):         write("Z")         sleep(0.5)  signal(SIGQUIT, process_quit) signal(SIGTSTP, process_tstp)  while 1:     write('.')     sleep(0.5) 
  • 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. 2026-05-10T14:58:01+00:00Added an answer on May 10, 2026 at 2:58 pm

    Your larger problem is blocking in signal handlers.

    This is usually discouraged since it can lead to strange timing conditions. But it’s not quite the cause of your problem since the timing condition you’re vulnerable to exists because of your choice of signal handlers.

    Anyway, here’s how to at least minimize the timing condition by only setting flags in your handlers and leaving the main while loop to do the actual work. The explanation for why your code is behaving strangely is described after the code.

    #!/usr/bin/python  from signal import * from time import sleep from sys import stdout  print_Qs = 0 print_Zs = 0  def write(text):     stdout.write(text)     stdout.flush()  def process_quit(signum, frame):      global print_Qs      print_Qs = 10  def process_tstp(signum, frame):      global print_Zs      print_Zs = 10  signal(SIGQUIT, process_quit) signal(SIGTSTP, process_tstp)  while 1:     if print_Zs:         print_Zs -= 1         c = 'Z'     elif print_Qs:         print_Qs -= 1         c = 'Q'     else:         c = '.'     write(c)     sleep(0.5) 

    Anyway, here’s what’s going on.

    SIGTSTP is more special than SIGQUIT.

    SIGTSTP masks the other signals from being delivered while its signal handler is running. When the kernel goes to deliver SIGQUIT and sees that SIGTSTP’s handler is still running, it simply saves it for later. Once another signal comes through for delivery, such as SIGINT when you CTRL+C (aka KeyboardInterrupt), the kernel remembers that it never delivered SIGQUIT and delivers it now.

    You will notice if you change while 1: to for i in range(60): in the main loop and do your test case again, the program will exit without running the SIGTSTP handler since exit doesn’t re-trigger the kernel’s signal delivery mechanism.

    Good luck!

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Attributes don't actually do anything, they basically just tag the… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer You'll want to use a UNION SELECT: SELECT p.id, COUNT(p.id),… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer Make sure to use alpha transparency only on tiles that… May 12, 2026 at 12:58 am

Related Questions

I am creating an .msi package for the application which has a prerequisite for
Greetings everyone. This is my first question here at stackoverflow so please bear with
I've just started learning how to use the Entity Framework to write a very
Note: this was inspired by WebBrowser Event Properties? Why am I able to access

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.