0% found this document useful (0 votes)
7 views49 pages

Lecture 5

Uploaded by

smilingpersonme
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)
7 views49 pages

Lecture 5

Uploaded by

smilingpersonme
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/ 49

1

Lahore Garrison University


CSC523-Operating System
MCS Week-3 Lecture-5
Semester 2 Spring 2019
2
Instructor Contact Details

 Name: Sabreena Nawaz


 Course Instructor: CSC351- Operating System
 Credit Hours: 5
 Office Location: CS-Female Staff Room
 Email: [email protected]
 Visiting Hours: Wednesday (9-11 am)
 Lab Work: Video Tutorials & Supervision by Instructor

Lahore Garrison University


3
Course Objectives

 To learn the fundamentals of Operating Systems.


 To learn the mechanisms of OS to handle processes and
threads and their communication
 To learn the mechanisms involved in memory
management in contemporary OS.
 To gain knowledge on distributed operating
system concepts that includes architecture,
Mutual exclusion algorithms, deadlock detection
algorithms and agreement protocolsUnderstand
how to read C++ doc library documentation and
reuse library code.
 To learn programmatically to implement simple
OS mechanisms.

Lahore Garrison University


4
Course Contents

 Following contents will be covered throughout the


semester in week wise lectures:
 OS basics, OS types, system calls, process management,
inter process communication, communication in client
server systems, Thread concepts, thread types, thread
control block, thread designs, CPU scheduling, process
synchronization, bakery algorithm, hardware solutions to
synchronization, Problems of synchronization, deadlock
occurrence, prevention, avoidance recovery, Memory
management, virtual memory, page faults, replacement
algorithms, operating system security.

Lahore Garrison University


5
Text Book

 Operating system concepts by


Abraham Silberschatz, Galvin,
Gagne, 9th edition

Lahore Garrison University


6

Process concept
►Queues
Preamble ►Schedulers
(Past lesson
brief)

Lahore Garrison University


7
Chapter 3: Processes

 Process Concept
 Process Scheduling
 Operations on Processes
 Interprocess Communication
 Examples of IPC Systems
 Communication in Client-Server Systems

Lahore Garrison University


8
Learning Outcomes

 To introduce the notion of a process -- a program in


execution, which forms the basis of all computation
 To describe the various features of processes,
including scheduling, creation and termination, and
communication

Lahore Garrison University


9
Operations on Processes

 System must provide mechanisms for:


 process creation,
 process termination,
 and so on as detailed next

Lahore Garrison University


1
Process Creation 0

 Parent process create children processes, which, in


turn create other processes, forming a tree of processes
 Generally, process identified and managed via a
process identifier (pid)
 Resource sharing options
 Parent and children share all resources
 Children share subset of parent’s resources
 Parent and child share no resources
 Execution options
 Parent and children execute concurrently
 Parent waits until children terminate
Lahore Garrison University
1
A Tree of Processes in Linux 1

Lahore Garrison University


1
2
Process Creation (Cont.)

 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 proce
memory space with a new program

Lahore Garrison University


1
C Program Forking Separate 3
Process

Lahore Garrison University


1
Creating a Separate Process via Windows 4API

Lahore Garrison University


1
Process Termination 5

 Process executes last statement and then asks the


operating system to delete it using the exit() system
call.
 Returns status data from child to parent (via wait())
 Process’ resources are deallocated by operating system
 Parent may terminate the execution of children processes
using the abort() system call. Some reasons for doing
so:
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 The parent is exiting and the operating systems does not
allow a child to continue if its parent terminates
Lahore Garrison University
1
Process Termination 6

 Some operating systems do not allow child to exists if its parent has
terminated. If a process terminates, then all its children must also
be terminated.
 cascading termination. All children, grandchildren, etc. are
terminated.
 The termination is initiated by the operating system.
 The parent process may wait for termination of a child process by
using the wait()system call. The call returns status information
and the pid of the terminated process
pid = wait(&status);
 If no parent waiting (did not invoke wait()) process is a zombie
(a zombie process or defunct process is a process that has
completed execution but still has an entry in the process table)
 If parent terminated without invoking wait , process is an orphan
Lahore Garrison University
1
Multiprocess Architecture – Chrome 7
Browser

 Many web browsers ran as single process (some still do)


 If one web site causes trouble, entire browser can hang or crash
 Google Chrome Browser is multiprocess with 3 different types of
processes:
 Browser process manages user interface, disk and network I/O
 Renderer process renders web pages, deals with HTML,
Javascript. A new renderer created for each website opened
 Plug-in process for each type of plug-in

Lahore Garrison University


1
Cooperating Processes 8

 Independent process cannot affect or be affected by the


execution of another process
 Cooperating process can affect or be affected by the
execution of another process
 Advantages of process cooperation
 Information sharing
 Computation speed-up
 Modularity
 Convenience

Lahore Garrison University


1
Cooperating 9
Processes(Cont…)

 Information sharing:
 Since several users may be interested in the same piece of
information (for instance, a shared file) we must provide an
environment to allow concurrent users to access these types of
resources.
 Computation speed up:
 If we want a particular task to run faster, we must break it into
subtasks each of which will be running in parallel with the
others. Such a speedup can be obtained only if the computer
has multiple processing elements (such as CPU’s or I/O
channels).

Lahore Garrison University


2
Cooperating 0
Processes(Cont…)

 Modularity:
 We may want to construct the system in a modular
fashion, dividing the system functions into separate
processes or threads.
 Convenience:
 Even an individual user may have many tasks on which
to work at one time. For instance, a user may be
editing, printing, and compiling in parallel.

Lahore Garrison University


2
Producer-Consumer Problem 1

 Paradigm for cooperating processes, 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

Lahore Garrison University


2
2
Bounded Buffer Problem
Empty
Pool

Producer
Producer Consumer
Consumer

Full Pool
Lahore Garrison University
2
3
Bounded-Buffer – Shared-Memory Solution

 Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;

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

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


Lahore Garrison University
2
Bounded-Buffer – Producer 4

item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}

Lahore Garrison University


2
Bounded Buffer – Consumer 5

item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;

/* consume the item in next consumed */


}

Lahore Garrison University


2
Inter process Communication 6

 Processes within a system may be independent or


cooperating
 Cooperating process can affect or be affected by other
processes, including sharing data
 Reasons for cooperating processes:
 Information sharing
 Computation speedup
 Modularity
 Convenience

Lahore Garrison University


2
Inter process 7
Communication

 Cooperating processes need interprocess


communication (IPC)
 Two models of IPC
 Shared memory
 Message passing

Lahore Garrison University


2
Communications Models 8

(a) Message passing. (b) shared memory.

Lahore Garrison University


Inter process Communication –29
Shared Memory

 An area of memory shared among the processes


that wish to communicate
 The communication is under the control of the users
processes not the operating system.
 Major issues is to provide mechanism that will allow
the user processes to synchronize their actions when
they access shared memory.
 Synchronization is discussed in great details in
Chapter 5.

Lahore Garrison University


Inter process Communication 3–
0
Message Passing

 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 two operations:


 send(message)
 receive(message)

 The message size is either fixed or variable

Lahore Garrison University


3
Message Passing (Cont.) 1

 If processes P and Q wish to communicate, they need to:


 Establish a communication link between them
 Exchange messages via send/receive
 Implementation issues:
 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?
 IsUniversity
Lahore Garrison a link unidirectional or bi-directional?
3
Message Passing (Cont.) 2

 Implementation of communication link


 Physical:
 Shared memory
 Hardware bus
 Network
 Logical:
 Direct or indirect
 Synchronous or asynchronous
 Automatic or explicit buffering

Lahore Garrison University


3
Direct Communication 3

 Processes must name each other explicitly:


 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process Q
 Properties of communication link
 Links are established automatically
 A link is associated with exactly one pair of communicating
processes
 Between each pair there exists exactly one link
 The link may be unidirectional, but is usually bi-directional

Lahore Garrison University


3
4
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
 Link established 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

Lahore Garrison University


3
Indirect Communication 5

 Operations
 create a new mailbox (port)
 send and receive messages through mailbox
 destroy a mailbox
 Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A

Lahore Garrison University


3
Indirect Communication 6

 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.

Lahore Garrison University


3
Synchronization 7

 Message passing may be either blocking or non-blocking


 Blocking is considered synchronous
 Blocking send -- the sender is blocked until the message is
received
 Blocking receive -- the receiver is blocked until a message is
available
 Non-blocking is considered asynchronous
 Non-blocking send -- the sender sends the message and continue
 Non-blocking receive -- the receiver receives:
 A valid message, or
 Null message
 Different combinations possible
 If both send and receive are blocking, we have a rendezvous
Lahore Garrison University
3
Synchronization (Cont.) 8

 Producer-consumer becomes trivial

message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);

/* consume the item in next consumed */


}
Lahore Garrison University
3
Buffering 9

 Queue of messages attached to the link.


 implemented in one of three ways
1. Zero capacity – no messages are queued on a link.
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits

Lahore Garrison University


4
Examples of IPC Systems - 0
POSIX

 POSIX Shared Memory


 Process first creates shared memory segment
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
 Also used to open an existing segment to share it
 Set the size of the object
ftruncate(shm fd, 4096);
 Now the process could write to the shared memory
sprintf(shared memory, "Writing to shared memory");

Lahore Garrison University


4
Examples of IPC Systems - 1
Mach

 Mach communication is message based


 Even system calls are messages
 Each task gets two mailboxes at creation- Kernel and Notify
 Only three system calls needed for message transfer
msg_send(), msg_receive(), msg_rpc()
 Mailboxes needed for commuication, created via
port_allocate()
 Send and receive are flexible, for example four options if mailbox full:
 Wait indefinitely
 Wait at most n milliseconds
 Return immediately
Lahore Garrison 
University
Temporarily cache a message
4
2
Examples of IPC Systems – Windows

 Message-passing centric via advanced local


procedure call (LPC) facility
 Only works between processes on the same system
 Uses ports (like mailboxes) to establish and maintain
communication channels
 Communication works as follows:
 The client opens a handle to the subsystem’s connection
port object.
 The client sends a connection request.
 The server creates two private communication ports and
returns the handle to one of them to the client.
 The client and server use the corresponding port handle to
send messages or callbacks and to listen for replies.
Lahore Garrison University
4
Local Procedure Calls in Windows
3

Lahore Garrison University


4
4
Communications in Client-Server Systems

 Sockets
 Remote Procedure Calls
 Pipes
 Remote Method Invocation (Java)

Lahore Garrison University


4
Sockets 5

 A socket is defined as an endpoint for communication

 Concatenation of IP address and port – a number included at


start of message packet to differentiate network services on a
host

 The socket 161.25.19.8:1625 refers to port 1625 on host


161.25.19.8

 Communication consists between a pair of sockets

 All ports below 1024 are well known, used for standard services

 Special IP address 127.0.0.1 (loopback) to refer to system on


which process is running
Lahore Garrison University
4
Socket Communication 6

Lahore Garrison University


4
7

Q&A

Lahore Garrison University


4
8
Reference

 To cover this topics , different reference material has


been used for consultation.
 Operating systems concept by Abraham siberchatz
edition 9
 Tutorialspoint.com
 Google.com

Lahore Garrison University


4
9

Thank you 

Lahore Garrison University

You might also like