Course contentsShow
← Computer Science
Lesson 1208 of 2,87228. Operating Systems FundamentalsFree lesson

System Call Parameters and Return Values

Learn how arguments are passed to system calls and how return values and error codes are handled.

System Call Parameters and Return Values

You'll learn how system calls receive input arguments from user programs and communicate results and errors back.

Passing Arguments to System Calls

When your program makes a system call, it usually needs to provide information—like which file to open or how many bytes to read. These parameters are passed from your user-mode program to the kernel.

Think of it like ordering at a restaurant: you don't just say "food," you specify what dish, how spicy, and what size. Similarly, a read() system call needs a file descriptor, a buffer location, and byte count.

How are they passed?
Typically through CPU registers (fast, limited slots), or for many arguments, on the program's stack. The calling convention depends on the architecture and OS, but the kernel knows exactly where to look for each parameter.

Receiving Return Values

Every system call reports back what happened. The return value usually goes in a dedicated register (like eax on x86).

Common patterns:

  • Success with data: Return a non-negative value (e.g., number of bytes read, or a new file descriptor)
  • Failure: Return -1 (or similar sentinel) to signal an error

Error Codes

When a system call fails, how do you know why? The return value alone isn't enough. Most systems use a global variable called errno (error number) that gets set to a specific code explaining the problem—like "file not found" or "permission denied."

Example flow:
Your program calls open() → kernel checks permissions → file doesn't exist → returns -1 and sets errno to ENOENT (No such file or directory).

Key Takeaway:
System calls use registers or stack for input parameters, return success/failure status directly, and store detailed error codes in errno for diagnosis.