Which of the following standard C library functions will always invoke a…

2021

Which of the following standard C library functions will always invoke a system call when executed from a single-threaded process in a UNIX/Linux operating system?

  1. A.

    exit

  2. B.

    malloc

  3. C.

    sleep

  4. D.

    strlen

Attempted by 440 students.

Show answer & explanation

Correct answer: A, C

  • exit — The C library performs user-level cleanup (runs atexit handlers, flushes stdio) and then invokes the kernel to terminate the process (commonly via the exit or exit_group system call). This termination always involves a system call.

  • malloc — Allocations are normally satisfied from memory the runtime already manages in user space. Only when the heap needs more memory will the allocator request it from the kernel (for example via brk/sbrk or mmap), and those requests use system calls. Therefore malloc does not always invoke a system call.

  • sleep — Suspending the calling process requires kernel involvement. The C library typically uses a timer-related system call (for example nanosleep or equivalent) to block the process for the requested duration, so sleep results in a system call.

  • strlen — This routine scans memory in user space until it finds the terminating null byte. It performs no kernel operations and does not invoke a system call.

Correct answers: exit and sleep will invoke system calls when executed in a single-threaded process.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir