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? $\textsf{exit}$ $\textsf{malloc}$ $\textsf{sleep}$ $\textsf{strlen}$ Operating System gatecse-2021-set1 multiple-selects operating-system system-call 1-mark + – Arjun asked Feb 18, 2021 retagged Nov 30, 2022 by Lakshman Bhaiya Arjun 10.5k views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Bhartendu Kumar commented Mar 1, 2021 reply Follow Share @arjun sir and others please help that “sleep()” is a system call or function call? 1 votes 1 votes zimgateoverflow commented Sep 1, 2021 reply Follow Share 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 votes 1 votes Please log in or register to add a comment.
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 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. 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. 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 . 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 Persistent answered Feb 20, 2021 selected Jun 13, 2021 by Arjun Persistent comment Share Follow See 1 comment See all 1 1 comment reply Huzaifa0111 commented Jan 30 reply Follow Share https://media.geeksforgeeks.org/wp-content/uploads/20231017212555/Types-of-System-Calls-(3)-(2).png 0 votes 0 votes Please log in or register to add a comment.
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 zxy123 answered Feb 18, 2021 edited Feb 18, 2021 by zxy123 zxy123 comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments debmalyaSEN commented Feb 18, 2021 reply Follow Share 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 votes 3 votes Babolat commented Feb 20, 2021 reply Follow Share 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 votes 4 votes HitechGa commented Mar 5, 2021 reply Follow Share Yes this to confirm further. This is a chart given in Galvin. 15 votes 15 votes Please log in or register to add a comment.
4 votes 4 votes The Correct answer is A,B,C. As exit , malloc, sleep invole system calls but strlen doesnt. Sherrinford03 answered Feb 19, 2021 Sherrinford03 comment Share Follow See all 13 Comments See all 13 13 Comments reply Show 10 previous comments Arjun commented Feb 20, 2021 reply Follow Share I have already confirmed in the GO keys used in Pragys app :) 1 votes 1 votes Arjun commented Feb 20, 2021 reply Follow Share “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 votes 4 votes Ashutosh777 commented Feb 20, 2021 reply Follow Share @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 votes 0 votes Please log in or register to add a comment.
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. Dharmendra_sinh_05 answered Dec 6, 2023 Dharmendra_sinh_05 comment Share Follow See all 0 reply Please log in or register to add a comment.