Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible implementing a C program which does not perform any system calls?

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.

like image 409
nemelianov Avatar asked Jun 14 '13 16:06

nemelianov


People also ask

What is a system call what system calls do we implement in our C code?

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.

Do C library functions use system calls?

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.

How do you check if a function is a system call?

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 .

What is the need for system calls in operating system?

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.


2 Answers

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.

like image 166
Sam Avatar answered Sep 25 '22 14:09

Sam


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   
like image 38
Carl Norum Avatar answered Sep 21 '22 14:09

Carl Norum