0% found this document useful (0 votes)
16 views

Operating System_1.1

Operating System

Uploaded by

Krishnendu Rarhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Operating System_1.1

Operating System

Uploaded by

Krishnendu Rarhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Unit I

Chapter 1

< Operating
/ System 22CSH-
} /> [
242 /
>
Presented by:
Dr. Krishnendu Rarhi

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


Course Objective
1. Understanding the OS Concepts
2. Operating System Design and Architecture
3. Process & Resource Management
4. Memory & Storage Management
5. Security & Protection

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


Course Outcome
After successfully completing this course, you will be
able to
1. Comprehend OS Concepts
2. Analyze OS Design
3. Apply Process and Resource Management
Techniques
4. Implement Memory and Storage Management
5. Evaluate OS Security

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


Resources

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


Syllabus
Introduction to Operating System: Computer System Organization & Architecture,
Operations of Operating System, Resource Management, Security & Protection, Distributed
Systems, Kernel Data Structures, Generation of Operating System (Tanenbaum). Virtualizing The
CPU, Virtualizing Memory, Concurrency, Persistence, Design Goals, Process Abstraction, Process
API, Process Creation, Process States.
Process Management: Process Concept, Process Scheduling, Operations on Processes,
Interprocess Communication, IPC in Shared-Memory Systems, IPC in Message-Passing Systems,
The fork() System Call, The wait() System Call, The exec() System Call, Motivating The API,
Process Control And Users, Limited Direct Execution, Restricted Operations, Switching Between
Processes, Worried About Concurrency.
Thread & Concurrency: Multicore Programming, Multithreading Models, Implicit Threading,
Threading Issues, Why It Gets Worse: Shared Data, The Heart Of The Problem: Uncontrolled
Scheduling, The Wish For Atomicity, One More Problem: Waiting For Another.

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</
>
Introduction 01
} /> [
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
Roadmap
Computer System Organization & Architecture

Operations of Operating System

Abstraction or Virtualization

Design Goals

1 0 1 1 0 1 1 0 1 1 0 1 1 0Krishnendu
0 1 1 0 Rarhi
1 1[E9621]]©
011 011 01 110110 110111 1101
APEX Institute of Technology
</ Operating System - Examples

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ What is Operating System? •• Application
System

An operating system is a System Program


that manages the hardware and software
resources of a computer. The operating
system is the foundation on which
Program>
applications run and provide a platform for
users to interact with the computer.

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ Computer Resources - Analogy

RAM Secondary Memory


Processor

I/O Users

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ Operating System - Analogy

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ Computer System Operations
• I/O devices and the CPU can execute concurrently
• Each device controller is in charge of a particular device type
• Each device controller has a local buffer
• CPU moves data from/to main memory to/from local buffers
• I/O is from the device to local buffer of controller
• Device controller informs CPU that it has finished its operation by causing an interrupt

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ Program Background
Compiler A compiler translates high level programs into an executable (“.c” to
“a.out”)

Executable The exe contains instructions that the CPU can understand, and data of the
program (all numbered with addresses)

Instructions Instructions run on CPU: hardware implements an instruction set


architecture (ISA)

CPU with Registers • Pointer to current instruction (Program Counter)

• Operands of instructions, memory addresses

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ Program Background

/> ** } /> [
To run an exe, CPU Cache
– fetches instruction pointed at by PC from Most recently used instructions and data
memory are in CPU caches for faster access
– loads data required by the instructions into
registers
– decodes and executes the instruction
– stores results to memory

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


Virtualization
● The OS takes a physical resource and transforms it into a virtual form of itself.
■ Physical resource: Processor, Memory, Disk …
○ The virtual form is more general, powerful and easy-to-use.
○ Sometimes, we refer to the OS as a virtual machine.

Dr. Krishnendu Rarhi


System call

● 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)

Dr. Krishnendu Rarhi


System call

● System call allows user to tell the OS what to do.


○ The OS provides some interface (APIs, standard library).
○ A typical OS exports a few hundred system calls.
■ Run programs
■ Access memory
■ Access devices

Dr. Krishnendu Rarhi


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
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
● Some of the System Programs are simply user interfaces to system calls; others
are considerably more complex
System Programs

● System programs provide a convenient environment for program development and


execution. They can be divided into:
○ File manipulation
■ Create, delete, copy, rename, print, dump, list, and generally manipulate files and
directories
○ Status information sometimes stored in a File modification
○ Programming language support
○ Program loading and execution
○ Communications
○ Background services
○ Application programs
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
■ 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
○ Programming language support
○ Program loading and execution
○ Communications
○ Background services
○ Application programs
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
■ 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
○ Background services
○ Application programs
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
■ 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
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
■ Don’t pertain to system
■ Run by users
■ Not typically considered part of OS
■ Launched by command line, mouse click, finger poke
Operating System Design & Implementation

● 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
</ What OS does?
Manages Program Memory (Loads program executable (code, data) from disk to
memory)

Manages CPU (Initializes program counter (PC) and other registers to begin execution)

Manages External Devices (Read/write files from disk.)

CPU with Registers • Pointer to current instruction (Program Counter)

• Operands of instructions, memory addresses

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


</ OS manages CPU
Process
Abstraction Illusion Timesharing
OS creates and manages Each process has the Timeshares CPU between
processes illusion of having the processes
complete CPU, i.e., OS
virtualizes CPU Enables coordination
between processes

A Running
Program

</>
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
Virtualizing the CPU
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <assert.h>
5 #include "common.h"
6
7 int
8 main(int argc, char *argv[])
9 {
10 if (argc != 2) {
11 fprintf(stderr, "usage: cpu <string>\n");
12 exit(1);
13 }
14 char *str = argv[1];
15 while (1) {
16 Spin(1); // Repeatedly checks the time and
returns once it has run for a second
17 printf("%s\n", str);
18 }
19 return 0;
20 }

Simple Example(cpu.c): Code That Loops


and Prints

Dr. Krishnendu Rarhi


Virtualizing the CPU (Cont.)

● Execution result 1. prompt> gcc -o cpu cpu.c -Wall


prompt> ./cpu "A"
A
A
A
ˆC
prompt>

Run forever; Only by pressing “Control-c” can we halt the


program

Dr. Krishnendu Rarhi


Virtualizing the CPU (Cont.)
● Execution result 2.
prompt> ./cpu A & ./cpu B & ./cpu C & ./cpu D &
[1] 7353
[2] 7354
[3] 7355
[4] 7356
A
B
D
C
A
B
D
C
A
C
B
D
...

Even though we have only one processor, all four of programs


seem to be running at the same time!

Dr. Krishnendu Rarhi


</ OS manages MEMORY
Memory Illusion Abstraction
OS manages the memory Each process thinks it has OS abstracts out the
of the process: code, data, a dedicated memory details of the actual
stack, heap etc space for itself, numbers placement in memory,
code and data starting translates from virtual
from 0 (virtual addresses) addresses to actual
physical addresses

</>
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
Virtualizing Memory
● A program that Accesses Memory (mem.c)
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "common.h"
5
6 int
7 main(int argc, char *argv[])
8 {
9 int *p = malloc(sizeof(int)); // a1: allocate some
memory
10 assert(p != NULL);
11 printf("(%d) address of p: %08x\n",
12 getpid(), (unsigned) p); // a2: print out the
address of the memmory
13 *p = 0; // a3: put zero into the first slot of the memory
14 while (1) {
15 Spin(1);
16 *p = *p + 1;
17 printf("(%d) p: %d\n", getpid(), *p); // a4
18 }
19 return 0;
20 }

Dr. Krishnendu Rarhi


Virtualizing Memory (Cont.)

● The output of the program mem.c


prompt> ./mem
(2134) memory address of p: 00200000
(2134) p: 1
(2134) p: 2
(2134) p: 3
(2134) p: 4
(2134) p: 5
ˆC
○ The newly allocated memory is at address 00200000.
○ It updates the value and prints out the result.

Dr. Krishnendu Rarhi


Virtualizing Memory (Cont.)

● Running mem.c multiple times


prompt> ./mem &; ./mem &
[1] 24113
[2] 24114
(24113) memory address of p: 00200000
(24114) memory address of p: 00200000
(24113) p: 1
(24114) p: 1
(24114) p: 2
(24113) p: 2
(24113) p: 3
(24114) p: 3
...

○ It is as if each running program has its own private memory.


■ Each running program has allocated memory at the same address.
■ Each seems to be updating the value at 00200000 independently.

Dr. Krishnendu Rarhi


Virtualizing Memory (Cont.)

● Each process accesses its own private virtual address space.


○ The OS maps address space onto the physical memory.
○ A memory reference within one running program does not affect the address space of other
processes.
○ Physical memory is a shared resource, managed by the OS.

Dr. Krishnendu Rarhi


</ OS manages DEVICES
Devices Drivers Filesystem
OS has code to manage Device driver talks the Persistent data organized
disk, network card, and language of the hardware as a filesystem on disk
other external devices: devices
device drivers – Issues instructions to
devices (fetch data from a
file)
– Responds to interrupt
events from devices (user
has pressed a key on
keyboard)

</>
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
The problem of Concurrency

● The OS is juggling many things at once, first running one process, then another, and so forth.

● Modern multi-threaded programs also exhibit the concurrency problem.

Dr. Krishnendu Rarhi


Concurrency Example
● A Multi-threaded Program (thread.c)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "common.h"
4
5 volatile int counter = 0;
6 int loops;
7
8 void *worker(void *arg) {
9 int i;
10 for (i = 0; i < loops; i++) {
11 counter++;
12 }
13 return NULL;
14 }
15
16 int
17 main(int argc, char *argv[])
18 {
19 if (argc != 2) {
20 fprintf(stderr, "usage: threads <value>\n");
21 exit(1);
22 }

Dr. Krishnendu Rarhi


Concurrency Example (Cont.)

23 loops = atoi(argv[1]);
24 pthread_t p1, p2;
25 printf("Initial value : %d\n", counter);
26
27 Pthread_create(&p1, NULL, worker, NULL);
28 Pthread_create(&p2, NULL, worker, NULL);
29 Pthread_join(p1, NULL);
30 Pthread_join(p2, NULL);
31 printf("Final value : %d\n", counter);
32 return 0;
33 }

○ The main program creates two threads.


■ Thread: a function running within the same memory space. Each thread
start running in a routine called worker().
■ worker(): increments a counter

Dr. Krishnendu Rarhi


Concurrency Example (Cont.)
● loops determines how many times each of the two workers will increment the shared
counter in a loop.
○ loops: 1000.
prompt> gcc -o thread thread.c -Wall -pthread
prompt> ./thread 1000
Initial value : 0
Final value : 2000

○ loops: 100000.

prompt> ./thread 100000


Initial value : 0
Final value : 143012 // huh??
prompt> ./thread 100000
Initial value : 0
Final value : 137298 // what the??

Dr. Krishnendu Rarhi


Why is this happening?
● Increment a shared counter  take three instructions.
1. Load the value of the counter from memory into register.
2. Increment it
3. Store it back into memory

● These three instructions do not execute atomically.  Problem of concurrency happen.

Dr. Krishnendu Rarhi


Persistence

● Devices such as DRAM store values in a volatile.


● Hardware and software are needed to store data persistently.
○ Hardware: I/O device such as a hard drive, solid-state drives(SSDs)
○ Software:
■ File system manages the disk.
■ File system is responsible for storing any files the user creates.

Dr. Krishnendu Rarhi


Persistence (Cont.)
● Create a file (/tmp/file) that contains the string “hello world”
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <assert.h>
4 #include <fcntl.h>
5 #include <sys/types.h>
6
7 int
8 main(int argc, char *argv[])
9 {
10 int fd = open("/tmp/file", O_WRONLY |
O_CREAT | O_TRUNC,
S_IRWXU);
11 assert(fd > -1);
12 int rc = write(fd, "hello world\n", 13);
13 assert(rc == 13);
14 close(fd);
15 return 0;
16 }

open(), write(), and close() system calls are routed to the part
of OS called the file system, which handles the requests

Dr. Krishnendu Rarhi


Persistence (Cont.)
● What OS does in order to write to disk?
○ Figure out where on disk this new data will reside
○ Issue I/O requests to the underlying storage device

● File system handles system crashes during write.


○ Journaling or copy-on-write
○ Carefully ordering writes to disk

Dr. Krishnendu Rarhi


</ Design Goals
Abstraction Efficiency Protection
Make the system • Efficiency of usage of Isolation between multiple
convenient and easy to CPU, memory, etc. processes
use. • Minimize the overhead
of the OS.
• OS must strive to
provide virtualization
without excessive
overhead.

“The one program running at all times on the computer” is the kernel.
</>
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
</ Design Goals
Reliability Other
High-degree of reliability • Energy Efficiency
by running the OS non- • Security
stop • Mobility

</>
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
Kernels

● Kernel is the main part of an Operating System. It is the first program that
is loaded after the boot loader whenever we start a system. The Kernel is
present in the memory until the Operating System is shut-down.
● Kernel provides an interface between the user and the hardware
components of the system. Whenever a process makes a request to the
Kernel, then it is called System Call.
Types of Kernels
● Functions of Kernel
• Process management
• Monolithic kernel.
• Access computer resources • Micro kernel.
• Device management • Hybrid kernel.
• Memory management • Nano kernel.
• Interrupt handling
• Exo kernel
• I/O communication
Reentrant Kernels

● Allows processes (or, more precisely, their corresponding kernel


threads) to give up the CPU.
● They have no effect on other processes entering kernel mode. Multiple
processor systems may be scheduled together in the case of single-
processor systems.
● Non-reentrant functions can still be executed by reentrant kernels
if locks are used to ensure that only one process can run the non-
reentrant function. Even though the current process is operating in
kernel mode, hardware interrupts can suspend it
Reentrant Kernels

Kernel Control Path: A set of instructions that the kernel executes in


order to handle a system call. Normally, instructions are executed in
order, but some activities force the CPU to interleave control routes. In
user mode, the following system call is made: The scheduler chooses a
new process to run and switches it on. On behalf of two separate
processes, two control pathways are executed.
Microkernel System Structure

● Moves as much from the kernel into user space


● Mach example of microkernel
○ Mac OS X kernel (Darwin) partly based on Mach
● 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
Microkernel System Structure
Modules
● Many modern operating systems implement loadable
kernel modules
○ Uses object-oriented approach
○ Each core component is separate
○ Each talks to the others over known interfaces
○ Each is loadable as needed within the kernel
● Overall, similar to layers but with more flexible
○ Linux, Solaris, etc
Monolithic Kernels
● Entire operating system works in the kernel space.
● The operating system is written as a collection of
procedures that are linked together into a single large
executable program.
● Each procedure in the system is free to call any other
process.
● Calling any procedure makes the system very efficient
● Every procedure is visible to every other procedure.
Monolithic Kernels
● Advantages
• The execution of this architecture is so fast.
• All the memory management, file management and process
scheduling is performed under one space.
• The process runs under single address space.
• This architecture is a single static binary file.
● Disadvantages
• If any service fails the entire system is failed.
• For adding any type of new service it must be modified by the
user.
Basics Micro Kernel Monolithic Kernel
Larger as OS and user both lie in the same
Size Smaller in
Monolithic Kernels address space.
Execution Slower Faster
Extendible Easily extendible Complex to extend
If the process/service crashes, the whole
If the service crashes then there is no
Security system crashes as both user and OS were in
effect on working on the microkernel.
the same address space.
More secure because only essential Susceptible to security vulnerabilities due to
Security
services run in kernel mode the amount of code running in kernel mode

More portable because most drivers and


Platform independence Less portable due to direct hardware access
services run in user space
Message passing between user-space
Communication Direct function calls within kernel
servers
Lower due to message passing and High due to direct function calls and less
Performance
more overhead overhead
Terms Monolithic Kernel microkernel

Definition A monolithic kernel is a type of kernel in operating systems A microkernel is a kernel type that
where the entire operating system works in the kernel space. provides low-level address space
Monolithic Kernels management, thread management,
and interprocess communication to
implement an operating system.
Address In a monolithic kernel, both user services and kernel services In microkernel user services and
space are kept in the same address space. kernel, services are kept in separate
address spaces.
Size The monolithic kernel is larger than the microkernel. The microkernel is smaller in size.

Execution It has fast execution. It has slow execution.


OS services In a monolithic kernel system, the kernel contains the OS In a microkernel-based system, the OS
services. services and kernel are separated.
Extendible The monolithic kernel is quite complicated to extend. The microkernel is easily extendible.

Customizatio It is difficult to add new functionalities to the monolithic It is easier to add new functionalities
n kernel. Therefore, it is not customizable. to the microkernel. Therefore, it is
more customizable.
Code Less coding is required to write a monolithic kernel. A microkernel is required more coding.

Example Linux, FreeBSD, OpenBSD, NetBSD, Microsoft Windows (95, QNX, Symbian, L4L.inux, Singularity,
98, Me), Solaries, HP-UX, DOS, OpenVMS, XTS-400, etc. K42, Mac OS X, Integrity, PikeOS,
HURD, Minix, and Coyotos.
OS History

Mini computer
PDP-11
Unix
Main frame computer Shell, pipe, signal
M360
Virtual Machine

1011 011 01 1011001 10 11011 011 01 110110 110111 1101


OS History

DOS, Windows, MAC Android, ios

1011 011 01 1011001 10 11011 011 01 110110 110111 1101

You might also like