Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Lecture2 - OS Structure

Uploaded by

mupo528
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture2 - OS Structure

Uploaded by

mupo528
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

COMP304

Operating Systems (OS)

Operating System Structure


Hakan Ayral
Lecture 2

Operating Systems-COMP 304 (original slides by Didem Unat) 1


Outline
• Operating System Services
• Command Interpreter
• Dual Mode Operation
• System Calls and Types
• I/O, Memory and CPU Protection
• Operating System Design Structure

Operating Systems-COMP 304 2


Computer Startup
• Bootstrap program is loaded at power-up or reboot
– Typically stored in ROM, generally known as firmware
– Initializes all aspects of a system
– Loads operating system kernel into main memory and starts execution
• The first system process is ‘init’ in Linux
– When the system is fully booted, it waits for some event to occur
• Kernel
– The ``one” program running at all times (the core of OS)
• Everything else is an application program
• Process
– An executing program (active program)

Operating Systems-COMP 304 3


Operating System Services
User and other system programs

User Interfaces (GUI, Batch, Command Line)

System Calls
Program I/O File Resource Error
Execution operations Systems Allocation Detection

Protection
Accounting Communication
and Security
Operating System

Hardware

Operating Systems-COMP 304 4


Operating System Services (1/3)
One set of operating system services provides functions that are helpful to
the user.
• User interface - Almost all operating systems have a user interface (UI).
– Varies between Command-Line (CLI), Graphics User Interface (GUI),
or Batch
• Program execution - The system must be able to load a program into
memory and to run that program, end execution, either normally or
abnormally (indicating error)
• I/O operations - A running program may require I/O, which may involve a
file or an I/O device
• File-system manipulation - Programs need to read and write files and
directories, create and delete them, search them, list file information,
manage permissions.

Operating Systems-COMP 304 5


Operating System Services (2/3)
• Communications – Processes may exchange information, on
the same computer or between computers over a network
– Communications may be via shared memory or through message
passing (packets moved by the OS)

• Error detection – OS needs to be constantly aware of possible


errors
– May occur in the CPU and memory hardware, in I/O devices, in user
program
– For each type of error, OS should take the appropriate action to ensure
correct and consistent computing

Operating Systems-COMP 304 6


Operating System Services (3/3)
Another set of operating-system functions exists not for helping the user but
rather for ensuring the efficient operation of the system itself.

• Resource allocation - When multiple users or multiple jobs running


concurrently, resources must be allocated to each of them
– Many types of resources - Some (such as CPU cycles, main memory,
and file storage) may have special allocation code, others (such as I/O
devices) may have general request and release code

• Logging - To keep track of which users use how much and what kind of
computer resources, improve response time to users

• Protection and security - The owners of information stored in a multiuser


or networked computer system may want to control use of that
information, concurrent processes should not interfere with each other

Operating Systems-COMP 304 7


Command Interpreters

Command Interpreters (Shells)

• In OS everything is a file
– Command interpreter does not understand the command (e.g. “rm”)
– It merely uses the command to identify a file to be loaded into memory and
executed.
• For example, shell would search for a file called ‘rm’, load the file into memory
and execute it with the parameter file.txt
– Thus, programmer can add new commands to the system easily by creating
new files (programs)
Operating Systems-COMP 304 8
Src code of Linux Commands
• All these basic commands are part of the coreutils
package.
– http://www.gnu.org/software/coreutils/
– commands such as rm, ls, chmod, cp …
• https://github.com/coreutils/coreutils/tree/master/src

• For example, “ls” command:


– https://github.com/coreutils/coreutils/blob/master/src/ls.c
– Only 5000+ code lines for a command 'easy enough’

Operating Systems-COMP 304 9


OS Protection: Dual-Mode Operation

• Dual-mode operation allows OS to protect itself and other


system components
– User mode and kernel mode
– Mode bit provided by hardware
• Provides ability to distinguish when system is running user code or
kernel code
– Some instructions designated as privileged, only executable
in kernel mode
• For example, I/O related instructions are privileged

• Ensures that an incorrect program cannot cause other


programs to execute incorrectly.

Operating Systems-COMP 304 10


Negative Rings

Operating Systems-COMP 304 11


Transition from User to Kernel Mode
• System Call
– Results in a transition from user to kernel mode
– Return from call resets it to user mode
• Software error or a user request creates an exception or trap

user process
user mode
user process executing calls system call return from system call (mode bit = 1)

trap return
kernel mode bit = 0 mode bit = 1 kernel mode
(mode bit = 0)
execute system call

Operating Systems-COMP 304 12


Example: Linux System Calls

• A system call number is a unique


integer in Unix-based OSs
– There are about >300 system calls
in Linux
– A list of all registered system calls is
maintained in the system call table
• Those numbers cannot be changed
or recycled
– See the list of system calls with a
command
• (Location might differ depending on the
Unix distribution)

cat /usr/include/asm/unistd.h | less

Operating Systems-COMP 304 13


Example: Linux System Calls

– One can also call a service by directly using


its number
• syscall( system_call_number, arguments)

– Actual Implementation of a system call


is in different files in the kernel src
• https://elixir.bootlin.com/linux/latest/source

– An example, open system call


• https://elixir.bootlin.com/linux/latest/source/fs/fsop
en.c

Operating Systems-COMP 304 14


System Calls
• Programming interface to the services provided by the OS
– Well-defined and safe implementation for service requests
– Typically written in a high-level language (C or C++)
• A typical OS executes 1000s of system calls per second

• Mostly accessed by programs via a high-level Application Program


Interface (API) rather than direct system call use
– Wrapper functions for the system calls

• Three most common APIs are


– Windows API for Windows,
– POSIX API for POSIX-based systems (including virtually all versions of UNIX,
Linux, and Mac OS X),
– Java API for the Java virtual machine (JVM)

Operating Systems-COMP 304 15


Standard C Library Example
#include <stdio.h>
int main ( ) • C program invoking printf() library call,
( intercepts function call in the API and
. invokes the necessary system calls within
printf (“Greetings”); the operating system
.
– Calls write() system call
return 0;
)
• Caller needs to know nothing about
user
– how the system call is implemented
mode
standard C library – what it does during execution
kernel
mode
write ( )
Why use APIs instead of using system calls
write ( ) directly?
system call

Operating Systems-COMP 304 16


Examples for Types of System Calls

• Types of system calls classified under 6 categories. Table gives an example for
Windows and Unix syscalls.
Operating Systems-COMP 304 17
Privileged Instructions
• The dual mode of operation provides us with the means for
protecting the operating system from errant users—and
errant users from one another.
• We accomplish this 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
kernel mode.
– If an attempt is made to execute a privileged instruction in user mode,
the hardware does not execute the instruction but rather treats it as
illegal and traps it to the operating system

Operating Systems-COMP 304 18


I/O Protection

• All I/O instructions are privileged instructions.


– Must ensure that a user program could never gain control
of the computer in kernel mode (i.e., a user program that,
as part of its execution, stores a new address in the
interrupt vector).

1. “normal” instructions, e.g., add, sub, etc.


2. “privileged” instructions, e.g., initiate I/O switch state vectors
or contexts load/save from protected memory etc.

Operating Systems-COMP 304 19


Memory Protection
• Must provide memory protection at least for the interrupt
vector and the interrupt service routines.

• In order to have memory protection, add two registers that


determine the range of legal addresses a process may access:
– Base register – holds the smallest legal physical memory address.
– Limit register – contains the size of the range

• Memory outside the defined range is protected.

Operating Systems-COMP 304 20


Use of a Base and Limit Registers
0
Kernel
256000
process 1

300040 300040
process 2 base register

420940 120900
process 3 limit register

880000
process 4

1024000

Operating Systems-COMP 304 21


Hardware Protection
• When executing in kernel mode, the operating system has unrestricted
access to both kernel and user’s memory.
• The load instructions for the base and limit registers are privileged
instructions.

base base + limit

address yes yes


CPU ≥ <

no no memory

A fault raised by hardware, notifying the operating system


about an addressing error

Operating Systems-COMP 304 22


CPU Protection
• Timer – interrupts computer after specified period to
ensure operating system maintains control.
– Timer is decremented every clock tick.
– When timer reaches the value 0, an interrupt occurs.

• Timer commonly used to implement time sharing


systems.
• Clearly, instructions that modify the content of the
timer are privileged.

Operating Systems-COMP 304 23


Operating System Structure
• General-purpose OS is very large program
– Typically written in assembly, C/C++, some scripts in Perl or Python
• Various ways to structure it
– Monolithic Kernel: All the OS services are implemented in the kernel.
Fast OS but hard to extend
• Ex: MS-DOS, Unix
– Microkernel: Moves all the nonessential components from the kernel
to user level. Smaller kernel, uses messages with system and user-level
programs
• Ex: Mach
– Modular Approach: Loadable kernel modules, load additional services
if needed at boot or run time
• Ex: Solaris
• Most current OS combines all three approaches nowadays
– Ex: Windows, Mac OS X, Linux
Operating Systems-COMP 304 24
Monolithic Kernel
All the OS services are implemented in the kernel. Fast OS but hard
to extend

Operating Systems-COMP 304 25


Microkernel
• Moves all the nonessential components from the kernel to user level.
Smaller kernel, uses messages with system and user-level programs

Application File Device user


Program System Driver mode

messages messages

Interprocess memory CPU kernel


Communication managment scheduling mode

microkernel

hardware

Operating Systems-COMP 304 26


Modular Kernel
• Modular Approach: Loadable kernel modules, load additional
services if needed at boot or run time

Operating Systems-COMP 304 27


Android

Operating Systems-COMP 304 28


Question
• Which of the following instructions should be
privileged?
a. Set value of timer.
b. Read the clock.
c. Clear memory.
d. Issue a trap instruction.
e. Turn off interrupts.
f. Modify entries in device-status table.
g. Access I/O device.
• a, c, e, f, g

Operating Systems-COMP 304 29


Question
• A ____ can be used to prevent a user program from
never returning control to the operating system.
A) Dual mode
B) Program counter
C) Kernel module
D) Timer

Operating Systems-COMP 304 30


Reading

• From text book


– Read Chapter 2: Section 2.1-2.4, 2.10
• Linux System Call References
– https://man7.org/linux/man-pages/man2/syscalls.2.html

• Kernel source code


– https://elixir.bootlin.com/linux/latest/source

• Acknowledgments
– Original slides are by Didem Unat which were adapted from
• Öznur Özkasap (Koç University)
• Operating System and Concepts (9th edition) Wiley

31

You might also like