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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:46:33+00:00 2026-06-02T04:46:33+00:00

What actually happens after calling read: n = read(fd, buf, try_read_size); here fd is

  • 0

What actually happens after calling read:

n = read(fd, buf, try_read_size);

here fd is a TCP socket descriptor. buf is the buffer. try_read_size is the number of bytes that the program tries to read.

I guess this may finally invokes a system call to the kernel. But could anyone provide some details? say the source code implementation in glibc or kernel source?

  • 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-02T04:46:34+00:00Added an answer on June 2, 2026 at 4:46 am

    From a high-level perspective, this is what happens:

    • A wrapper function provided by glibc is called
    • The wrapper function puts the parameters passed on the stack into registers and sets the syscall number in the register dedicated for that purpose (e.g. EAX on x86)
    • The wrapper function executes a trap or equivalent instruction (e.g. SYSENTER)
    • The CPU switches to ring0, and the trap handler is invoked
    • The trap handler checks the syscall number for validity and looks it up in a jump table to kernel functions
    • The respective kernel function checks whether arguments are valid (e.g. the range buf to buf+try_read_size refers to accessible memory pages, fd is really a file descriptor). If something is amiss, a negative error code (e.g. -EFAULT) is generated, the cpu is switched back to user mode and the call returns to the wrapper.
    • Another function is called depending on the file descriptor’s type (in your case a socket, but one could read from a block device or a proc entry or something more exotic)
    • The socket’s input buffer is checked:
      • If there is some data in the buffer, min(available, try_read_size) is copied to buf, the amount is written to the return code register (EAX on x86), the cpu is switched back to user mode and the call returns to the wrapper.
      • If the input buffer is empty
        • If the connection has been closed, zero is written to the return code register, the cpu is switched back to user mode and the call returns to the wrapper
        • If the connection has not been closed
          • A negative error code (-EAGAIN) is written to the return code register if the socket is nonblocking, the cpu is switched back to user mode and the call returns to the wrapper.
          • The process is suspended if the socket is not non-blocking
    • The wrapper function checks whether the return value is negative (error).
      • If positive or zero, it returns the value.
      • If negative, it sets errno to the negated value (a positive error is reported) and returns -1
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering what actually happens after you exceed 2147483647 records?
What actually happens to the file system when you do a Subclipse Share Project
I want to know what actually happens when you annotate a method with @Transactional
It just hit me the other day. What actually happens when I tell the
Actually here is a similar topic with little practical value. As far as I
if((x == 5) || (x == 2)) { [huge block of code that happens]
How should I check a (TCP) socket to find out whether it is connected?
actually I'm have a very simple problem (that's for sure) I just can't find
After doing a chunk of reading about authenticating users (both here in SO land
I have a foreach loop that forks within it. After the process forks, it

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.