Processes
Chapter 3
Objectives
Process Concept
Process Concept (Cont.)
Process in Memory
Memory Layout of a C Program
Process State
Diagram of Process State
Process Control Block (PCB)
Threads
Process Representation in Linux
Represented by the C structure task_struct�pid t_pid; /* process identifier */ �long state; /* state of the process */ �unsigned int time_slice /* scheduling information */ �struct task_struct *parent;/* this process’s parent */ �struct list_head children; /* this process’s children */ �struct files_struct *files;/* list of open files */ �struct mm_struct *mm; /* address space of this process */
Process Scheduling
Ready and Wait Queues
Representation of Process Scheduling
CPU Switch From Process to Process
Context Switch
Multitasking in Mobile Systems
Operations on Processes
Process Creation
Process Creation (Cont.)
A Tree of Processes in Linux
C Program Forking Separate Process
Creating a Separate Process via Windows API
Process Termination
Process Termination
pid = wait(&status);
Android Process Importance Hierarchy
Multiprocess Architecture – Chrome Browser
Interprocess Communication
Communications Models
Producer-Consumer Problem
IPC – Shared Memory
Cooperating Processes
Bounded-Buffer – Shared-Memory Solution
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Producer Process – Shared Memory
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;
}
Consumer Process – Shared Memory
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 */
}
What about Filling all the Buffers
Producer
while (true) {� /* produce an item in next produced */
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Race Condition
S0: producer execute register1 = counter {register1 = 5}�S1: producer execute register1 = register1 + 1 {register1 = 6} �S2: consumer execute register2 = counter {register2 = 5} �S3: consumer execute register2 = register2 – 1 {register2 = 4} �S4: producer execute counter = register1 {counter = 6 } �S5: consumer execute counter = register2 {counter = 4}
Race Condition (Cont.)
IPC – Message Passing
Message Passing (Cont.)
Implementation of Communication Link
Direct Communication
Indirect Communication
Indirect Communication (Cont.)
Indirect Communication (Cont.)
Synchronization
Producer-Consumer: Message Passing
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 */� }
Buffering
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
Examples of IPC Systems - POSIX
ftruncate(shm_fd, 4096);
IPC POSIX Producer
IPC POSIX Consumer
Examples of IPC Systems – Windows
Local Procedure Calls in Windows
Pipes
Ordinary Pipes
Named Pipes
Communications in Client-Server Systems
Sockets
Socket Communication
Sockets in Java
Sockets in Java – The equivalent Date client�
Remote Procedure Calls
Remote Procedure Calls (Cont.)
Execution of RPC
The End!