Week 2
Week 2
▪ Processes
▪ Address spaces
▪ Files
▪ Input/Output
▪ Protection
▪ The shell
Processes 3
▪ Program in execution
▪ Lives in address space
o a list of memory locations from 0 to some maximum, which the process can read and
write.
▪ Associated with each process is a set of resources e.g.,
o Registers (including the program counter and stack pointer),
o A list of open files
o Outstanding alarms
o Lists of related processes, and
o All the other information needed to run the program.
▪ A process is fundamentally a container that holds all the information needed to
run a program.
Operating Systems
Suspended Processes 4
Operating Systems
A Process Tree
Operating Systems
File directory 9
Operating Systems
File directory 10
Operating Systems
Mounted File System in UNIX 11
• Before the mount call, the root file system, on the hard disk, and a second file
system, on a CDROM, are separate and unrelated.
• The file system on the CD-ROM has been mounted on directory b, thus
allowing access to files /b/x and /b/y.
• First / represents root in UNIX
Operating Systems
Special Files 13
▪ Special files are treated like regular files by the OS, so you can use
the same I/O commands with them.
▪ There are two types of special files: block special files and
character special files.
▪ Block special files represent devices made up of blocks of data, like
hard disks.
▪ Character special files represent devices that take or produce a
stream of characters, like printers or modems.
▪ Special files are typically kept in the /dev directory in UNIX systems.
▪ The names of special files reflect the device they represent, for
example, /dev/lp represents a line printer.
Operating Systems
File, Block or Object Storage 14
There are different ways to store data, and each has its own strengths
and weaknesses.
▪ File storage organizes data as a hierarchy of files within folders. This is
a common way of storing data on computers, where each file can
be accessed by its name and location in the folder structure.
▪ Block storage, on the other hand, divides data into fixed-size chunks
called blocks. These blocks can be accessed directly, which allows
for faster data transfers and more efficient use of storage space.
▪ Object storage is a newer way of storing data, where each object is
given a unique identifier and associated metadata. This makes it
easier to manage large amounts of unstructured data.
Operating Systems
Pipes 15
▪ A pipe is a mechanism by which the output of one process is directed into the
input of another process
▪ From implementation perspective, a pipe is a sort of pseudofile that can be
used to connect two processes
▪ Pipes allow the output of one process to be used as input for another process.
▪ Process A writes to the pipe as though it were an output file, and process B reads
from the pipe as though it were an input file.
▪ In Linux, the pipe command is denoted by '|'.
▪ For example, "cat filename | grep Apple" searches for the word "Apple" in the
contents of "filename".
Operating Systems
I/O, Protection/Shell 16
20
System Call Implementation and Calling
21
UNIX Read System Call 22
onbytes: The number of bytes in file or The number of bytes to read from the
file
o Buffer: where read deposits the bytes or where the read data will be stored.
▪ The system call (and the library procedure) return the number of
bytes actually read in count
Operating Systems
System Stack 23
▪ The system stack, also known as the call stack or just the stack,
is a specific region of memory used by the operating system
and programs to manage function calls and temporary data
during program execution. It follows the Last In, First Out (LIFO)
principle.
Operating Systems
Read System Call 24
1 – 3 Before the Read system call, the calling Count=read(fd, buffer, nbytes)
program first pushes the parameters onto the
stack
Preparation: The user program pushes the
necessary information (parameters) onto the
system stack. In the context of a read system
call, this typically includes:
o File descriptor: A unique identifier that specifies
the file the program wants to read from (obtained
via a separate system call like open).
o Buffer address: The memory location within the
user program where the read data will be stored.
o Number of bytes to read: Indicates the maximum
amount of data the program wants to read from the
file.
Operating Systems
Read System Call 25
Operating Systems
Read System Call 26
Operating Systems
Read System Call 27
6 – Start execution at a fixed address within the kernel Count=read(fd, buffer, nbytes)
o Kernel Entry Point: Execution jumps to a fixed
location within the kernel code, specifically
designed to handle system calls.
Operating Systems
Read System Call 28
7 – The kernel code that starts following the TRAP Count=read(fd, buffer, nbytes)
examines the system-call number and then dispatches to
the correct system-call handler, usually via a table of
pointers to system-call handlers indexed on system-call
number
o System Call Handler Dispatch: The kernel
examines the system call number you provided
(like number 3 for read) and consults a table to
locate the appropriate handler function for that
specific system call. This table acts like an index,
mapping system call numbers to their
corresponding handler functions.
Operating Systems
Read System Call 29
Some of the major POSIX system calls. The return code s is −1 if an error has occurred. The return codes
are as follows: pid is a process id, fd is a file descriptor, n is a byte count, position is an offset within the
file, and seconds is the elapsed time. The parameters are explained in the text.`
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer
Society for maintaining compatibility between operating systems.
Operating Systems
Accessing and executing System Calls
▪ System calls typically not accessed directly by programs: Its
complex, error-prone, and non-portable & security risks
▪ An API is a set of clearly defined functions, methods, or
protocols that provide a standardized and user-friendly way for Program
programs to interact with an operating system, library, or other API (std lib)
software component
OS
▪ System calls mostly accessed by programs via a high-level Sys Calls
Application Program Interface (API) provide a standardized
Rest of Kernel
and user-friendly way for programs to interact with the
operating system rather than direct system call use
▪ Three most common APIs are :
o Win32 API for Windows,
▪ Abstraction: APIs allow programmers to focus on the logic of their programs rather than the
specifics of the underlying system, enhancing productivity and ease of development
▪ Performance: While system calls are faster than APIs due to direct interaction with the
operating system, APIs offer a balance between performance and portability, making them more
versatile for cross-platform development
▪ Functionality: APIs offer a structured way to interact with various components like web-
based systems, databases, software libraries, and operating systems, enabling seamless
communication between different applications and devices
Why use APIs Rather than System Calls
Directly?
code user level code
…
Your Program fd =open(…); Your Program Code
….
API open (…) fopen(…) Standard C library
{…} {…} Code
kernel
sys_open (…)
level
Operating Systems
Windows and UNIX API 37
o LPVOID buffer—a buffer where the data will be read into and written from
o DWORD bytesToRead—the number of bytes to be read into the buffer
38
Windows Win32 API 39
Operating Systems
Microkernels 43
Operating Systems
Microkernels 45
▪ An obvious generalization of this idea is to have the clients and servers run
on different computers , connected by a local or wide-area network
▪ Since clients communicate with servers by sending messages, The clients
need not know whether the messages are handled locally on their own
machines, or whether they are sent across a network to servers on a
remote machine.
▪ Increasingly many systems involve users at their home PCs as clients and
large machines elsewhere running as servers.
Operating Systems
Virtual Machines 49
Installed on an existing
Bare-metal” hypervisor
Operating System
Citrix/Xen Server, VMware ESXi and Microsoft Virtual PC, Oracle Virtual Box, VMware
Microsoft Hyper-V. Workstation, Oracle Solaris Zones
Operating Systems
Distinction Between a Type 1 & a Type 2 5050
Hypervisor
Type 1 Hypervisor (Bare-Metal) Type 2 Hypervisor (Hosted)
▪ Installs directly on the physical hardware ▪ Requires a host operating system to be
of the host system. installed first.
▪ It operates without the need for a host ▪ The hypervisor runs as an application
operating system within the host OS and manages
▪ Generally offers better performance ▪ Some performance overhead as it relies
because it has direct access to the on the host operating system for resource
physical hardware resources. management.
▪ Preferred for enterprise-level virtualization, ▪ Commonly used for development,
data centers, and cloud environments testing, and scenarios where
where performance and resource performance is not the primary concern.
efficiency are critical.
Operating Systems
51
Operating Systems