I . Shell

The OS is the code that carries out the system calls . Editors  , compilers , assemblers , linker , and command interpreters definitely are not part of the OS , even through they are important and useful . At the risk of confusing things somewhat , in this section we will look briefly at the MINIX 3 command interpreter , called the shell . Although it is not part of the OS , it makes heavy use of many OS features and thus serves as a good example of how the system calls can be used . It is also the primary interface between a user sitting at his terminal and the operating system, unless the user is using a graphical user interface. Many shells exist, including csh, ksh, zsh, and bash. All of them support the functionality described below, which derives from the original shell (sh).

When any user logs in, a shell is started up. The shell has the terminal as standard input and standard output. It starts out by typing the prompt, a character such as a dollar sign, which tells the user that the shell is waiting to accept a command. If the user now types


for example, the shell creates a child process and runs the date program as the child. While the child process is running, the shell waits for it to terminate. When the child finishes, the shell types the prompt again and tries to read the next input line.
The user can specify that standard output be redirected to a file, for example,

date >file

Similarly, standard input can be redirected, as in

sort <file1 >file2

which invokes the sort program with input taken from file1 and output sent to file2.
The output of one program can be used as the input for another program by connecting them with a pipe. Thus

cat file1 file2 file3 | sort >/dev/lp

invokes the cat program to concatenate three files and send the output to sort to arrange all the lines in alphabetical order. The output of sort is redirected to the file /dev/lp, typically the printer.
If a user puts an ampersand after a command, the shell does not wait for it to complete. Instead it just gives a prompt immediately. Consequently,

cat file1 file2 file3 | sort >/dev/lp &

starts up the sort as a background job, allowing the user to continue working normally while the sort is going on. The shell has a number of other interesting features, which we do not have space to discuss here. Most books for UNIX beginners are useful for MINIX 3 users who want to learn more about using the system. Examples are Ray and Ray (2003) and Herborth (2005).

II . System Calls

Armed with our general knowledge of how MINIX 3 deals with processes and files , we can now begin to look at the interface between the OS and its application programs , that is , the set of system calls . Although this discussion specifically refers to POSIX (International Standard 9945-1), hence also to MINI 3, UNIX, and Linux, most other modern operating systems have system calls that perform the same functions, even if the details differ. Since the actual mechanics of issuing a system call are highly machine dependent, and often must be expressed in assembly code, a procedure library is provided to make it possible to make system calls from C programs.

It is useful to keep the following in mind: any single-CPU computer can execute only one instruction at a time. If a process is running a user program in user mode and needs a system service, such as reading data from a file, it has to execute a trap or system call instruction to transfer control to the operating system. The operating system then figures out what the calling process wants by inspecting the parameters. Then it carries out the system call and returns control to the instruction following the system call. In a sense, making a system call is like making a special kind of procedure call, only system calls enter the kernel or other privileged operating system components and procedure calls do not.

To make the system call mechanism clearer, let us take a quick look at read . It has three parameters: the first one specifying the file, the second one specifying the buffer, and the third one specifying the number of bytes to read. A call to read from a C program might look like this:

count = read(fd, buffer, nbytes);

The system call (and the library procedure) return the number of bytes actually read in count . This value is normally the same as nbytes , but may be smaller, if, for example, end-of-file is encountered while reading.
If the system call cannot be carried out, either due to an invalid parameter or a disk error, count is set to 1, and the error number is put in a global variable, errno . Programs should always check the results of a system call to see if an error occurred.

Process management

pid = fork ()

Create a child process identical to the parent

pid = waitpid (pid, &statloc, opts)

Wait for a child to terminate

s = wait (&status)

Old version of waitpid

s = execve (name, argv, envp)

Replace a process core image

exit (status)

Terminate process execution and return status

size = brk (addr)

Set the size of the data segment

pid = getpid ()

Return the caller’s process id

pid = getpgrp ()

Return the id of the caller’s process group

pid = setsid ()

Create a new session and return its proc. group id

l = ptrace (req, pid, addr, data)

Used for debugging


s = sigaction (sig, &act, &oldact)

Define action to take on signals

s = sigreturn (&context)

Return from a signal

s = sigprocmask (how, &set, &old)

Examine or change the signal mask

s = sigpending (set)

Get the set of blocked signals

s = sigsuspend (sigmask)

Replace the signal mask and suspend the process

s = kill (pid, sig)

Send a signal to a process

residual = alarm (seconds)

Set the alarm clock

s = pause ()

Suspend the caller until the next signal
File Management

fd = creat (name, mode)

Obsolete way to create a new file

fd = mknod (name, mode, addr)

Create a regular, special, or directory i-node

fd = open (file, how, ...)

Open a file for reading, writing or both

s = close (fd)

Close an open file

n = read (fd, buffer, nbytes)

Read data from a file into a buffer

n = write (fd, buffer, nbytes)

Write data from a buffer into a file

pos = lseek (fd, offset, whence)

Move the file pointer

s = stat (name, &buf)

Get a file’s status information

s = fstat (fd, &buf)

Get a file’s status information

fd = dup (fd)

Allocate a new file descriptor for an open file

s = pipe (&fd[0])

Create a pipe

s = ioctl (fd, request, argp)

Perform special operations on a file

s = access (name, amode)

Check a file’s accessibility

s = rename (old, new)

Give a file a new name

s = fcntl (fd, cmd, ...)

File locking and other operations
Dir. & File System Mgt .

s = mkdir (name, mode)

Create a new directory

s = rmdir (name)

Remove an empty directory

s = link (name1, name2)

Create a new entry, name2, pointing to name1

s = unlink (name)

Remove a directory entry

s = mount (special, name, flag)

Mount a file system

s = umount (special)

Unmount a file system

s = sync ()

Flush all cached blocks to the disk

s = chdir (dirname)

Change the working directory

s = chroot (dirname)

Change the root directory

s = chmod (name, mode)

Change a file’s protection bits

uid = getuid ()

Get the caller’s uid

gid = getgid ()

Get the caller’s gid

s = setuid (uid)

Set the caller’s uid

s = setgid (gid)

Set the caller’s gid

s = chown (name, owner, group)

Change a file’s owner and group

oldmask = umask (complmode)

Change the mode mask
Time Management

seconds = time (&seconds)

Get the elapsed time since Jan. 1, 1970

s = stime (tp)

Set the elapsed time since Jan. 1, 1970

s = utime (file, timep)

Set a file’s “last access” time

s = times (buffer)

Get the user and system times used so far

This is a good place to point out that the mapping of POSIX procedure calls onto system calls is not necessarily one-to-one. The POSIX standard specifies a number of procedures that a conformant system must supply, but it does not specify whether they are system calls, library calls, or something else. In some cases, the POSIX procedures are supported as library routines in MINIX 3. In others, several required procedures are only minor variations of one another, and one system call handles all of them.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s