Sei sulla pagina 1di 87

Operating System

Services

1
Operating System Services
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), Batch
• Program development
• Editors and debuggers
• 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)

2
Operating System Services
• I/O operations - A running program may require I/O, which
may involve a file or an I/O device.
• File-system manipulation - The file system is of particular
interest. Obviously, programs need to read and write files
and directories, create and delete them, search them, list file
Information, permission management.
• 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)

3
Operating System Services (Cont.)
• 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
• Software errors
• Arithmetic overflow
• Access forbidden memory locations
• 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 to efficiently use the system

4
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 - 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.
• Accounting - To keep track of which users use how much
and what kinds of computer resources

5
Operating System Services (Cont.)
• Protection and security - The owners of information
stored in a multi-user 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
• If a system is to be protected and secure, precautions must be
instituted throughout it. A chain is only as strong as its weakest
link.

6
A View of Operating System Services
• Windows system services
• https://msdn.microsoft.com/en-
us/library/windows/desktop/ee663297(v=vs.85).aspx

• Linux system services (RedHat)


• https://access.redhat.com/documentation/en-
us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-
managing_services_with_systemd-services

• Android system services


• https://commonsware.com/Android/previews/system-services

• Unix system services


• http://www.redbooks.ibm.com/redbooks/pdfs/sg247035.pdf

• iOS ??
User Operating System Interface - CLI

CLI or command interpreter allows direct


command entry
• Sometimes implemented in kernel, sometimes by
systems program
• 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
A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter.[1] The
various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell
scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs
the program, and does any necessary cleanup, logging, etc. is called a wrapper.
The term is also used more generally to mean the automated mode of running an operating system shell; in specific
operating systems they are called other things such as batch files (MSDos-Win95 stream, OS/2), command
procedures (VMS), and shell scripts (Windows NT stream and third-party derivatives like 4NT—article is
at cmd.exe), and mainframe operating systems are associated with a number of terms.
The typical Unix/Linux/Posix-compliant installation includes the Korn Shell (ksh) in several possible versions such as
ksh88, Korn Shell '93 and others. The oldest shell still in common use is the Bourne shell (sh); Unix systems
invariably also include the C Shell (csh), Bourne Again Shell (bash), a remote shell (rsh), a secure shell for SSL
telnet connections (ssh), and a shell which is a main component of the Tcl/Tk installation usually called tclsh; wish is
a GUI-based Tcl/Tk shell. The C and Tcl shells have syntax quite similar to that of said programming languages, and
the Korn shells and Bash are developments of the Bourne shell, which is based on the ALGOL language with
elements of a number of others added as well.[2] On the other hand, the various shells plus tools like awk, sed, grep,
and BASIC, Lisp, C and so forth contributed to the Perl programming language.[3]
Other shells available on a machine or available for download and/or purchase include ash, msh, ysh, zsh (a
particularly common enhanced Korn Shell), the Tenex C Shell (tcsh), a Perl-like shell (psh) and others. Related
programs such as shells based on Python, Ruby, C, Java, Perl, Pascal, Rexx &c in various forms are also widely
available. Another somewhat common shell is osh, whose manual page states it "is an enhanced, backward-
compatible port of the standard command interpreter from Sixth Edition UNIX."
In Windows NT (XP, Vista, 7, 8, 10,...) one is able to write batch files that are interpreted by the
Command Prompt (cmd.exe). They can be used to automate file-system tasks such as backups or
basic installations and can be used with other command-line utilities as well. The batch files can
be considered to be a simple scripting language with logic and jumps. The advantages of using
batch files are the ease of writing them, the ability to edit the files without compiling anything, their
cross-compatibility across Windows NT Operating Systems and their inherent ability to manipulate
file systems due to their basis on MS-DOS. Batch file scripts are not case-sensitive, although
strings and data are. The file is controlled by a list of commands separated into lines which are run
like normal commands would be at the Command Prompt, although some functionality is different.
Batch files can be run from Windows Explorer but the console used to display them closes
automatically at the end of the batch file, so a command at the end that prevents instantaneous
exit is needed for any remaining output to be read before the console window closes. Although the
batch files are able to manipulate their environment, such as color settings and environment
variables, the changes are all temporary, as in a standard Command Prompt session. Color
settings, however, are retained on later editions of Windows NT. In order to try to learn about batch
files, it is useful to understand Command Prompt commands. See: Guide to Windows commands.
Bourne Shell Command Interpreter
Batch File
The script is kept inside a batch file, with the extension .bat or .cmd. Although .bat is more
recognisable, as it was used in the MS-DOS environment that preceded the Command
Prompt, the Command Prompt's interpretations of batch files is very different to the manner of
interpreting DOS batch files, and .cmd files are only interpreted by the Command Prompt, so
using the .cmd extension prevents mis-use in older environments.
Execution starts at the top of the file and ends at the end. When the end of the file is reached,
the file exits to the Command Prompt if it was invoked from there, or the console window
closes if it was invoked from Windows Explorer or the START command.
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
• 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)
Touchscreen Interfaces

nTouchscreen devices
require new interfaces
l Mouse not possible or not desired
l Actions and selection based on
gestures
l Virtual keyboard for text entry
l Voice commands.
The Mac OS X GUI
System Calls
• 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
An application program interface (API) is a set of routines, protocols, and tools
for building software applications. Basically, an API specifies how software
components should interact. Additionally, APIs are used when programming
graphical user interface (GUI) components. A good API makes it easier to
develop a program by providing all the building blocks. A programmer then puts
the blocks together.

There are many different types of APIs for operating systems, applications or
websites. Windows, for example, has many API sets that are used by system
hardware and applications — when you copy and paste text from one application
to another, it is the API that allows that to work.

Most operating environments, such as MS-Windows, provide APIs, allowing programmers to


write applications consistent with the operating environment. Today, APIs are also specified
by websites. For example, Amazon or eBay APIs allow developers to use the existing retail
infrastructure to create specialized web stores. Third-party software developers also use
Web APIs to create software solutions for end-users.
API is the acronym for Application Programming Interface, which is a software intermediary that allows
two applications to talk to each other. Each time you use an app like Facebook, send an instant
message, or check the weather on your phone, you’re using an API.

When you use an application on your mobile phone, the application connects to the Internet and sends
data to a server. The server then retrieves that data, interprets it, performs the necessary actions and
sends it back to your phone. The application then interprets that data and presents you with the
information you wanted in a readable way. This is what an API is - all of this happens via API.
Example of Standard API
Example of System Calls

• System call sequence to copy the contents of one file to another file
System Call Implementation
• Typically, a number 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)
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
• 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
• 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
Parameter Passing via Table
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
Types of System Calls

• File management
• create file, delete file
• open, close file
• read, write, reposition
• get and set file attributes
• Device management
• request device, release device
• read, write, reposition
• get device attributes, set device attributes
• logically attach or detach devices
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
Types of System Calls (Cont.)
• Protection
• Control access to resources
• Get and set permissions
• Allow and deny user access
Examples of Windows and Unix System Calls
Standard C Library Example
• C program invoking printf() library call, which calls
write() system call
System Programs
• System programs provide a convenient
environment for program development and
execution. They can be divided into:
• File manipulation
• Status information sometimes stored in a File
modification
• Programming language support
• Program loading and execution
• Communications
• Background services
• Application programs
• Most users’ view of the operation system is
defined by system programs, not the actual system
calls
System Programs
• Provide a convenient environment for program
development and execution
• Some of them are simply user interfaces to system calls;
others are considerably more complex
• File management - Create, delete, copy, rename,
print, dump, list, and generally manipulate files and
directories
• Status information
• Some ask the system for info - date, time, amount of
available memory, disk space, number of users
• Others provide detailed performance, logging, and
debugging information
• Typically, these programs format and print the output to
the terminal or other output devices
• Some systems implement a registry - used to store and
retrieve configuration information
The registry is a system-defined database in which applications and system components store
and retrieve configuration data. The data stored in the registry varies according to the version
of Microsoft Windows. Applications use the registry API to retrieve, modify, or delete registry
data.
You should not edit registry data that does not belong to your application unless it is
absolutely necessary. If there is an error in the registry, your system may not function properly.
If this happens, you can restore the registry to the state it was in when you last started the
computer successfully. For more information, see the help for your operating system.
System Programs (Cont.)
• File modification
• Text editors to create and modify files
• Special commands to search contents of files or perform
transformations of the text
• Programming-language support - Compilers,
assemblers, debuggers and interpreters sometimes
provided
• Program loading and execution- Absolute loaders,
relocatable loaders, linkage editors, and overlay-
loaders, debugging systems for higher-level and
machine language
• Communications - Provide the mechanism for
creating virtual connections among processes, users,
and computer systems
• Allow users to send messages to one another’s screens,
browse web pages, send electronic-mail messages, log in
remotely, transfer files from one machine to another
System Programs (Cont.)
• Background Services
• Launch at boot time
• Some for system startup, then terminate
• Some from system boot to shutdown
• Provide facilities like disk checking, process scheduling,
error logging, printing
• Run in user context not kernel context
• Known as services, subsystems, daemons

• Application programs
• Don’t pertain to system
• Run by users
• Not typically considered part of OS
• Launched by command line, mouse click, finger poke
In multitasking computer operating systems, a daemon (/ˈdiːmən/ or /ˈdeɪmən/) is
a computer program that runs as a background process, rather than being under the direct
control of an interactive user. Traditionally, the process names of a daemon end with the
letter d, for clarification that the process is, in fact, a daemon, and for differentiation
between a daemon and a normal computer program. For example, syslogd is the
daemon that implements the system logging facility, and sshd is a daemon that serves
incoming SSH connections.
In a Unix environment, the parent process of a daemon is often, but not always,
the init process. A daemon is usually either created by a process forking a child process
and then immediately exiting, thus causing init to adopt the child process, or by the init
process directly launching the daemon. In addition, a daemon launched by forking and
exiting typically must perform other operations, such as dissociating the process from any
controlling terminal (tty). Such procedures are often implemented in various convenience
routines such as daemon(3) in Unix.
Systems often start daemons at boot time which will respond to network requests,
hardware activity, or other programs by performing some task. Daemons such as cron may
also perform defined tasks at scheduled times.
Daemon is a process that runs in the background and performs a specified
operation at predefined times or in response to certain events.
The term daemon is a UNIX term, though many other operating systems provide
support for daemons, though they're sometimes called other names. Windows, for
example, refers to daemons as System Agents and services.
Typical daemon processes include print spoolers, e-mail handlers, and other
programs that perform administrative tasks for the operating system. The term
comes from Greek mythology, where daemons were guardian spirits.
Operating System Design and Implementation

• Design and Implementation of OS not “solvable”,


but some approaches have proven successful
• Internal structure of different Operating Systems
can vary widely
• Start the design by defining goals and specifications
• Affected by choice of hardware, type of system
• User goals and System goals
• User goals – operating system should be convenient to
use, easy to learn, reliable, safe, and fast
• System goals – operating system should be easy to
design, implement, and maintain, as well as flexible,
reliable, error-free, and efficient
Operating System Design and Implementation (Cont.)

• Important principle to separate


Policy: What will be done?
Mechanism: How to do it?
• Mechanisms determine how to do
something, policies decide what will be done
• The separation of policy from mechanism is a
very important principle, it allows maximum
flexibility if policy decisions are to be
changed later (example – timer)
• Specifying and designing an OS is highly
creative task of software engineering
Implementation

• Much variation
• Early OSes in assembly language
• Then system programming languages like Algol, PL/1
• Now C, C++
• Actually usually a mix of languages
• Lowest levels in assembly
• Main body in C
• Systems programs in C, C++, scripting languages like PERL,
Python, shell scripts
• More high-level language easier to port to other
hardware
• But slower
• Emulation can allow an OS to run on non-native
hardware
Operating-System Debugging
• Debugging is finding and fixing errors, or bugs
• OS generate log files containing error information
• Failure of an application can generate core dump file
capturing memory of the process
• Operating system failure can generate crash dump file
containing kernel memory
• Beyond crashes, performance tuning can optimize system
performance
• Sometimes using trace listings of activities, recorded for analysis
• Profiling is periodic sampling of instruction pointer to look for
statistical trends
Kernighan’s Law: “Debugging is twice as hard as writing the
code in the first place. Therefore, if you write the code as
cleverly as possible, you are, by definition, not smart
enough to debug it.”
Performance Tuning
• Improve
performance by
removing
bottlenecks
• OS must provide
means of computing
and displaying
measures of system
behavior
• For example, “top”
program or
Windows Task
Manager
DTrace

 DTrace tool in Solaris,


FreeBSD, Mac OS X allows
live instrumentation on
production systems
 Probes fire when code is
executed within a provider,
capturing state data and
sending it to consumers of
those probes

 Example of following
XEventsQueued system call
move from libc library to
kernel and back
Dtrace (Cont.)

 DTrace code to record


amount of time each
process with UserID 101 is
in running mode (on CPU)
in nanoseconds
Operating System Generation
Operating systems are designed to run on
any of a class of machines; the system must
be configured for each specific computer
site
SYSGEN program obtains information
concerning the specific configuration of the
hardware system
Used to build system-specific compiled kernel or
system-tuned
Can general more efficient code than one
general kernel
System Boot
• When power initialized on system, execution starts
at a fixed memory location
• Firmware ROM used to hold initial boot code
• Operating system must be made available to
hardware so hardware can start it
• Small piece of code – bootstrap loader, stored in ROM or
EEPROM locates the kernel, loads it into memory, and
starts it
• Sometimes two-step process where boot block at fixed
location loaded by ROM code, which loads bootstrap
loader from disk
• Common bootstrap loader, GRUB, allows selection
of kernel from multiple disks, versions, kernel
options
• Kernel loads and system is then running
UNIX
• Unix programmers model every high-level device as a file, because they believed
the purpose of computation was data transformation.
• E.g. printers were represented as a "file" at a known location - when data was copied to the
file, it printed out.
• In Unix, the OS consists of two parts;
• (1) the huge collection of utility programs that drive most operations,
• (2) kernel that runs the programs.
• the kernel is a program running in supervisor mode that acts as a program loader and
supervisor for the small utility programs making up the rest of the system, and to provide
locking and I/O services for these programs; beyond that, the kernel didn't intervene at all in
user space.
• Over the years the computing model changed, and Unix's treatment of everything
as a file or byte stream no longer was as universally applicable:
• Although a terminal could be treated as a file or a byte stream, which is printed to or read
from, the same did not seem to be true for a GUI.
• Networking - Even if network communication can be compared to file access, the low-level
packet-oriented architecture dealt with discrete chunks of data and not with whole files.
• As the capability of computers grew, Unix became increasingly cluttered with code. While
kernels might have had 100,000 lines of code in the 70s and 80s, kernels of modern Unix
successors like Linux have more than 4.5 million lines.

53
UNIX
• Modern Unix-derivatives are generally based on module-loading monolithic
kernels. Examples of this are the Linux kernel in its many distributions as
well as the Berkeley software distribution variant kernels such as FreeBSD,
DragonflyBSD, OpenBSD and NetBSD.
• Apart from these alternatives, amateur developers maintain an active OS
development community, populated by self-written hobby kernels which
mostly end up sharing many features with Linux, FreeBSD, DragonflyBSD,
OpenBSD or NetBSD kernels and/or being compatible with them.

54
Mac OS
• Apple Computer first launched Mac OS in 1984, bundled with its Apple Macintosh
personal computer. For the first few releases, Mac OS (or System Software, as it
was called) lacked many essential features, such as multitasking and a hierarchical
filesystem. With time, the OS evolved and eventually became Mac OS 9 and had
many new features added, but the kernel basically stayed the same.[citation needed]
Against this, Mac OS X is based on Darwin, which uses a hybrid kernel called XNU,
which was created combining the 4.3BSD kernel and the Mach kernel.

55
Microsoft Windows
• Microsoft Windows was first released in 1985 as an add-on to MS-DOS. Because of its
dependence on another OS, initial releases of Windows, prior to Windows 95, were considered an
operating environment (do not confuse with operating system).
• This product line continued to evolve through the 1980s and 1990s, culminating with release of
the Windows 9x series (upgrading the system's capabilities to 32-bit addressing and pre-emptive
multitasking) through the mid 1990s and ending with the release of Windows Me in 2000.
• Microsoft also developed Windows NT, an OS intended for high-end and business users. This line
started with the release of Windows NT 3.1 in 1993, and has continued through the years of 2000
with Windows Vista and Windows Server 2008.

56
Microsoft Windows
• The release of Windows XP in October 2001 brought these two product lines together, with the
intent of combining the stability of the NT kernel with consumer features from the 9x series. The
architecture of Windows NT's kernel is considered a hybrid kernel because the kernel itself
contains tasks such as the Window Manager and the IPC Manager, but several subsystems run in
user mode. The precise breakdown of user mode and kernel mode components has changed from
release to release, but with the introduction of the User Mode Driver Framework in Windows
Vista, and user-mode thread scheduling in Windows 7, have brought more kernel-mode
functionality into user-mode processes.

57
Microkernels
• Small operating system core
• Contains only essential core operating systems
functions
• Many services traditionally included in the
operating system are now external subsystems
• Device drivers
• File systems
• Virtual memory manager
• Windowing system
• Security services

65
66
Microkernel System Structure
• Moves as much from the kernel into “user” space
• Communication takes place between user modules using
message passing
• Benefits:
• Easier to extend a microkernel
• Easier to port the operating system to new architectures
• More reliable (less code is running in kernel mode)
• More secure
• Detriments:
• Performance overhead of user space to kernel space
communication

67
Benefits of a Microkernel Organization
• Uniform interface on request made by a process
• Don’t distinguish between kernel-level and user-level services
• All services are provided by means of message passing
• Extensibility
• Allows the addition of new services
• Flexibility
• New features added
• Existing features can be subtracted
• Portability
• Changes needed to port the system to a new processor is changed in
the microkernel - not in the other services

68
Benefits of a Microkernel Organization
• Reliability
• Modular design
• Small microkernel can be rigorously tested
• Distributed system support
• Message are sent without knowing what the target
machine is
• Object-oriented operating system
• Components are objects with clearly defined interfaces
that can be interconnected to form software

69
Monolithic kernel
• all OS services run along with the main kernel thread, thus
also residing in the same memory area.
• provides rich and powerful hardware access.
• "easier to implement a monolithic kernel" than
microkernels [Ken Thompson, UNIX developer ].
• main disadvantages:
• dependencies between system components – a bug in a device driver
might crash the entire system
• large kernels can become very difficult to maintain.

70
Microkernel
• a simple abstraction over the hardware, with a set of primitives or
system calls to implement minimal OS services (memory management,
multitasking, and inter-process communication).
• Other services, including those normally provided by the kernel such as
networking, are implemented in user-space programs, referred to as
servers.
• easier to maintain than monolithic kernels,
• but the large number of system calls and context switches might slow
down the system because they typically generate more overhead than
plain function calls.
• A microkernel allows the implementation of the remaining part of the
OSs a normal application program written in a high-level language, and
the use of different OSs on top of the same unchanged kernel. It is also
possible to dynamically switch among OSs and to have more than one
active simultaneously.

71
Monolithic kernel vs. Microkernel

Microkernel
In the microkernel approach, the
Monolithic kernels kernel itself only provides basic
functionality that allows the execution
of servers, separate programs that
assume former kernel functions, such
as device drivers, GUI servers, etc.
73

Potrebbero piacerti anche