Coffee break activity: system call counting
Ok, we have two hello worlds, one written in C:
cat >hello.c<<EOF #include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; } EOF
Compiled:
gcc -ohello hello.c
The other hello world written in Python:
cat >hello.py<<EOF print("Hello, World!") EOF
Try to guess how many system calls they make.
C version:
strace -c ./hello Hello, World! % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- -nan 0.000000 0 1 read -nan 0.000000 0 1 write -nan 0.000000 0 2 open -nan 0.000000 0 2 close -nan 0.000000 0 3 fstat -nan 0.000000 0 9 mmap -nan 0.000000 0 4 mprotect -nan 0.000000 0 1 munmap -nan 0.000000 0 1 brk -nan 0.000000 0 3 3 access -nan 0.000000 0 1 execve -nan 0.000000 0 1 arch_prctl ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000000 29 3 total
Python version:
strace -c python hello.py Hello, World! % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- -nan 0.000000 0 91 read -nan 0.000000 0 1 write -nan 0.000000 0 174 116 open -nan 0.000000 0 58 close -nan 0.000000 0 100 55 stat -nan 0.000000 0 88 fstat -nan 0.000000 0 1 lstat -nan 0.000000 0 3 lseek -nan 0.000000 0 60 mmap -nan 0.000000 0 18 mprotect -nan 0.000000 0 29 munmap -nan 0.000000 0 13 brk -nan 0.000000 0 68 rt_sigaction -nan 0.000000 0 1 rt_sigprocmask -nan 0.000000 0 5 1 ioctl -nan 0.000000 0 10 10 access -nan 0.000000 0 1 execve -nan 0.000000 0 1 fcntl -nan 0.000000 0 4 getdents -nan 0.000000 0 1 getcwd -nan 0.000000 0 3 2 readlink -nan 0.000000 0 1 getrlimit -nan 0.000000 0 1 getuid -nan 0.000000 0 1 getgid -nan 0.000000 0 1 geteuid -nan 0.000000 0 1 getegid -nan 0.000000 0 1 arch_prctl -nan 0.000000 0 2 1 futex -nan 0.000000 0 1 set_tid_address -nan 0.000000 0 1 set_robust_list ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000000 740 185 total
Quite a difference! One could argue that writing a hello world program with Python is a massive overkill.
But what are those errors? Perhaps I'll examine them during the next coffee break..