My teacher asked me to write a C function which does not perform any syscalls. It doesn't matter that the function accomplishes nothing.
Does the following function perform any syscall?
int func() {
return 0;
}
If it does, can you give me an exemplar function like the one I'm looking for?
Thanks a lot.
A system call can be defined as a request to the operating system to do something on behalf of the program. During the execution of a system call, the mode is change from user mode to kernel mode (or system mode) to allow the execution of the system call.
Programmers don't normally need to be concerned with system calls because there are functions in the GNU C Library to do virtually everything that system calls do. These functions work by making system calls themselves.
To know which functions out of standard library are system calls you need to check manual pages. Moreover, you can check what kind of system calls does your program call using strace tool. Some more complicated functions are not equivalent to system calls, but use them underneath - like your printf and malloc .
System call provides the services of the operating system to the user programs via Application Program Interface(API). It provides an interface between a process and operating system to allow user-level processes to request services of the operating system. System calls are the only entry points into the kernel system.
Even if the code itself does not contain system calls (what could meet the requirements), there are some implied system calls to actually initialize, run, stop and cleanup the process, even if they're not part of your binary. Which system calls are performed is platform dependent. Furthermore, at least the exit status will be set according to how you shut down your process: return statement vs exit() in main()
I guess, your teacher will be happy with that code, it doesn't use the standard library, which itself contains many system calls for different purposes (just like most other libraries). You won't be able to read/write from/to stdin/out and files/sockets, etc.. So you can't do IO, process creation & multithreading, synchronization, etc. since all that requires system calls (things like user threads and spinlocks may be a notable exception here).
One cannot write useful userland programs without system calls, except for programs taking some args, with a result returned as an int
(e.g. command line tools). You can also implement 'fully quiet' CPU heating stuff.
No, your example function doesn't make any system calls. You could just compile and disassemble it to be sure:
$ cc -O3 -c example.c
$ objdump -d example.o
example.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <func>:
0: 31 c0 xor %eax,%eax
2: c3 retq
Or without optimizations, if that's important:
$ cc -c example.c
$ objdump -d example.o
example.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <func>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: b8 00 00 00 00 mov $0x0,%eax
9: 5d pop %rbp
a: c3 retq
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With