View on GitHub

Computer Architecture and Operating Systems

Course taught at Faculty of Computer Science of Higher School of Economics

Lecture 3

System calls

Lecture

Slides (PDF, PPTX).

Outline:

System Call Types

There are several types of system calls. Each type solves a specific kind of task:

These tasks will be discussed in upcoming lectures and workshops.

Workshop

Outline

Theory

System calls are operations (functions) provided by the operating system kernel, which are available to user applications. They are designed and documented by operating system kernel developers. System calls are typically executed with the help of so-called wrapper functions, which can be conveniently used in user-mode applications (e.g. the glibc library in Linux). System calls allow executing kernel tasks upon user’s requests. Modern operating systems isolate kernel memory from user applications. Therefore, when a user application needs to make a request to the kernel (open a file, create a process, send data to network, etc.), a switch between kernel and user modes is required. This makes system calls much slower than regular function calls. System calls are made in architecture-dependent way, employing specific features of the instruction set architecture. A basic solution for system calls is employing the processor interrupt feature. However, a processor can also provide special instructions for this job. Arguments are passed via registers if they are available, extra arguments are passed via stack. The operating system kernel saves and restores execution state (e.g. registers) when switching between the use and the kernel modes.

System call

System calls in Linux API (C language)

Linux provides the following facilities to execute system calls:

API-functions that perform system calls

Functions that are called in user program to execute system calls are defined in special header files. To make them available, a corresponding header must be included into the program source code and then a function can be called.

#include <sys/stat.h>
#include <fcntl.h>

int open(const char *path, int oflag, ...);
#include <stdio.h>

FILE *fopen(const char *pathname, const char *mode);
#include <unistd.h>
#include <sys/syscall.h>   /* For SYS_xxx definitions */

long syscall(long number, ...);

In Ubuntu 20.04 LTS, more system call declarations can be found in the following file: /usr/src/linux-headers-5.4.0-53/include/linux/syscalls.h.

Examples

Example 1: Using the printf glibc function:

#include <stdio.h>
int main () {
    printf("Hello World\n");
    return 0;
}

Example 2: Using the write POSIX function:

#include <fcntl.h>
#include <unistd.h>
int main () {
    write (1, "Hello World\n", 12);
    return 0;
}

Example 3: Using the syscall function:

#include <unistd.h>
#include <sys/syscall.h>
int main () {
    syscall (1, 1, "Hello World\n", 12);
    return 0;
}

All the three examples, do the same: they print the “Hello World” message to the console. To compile and run them, the following commands need to be executed:

acos@acos-vm:~$ gcc test.c –o test
acos@acos-vm:~$ ./test

System calls in C:

  1. Read documentation on the read and write system calls. Note descriptors standard numbers for stdin, stdout, and stderr. Write a program that reads chars (note the &c notation) from stdin, increments them by 1, and writes them to stdout. Do not forget to include all the headers mentioned in the manual. To close stdin from the terminal, use the ^D key combination (it is not passed to program, but interpreted by operating system as end of output).

  2. Read documentation on the open system call. Take notice of flags, which are used to indicate how the file is opened. Flags are bits and can be combined with bitwise OR (|). The O_RDONLY flag is used to open a file for reading. The O_WRONLY|O_CREAT|O_TRUNC combination is used for open a file for writing. The mode parameter is required when creating the file. It specifies file access rights. For example, the S_IRUSR flag means that user has read permission, the S_IRGRP means that group has read permission, the S_IROTH flag means others have read permission. Write a program that reads 100 words from stdin and writes them to a file named outfile. Do not forget to close the file.

  3. Modify the previous program to accept command-line arguments (argc/argv). Pass via command-line arguments the number of words (use sscanf to get an integer from argv[1] and the name of output file (argv[2]).

Homework

TODO

References