Lec 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Chapter 2: Operating-System

Services

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline

▪ Operating System Services


▪ User and Operating System-Interface
▪ System Calls
▪ System Services
▪ Linkers and Loaders
▪ Why Applications are Operating System Specific
▪ Design and Implementation
▪ Operating System Structure
▪ Building and Booting an Operating System
▪ Operating System Debugging

Operating System Concepts – 10th Edition 2.2 Silberschatz, Galvin and Gagne ©2018
Objectives

▪ Identify services provided by an operating system


▪ Illustrate how system calls are used to provide operating
system services
▪ Compare and contrast monolithic, layered, microkernel,
modular, and hybrid strategies for designing operating
systems
▪ Illustrate the process for booting an operating system
▪ Apply tools for monitoring operating system performance
▪ Design and implement kernel modules for interacting with a
Linux kernel

Operating System Concepts – 10th Edition 2.3 Silberschatz, Galvin and Gagne ©2018
Operating System Services
▪ Operating systems provide an environment for execution of programs and services
to programs and users
▪ The specific services provided, of course, differ from one operating system to
another, but we can identify common classes
▪ Note that these services also make the programming task easier for the
programmer
▪ 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), touch-
screen, 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. . For efficiency and protection, users usually cannot control
I/O devices directly. Therefore, the operating system must provide a means to
do I/O
• File-system manipulation - The file system is of particular interest. Programs
need to read and write files and directories, create and delete them, search
them, list file Information, permission management.
Operating System Concepts – 10th Edition 2.4 Silberschatz, Galvin and Gagne ©2018
Operating System Services (Cont.)
▪ One set of operating-system services provides functions that are helpful to the
user (Cont.):
• Communications – Processes may exchange information, on the same
computer or between computers over a network
 Communications may be via shared memory, in which two or more
processes read and write to a shared section of memory or through
message passing, in which packets of information in predefined formats
are moved between processes by the operating system (packets moved
by the OS)
• Error detection – OS needs to be constantly aware of possible errors
 May occur in the CPU and memory hardware (such as a memory error
or a power failure), in I/O devices (such as a parity error on disk, a
connection failure on a network, or lack of paper in the printer), in user
program (such as an arithmetic overflow or an attempt to access an
illegal memory location)
 For each type of error, OS should take the appropriate action to ensure
correct and consistent computing
 Debugging facilities can greatly enhance the user’s and programmer’s
abilities
th
Operating System Concepts – 10toEdition
efficiently use the system
2.5 Silberschatz, Galvin and Gagne ©2018
Operating System Services (Cont.)
▪ Another set of OS functions exists for ensuring the efficient operation of the
system itself via resource sharing
• Resource allocation - When multiple users or multiple jobs running
concurrently, resources must be allocated to each of them
 Many types of resources - CPU cycles, main memory, file storage, I/O devices.
 Some (such as CPU cycles, main memory, and file storage) may have special
allocation code, whereas others (such as I/O devices) may have much more
general request and release code
 In determining how best to use the CPU, operating systems have CPU-
scheduling routines that take into account the speed of the CPU, the process
that must be executed, the number of processing cores on the CPU, and
other factors
• Logging - To keep track of which users use how much and what kinds of
computer resources
• 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
 Protection involves ensuring that all access to system resources is
controlled
 Security of the system from outsiders requires user authentication,
extends to defending external I/O devices from invalid access attempts
Operating System Concepts – 10th Edition 2.6 Silberschatz, Galvin and Gagne ©2018
A View of Operating System Services

Operating System Concepts – 10th Edition 2.7 Silberschatz, Galvin and Gagne ©2018
Command Line interpreter
▪ CLI allows direct command entry
▪ Operating systems, including Linux, UNIX, and Windows, treat the command
interpreter as a special program that is running when a process is initiated or
when a user first logs on
▪ On systems with multiple command interpreters to choose from, the interpreters
are known as shells
• For example, on UNIX and Linux systems, a user may choose among
several different shells, including the C shell, Bourne-Again shell, Korn
shell, and others
▪ Third-party shells and free user-written shells are also available
▪ Sometimes implemented in kernel, sometimes by systems program
▪ The main function of the command interpreter is to get and execute the next
user-specified command
▪ Many of the commands given at this level manipulate files: create, delete, list,
print, copy, execute, and so on
▪ The various shells available on UNIX systems operate in this way

Operating System Concepts – 10th Edition 2.8 Silberschatz, Galvin and Gagne ©2018
Command Line interpreter
▪ The commands can be implemented in two general ways
• In one approach, the command interpreter itself contains the code to
execute the command. For example, a command to delete a file may cause
the command interpreter to jump to a section of its code that sets up the
parameters and makes the appropriate system call. In this case, the number
of commands that can be given determines the size of the command
interpreter, since each command requires its own implementing code
• An alternative approach—used by UNIX, among other operating systems —
implements most commands through system programs. In this case, the
command interpreter does not understand the command in any way; it
merely uses the command to identify a file to be loaded into memory and
executed. Thus, the UNIX command to delete a file
• rm file.txt
▪ These commands can be implemented in two general way
▪ Sometimes multiple flavors implemented – shells
▪ Primarily fetches a command from user and executes it
▪ Sometimes commands built-in, sometimes just names of programs
• If the latter, adding new features doesn’t require shell modification
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
2.9
User Operating System Interface - GUI
▪ User-friendly desktop metaphor interface
• Usually mouse, keyboard, and monitor
• Icons represent files, programs, actions, etc
• Various mouse buttons over objects in the interface cause various actions
(provide information, options, execute function, open directory (known as a
folder)
• Invented at Xerox PARC Facility in 1970s
• The first GUI appeared on the Xerox Alto computer in 1973
• Microsoft’s first version of Windows— Version 1.0—was based on the
addition of a GUI interface to the MS-DOS operating system
▪ Many systems now include both CLI and GUI interfaces
• Microsoft Windows is GUI with CLI “command” shell
• Apple Mac OS X is “Aqua” GUI interface with UNIX kernel underneath
and shells available
• Unix and Linux have CLI with optional GUI interfaces (CDE, KDE,
GNOME)
Operating System Concepts – 10th Edition 2.10 Silberschatz, Galvin and Gagne ©2018
Touchscreen Interfaces

▪ Touchscreen devices require new


interfaces
• Mouse not possible or not desired
• Actions and selection based on
gestures
• Virtual keyboard for text entry
▪ Voice commands

Operating System Concepts – 10th Edition 2.11 Silberschatz, Galvin and Gagne ©2018
Choice of Interface
▪ The choice of whether to use a command-line or GUI interface is mostly one of
personal preference
▪ System administrators who manage computers and power users who have deep
knowledge of a system frequently use the command-line interface
▪ For them, it is more efficient, giving them faster access to the activities they
need to perform
▪ On some systems, only a subset of system functions is available via the GUI,
leaving the less common tasks to those who are command-line knowledgeable
▪ Further, command-line interfaces usually make repetitive tasks easier, in part
because they have their own programmability
▪ In contrast, most Windows users are happy to use the Windows GUI
environment and almost never use the shell interface. Recent versions of the
Windows operating system provide both a standard GUI for desktop and
traditional laptops and a touch screen for tablet

Operating System Concepts – 10th Edition 2.12 Silberschatz, Galvin and Gagne ©2018
System Calls

▪ Provides programming interface to the services provided by the OS


▪ Typically written in a high-level language (C or C++)
▪ Mostly accessed by programs via a high-level Application
Programming Interface (API) rather than direct system call use
▪ Three most common APIs are Win32 API for Windows, POSIX API for
POSIX-based systems (including virtually all versions of UNIX, Linux,
and Mac OS X), and Java API for the Java virtual machine (JVM)

Note that the system-call names used throughout this text are
generic

Operating System Concepts – 10th Edition 2.13 Silberschatz, Galvin and Gagne ©2018
Example of System Calls
▪ Llet’s first use an example to illustrate how system calls are used: writing
a simple program to read data from one file and copy them to another
file
▪ System call sequence to copy the contents of one file to another file
▪ These names can be specified in many ways, depending on the
operating-system design
• One approach is to pass the names of the two files as part of the
command— for example, the UNIX cp command: cp in.txt out.txt
• .A second approach is for the program to ask the user for the
names. In an interactive system, this approach will require a
sequence of system calls, first to write a prompting message on the
screen and then to read from the keyboard the characters that define
the two files. On mouse-based and icon-based systems, a menu
of file names is usually displayed in a window. The user can then use
the mouse to select the source name, and a window can be opened
for the destination name to be specified. This sequence requires
many I/O system calls

Operating System Concepts – 10th Edition 2.14 Silberschatz, Galvin and Gagne ©2018
System Calls
▪ Once the two file names have been obtained:
• Open the input file and create and open the output file. Each of these operations
requires another system call
• Possible error conditions for each system call must be handled. For example, when the
program tries to open the input file, it may find that there is no file of that name or that
the file is protected against access (error message system call) and then terminate
abnormally (another system call)
• We may find that there is already an output file with the same name. This situation
may cause the program to abort (a system call), or we may delete the existing file
(another system call) and create a new one (yet another system call)
• Another option, in an interactive system, is to ask the user (via a sequence of system
calls to output the prompting message and to read the response from the terminal)
whether to replace the existing file or to abort the program.
• When both files are set up, we enter a loop that reads from the input file (a system
call) and writes to the output file (another system call). Each read and write must
return status information regarding various possible error conditions.
• After the entire file is copied, the program may close both files (two system calls),
write a message to the console or window (more system calls), and finally terminate
normally (the final system call)

Operating System Concepts – 10th Edition 2.15 Silberschatz, Galvin and Gagne ©2018
System Calls

Operating System Concepts – 10th Edition 2.16 Silberschatz, Galvin and Gagne ©2018
Example of Standard API
• Systems execute thousands of system calls per second
• Most programmers never see this level of detail
• however. Typically, application developers design programs according to an
application programming interface (API)
• The API specifies a set of functions that are available to an application
programmer, including the parameters that are passed to each function and the
return values the programmer can expect
• Program portability
• Three of the most common APIs available to application programmers are the
Windows API for Windows systems, the POSIX API for POSIX-based
systems (which include virtually all versions of UNIX, Linux, and macOS), and
the Java API for programs that run on the Java virtual machine
• A programmer accesses an API via a library of code provided by the
operating systemIn the case of UNIX and Linux for programs written in the C
language, the library is called libc.
• Each operating system has its own name for each system call
• Behind the scenes, the functions that make up an API typically invoke the actual
system calls on behalf of the application programmer. For example, the Windows
function CreateProcess() actually invokes the NTCreateProcess() system
call in the Windows kernel

Operating System Concepts – 10th Edition 2.17 Silberschatz, Galvin and Gagne ©2018
Example of Standard API

Operating System Concepts – 10th Edition 2.18 Silberschatz, Galvin and Gagne ©2018
System Call Implementation

▪ Typically, a number is associated with each system call


• System-call interface maintains a table indexed according to
these numbers
▪ The system call interface invokes the intended system call in OS
kernel and returns status of the system call and any return values
▪ The caller need know nothing about how the system call is
implemented
• Just needs to obey API and understand what OS will do as a
result call
• Most details of OS interface hidden from programmer by API
 Managed by run-time support library (set of functions built into
libraries included with compiler)

Operating System Concepts – 10th Edition 2.19 Silberschatz, Galvin and Gagne ©2018
API – System Call – OS Relationship

Operating System Concepts – 10th Edition 2.20 Silberschatz, Galvin and Gagne ©2018
System Call Parameter Passing
▪ Often, more information is required than simply identity of desired system call
• Exact type and amount of information vary according to OS and call
• For example, to get input, we may need to specify the file or device to use
as the source, as well as the address and length of the memory buffer into
which the input should be read. Of course, the device or file and length may be
implicit in the call
▪ Three general methods used to pass parameters to the OS
• Simplest: pass the parameters in registers
 In some cases, may be more parameters than registers
• Parameters stored in a block, or table, in memory, and address of block
passed as a parameter in a register (more than 5 parameters)
 This approach taken by Linux and Solaris
• Parameters placed, or pushed, onto the stack by the program and
popped off the stack by the operating system
• Block and stack methods do not limit the number or length of parameters being
passed

Operating System Concepts – 10th Edition 2.21 Silberschatz, Galvin and Gagne ©2018
Parameter Passing via Table

Operating System Concepts – 10th Edition 2.22 Silberschatz, Galvin and Gagne ©2018
Types of System Calls

▪ Process control
• create process, terminate process
• end, abort
• load, execute
• get process attributes, set process attributes
• wait for time
• wait event, signal event
• allocate and free memory
• Dump memory if error
• Debugger for determining bugs, single step execution
• Locks for managing access to shared data between processes

Operating System Concepts – 10th Edition 2.23 Silberschatz, Galvin and Gagne ©2018
Types of System Calls (Cont.)

▪ File management
• create file, delete file
• open, close file
• read, write
• get and set file attributes
▪ Device management
• request device, release device
• read, write
• get device attributes, set device attributes
• logically attach or detach devices

Operating System Concepts – 10th Edition 2.24 Silberschatz, Galvin and Gagne ©2018
Types of System Calls (Cont.)

▪ Information maintenance
• get time or date, set time or date
• get system data, set system data
• get and set process, file, or device attributes
▪ Communications
• create, delete communication connection
• send, receive messages if message passing model to host
name or process name
 From client to server
• Shared-memory model create and gain access to memory
regions
• transfer status information
• attach and detach remote devices

Operating System Concepts – 10th Edition 2.25 Silberschatz, Galvin and Gagne ©2018
Types of System Calls (Cont.)

▪ Protection
• Control access to resources
• Get and set permissions
• Allow and deny user access

Operating System Concepts – 10th Edition 2.26 Silberschatz, Galvin and Gagne ©2018
Examples of Windows and Unix System Calls

Operating System Concepts – 10th Edition 2.27 Silberschatz, Galvin and Gagne ©2018
Standard C Library Example
▪ C program invoking printf() library call, which calls write() system call

Operating System Concepts – 10th Edition 2.28 Silberschatz, Galvin and Gagne ©2018
Example: Arduino

▪ Single-tasking
▪ No operating system
▪ Programs (sketch) loaded via
USB into flash memory
▪ Single memory space
▪ Boot loader loads program
▪ Program exit -> shell
reloaded

At system startup running a program

Operating System Concepts – 10th Edition 2.29 Silberschatz, Galvin and Gagne ©2018
Example: FreeBSD
▪ Unix variant
▪ Multitasking
▪ User login -> invoke user’s choice of
shell
▪ Shell executes fork() system call to create
process
• Executes exec() to load program into
process
• Shell waits for process to terminate or
continues with user commands
▪ Process exits with:
• code = 0 – no error
• code > 0 – error code

Operating System Concepts – 10th Edition 2.30 Silberschatz, Galvin and Gagne ©2018
End

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

You might also like