in Operating System retagged by
10,584 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}$
in Operating System retagged by
by
10.6k views

2 Comments

@arjun sir and others please help that “sleep()” is a system call or function call?
1
1
either the question is wrong or given options(as the final answer is wrong) because sleep is not a c standard lib function. it belongs to a header file <unistd.h>( or <windows.h> ) present inside c posix os api which works on posix conformant systems like linux etc.interesting point to note is c posix lib is a superset of c standard lib . only exit() is a function which belongs to c standard lib and requires system call because it will ultimately change the state of a process (along with deallocation of many resources).

also as stated in some comments the same doubt exist for malloc implementation.
1
1

4 Answers

27 votes
27 votes
Best answer

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 Comments

always is the key term

sleep and exit  are sys calls.

malloc  libary call ,but may invoke sbrk() sys call sometimes

strlen library call
3
3

When a process needs memory, some room is created by moving the upper bound of the heap forward, using the brk() or sbrk() system calls. Because a system call is expensive in terms of CPU usage, a better strategy is to call brk() to grab a large chunk of memory and then split it as needed to get smaller chunks. This is exactly what malloc() does. It aggregates a lot of smaller malloc() requests into fewer large brk() calls. Doing so yields a significant performance improvement. The malloc() call itself is much less expensive than brk(), because it is a library call, not a system call. Symmetric behavior is adopted when memory is freed by the process. Memory blocks are not immediately returned to the system, which would require a new brk() call with a negative argument. Instead, the C library aggregates them until a sufficiently large, contiguous chunk can be freed at once.

Reference : Advanced Memory Allocation | Linux Journal

4
4

Yes this to confirm further.  This is a chart given in Galvin.

15
15
4 votes
4 votes

The Correct answer is A,B,C.

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

4 Comments

I have already confirmed in the GO keys used in Pragys app :)
1
1
“will always” – normally there is no need to add this “always” in the given sentence. So, this is a very intentional addition and the question asker would have wanted to check the knowledge of how malloc works – is it a graduate level question? I dont think so. “can cause a system call” should have been a better question for GATE level.
4
4
@arjun sir there is one more ques of hamming code where they gave d4 twice,don’t you think it should be marks to all?
0
0
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