PROCESS
UNIT 2
Arpita Shah 1
• A program under execution [instance of a
program]
Process = program + runtime activity
CODE / A set of instructions Operands / Data(s)
Processes & Thread
• Process is created ( via fork() or exec())
and then scheduled to run on one or more
CPU cores
• CPU scheduler decides when to run the
process
• Dispatcher starts the process
Process Termination
exit() is called
• User logs off
• Batch job issues Halt instruction
• Quit an application
• Error and fault conditions
• External signals (e.g. kill())
• OS data structure used to represent a process
internally.
• Blocks are stored in linked lists within the OS
Credentials
• Every process has six or more IDs associated
with it.
Real user ID
Real group ID Who we really are
Effective user ID
Effective group ID Used for file access permission
Supplementary group IDs checks
Saved set-user-ID
Saved set-group-ID Saved by exec function
Example: Process Representation
System Memory Process P2 State
init P0
Program counter
Kernel Process Table
…
P0: HW state; resources
Process P3 Memory base register
…
Process State (logical)
P2: HW state; resources
Open file table
Process P1
…
Pending Requests
PN: HW state; resources
Process P2 …
Memory mappings
When to Switch a Process
• Clock interrupt
– process has executed for the max. allowable time slice
• I/O interrupt / S/W Interrupt
• Memory (page) fault
– The address referenced is not in main memory so it must
be brought into main memory
• Trap
– error occurred
– may cause process to be moved to Exit state
• A system call which runs an OS routine
• e.g. fopen() (file open)
Switching Process
What is the primary purpose of context
switching in an operating system?
a) To save power
b) To increase CPU spееd
c) To allow for multitasking
d) To reduce memory uSSsage
Which of thе following еvеnts can triggеr
a contеxt switch?
a) Usеr input
b) Disk rеad/writе opеrations
c) Timеr intеrrupts
d) All of thе abovе
What arе thе different categories of
contеxt-switching triggеrs?
a) Intеrrupts
b) Multitasking
c) Usеr/Kеrnеl switch
Preemption
• Some processes utilize large chunks of CPU
time without performing I/O event
• CPU scheduler must share processor core(s) by
removing (preemptive) one process so other can
run
• Hardware device also produce event that OS
must handle, preempting running process
Process Scheduling Queues
• Job Queue
– Linked list of PCBs for all processes in the system
• Ready List (Ready Queue)
– Linked list of PCBs for processes in the ready queue
• Device Queue
– Linked list of PCBs for processes waiting on device
Processes migrates between ready queue, device
queue and CPU during scheduling and execution.
Same state
Thread
A thread of execution is the smallest
sequence of programmed instructions
that can be managed independently by
a scheduler. Also called LWP.
In process, threads executing
concurrently and sharing resources
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Global variable (data segment)
int global_var = 42;
// Uninitialized global variable (BSS)
int bss_var;
int main() {
int local_var = 10; // Stack
char *heap_var = malloc(100); // Heap
printf("PID: %d\n", getpid());
printf("Address of main() (Code Segment): %p\n", (void *)main);
printf("Address of global_var (Data Segment): %p\n", (void *)&global_var);
printf("Address of bss_var (BSS Segment): %p\n", (void *)&bss_var);
printf("Address of heap_var (Heap Segment): %p\n", (void *)heap_var);
printf("Address of local_var (Stack Segment): %p\n", (void *)&local_var);
printf("\nSleeping for 60 seconds... Check /proc/%d/maps\n", getpid());
sleep(60);
free(heap_var);
return 0;
}
Variable Type Segment Explanation
Stored in memory and
Global initialized
Data segment initialized before main()
variable
starts.
Although declared
Static local initialized inside a function, it
Data segment
variable retains value across
calls.
Allocated memory but
Global uninitialized
BSS segment initialized to zero
variable
automatically.
Static local Same behavior as
BSS segment
uninitialized variable global uninitialized.
Created when the
Local variable function is called,
Stack
(normal) destroyed when it
returns.
Managed manually
Dynamically allocated
Heap using malloc() and
memory (malloc)
free().
#include <stdio.h>
#include <stdlib.h>
int global_init = 1; // Data segment
int global_uninit; // BSS segment
void demo() {
static int static_init = 2; // Data segment
static int static_uninit; // BSS segment
int local_var = 3; // Stack
int *heap_var = malloc(100); // Heap (data), pointer is on stack
printf("global_init : %p (Data)\n", (void*)&global_init);
printf("global_uninit : %p (BSS)\n", (void*)&global_uninit);
printf("static_init : %p (Data)\n", (void*)&static_init);
printf("static_uninit : %p (BSS)\n", (void*)&static_uninit);
printf("local_var : %p (Stack)\n", (void*)&local_var);
printf("heap_var (data) : %p (Heap)\n", (void*)heap_var);
free(heap_var);
}
int main() {
demo();
return 0; // Stack
}
Context switch between two threads in the same process
Context switch between two threads in the different processes
Benefits of Threads
Threads
Less time to enhance
terminate a Switching efficiency in
Takes less thread than a between two communication
time to process threads takes between
create a less time than programs
new thread switching
than a between
process processes
System call – programmatic way in which a computer program requests a
service from a kernel; that acts as a bridge between a process and the kernel.
Services provided by the system calls
Processes & Thread
• Process is created ( via fork() or exec())
and then scheduled to run on one or more
CPU cores
• CPU scheduler decides when to run the
process
• Dispatcher starts the process
fork() and exec()
• A fork() system call is used to create a
new process, which is called child process
which run concurrently with the process
that make the fork() call by parent process.
pid_t pid = fork()
no parameters and returns integer values
GATE
A process execute the code
fork();
fork();
fork();
The total number of child process created is
(A) 3 (C) 7
(B) 4 (D) 8
exec
• A exec() system call is replace entire
process – include all threads
• The exec system call is used to execute a
file which residing in an active process.
So; when exec is called the previous
executable file replaced and new file is
executed.
• Exec is used when the user wants to lunch
a new file or program in the same process.
Standard names for functions
• execl
• execle
• execlp
• execv
• execve
• execvp
#include<unistd.h>
Working step(s) of exec
• Current process image overwritten by new
process image.
• New process image is that which you
passed as an argument [exec]
• The current running process is ended.
• New process image has same ID . Same
FD.
• CPU and VM stat is changed.
Process Termination
exit() is called
• User logs off
• Batch job issues Halt instruction
• Quit an application
• Error and fault conditions
• External signals (e.g. kill())