简体   繁体   中英

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.

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 (eg 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   

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM