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

  • Home
  • SEARCH
  • 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 5985033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:28:08+00:00 2026-05-22T22:28:08+00:00

My program goes through a loop like this: … while(1){ read(sockfd,buf,sizeof(buf)); … } The

  • 0

My program goes through a loop like this:

...
while(1){
  read(sockfd,buf,sizeof(buf));
  ...
}

The read function blocks when it is waiting for input, which happens to be from a socket. I want to handle SIGINT and basically tell it to stop the read function if it is reading and then call an arbitrary function. What is the best way to do this?

  • 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-22T22:28:09+00:00Added an answer on May 22, 2026 at 10:28 pm

    From read(2):

       EINTR  The call was interrupted by a signal before any data
              was read; see signal(7).
    

    If you amend your code to look more like:

    cont = 1;
    while (1 && cont) {
        ret = read(sockfd, buf, sizeof(buf));
        if (ret < 0 && errno == EINTR)
            cont = arbitrary_function();
    }
    

    This lets arbitrary_function() decide if the read(2) should be re-tried or not.

    Update

    You need to handle the signal in order to get the EINTR behavior from read(2):

    #include<unistd.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<signal.h>
    #include<errno.h>
    
    int interrupted;
    
    void handle_int(int num) {
      interrupted = 1;
    }
    
    int main(void){
      char buf[9001];
      struct sigaction int_handler = {.sa_handler=handle_int};
      sigaction(SIGINT,&int_handler,0);
      while(!interrupted){
        printf("interrupted: %d\n", interrupted);
        if(read(0,buf,sizeof(buf))<0){
          if(errno==EINTR){
            puts("eintr");
          }else{
            printf("%d\n",errno);
          }
          puts(".");
        }
      }
      puts("end");
      return 0;
    }
    

    Gives output:

    $ ./foo
    interrupted: 0
    hello
    interrupted: 0
    ^Ceintr
    .
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a small program in C# which goes through my
Occasionally a program on a Windows machine goes crazy and just hangs. So I'll
Strange program hang, what does this mean in debug? After attaching windbg I found
I have a program that will analyzes source code. It can recursively go through
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
Most program languages have some kind of exception handling; some languages have return codes,
My program generates relatively simple PDF documents on request, but I'm having trouble with
A program receives a list of Messages (base type). Each message in the list
My Program overrides public void paint(Graphics g, int x, int y); in order to
What program can I use to decompile a class file? Will I actually get

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.