7

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.

Matheus Santana
  • 581
  • 1
  • 6
  • 22
nemelianov
  • 911
  • 1
  • 7
  • 15

3 Answers3

6

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.

Community
  • 1
  • 1
Sam
  • 7,778
  • 1
  • 23
  • 49
2

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   
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

Yes the above has system calls. You can use ptrace() to stop every time there is a system call, and you'll see that there are system calls. How else would the program be loaded and unloaded from memory without the operating system?

KrisSodroski
  • 2,796
  • 3
  • 24
  • 39
  • 1
    The original question had an empty `main` function. Does it have a system call? Depends on what you mean by "have". The calling environment undoubtedly performs system calls, but the function itself doesn't. The revised question has an empty function called `func`. That function clearly does not explicitly make any system calls -- but for all we know the implementation could require a system call to allocate a function call activation record. If it's possible at all to have a function that doesn't "have" any system calls, then an empty non-main function certainly qualifies. – Keith Thompson Jun 14 '13 at 18:52