79
PAN African e-Network Project PGDIT Operating Systems and Data Storage Semester - II Session - 2 Mr. Abhishek Singhal

Operating System & Data Storage Lec 2

Embed Size (px)

Citation preview

PAN African e-Network Project

PGDIT

Operating Systems and Data Storage

Semester - II

Session - 2

Mr. Abhishek Singhal

Topics to be covered

• System Calls• System Programs• System Structure• Virtual Machines• Process Concept• Process Scheduling• Operation on processes

Topics to be covered

• Cooperating Processes• Inter-process Communication• Back ground process

System Calls

• It provide the interface between a process and operating system.

• These calls are generally available as assembly language instructions.

• Systems calls are usually listed in the manuals used by assembly language programmers.

• Typically written in a high-level language (C or C++)

System Calls…

• Mostly accessed by programs via a high-level Application Program 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)

Example of System Calls[1]

• 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 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

System Call Implementation

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

API – System Call – OS Relationship[1]

Standard C Library Example• C program invoking printf() library call, which

calls write() system call

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

System Call Parameter Passing

– 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[1]

Types of System Calls

• Process control– End, abort– Load, execute– Create Process, terminate process– Get Process attributes, set process attributes– Wait for time– Wait event, signal event– Allocate and free memory

Types of System Calls…..

• File manipulation– Create file, delete file– Open, close– Read, write, reposition– Get file attributes, 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…..

• Information maintenance– Get time or data, set time or data– Get system data, set system data– Get process, file, or device attributes– Set process, file, or device attributes

• Communications– Create, delete communication connection– Send, receive messages– Transfer status information– Attach or detach remote devices

MS-DOS execution (Case Study) [1]

(a) At system startup (b) running a program

System Programs• System programs provide a convenient

environment for program development and execution.

• Some of them are simply user interfaces to system calls, whereas others are considerably more complex.

• The can be divided into following categories:– File manipulation

• These programs create, delete, copy, rename, print, dump, list and generally manipulate files and directories.

System Programs….– Status information

• Some programs simply ask the system for the date, time, amount of available memory or disk space, number of users, or similar status information.

• That information is then formatted, and is printed to the terminal or other output device or file

– File modification• Several text editors may be available to

create and modify the contents of files stored on disk or tape.

System Programs….– Programming language support

• Compilers, assemblers, and interpreters for common programming languages are often provided to the user with the operating system.

– Program loading and execution• Once a program is assembled or

compiled, it must be loaded into memory to be executed. The system may provide absolute loaders, re-locatable loaders, linkage editors, and overlay editors.

System Programs….– Communications

• These programs provide the mechanism for creating virtual connections among processes, users, and different computer systems.

– Application programs• Most OS are supplied with programs that are useful to

solve common problems, or to perform common operations. Such programs include web browsers, word processors and text formatters, spreadsheets, database systems, compilers and games.

• Most users’ view of the operation system is defined by system programs, not the actual system calls

Simple Structure

• MS-DOS – written to provide the most functionality in the least space– Because of the limited hardware on which it

ran, so it was not divided into modules– Although MS-DOS has some structure, its

interfaces and levels of functionality are not well separated

MS-DOS Layer Structure[1]

Layered Approach

• The operating system is divided into a number of layers (levels), each built on top of lower layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface.

• The main advantage of the layered approach is modularity.

• Layers are selected such that each uses functions (operations) and services of only lower-level layers

• This approach simplifies debugging and system verification.

Layered Operating System[1]

UNIX

• UNIX – limited by hardware functionality, the original UNIX operating system had limited structuring. The UNIX OS consists of two separable parts– Systems programs– The kernel

• Consists of everything below the system-call interface and above the physical hardware

• Provides the file system, CPU scheduling, memory management, and other operating-system functions; a large number of functions for one level

UNIX System Structure[1]

Virtual Machines• Layered approach is taken the to its logical

conclusion in the concept of a virtual machine. It treats hardware and the operating system kernel as though they were all hardware

• A virtual machine provides an interface identical to the underlying bare hardware

• The operating system creates the illusion of multiple processes, each executing on its own processor with its own (virtual) memory

Virtual Machines…

• The resources of the physical computer are shared to create the virtual machines– CPU scheduling can create the appearance

that users have their own processor– Spooling and a file system can provide virtual

card readers and virtual line printers– A normal user time-sharing terminal serves as

the virtual machine operator’s console

Virtual Machines….[1]

(a) Nonvirtual machine (b) virtual machine

Non-virtual Machine Virtual Machine

Virtual Machines….

• The virtual-machine concept provides complete protection of system resources since each virtual machine is isolated from all other virtual machines. This isolation, however, permits no direct sharing of resources.

• A virtual-machine system is a perfect vehicle for operating-systems research and development. System development is done on the virtual machine, instead of on a physical machine and so does not disrupt normal system operation.

• The virtual machine concept is difficult to implement due to the effort required to provide an exact duplicate to the underlying machine

The Java Virtual Machine[1]

Process

• A process also known as task is an instance of a program in execution.

• A program is a passive entity. On the other hand, a process is an active entity performing the intended functions of its related program

• Process is smallest unit of work.• Process can be scheduled independently

Components of Process

• Code– This refers to the set of instructions in machine

language that are loaded into RAM. The execution of these instructions will perform the intended functions of the related program

• Stack– This refers to the temporary data pertaining to the

process during its execution. Since the extent of this data is time-variant. Data is stored is on a stack. The stack is per process, which is created at the time of process creation.

Components of Process….

• Process Control Block– Process state– Program counter– CPU registers– CPU scheduling information– Memory management information– Accounting information– I/O status information

Process Control Block (PCB)….

Process in Memory[1]

Process State• As a process executes, it changes state. The state

of a process is defined in part by the current activity of that process. Each process may be in one of the following states:– new: The process is being created– running: Instructions are being executed– waiting: The process is waiting for some event

to occur– ready: The process is waiting to be assigned to

a process– terminated: The process has finished execution

Diagram of Process State[1]

Process Scheduling

• The objective of multiprogramming is to have some process running at all times to maximize CPU utilization.

• The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each program while it is running.

• For a uni-processor system, there will never be more than one running process.

• If there are more processes, rest will have to wait until the CPU is free and can be rescheduled.

Process Scheduling Queues

• Job queue – As processes enter the system, they are put into a job queue. set of all processes in the system

• Ready queue – set of all processes residing in main memory, ready and waiting to execute

• Device queues – set of processes waiting for a particular I/O device

• Processes migrate among the various queues

Ready Queue And Various I/O Device Queues[1]

Representation of Process Scheduling[1]

Schedulers

• A process migrates between the various scheduling queues throughout its life time. The operating system must select, for scheduling purposes, processes from these queues in some fashion. The selection process is carried out by the scheduler.

• There are two types of scheduler:• Long-term scheduler (or job scheduler)• Short-term scheduler (or CPU scheduler)

Long-term scheduler (or job scheduler)

• It selects processes from the pool of processes and loads them into memory for execution. It also brought into the ready queue

• Long term scheduler controls the degree of multiprogramming (the number of processes in the memory).

• Long term scheduler will invoke when a process leaves the system.

• It can afford to take more time to decide which process should be selected for execution.

Short-term scheduler (or CPU scheduler)

• Short term scheduler from among the processes that are ready to execute, and allocates the CPU to one of them.

• It selects which process should be executed next and allocates CPU.

• The short term scheduler must select a new process for the CPU quite frequently.

• Short term scheduler is very fast.

Addition of Medium Term Scheduling[1]

Schedulers…..

• Processes can be described as either:– I/O-bound process – spends more time

doing I/O than computations, many short CPU bursts

– CPU-bound process – spends more time doing computations; few very long CPU bursts

Context Switch• It refers to the process of transferring

control of the CPU from the currently running process to another process from ready queue.

• This occurs when running process requests some I/O or requests wait for some extent or the running process is to be pre-empted as per scheduling criteria.

Context Switch…..

• A context switching involves the following steps:• Save context of the running process in its PCB• Transfer the running process from run state to wait

state or ready state as per condition necessary switch.

• As per scheduling algorithm, choose a process from the ready queue to take control of the CPU, load context of the chosen process from its PCB.

• Transfer the chosen process from ready queue to run queue.

Operation on Processes

• The processes in the system can execute and must be created and deleted dynamically.

• Thus, Operating system must provide a mechanism for process creation and termination.

Process Creation

• A process may create several new processes, via a create process system call. During the course of execution.

• Creating process is called a parent process.• New processes are called the children of that

process.• Parent process create children processes,

which, in turn create other processes, forming a tree of processes

Process Creation….

• Resource sharing– Parent and children share all resources– Children share subset of parent’s resources– Parent and child share no resources

• Execution– Parent and children execute concurrently– Parent waits until children terminate

Process Creation…..

• Address space– Child duplicate of parent– Child has a program loaded into it

• UNIX examples– fork system call creates new process– exec system call used after a fork to replace

the process’ memory space with a new program

C Program Forking Separate Process[1]

int main(){Pid_t pid;

/* fork another process */pid = fork();if (pid < 0) { /* error occurred */

fprintf(stderr, "Fork Failed");exit(-1);

}else if (pid == 0) { /* child process */

execlp("/bin/ls", "ls", NULL);}else { /* parent process */

/* parent will wait for the child to complete */

wait (NULL);printf ("Child Complete");exit(0);

}}

Process Termination

• A process terminates when it finishes executing its last statement and asks the operating system to delete it by using the exit system call(exit)

• At that point, the process may return data (output) to its parent process (via wait system call)– Process’ resources are de-allocated by

operating system

Process Termination…

• A process can cause the termination of another process via a appropriate system call (abort)– Child has exceeded allocated resources– Task assigned to child is no longer required– If parent is exiting

• Some operating system do not allow child to continue if its parent terminates

–All children terminated - cascading termination

Cooperating Processes

• The concurrent processes executing in the operating system may be either independent processes or cooperating processes.

• A process is Independent process if it cannot affect or be affected by the other processes executing in the system.

• A process is Cooperating process if it can affect or be affected by the other processes executing in the system.

Cooperating Processes…

• Advantages of process cooperation– Information sharing – Computation speed-up– Modularity– Convenience

Producer-Consumer Problem• It is common Paradigm for cooperating

processes• A producer process produces

information that is consumed by a consumer process– unbounded-buffer places no practical

limit on the size of the buffer– bounded-buffer assumes that there is

a fixed buffer size

Bounded-Buffer – Shared-Memory Solution[1]

• Shared data#define BUFFER_SIZE 10Typedef struct {

. . .} item;

item buffer[BUFFER_SIZE];int in = 0;int out = 0;

• Solution is correct, but can only use BUFFER_SIZE-1 elements

Bounded-Buffer – Insert() Method

while (true) { /* Produce an item */

while (((in = (in + 1) % BUFFER SIZE count) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE;

{

Bounded Buffer – Remove() Methodwhile (true) {

while (in == out) ; // do nothing -- nothing to

consume

// remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE;return item;

{

Interprocess Communication (IPC)• Operating system provides the means for

cooperating processes to communicate with each other via an inter-process communication.

• IPC provides a mechanism for processes to communicate and to synchronize their actions

• Message system – processes communicate with each other without resorting to shared variables

• IPC facility provides at least two operations:– send(message) – message size fixed or

variable – receive(message)

Inter-process Communication (IPC)…

• If P and Q wish to communicate, they need to:– establish a communication link between them– exchange messages via send/receive

• Implementation of communication link– physical (e.g., shared memory, hardware bus)– logical (e.g., logical properties)

Implementation Questions[1]

• How are links established?• Can a link be associated with more than two

processes?• How many links can there be between every pair

of communicating processes?• What is the capacity of a link?• Is the size of a message that the link can

accommodate fixed or variable?• Is a link unidirectional or bi-directional?

IPC

• In addition, there are several methods for logically implementing a link and the send/receive operations:– Direct or indirect communication– Symmetric or asymmetric communication– Automatic or explicit buffering– Send by copy or send by reference– Fixed-sized or variable-sized message

Naming

• Processes that want to communicate must have a way to refer to each other.

• They can use either direct communication or indirect communication

Direct Communication

• Each process that wants to communicate must explicitly name the recipient or sender of the communication.

• They are defined as follows:– send (P, message) – send a message to

process P– receive(Q, message) – receive a message

from process Q

Direct Communication

• A communication link must have the following properties:– Links are established automatically between

every pair of processes.– A link is associated with exactly two

communicating processes– Between each pair of processes, there exists

exactly one link– The link may be unidirectional, but is usually bi-

directional

Indirect Communication• Messages are directed and received from mailboxes

(also referred to as ports)– Each mailbox has a unique id– Processes can communicate only if they share a

mailbox• Properties of communication link

– A link is established between a pair of processes only if processes share a common mailbox

– A link may be associated with many processes– Each pair of processes may share several

communication links– Link may be unidirectional or bi-directional

Indirect Communication• Operations

– create a new mailbox– send and receive messages through mailbox– destroy a mailbox

• Primitives are defined as:send(A, message) – send a message to mailbox Areceive(A, message) – receive a message from mailbox A

Indirect Communication

• Mailbox sharing– P1, P2, and P3 share mailbox A– P1, sends; P2 and P3 receive– Who gets the message?

• Solutions– Allow a link to be associated with at most two processes– Allow only one process at a time to execute a receive

operation– Allow the system to select arbitrarily the receiver.

Sender is notified who the receiver was.

Buffering

• Queue of messages attached to the link; implemented in one of three ways– Zero capacity – The queue has maximum length

0; i.e. 0 messagesSender must wait for receiver (rendezvous)

– Bounded capacity – The queue has finite length; finite length of n messages Sender must wait if link full

– Unbounded capacity – The queue has potentially infinite; infinite length Sender never waits

Client-Server Communication

• Sockets• Remote Procedure Calls• Remote Method Invocation (Java)

Text Books

• Operating Systems Concepts, Silberschatz Galvin, Fifth Edition Addition Wesley Publication.

• Modern Operating Systems, A S Tanenbaum, Prentice Hall of India New Delhi, 1995.

Reference books

• Design of UNIX Operating System, Maurice J. Bauch, Prentice Hall of India.

• Operating Systems Design, Peterson & Galvin

References

• Operating Systems Concepts, Silberschatz Galvin, Fifth Edition Addition Wesley Publication.

• Modern Operating Systems, A S Tanenbaum, Prentice Hall of India New Delhi, 1995.

•  Design of UNIX Operating System, Maurice J. Bauch, Prentice Hall of India.

• Operating Systems Design, Peterson & Galvin• Operating Systems:Internals and Design

Principles, 6/E William Stallings, Prentice Hall

Thank You

Please forward your query To: [email protected]: [email protected]