Session 3 4-Revision

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Operating Systems Design (19CS2106S )

Session 3 & 4
Revision
Session-3-Revision

• Operating System Interfaces


• User & Kernel Mode
• System Calls
• Kernel Organization
Operating System Interfaces
• An operating system provides services to user
programs through an interface.
• Designing a good interface turns out to be
difficult.
• Preferably, the interface to be simple and
narrow because that makes it easier to get the
implementation right.
Popular Computing Device Interfaces

• Command Line Interface(CLI)


• Graphical User Interface(GUI)
Review of Basics
• A kernel, a special program that provides services
to running programs.
• Each running program, called a process, has
memory containing instructions, data, and a stack.
• The instructions implement the program’s
computation.
• The data are the variables on which the
computation acts.
• The stack organizes the program’s procedure calls.
System Call
• When a process needs to invoke a kernel
service, it invokes a procedure call in the
operating system interface. Such a procedure
is called a System Call
• The system call enters the kernel; the kernel
performs the service and returns.
• The shell is an ordinary program that reads
commands from the user and executes them.
Various System Calls in xv6
Understanding User & Kernel Space
• As shown in the diagram, the system call works in
TWO regions/spaces called User space & Kernel
space.
• Strong isolation requires a hard boundary between
applications and the operating system.
• Initially the system call will be with the process and
is in User space
• When the process seeks some service from kernel,
then the system call enters into kernel space.
• Once the kernel performs the service, then the
system call returns to the user space.
• An application can execute only user-mode
instructions (e.g., adding numbers, etc.) and is
said to be running in user space, while the
software in kernel mode can also execute
privileged instructions and is said to be
running in kernel space.
• The software running in kernel space (or in
kernel mode) is called the kernel.
• In kernel mode ,the processor is allowed to
execute privileged instructions.
• For example, reading and writing the disk (or any
other I/O device) involves privileged instructions.
• If an application in user mode attempts to
execute a privileged instruction, then the
processor doesn’t execute the instruction, but
switches to kernel mode so that the software in
kernel mode can clean up the application,
because it did something it shouldn’t be doing
Kernel Organization-Monolithic Vs Micro
• In Monolithic Kernel, the entire operating
system resides in the kernel, so that the
implementations of all system calls run in
kernel mode.
• In this organization the entire operating
system runs with full hardware privilege.
• This organization is convenient because the OS
designer doesn’t have to decide which part of
the operating system doesn’t need full
hardware privilege.
Issues with Monolithic Kernel
• In a monolithic kernel, a mistake is fatal,
because an error in kernel mode will often
result in the kernel to fail.
• If the kernel fails, the computer stops working,
and thus all applications fail too.
• The computer must reboot to start again.
Micro Kernel
• To reduce the risk of mistakes in the kernel, OS
designers can minimize the amount of
operating system code that runs in kernel
mode, and execute the bulk of the operating
system in user mode.

• This kernel organization is called a microkernel


Process Creation
• A process may create a new process using the
fork system call.
• fork creates a new process, called the child
process, with exactly the same memory
contents as the calling process, called the
parent process.
• fork returns in both the parent and the child.
• In the parent, fork returns the child’s pid; in
the child, it returns zero
Code to implement fork
• #include”type.h”
• #include”stat.h”
• #include”user.h”
• int main(vod)
• {
• Int pid = fork();
• if(pid > 0){
• printf("parent: child=%d\n", pid);
• pid = wait();
• printf("child %d is done\n", pid);
• } else if(pid == 0){
• printf("child: exiting\n");
• exit();
• } else {
• printf("fork error\n");
• }
Output

parent: child=1234
child: exiting
I/O and File Descriptors
• A file descriptor is a small integer representing a
kernel-managed object that a process may read
from or write to.
• A process may obtain a file descriptor by opening
a file, directory, or device, or by creating a pipe, or
by duplicating an existing descriptor.
• By convention, a process reads from file
descriptor 0 (standard input), writes output to file
• descriptor 1 (standard output), and writes error
messages to file descriptor 2 (standard error).
read and write System Calls
• The read and write system calls read bytes from
and write bytes to open files named by file
descriptors.
• The call read(fd, buf, n) reads at most n bytes
from the file descriptor fd, copies them into buf,
and returns the number of bytes read.
• Each file descriptor that refers to a file has an
offset associated with it.
• The call write(fd, buf, n) writes n bytes from buf
to the file descriptor fd and returns the number
of bytes written.
To copy data from its standard input to its standard output
• #include”type.h”
• #include”stat.h”
• #include”user.h”
• int main(vod)
• {
• char buf[512];
• int n;
• for(;;){
• n = read(0, buf, sizeof buf);
• if(n == 0)
• break;
• if(n < 0){
• fprintf(2, "read error\n");
• exit();
• }
• if(write(1, buf, n) != n){
• fprintf(2, "write error\n");
• exit();
• }
• }
Review of Session-4

• xv6 Functions Case Study : getblk, brelse,


bread, bwrite.
getblk-Algorithm for Buffer alocation
brelse-Algorithm for Releasing Buffer
bread-Algorithm for reading a disk block
bwrite-Algorithm for writing a disk block
Exploring bio.c file

• It’s an xv6 file that consists of the code sheets


of all the algorithms of the topic Buffer
Cache.
Exploring bio.c
• LTC Activity on fork.c & io.c -in Breakout
Session

You might also like