3.3 System Call



System calls provide the interface between a process and the operating system. System calls are in assembly language. Several high level programming languages provide instructions to the system calls. When the process makes a system call, it needs to pass parameters to the operating system. Three different methods are used for passing parameters which are:
  1. Registers: When the number of parameters is small.
  2. The parameters are stored in a block, or table, in memory, and the address of the block is passed as a parameter in a register as shown in Figure 3.2.
Passing of parameters as a table (Silberschatz et.al., (2008)


3.3 System Call


  1. Stack: The user program pushed the parameters onto the stack and popped off the stack by the operating system.
System calls can be classified into five major categories: process control, file manipulation, device manipulation, information maintenance, and communications which will be explained in the following subsections (Silberschatz et.al., (2008)).

      i. Process control

Some system calls that control the processes can be:
  1. Load and execute programs.
  2. Terminate a process if it causes an error trap.
  3. Get and set process attributes.
  4. Wait for a certain amount of time or an event. Signal the process that has been waiting for an event.
Figure 3.3 shows MS-DOS operating system as an example for single-tasking system. Programs are loaded in the memory. The instruction pointer is set to the first instruction in the program. If error happens, the error code is saved in the system memory.

3.3 System Call



MS-DOS execution. (a) At system startup. (b) Running a program. (Silberschatz et.al., (2008)


3.3 System Call



Figure 3.4 shows FreeBSD (derived from Berkeley UNIX: Berkeley Software Distributions) as an example of a multitasking system. More than one program can be loaded in the memory to be executed.
Free BSD running multiple programs (Silberschatz et.al., (2008)


3.3 System Call



      ii. File management

Some system calls that deal with files can be:
  1. Create and delete files. The name of the file and its attributes are needed.
  2. Open and close the files.
  3. Get and set process attributes.
  4. Read, write, or reposition.
      iii. Device management

Some system calls that deal with devices can be:
  1. Request access to a device and release the device.
  2. Get the device attributes and set the device attributes.
  3. Read, write, or reposition.

3.3 System Call



      iv. Information maintenance

Some system calls for the purpose of transferring information between the user program and the operating system can be:
  1. Get time or date, set time or date.
  2. Get system data and set system data.
  3. Get and set process, file, or device attributes.
      v. Communication

There are two communication models which are:
  1. The message-passing model: The information is exchanged through an interprocess-communication provided by the operating system. Each process has a name which is translated to an identifier. Processes that receive connections are special-purpose daemons. The source of the communication is known as the client, and the receiving daemon, is known as a server. The messages exchange is done using read message and write message system calls. This type of communication is shown in Figure 3.5(a).
  2. The shared-memory model: Shared memory allows more than process to access the same memory. Information exchange can be done by reading and writing data in the shared areas. This type of communication is shown in Figure 3.5(b)..

3.3 System Call



Device-status table. (Silberschatz et.al., (2008)


3.3 System Call



      ii. DMA transfers

DMA technique is more efficient when large volumes of data are to be moved. When CPU needs to read or write a block of data, it sends the DMA module the following information (William Stallings (2012)):
  1. Whether a read or write is requested.
  2. The address of the I/O device involved.
  3. The starting location in memory to read data from or write data to.
  4. The number of words to be read or written.
The CPU delegates the I/O operation to DMA module. DMA transfers the data blocks to the main memory directly. When data transfer is complete the DMA module sends an interrupt signal to CPU.

b. Storage structure

To execute the computer programs, they have to be in the main memory (also called random-access memory (RAM)). Main memory is too small to store all needed programs and data permanently. Besides, main memory is a volatile storage device that loses its contents when it has no power. That is the reason the computer system has secondary storage to store the data permanently such as a magnetic tape and a hard disk.

3.3 System Call



      i. Main memory

CPU can only access main memory and the registers built into CPU. Thus, data has to be transferred to main memory for CPU to use it. Memory mapped I/O technique is used to provide more convenient access to I/O devices. Ranges of memory addresses are set aside and mapped to the device registers. When data transfer is needed to and from the device registers, data reads and writes can be done to these memory addresses.
The built-in registers in CPU are accessible using one clock cycle. Instructions and data have to be transferred from main memory to the CPU registers. The main memory access is much slower than the CPU registers access. This is the reason of using a cache memory between the CPU and main memory.

      ii. Magnetic disks

Magnetic disks are secondary storage used to store data permanently. It consists of circular platters as shown in Figure 2.5. The platter surfaces are covered with a magnetic material. Information is stored by recording it magnetically on the platters. A read-write head moves above each platter surface. The heads are attached to a disk arm. Each surface is divided into tracks. Each track is divided into sectors. The storage capacity of common disk drives is measured in gigabytes. The time of the data transfer between the drive and the computer consists of the following:
  1. The seek time which is the time required to move the disk arm to the desired cylinder.
  2. The rotational latency time which is the time required for the desired sector to rotate to the disk head.

3.3 System Call



Moving-head disk mechanism. (Silberschatz et.al., (2008)


3.3 System Call



c. Storage hierarchy

Figure 2.6 shows the hierarchy for different storage systems according to the speed and cost. As one goes down the hierarchy, the following features occur (William Stallings (2012)):
  1. Decreasing cost per bit
  2. Increasing capacity
  3. Increasing access time
  4. Decreasing frequency of access to the memory by the processor
These storage systems are either volatile or nonvolatile. Volatile storage loses its contents when the power to the device is removed. However, nonvolatile storage keeps its content when no power is connected to the device. In figure 2.6, the storage devices in inboard memory category are volatile while storage devices in outboard storage and off-line storage are nonvolatile.

3.3 System Call



The memory hierarchy (William Stallings (2012)


3.3 System Call



      i. Cache memory

To execute an instruction, CPU accesses the main memory to fetch the instruction and its operands. CPU speed is much faster than the main memory access speed. The solution is to provide a small, fast memory between CPU and the main memory namely the cache. The main memory is relatively large and slow while cache memory is smaller and faster. The cache contains a copy of a part of main memory. When the processor tries to access a word of memory, CPU checks if this word is in the cache. If the word is found in the cache, CPU uses the word in the cache. If the word is not found in the cache, a block of main memory is read into the cache and then the word is delivered to CPU as shown in Figure 2.7. The cache memory concept is working because of a phenomenon called locality of reference which means when a block of data is fetched into the cache to satisfy a single memory reference, it is likely that many of the near-future memory references will be to other bytes in the block (William Stallings (2012)).
Cache and main memory (William Stallings (2012)


3.3 System Call



Figure 2.8 shows the use of multiple levels of cache. The L2 cache is slower and larger than the L1 cache, and the L3 cache is slower and larger than the L2 cache.
Three-level cache organization (William Stallings (2012)


3.3 System Call



d. Hardware protection

To improve the resource utilization, the operating system shares the resources among different programs simultaneously. This resources sharing can cause problems as many processes could be affected by a bug in one program. In a multiprogramming system one erroneous program might modify another program data. The operating system has to be deigned to ensure that an erroneous program cannot cause other programs to execute incorrectly.

      i. Dual-mode operation

When a resource sharing is used, the operating system protects the programs using two separate modes of operation:
  1. User mode: when a task is executed on behalf of the user.
  2. Monitor mode (also called supervisor mode, system mode, or privileged mode): when a task is executed on behalf of the operating system.
To indicate the current mode, a bit, called the mode bit, is used. When the system starts, the operating system gains control of the system and it is in monitor mode (the mode bit is 0). When the use program starts, the system is in user mode (the mode bit is 1). Whenever a trap or interrupt occurs, the hardware switches from user mode to monitor mode (Silberschatz et.al., (2008)).

3.3 System Call



The dual mode of operation provides us with protection by designating some of the machine instructions that may cause harm as privileged instructions. The hardware allows privileged instructions to be executed only in monitor mode.
The user program can ask the operating system to perform reserved operating system tasks that are reserved to the operating system by executing a privileged instruction. This request is called a system call or a monitor call or an operating-system function call.

      ii. I/O protection

All I/O instructions are defined to be privileged instructions to prevent illegal operations. When the user program needs to perform I/O operation, it issues a system call to the operating system instead as shown in Figure 2.9. The operating system returns to the user after executing the I/O operation.

3.3 System Call



Use of a system call to perform I/O. (Silberschatz et.al., (2008)


3.3 System Call



      iii. Memory protection

The operating system needs to provide the following memory protections:
  1. Protect the interrupt vector from modification by a user program.
  2. Protect the interrupt-service routines in the operating system from modification.
  3. Protect the operating system from access by user programs.
  4. Protect user programs from one another.
These protection types can be provided using two registers called a base and a limit, as shown in Figure 2.10. The base register contains the smallest legal physical memory address while the limit register contains the size of the range. Figure 2.10 shows an example when the base register holds 300040 and the limit register has 120900. Then the program can only access all addresses from 300040 through 420940 inclusive.

3.3 System Call



A base and a limit register define a logical address space. (Silberschatz et.al., (2008)


3.3 System Call



Figure 2.11 shows the flowchart that CPU performs using the base and the limit registers for the memory protection. If the user program tries to access a memory location illegally, a trap to operating system monitor is issued.

Hardware address protection with base and limit registers. (Silberschatz et.al., (2008)


3.3 System Call



      iv. CPU protection

The user program should not be allowed to run for long time and never return control to the operating system. A timer can be used to issue an interrupt if the user program monopolies CPU after a specified period. A variable timer can be used using a fixed-rate clock and a counter. When the timer interrupts, the operating system gains control from the user program. The operating system can terminate the user program or gives it more time.
The time sharing technique can be used instead. Each user program is assigned a time slice. At the end of the time slice, the operating system performs context switching to another user program.