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? $\textsf{exit}$ $\textsf{malloc}$ $\textsf{sleep}$ $\textsf{strlen}$ Operating System gatecse-2021-set1 multiple-selects operating-system system-calls one-mark + – Arjun 25.0k views answer comment Share Follow Print See all 3 Comments 3 3 Comments reply Bhartendu Kumar commented Mar 1, 2021 reply Follow flag @arjun sir and others please help that “sleep()” is a system call or function call? 1 1 replyShare zimgateoverflow commented Sep 1, 2021 reply Follow flag 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. 2 2 replyShare js__ commented Oct 20, 2025 reply Follow flag . 23 23 replyShare Please log in or register to add a comment.
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 optionsexit- 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(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. 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 • edited Jul 31, 2024 by Thadymademe Persistent comment Share Follow See 1 comment 1 1 comment reply Huzaifa0111 commented Jan 30, 2024 reply Follow flag https://media.geeksforgeeks.org/wp-content/uploads/20231017212555/Types-of-System-Calls-(3)-(2).png 0 0 replyShare Please log in or register to add a comment.
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 zxy123 answered Feb 18, 2021 • edited Feb 18, 2021 by zxy123 1 flag: ✌ Edit necessary (Abhianav) zxy123 comment Share Follow See all 7 Comments 7 7 Comments reply Show 4 previous comments debmalyaSEN commented Feb 18, 2021 reply Follow flag always is the key term sleep and exit are sys calls. malloc libary call ,but may invoke sbrk() sys call sometimes strlen library call 5 5 replyShare Babolat commented Feb 20, 2021 reply Follow flag 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 6 6 replyShare HitechGa commented Mar 5, 2021 reply Follow flag Yes this to confirm further. This is a chart given in Galvin. 28 28 replyShare Please log in or register to add a comment.
3 3 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 13 13 Comments reply Show 10 previous comments Arjun commented Feb 20, 2021 reply Follow flag I have already confirmed in the GO keys used in Pragys app :) 2 2 replyShare Arjun commented Feb 20, 2021 reply Follow flag “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. 6 6 replyShare Ashutosh777 commented Feb 20, 2021 reply Follow flag @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 replyShare Please log in or register to add a comment.
1 1 vote Famous Calls that invokes as well as not invokes a system are as stated below: Siddharth_Perkar answered Aug 25 Siddharth_Perkar comment Share Follow 0 reply Please log in or register to add a comment.
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. Dharmendra_sinh_05 answered Dec 6, 2023 Dharmendra_sinh_05 comment Share Follow See 1 comment 1 1 comment reply aspirant9090 commented Jan 2 reply Follow flag FunctionSystem call?NotesexitYesEnds program, flushes streamsmallocSometimesOnly if heap memory needs expandingsleepYesOS has to pause your programstrlenNoJust counts characters in memory 0 0 replyShare Please log in or register to add a comment.
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. om_30 answered Jul 20 om_30 comment Share Follow 0 reply Please log in or register to add a comment.