What is a system call? Name the common process-control and file-management calls.
EasyConceptOS Basics & System Calls
Answer
A system call is the way a user program asks the kernel to do something it is not allowed to do itself, such as create a process or read a file. It looks like a function call, but it switches the CPU into kernel mode, the kernel does the work, and control returns with a result. The names below are the Unix/Linux (POSIX) ones; Windows has equivalents such as CreateProcess and ReadFile.
- Process control:
fork()creates a child process,exec()replaces the current program with a new one,wait()makes a parent wait for a child to finish, andexit()ends a process with a status code. - File management:
open()returns a file descriptor (a small integer naming the open file),read()andwrite()transfer bytes, andclose()releases the descriptor. - Other groups cover devices (
ioctl()), information (getpid()) and communication (pipe(),socket()).
Example: copying a file in C:
int in = open("a.txt", O_RDONLY);
int out = open("b.txt", O_WRONLY | O_CREAT, 0644);
char buf[512]; ssize_t n;
while ((n = read(in, buf, sizeof buf)) > 0)
write(out, buf, n);
close(in); close(out);
On Linux, strace cp a.txt b.txt shows the same calls made by cp.