• retagged by
24,995 views
54 54 votes

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

  1. $\textsf{exit}$
  2. $\textsf{malloc}$
  3. $\textsf{sleep}$
  4. $\textsf{strlen}$

7 Answers

Best answer
73 73 votes

System calls are used to get some service from operating system which generally requires some higher level of privilege. 

This question uses two important words “always” and “standard C library functions”.
Let’s check options

  1. exit- This is a function defined in standard C library and it always invokes system call every time, flushes the streams, and terminates the caller.
  2. malloc – This is a function defined in standard C library and it does not always invoke the system call. When a process is created, certain amount of heap memory is already allocated to it, when required to expand or shrink that memory, it internally uses sbrk/brk system call(this does not happen everytime) on Unix/Linux. i.e., not every malloc call needs a system call but if the current allocated size is not enough, it’ll do a system call to get more memory. 
  3. sleep- This is not even  standard C library function, it is a POSIX standard C library function. Unix and Windows uses different header files for it. Now as question has said the following standard C library function, let’s consider it as that way. Yes it always invokes the system call .
  4. strlen – This is a function defined in standard C library and doesn’t require any system call to perform its function of calculating the string length. 

Answer : A,C

• edited by
16 16 votes

Answer should be sleep, as it is a blocking system call the OS would consider the process as blocked.

The exit call in Linux is also a system call.

malloc is a library call and not always a system call

• edited by
1 flag:
✌ Edit necessary (Abhianav)
3 3 votes

The Correct answer is A,B,C.

As exit , malloc, sleep invole system calls but strlen doesnt.

0 0 votes
Exit is the correct ans

This is a function defined in standard C library and it always invokes system call every time, flushes the streams, and terminates the caller.
0 0 votes

The correct options are A (exit) and C (sleep).

  • A. exit (Correct)
    • Why: The standard C library exit() function terminates a process. To clean up the process resources, close file descriptors, and return the exit status to the parent process, it must invoke the underlying kernel system call (such as _exit() or exit_group() in Linux). This step always requires operating system intervention.
  • C. sleep (Correct)
    • Why: The sleep() function suspends the execution of the calling process for a specified amount of time. A user-space program cannot yield the CPU or track time independently without kernel assistance. Thus, it always invokes a system call (like nanosleep() or alarm()) so the OS scheduler can temporarily remove the process from the running state.
  • B. malloc (Incorrect)
    • Why: The malloc() function manages a chunk of memory called the heap in user space. If malloc() has enough contiguous free space already allocated within its internal pool, it satisfies the request entirely in user space. It only invokes a system call (such as brk(), sbrk(), or mmap()) when it needs to request additional memory from the kernel. Therefore, it does not always trigger a system call.
  • D. strlen (Incorrect)
    • Why: The strlen() function simply calculates the length of a string by scanning memory sequentially until it hits the null terminator (\0). This is a pure computational operation handled entirely within the CPU registers and user-space memory, requiring no system call at all.
Answer:
Position:
Show:

Related questions

38 38 votes
8 answers 8 answers
31.4k
31.4k views
Arjun asked Feb 18, 2021
31,381 views
In the context of operating systems, which of the following statements is/are correct with respect to paging?Paging helps solve the issue of external fragmentationPage si...
48 48 votes
4 answers 4 answers
18.8k
18.8k views
Arjun asked Feb 18, 2021
18,780 views
Consider a linear list based directory implementation in a file system. Each directory is a list of nodes, where each node contains the file name along with the file meta...
58 58 votes
7 answers 7 answers
29.2k
29.2k views
Arjun asked Feb 18, 2021
29,160 views
Consider the following pseudocode, where $\textsf{S}$ is a semaphore initialized to $5$ in line $\#2$ and $\textsf{counter}$ is a shared variable initialized to $0$ in li...