UMBC CMSC 313 -- System Calls Previous | Next


System Calls

No meaningful program can be run with using the services of the operating system. First, no one should have to write the code that will provide access to files, memory allocation, and all of the other system resources again and again. NEVER WRITE THE SAME CODE TWICE!!!! Plus, Linux is a multiprocessing system and all programs must play well together. That means getting access to system resources must be done in a controlled manner so that the system integrity is maintain.

Because of that, access to the system resources is what we call privileged access and can only be done by the operating system. The user program requests that the operating system provide that access via a well-defined service. Then, assuming the service was properly requested, that service will be provided. What services are there? Currently, we are using a version of the Linux kernel that is about 2.4.20-8 (different sites are using different versions) which as 258 different services more /usr/include/asm/unistd.h This will give you the name and number of all of the services. The write system call that we used is listed in that file as:

__NR_write All system calls get __NR_ added to their name.

The good news is that all systems have the same calls if that call is defined for that system. This gives the programmer a standard API when programming.

Finding Out about System Calls

The system calls are usually given meaningful names, like read, write, kill, fork, rename or lseek. The man page system is set up to help you find out how to use them.

If you look at the write man page, which is written for a C program, you find how to use it:

       ssize_t write(int fd, const void *buf, size_t count);

OK, what is a ssize_t and size_t? That is defined by your system in the appropriate header file.

Variable type Source of definition Definition Container
size_t include/asm/posix_types.h
include/linux/types.h
typedef unsigned int __kernel_size_t;
typedef __kernel_size_t size_t;
unsigned 4 byte integer
ssize_t include/asm/posix_types.h
include/linux/types.h
typedef int __kernel_ssize_t;
typedef __kernel_ssize_t ssize_t;
signed 4 byte integer

API For System Calls

EAX contains the System call number and the parameters are stored from left to right in the registers in the following order: EBX, ECX, EDX, EDI, ESI.

Example

In the last lecture we used the write system call:

;; Use the write system call to output our hex character
	mov	edx, 1                  ; How much are we going to output
	mov	ecx, hex                ; Where is what we are going to output
	mov	ebx, 1                  ; Which file are we going to use
                                        ; 1 is stdout
	mov	eax, 4                  ; Use system call number 4
	int	80h                     ; Just do it!

In the file /usr/include/asm/unistd.h> we find out the system call number is 4:

#define __NR_write 4

EAX is set to four.
EBX register is set to the file descriptor.
ECX gets set to the address of the buffer (which in our case was a one byte buffer).
EDX gets the number of bytes to write out.

Remember, the man page listed the call as:

       ssize_t write(int fd, const void *buf, size_t count);

What about the return value? It is returned in the EAX register every time.


Previous | Next

©2004, Gary L. Burt