So I get system calls in the context of a C program, but my textbook doesn't really address making system calls in Terminal (on Mac OSX). Can you do these on the command line like with "commands", or is it a totally different concept? Sorry, I'm sure this is very elementary, just can't find an answer.
Can you perform system calls from OSX Terminal
command lineosxsystem-calls
Related Solutions
There are in fact three gradations in system calls.
- Some system calls return immediately. “Immediately” means that the only thing they need is a little processor time. There's no hard limit to how long they can take (except in real-time systems), but these calls return as soon as they've been scheduled for long enough.
These calls are usually called non-blocking. Examples of non-blocking calls are calls that just read a bit of system state, or make a simple change to system state, such asgetpid
,gettimeofday
,getuid
orsetuid
. Some system calls can be blocking or non-blocking depending on the circumstances; for exampleread
never blocks if the file is a pipe or other type that supports non-blocking reads and theO_NONBLOCK
flag is set. - A few system calls can take a while to complete, but not forever. A typical example is
sleep
. - Some system calls will not return until some external event happens. These calls are said to be blocking. For example,
read
called on a blocking file descriptor is blocking, and so iswait
.
The distinction between “fast” and “slow” system calls is close to non-blocking vs. blocking, but this time from the point of view of the kernel implementer. A fast syscall is one that is known to be able to complete without blocking or waiting. When the kernel encounters a fast syscall, it knows it can execute the syscall immediately and keep the same process scheduled. (In some operating systems with non-preemptive multitasking, fast syscalls may be non-preemptive; this is not the case in normal unix systems.) On the other hand, a slow syscall potentially requires waiting for another task to complete, so the kernel must prepare to pause the calling process and run another task.
Some cases are a bit of a gray area. For example a disk read (read
from a regular file) is normally considered non-blocking, because it's not waiting for another process; it's only waiting for the disk, which normally takes only a little time to answer, but won't take forever (so that's case 2 above). But from the kernel's perspective, the process has to wait for the disk driver to complete, so it's definitely a slow syscall.
User-space kernel-space communication via system calls is done in terms of memory locations and machine registers. That's way below the abstraction level of shells, which operate mainly with text strings.
That said, in bash, you can use the https://github.com/taviso/ctypes.sh plugin to get through the text-string abstraction down to C-level granularity:
$ . ctypes.sh
$ dlcall -r long geteuid
long:1001
For this particular operation though, it would be much simpler, more idiomatic, and more efficient to simply use bash's magic $UID
variable.
$ echo "$EUID" #effectively a cached geteuid call
1001
Best Answer
No, you can't perform system calls directly because the shell running under Terminal doesn't give you low level access to memory that you would need to call system calls and deal with the results. The shell's job is to make it easy for you to run whole programs. Some of these programs give you a more convenient interface to system calls and other operating system resources. For example, the
mv
command gives you a pleasant interface to therename
system call. Theln
command gives you an interface to thelink
andsymlink
system calls. The built-in shell commandcd
gives you convenient access tochdir
. But for the most part system calls provide services too basic to be useful for the shell to provide direct access to them.