慶應義塾大学
2007年度 春学期

システム・ソフトウェア
System Software / Operating Systems

2007年度春学期 火曜日2時限
科目コード: 60730
開講場所:SFC
授業形態:講義
担当: Rodney Van Meter
E-mail: rdv@sfc.keio.ac.jp

第2回 4月17日 システムコール
Lecture 2, April 17: System Calls

Review of last week: definition of an operating system, history of operating systems

The four key characteristics of an operating system are:

Recall that mainframes went from bare metal, to batch systems, to unprotected multiprogramming, to protected multi-user systems. PC operating systems followed the same pattern, and embedded OSes for devices such as PDAs are following the same pattern.

先週の授業の復習です。最初のプログラマブルコンピューターはただ、裸ん坊 の金属でプログラムをつくっていたでしょう。次はバッチシステムを開発され ました。そのあと、保護のないマルチプログラミング用のOSができて、それか ら保護のあるOSをできました。PC用と組み込み用のOSは同じ経路で行っていま す.

Finally, remember that we discussed the light cone of information and its impact on systems, where we must consider that information is always distributed (and therefore out of date), and that systems are definitely concurrent.

光円錐の影響で、今の知っていることが古いでしょう。そのふたつの概念:全 ての情報は分散である、とすべてのことは同時で行う可能性がある。

Types of Operating Systems
オペレーティングシステムの種類

Very briefly, let us note that there are a number of types of operating systems. During this class, we will generally focus on multi-purpose PC operating systems, but many of the principles in today's systems inherit from older systems, and many small operating systems will eventually include many of these features.

短いですが、少しOSの種類を説明します。この授業で、普通のマルチパーパス PC用のOSに集中しますが、ほかの種類があると知ってほしい。OSの概念はほと んど変わらない。新しいOSは、既存のOSの特徴を継承する。

Lampson, Hints

(We will discuss some of the slogans in the figure from the paper.)

Number of lines of C in the Linux kernel distribution, 2.6.19:

Printing that out would require a million pages! The original Unix kernel was 5,000 lines; the current Linux kernel is more than 17,000 files! How is Linux following the general dictum to KISS: keep it simple, stupid? Well, partly because the breakdown is actually like this (C files only):

それを印刷なら、100万ページなっちゃう!最初のUnixカーネルは5,000行 だったが、現在のLinuxカーネルは17,000ファイル!KISS: keep it simple, stupidという概念は守っていないんじゃいない?カーネルの中の サブシステムのインタフェースをちゃんと遵奉しているでしょう。

More than half of the total volume, and a third of the total number of files, is in drivers for various types of devices, most of which are not used on any given system.

The Linux kernel hackers are generally reasonable about maintaining comments, so consider these numbers to be high by almost a factor of two. Note also that this does not include any of the following:

The original Unix system also did not include:

In a system of this size, well-defined, high-performance interfaces with minimal hidden assumptions are critical. However, in keeping with Lampson's command to throw the first implementation away, almost all of Linux, including the virtual memory subsystem, has been rewritten frequently (as has Windows!). We will talk more about how to engineer such systems (including "The Cathedral and the Bazaar") later in the term.

System Calls

System calls provide the defined interface between the operating system and user application programs. The system calls generally provide several types of services:

In Unix and operating systems influenced by Unix, the last category is usually implemented using the file I/O interface, but there are often additional system calls that must be made to, for example, correctly open a network connection.

We discussed resource management, data movement and naming as critical functions of an OS. These system calls are an application's means of requesting that the OS perform one of these functions.

System calls are different from library functions. Many OS environments (whether the library is or is not a part of the OS itself is arguable) provide libraries of functions, often standardized, that application programmers may wish to use. What's the difference? Library functions start by running in user space, though they may also make system calls on behalf of the user process. Library functions perform actions like string formatting, calculating math functions, etc. System calls generally involve access to things that must be protected: disk drives, files on those disk drives, process control structures, etc.

Last week we discussed naming as a critical function of an OS. Humans use a readable form of the name of a system call, such as write(). However, the operating system itself does not actually use the human-readable names. In this case, the C compiler uses header files as a means to translate the human-readable name into a machine-readable one.

Here is part of the list in a Linux 2.6.19 kernel:

[rdv@2 ~]$ more /usr/include/asm/unistd.h 
#ifndef _ASM_I386_UNISTD_H_
#define _ASM_I386_UNISTD_H_

/*
 * This file contains the system call numbers.
 */

#define __NR_restart_syscall      0
#define __NR_exit                 1
#define __NR_fork                 2
#define __NR_read                 3
#define __NR_write                4
#define __NR_open                 5
#define __NR_close                6
...
#define __NR_move_pages         317
#define __NR_getcpu             318
#define __NR_epoll_pwait        319

#endif /* _ASM_I386_UNISTD_H_ */
...that's it. In Linux, there are 319 system calls that do everything.

The execution of a system call occurs in several phases:

The application side may or may not wrap the system call in a library routine. The compiler will take care of most of that for you.

Note that this same essential structure is followed for making calls to remote servers, as well as to local system services. Again, our principles of distributed and concurrent actions and information (the light cone) applies.

Most system calls are synchronous; your application program stops until the OS completes the call and returns (or decides that it cannot complete, in which case an error is returned).

Looking in a little more detail at the setuid system call:

_syscall1(int,setuid,uid_t,uid);
which will expand to:

_setuid:
  subl $4,%exp
  pushl %ebx
  movzwl 12(%esp),%eax
  movl %eax,4(%esp)
  movl $23,%eax
  movl 4(%esp),%ebx
  int $0x80
  movl %eax,%edx
  testl %edx,%edx
  jge L2
  negl %edx
  movl %edx,_errno
  movl $-1,%eax
  popl %ebx
  addl $4,%esp
  ret
L2:
  movl %edx,%eax
  popl %ebx
  addl $4,%esp
  ret
(This code is a little old, but illustrates the necessary points.)

Term Projects

Everyone seems concerned about their term projects. I should have been prepared to explain them in more detail last week. My apologies.

The goal of your term project is to learn how one particular part of an operating system works, without having to hack the kernel and threaten the stability of your system. You should be able to conduct your term project on your own PC safely.

Schedule and Grading

The schedule is as follows:

That gives you seven to eight weeks to actually implement your project. I would expect that your project will take 30-40 hours total, including writing and debugging code, taking data, analyzing the data, and writing up a report of the results. This is actually not very much time for a project, so they must be sized appropriately.

Your grade on your project will be 40% of your total grade, split 10% for the mid-term progress review June 12, and 30% for the final evaluation. The things I will look for are those detailed in the Levin and Redell paper. Because many of your projects will involve performance measurements, I also expect data with error bars and carefully designed experiments. One great book on the topic is Jain, The Art of Computer Systems Performance Analysis, but there are probably also good books available in Japanese.

Implementation

Some people have asked what language they must write their program(s) in. I don't care what language you use; I care what you learn about the operating system. In order to learn about the OS, a low-overhead, predictable, compiled language is probably preferable. C would be the obvious choice. Interpreted scripting languages are probably bad choices.

Likewise, there is no requirement to perform your project on a particular operating system. Class lectures will focus on principles highlighted by Unix and Linux examples, as above. The importance of Unix in the history of operating systems cannot be overstated, and Linux and MacOS are (arguably) its most vibrant current implementations; students must have some familiarity with the basic ideas of Unix. However, if your OS of choice is Windows, you will learn a great deal by comparing concepts from lectures and the book with what you see on your Windows machine. One obvious advantage of Linux is the easy availability of source code, turning "black box" experiments into "white box" ones.

The first step in either research or development is to identify a problem. Most of these projects will help you carefully characterize a system problem that you might want to attack more thoroughly in research later.

Project Suggestions

The ideal project for this class is probably a performance measurement of the system. Examples include:

Some of these projects will be more difficult on Windows systems, due to the lack of source code. Some of the measurements will also be difficult if you cannot flush the cache, either by unmounting the file system or rebooting the machine.

I will ask you not just what happened, but why it happened. In most cases, you should be able to point to some kernel source code, or a conference paper, design document, book, or web site that supports your understanding of why the system behaves the way it does. For most of these projects, I will also ask you to predict what will happen as technology continues to improve: modest improvements in clock speed and disk speed, significant increases in number of processors and disk capacity.

Last week I mentioned the OS class at Wisconsin. Here are links to some prior years' projects:

Readings

Readings to match this week's lecture:

Homework

This week's homework. You probably want to do the second problem first.

Next Lecture

Next lecture:

第3回 4月24日 プロセスとスレッド
Lecture 3, April 24: Processes and Threads

Readings for next week:

その他 Additional Information