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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:43:26+00:00 2026-06-18T05:43:26+00:00

a c program that forks a child and gets int numbers from the keyboard

  • 0

a c program that forks a child and gets int numbers from the keyboard up to 10 seconds,after 10 seconds stops reading and check if any number is read or not,if nothing is read,it terminates the son otherwise it will print the read numbers on the screen and terminates,it does not show the result.

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>
#include<sys/wait.h>
#include<sys/errno.h>



int status,j,i=0;
pid_t son1, son2;
int string[20];
char s[5];


void handler(int signum)
{
if(i==0)
kill(son1,SIGKILL);
}

void signal_handler_1(int sig_num)
{
  printf("\nI sent the signal SIGUSR1 to my child\n");

}


main(){

    son1=fork();
     if(son1==0)
     {
        signal(SIGUSR1 , signal_handler_1);
        printf("I'm son1, my id is: %d\n", getpid());

      pause();
        printf("I'm son1 exitting: %d\n", getpid());

      exit(1);

    }
    if(son1>0){

    printf("I'm father, my id is: %d\n", getpid());
    signal(SIGALRM,handler);

         alarm(10);//how can i terminate the reading process after alarm??

               while(fgets(s,5,stdin)!=NULL)
                {
                  sscanf(s,"%d",&string[i]);
                  i++;
                }


              for(j=0;j<i;j++)
                printf("%d\n",string[j]);

              kill(son1,SIGUSR1);


              wait(&status);


    }
    return 0;
}
  • 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-18T05:43:27+00:00Added an answer on June 18, 2026 at 5:43 am

    The parent returns to the while-loop after running the signal handeler for SIGALRM.

    Also, like linkdd said, please describe the problem in more detail.

    In your code the parent sends SIGKILL to the child and it becomes defunct, i.e. a “zombie” since the parent is stuck. The parent never notices that the 10 seconds have elapsed and reads input forever.

    Not the prettiest way, but this will make it work.

    Define global

    int alarm_received = 0;
    

    Edit parent signal handeler to set a flag when the alarm has gone off.

    void handler(int signum)
    {
        if(i==0){
            kill(son1,SIGUSR1);
        }
        alarm_received = 1;
    }
    

    The child signal handeler could be like this. It is not good to call printf inside a signal handeler.

    void signal_handler_1(int sig_num)
    {
        char msg[] = "Child received SIGUSR1\n";
        write(STDOUT_FILENO, msg, sizeof(msg));
    }
    

    Change while loop to check if the signal has been sent. So the parent doesnt get stuck.

    while(alarm_received == 0)
    {
        fgets(s,5,stdin);
        sscanf(s,"%d",&string[i]);
        i++;
    }
    

    Its still pretty messy. The fgets() will most probably require (depending on operating system) an empty line after the signal has been received, because the read() (inside fgets) is restarted by the OS. The parsing of the input is also very fragile.

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

Sidebar

Related Questions

I have a program that gets a JSON from the server using getJSON and
OK I have a program that creates two pipes -> forks -> the child's
The following program forks off a child, that runs /bin/sleep 10 repeatedly. The parent
I have this program that forks and creates three POSIX pipes for the child's
I have a program that forks() , and the child process is replaced by
Heres a breakdown of my code. I have a program that forks a child
I'd like to profile a program that forks and spawns several child processes. I
I wrote a program that forks some processes with fork(). I want to kill
I'm writing a program that handles DBs and writes any changes into ListView for
Bassicly im creating a program that reads information from an xml file into a

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.