Unit 2
Unit 2
and Project
Management
(SEPM)
Process
• A program in execution,
Citations
Abraham Silberschatz, Peter Baer Galvin and Greg Gagne, “Operating
System Concepts”, 9th Edition, John Wiley and Sons Inc., 2012.
Department of Computer Science and Engineering
Difference between a program & process
PROGRAM PROCESS
I. Objective of Multiprogramming
Schedulers
• 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
• Long-term scheduler strives for good process mix
Medium-term scheduler
• Remove process from memory, store on disk, bring back in from disk to
continue execution: swapping
• process termination,
• Process may create several new processes via create process system call
during the course of execution.
• The creating process are called parent process and the new processes are
called the children of that process.
• Each of these new processes may in turn create other processes, forming a
tree of processes.
• Generally, process identified and managed via a process identifier
number (pid)
init
pid = 1
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
• Address space
• UNIX examples
• A process terminates when its finishes executing its final statement and asks
the operating system to delete it by using the exit() system call.
• At that point , the processes may return a status value (typically an integer) to
its parent process(via the wait() system call).
• All the resources of the process – including physical and virtual memory, open
files and I/O buffers – are de allocated by operating system
• Usually, such a system call can be invoked only by the parent of the
process that is to be terminated.
Parent may terminate the execution of one of its children for a variety of
reasons such as these:
• The parent is exiting and the operating systems does not allow a child to
continue if its parent terminates.
• 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);
Independent processes
Cooperative processes
• Any processes that shares data with other processes is a cooperative process.
• Shared memory
• Message passing
• Processes can then exchange information by reading and writing data to the
shared region.
• Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
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;
}
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
2. Bounded capacity– finite length of n messages Sender must wait if link full
• Each process must ask permission to enter critical section in entry section,
may follow critical section with exit section, then remainder section
do {
3.Bounded Waiting - A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has made
a request to enter its critical section and before that request is granted
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n processes
Department of Computer Science and Engineering
Semaphore
• Synchronization tool that provides more sophisticated ways (than Mutex locks)
for process to synchronize their activities.
• Semaphore S – integer variable
• Can only be accessed via two indivisible (atomic) operations
• wait() and signal()
• Originally called P() and V()
• Definition of the wait() operation
wait(S) {
while (S <= 0) C
; // busy wait
S--;
} E
• Definition of the signal() operation
signal(S) {
Department of Computer Science and Engineering
S++;
Semaphore Usage
• Counting semaphore – integer value can range over an unrestricted
domain
• Binary semaphore – integer value can range only between 0 and 1
• Same as a mutex lock
• Can solve various synchronization problems
• Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1 ;
signal(synch);
P2:
wait(synch);
S2 ;
• Can implement a counting semaphore S as a binary semaphore
Department of Computer Science and Engineering
Semaphore Implementation
• Must guarantee that no two processes can execute the wait() and
signal() on the same semaphore at the same time
• Thus, the implementation becomes the critical section problem where the
wait and signal code are placed in the critical section
• Could now have busy waiting in critical section implementation
• But implementation code is short
• Little busy waiting if critical section rarely occupied
• Note that applications may spend lots of time in critical sections and
therefore this is not a good solution
• Bounded-Buffer Problem
• Dining-Philosophers Problem
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
• What is the problem with this algorithm?
• Deadlock handling
• condition x, y;