retagged by
10,527 views
18 votes
18 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}$
retagged by

4 Answers

Best answer
27 votes
27 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 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

selected by
10 votes
10 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
4 votes
4 votes

The Correct answer is A,B,C.

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

0 votes
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.
Answer:

Related questions