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

The Archive Base Latest Questions

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

Im a beginner C dev. Im trying to understand in what way C manages

  • 0

Im a beginner C dev. Im trying to understand in what way C manages kills, signals and sleep. See:

void son1_handler(int sig)
{
    printf("[SON1]:");
    fflush(stdout);
}

void son2_handler(int sig)
{
    printf("[SON2]:");
    fflush(stdout);
}

int main(int argc, char **argv)
{
    pid_t son1, son2;
    son1 = fork();
    if (son1 == 0)
    {
        while (1)
        {
            signal(SIGUSR1, son1_handler);
        };
    }

    son2 = fork();
    if (son2 == 0)
    {
        while (1)
        {
            signal(SIGUSR2, son2_handler);
        };
    }

    while (1)
    {
        sleep(1);
        kill(son1, SIGUSR1);
        kill(son2, SIGUSR2);
    }
}

My objective is to print: [SON1]:[SON2]:[SON1]:[SON2]….
With code above, sometimes got that, sometimes [SON2]:[SON1]:[SON2]…

I thought changing using sleep between kills:

kill(son1, SIGUSR1);
sleep(1);
kill(son2, SIGUSR2);

but surprisingly the result was: [SON2]:[SON2]:[SON2]:[SON2]:[SON2]….

I think that i must be misunderstangding something about sleep, kill, or signal. Do you have any ideas? Really thanks in advance.

  • 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-05-25T14:01:43+00:00Added an answer on May 25, 2026 at 2:01 pm

    This is because of process scheduling.

    When you ‘send’ a signal, the corresponding bit in the PCB (Process Control Block) is set.
    and then when the process to which the signal was sent is scheduled, it checks for signals and runs the handlers.

    In your above example, parent process is sending the signals one after another but there is no guarantee that ‘son1’ will be executed before ‘son2’.

    Sleep just allows the operating system to ‘switch the context’ i.e. allow other program to run. So, in this case

    kill(son1, SIGUSR1);
    sleep(1);
    kill(son2, SIGUSR2);
    

    You send a signal to son1 and then put parent process to sleep. However, it is not necessary that son1 process will be scheduled before parent process again. This depends on the operating system’s process scheduler. You would need to use some sort of mutex mechanism like semaphores if you want to achieve synchronization like you mentioned.

    Hope that clears things up.

    Edit:
    Also, in Linux, the signal handlers stay registered even after subsequent calls to them unlike in Unix where signal handlers get unregistered after one call. This means you only have to call signal() once in your child programs instead of calling in every iteration of your while() loops.

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

Sidebar

Related Questions

I am a total total beginner in Java actually I am .net dev trying
I am a complete beginner trying to develop for FCKeditor so please bear with
As a complete beginner with no programming experience, I am trying to find beautiful
I'm a beginner with iPhone dev in Objective C and one thing I find
Beginner at Django here, I've been trying to fix this for a long time
Beginner here See - http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells In this example, when change content is pressed, new
I am beginner and wanna to learn few things before i start php dev/learning
import serial import imaplib from time import sleep IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 ser= serial.Serial ('/dev/ttyACM0',9600) M
import serial import imaplib from time import sleep IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 ser= serial.Serial ('/dev/ttyACM0',9600) while
I was doing some beginner AppEngine dev on a Windows box and installed Eclipse

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.