Operating System
Operating System
– a program in execution
– process execution progress in sequential fashion
• program vs process
– Program - Passive and on disk
– Process - Active and in memory
int global1 = 0;
int global2 = 0; Main Memory
void DoSomething() local 1
{ local 2 5 Stack
int local2 = 5;
CPU Registers
local2 = local2 + 1;
...
}
CPU Registers
Kernel
local2 = local2 + 1; Address
...
Space
}
global 1
int main()
global 2 Data
{
char local1[10]; .start main
.call DoSomething Text
DoSomething(); …
...
}
• Processes are identified using unique process identifier (pid) : integer number
• each process has one parent but zero, one, two, or more children
• Execution : 2 possibilities
– Parent and children execute concurrently.
– Parent waits until some or all its children terminate.
• 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.
– ps command can be used to list processes
• When the OS creates a process at the explicit request of another process, the action
is referred to as process spawning
Data Resources
Text
Stack
PCB File
ret = fork();
switch(ret)
{
case -1:
perror(“fork”);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
UNIX
<code for parent >
wait(0);
}
Data Resources
Text
Stack
Process Status File
• Process executes last statement and asks the operating system to decide it (exit).
– Output data from child to parent (via wait).
– Process’ resources are deallocated by operating system.
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *status, int options);
• By default, waitpid() waits only for terminated children, but this behaviour is
modifiable via the options argument, as described below.
• The value of pid can be:
– < -1 : Wait for any child process whose process group ID is equal to the absolute
value of pid.
– -1 : Wait for any child process.
– 0 : Wait for any child process whose process group ID is equal to that of the
calling process.
– > 0 : Wait for the child whose process ID is equal to the value of pid.
Mar 14 - 22, 2022 DAC , Operating System - Process 28
Types of Process
• Parent Process
• Child process
• Foreground process
• Background process
• Interactive process
• Non-interactive process
• Zombie process
• Orphan process
• Daemon Process
char * argv[ ] = {“/bin/ls”, 0}; cpid = 26 cpid = 0 char * argv[ ] = {“/bin/ls”, 0};
<…> <…>
int cpid = fork( ); int cpid = fork( );
if (cpid = = 0) { if (cpid = = 0) {
execv(argv[0], argv); execv(argv[0], argv);
exit(0); exit(0);
} }
<parent code> <parent code>
wait(&cpid); wait(&cpid);
Data Resources
Text
Stack
PCB File
Same as execv, but takes the arguments of the new program as a list, not a vector:
Example:
execl(“/bin/ls”, “/bin/ls”, “-l”, 0);
Is equivalent to
Note the NULL string at the end
char * argv[] = {“/bin/ls”, “-l”, 0};
execv(argv[0], argv);
execl is mainly used when the number of arguments is known in advance
execvp
o Searches for the program name in the PATH environment
execl
o Program arguments passed directly as a list
execlp
o Searches for the program name in the PATH environment
Example
int main()
{
system(“echo Hello world”);
}
• The system call is the means by which a process requests a specific operating
system service.
• Dispatch latency
– Time taken by dispatcher to stop on
process and start execution of the other
process
Medium Term
Scheduler
Short Term Scheduler
• Preemptive scheduling
– An interrupt occurs
– When new processes become ready with higher priority
• Turnaround time – amount of time taken between submission of program to execute and
return of the output.
• Response time – amount of time it takes from when a request was submitted until the first
response is produced, not output (for time-sharing environment)
• Arrival Time - time when a process enters into the ready state and is ready for its execution
P1 0
P2 7
P3 10
P4 14
Average 7.75
• Non pre-emptive
– once CPU given to the process it cannot be preempted until completes its CPU
burst
• Pre-emptive
– if a new process arrives with CPU burst length less than remaining time of current
executing process, preempt. This scheme is known as the Shortest-Remaining-
Time-First (SRTF)
P1 7 13
P2 3 0
P3 4 3
P4 6 7
Avg: 5.75
P2 P3 P4 P1
0 3 7 13 20
Mar 14 - 22, 2022 DAC , Operating System - Scheduling 13
SRTF
P1 0.0 7 9
P2 2.0 4 1
P3 4.0 1 0
P4 5.0 4 2
Avg : 3
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
0 3 5 8 11 14 16 19 22 25 28 29 32 33 36 39 42 45
P2 15 4 29 14 10
P3 20 12 45 25 13
P4 5 8 16 11 3
P5 25 6 39 14 8
Request Queue Avg: 87/5 Avg: 47/5
=17.4 =9.4
P4 P4 P1 P4 P1 P2 P1 P3 P2 P5 P1 P3 P5 P3 P3
5 8 10 11 14 15 19 20 22 25 25 28 32 36 42
Mar 14 - 22, 2022 DAC , Operating System - Scheduling 17
Basics of computer memory
Need for Memory Management
Concepts
Address, Address space
Address Translation
Address Binding
Loading, Linking
Swapping
Memory Allocation
Continuous/Contiguous Allocation
o Fixed/Static Partitioning
o Internal and External Fragmentation, Compaction
o Variable/Dynamic Partitioning
o Memory Allocation Techniques
First Fit
Best Fit
Worst Fit
Address: 2k
k bits
SECONDARY
CPU RAM MEMORY
CACHE
HARD DISKS
0x000…
Processor Memory
translator
Registers
0xFFF…
Challenges:
Memory Allocation
Memory Protection
Garbage Collection
IO Support
Swapping, fragmentation and compaction
CONTIGUOUS NON-CONTIGUOUS
PAGING
FIXED VARIABLE
PARTITION PARTITION SEGMENTATION
SCHEME SCHEME
P5 – 8MB OS
4MB
P4 – 4MB 8MB
P4 – 4MB
P1 – 4MB 4 MB
7 MB
P2 – 7MB
P3 – 16MB 16 MB
P4 – 9MB 9 MB
P5 – 4MB 4 MB
P1 – 4MB 4 MB
7 MB
P2 – 7MB
P3 – 16MB 16 MB
P4 – 9MB 9 MB
P5 – 4MB 4 MB
Can we P6 – 8MB 4 MB
allocate space
for P6?
7 MB
P2 – 7MB
P3 – 16MB 164MB
P4 – 9MB 9 MB
External
Fragmentation
4 MB
Disadvantages
◦ Difficult Implementation: it involves allocation of memory during
run-time rather than during system configure.
◦ External Fragmentation: There will be external fragmentation inspite
of absence of internal fragmentation.
First fit
Best fit
Worst fit
Advantages
o It is fast in processing.
Disadvantages
o Wastage of memory - smaller processes may be allocated larger
spaces resulting in in-efficiency
P8
P3 – 25MB
20MB
P9
10MB
Advantages
o Memory efficient - allocates the process to the minimum possible
space available in the memory
o Minimal internal fragmentation
Disadvantages
o Slow Process - Checking the whole memory for each job makes
the working of the operating system very slow.
P1- 16 MB 25MB
8MB
15MB
P14
P2 – 100M
16MB B
15 MB P22
P3 – 8MB
20MB
P9
10MB
15-03-22 Prachi Pandey, C-DAC Bangalore 36
Worst Fit Memory Allocation
The process traverses the whole memory and always search for the
largest hole/partition, and then the process is placed in that
hole/partition.
Advantages
o Since this process chooses the largest hole/partition, therefore
there will be large internal fragmentation. This internal
fragmentation is big enough for other small processes to be
allocated memory.
Disadvantages
o Slow Process - Checking the whole memory for each job makes
the working of the operating system very slow.
25MB
P1- 15MB
P14
15 MB
P2 – 16MB 100MB
16 MB
8MB
P22
P3 – 8MB 20MB
P9
10MB
OS
P1
4 KB
4 KB
6 KB
6 KB
2 KB
12 KB
2 KB
Main Memory
Process
Frame 1 2 KB
Process
Frame 2 2 KB
Page 1 2 KB
2 KB
Page 2 2 KB
Page 3 2 KB
6 KB
Frame n 2 KB
Main Memory
Frame 0 0 1
Process Bytes 2 3
Frame 1
Page 0 0 1 MM size = 16 B 4 5
Frame 2
Frame size = 2 B
No. of frames = 8 Frame 3 6 7
Page 1 2 3
Frame 4 8 9
4B
Frame 5 10 11
Process size = 4 B
Page size = 2 B 12 13
Frame 6
No. of pages = 2
Frame 7 14 15
0 0 1
1 2 3
Process P1
2 4 5
Page 0 0 1
2 3 3 6 7
Page 1
4 8 9
5 10 11
6 12 13
7 14 15
0 0 1
1 2 3 Page 0
Process P1
2 4 5
Page 0 0 1
2 3 3 6 7
Page 1
4 8 9
5 10 11
6 12 13
7 14 15
0 0 1
1 2 3 Page 0
Process P1
2 4 5
Page 0 0 1
2 3 3 6 7
Page 1
4 8 9
5 10 11
6 12 13
7 14 15
0 0 1
1 2 3 Page 0
Process P1
2 4 5
Page 0 0 1
3 6 7 Page 1
Page 1 2 3
4 8 9
5 10 11
6 12 13
7 14 15
3 6 7 Page 1
Translate byte 3 of
process memory to 4 8 9
byte 7 of MM
5 10 11
6 12 13
Mapping
7 14 15
MMU
3 6 7 Page 1
Page table of P1 4 8 9
5 10 11
Page 0 Frame 1
6 12 13
Page 1 Frame 3 7 14 15
p d
m-n n
Dirty Bit /
Valid (1) / Protection
Frame No. Reference Caching Modified
Invalid (0) (rwx)
bit
• Disadvantages:
– Page address mapping hardware usually increases cost of computer and also
slows down processor
– Memory is used to store PMT; processor time (overhead) must be expended
to maintain and update these tables
– Though external fragmentation is eliminated, Internal Fragmentation / Page
Breakage does occur.
Prachi Pandey
C-DAC Bangalore
[email protected]
Topics
• Virtual Memory
Demand Paging
Page Faults
Page Replacement algorithms
o FIFO
o LRU
o Optimal
0 0 0 0 4 4
0 2 1 6 4 0 0
2 2 2 2
1 1 1 1
6 6 6
M M M M M M
1 0 4 3 4 1 4 2 2 1 2
4
0 0 0
0 0
0
3 3 3
1 3
1
1 1 1
6 6
6
H M M M H
H
f2 0 0 0 0 3 3 3 2 2 2 2 1 1 1
f1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0
MMMMH M M M MM M H M M H
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frames (3 pages can be in memory 1 1 4 5
at a time per process):
2 2 1 3 9 page faults
3 3 2 4
• 4 frames: 1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
0 0 0 0 4 4
0 2 1 6 4 0 0
2 2 2 2
1 1 1 1
6 6 6
M M M M M M
1 0 4 3 4 1 4 2 2 1 2
4
0 0 0
0 0
0
1 1 1
1 1
1
3 3 3
6 3
6
H M H M H
H
0 0 0 0 0 0
0 2 1 6 4 0 2
2 2 2 2
1 1 1 1
6 4 4
M M M M M H
1 0 0 3 3 1 3 2 3 1 3
0
2 2 2
2 2
2
1 1 1
1 1
1
4 4 4
4 4
4
H M H H H
H
Deepika H V
C-DAC Bengaluru
([email protected])
Agenda
• What is a Deadlock?
• Why does it happen?
• How can you detect deadlock happen?
• Consequences
• Dealing with deadlocks
• Semaphores
• Mutex
• Producer consumer problem
• Deadlock vs Starvation
• Definition
– When a waiting process is never again able to change state because the resource
requested is held by other waiting process. This situation is Deadlock
• Resources in a system
– CPU cycles ; I/O devices ( printers, tape drives) ; database (tables)
– Two types – preemptable and non-premptable
• Resources :
• Process:
P1 P2 P1 P2
r1 r2
P3 P3
(A) (B) (C) (D)
• Handling Deadlocks
– Ignorance
– Prevention
– Avoidance
– Detection and recovery
• Ostrich Algorithm
– Pretend there’s no problem
– Reasonable if
• Deadlocks occur very rarely
• Cost of prevention is high
– UNIX and Windows take this approach
• Resources (memory, CPU, disk space) are plentiful
• Deadlocks over such resources rarely occur
• Deadlocks typically handled by rebooting
– Trade off between convenience and correctness
• Ensure that at least one of the conditions for deadlock never occurs
– Mutual exclusion
– No preemption
– Hold & wait
– Circular wait
sort
data print
Copy
data
data
4.
P3
5.
• Mutual exclusion
– Spool everything
• Hold and wait
– Request all resources initially
• No preemption
– Take resources away
• Circular wait
– Order resources numerically
• At Time T0 A
– Total Resources
• A : 10
• B:5
B
• C:7
• At Time T0 A
– Total Resources
• A : 10
• B:5
• C:7
• At Time T6 B
– Resource Avail
• A:3
• B:3
C
• C:2
3 3 2
- 1 2 2
Rem 2 1 0
Complete P1
Rem 2 1 0
Ret + 3 2 2
Avail 5 3 2
Mar 14 - 22, 2022 DAC , Operating System - Deadlock 22
6 0 0
5 3 2
- 0 1 1
Rem 5 2 1
Complete P3
Rem 5 2 1
Ret + 2 2 2
Avail 7 4 3
Mar 14 - 22, 2022 DAC , Operating System - Deadlock 24
4 3 1
7 4 3
- 4 3 1
Rem 3 1 2
Complete P4
Rem 3 1 2
Ret + 4 3 3
Avail 7 4 5
7 4 5
- 7 4 3
Rem 0 0 2
Complete P0
Rem 0 0 2
Ret + 7 5 3
Avail 7 5 5
7 5 5
- 6 0 0
Rem 1 5 5
Complete P2
Rem 1 5 5
Ret + 9 0 2
Avail 10 5 7
Deepika H V
C-DAC Bengaluru
([email protected])
• Process
– Independent Process : It cannot affect or be effected by the other processes
executing in the system ;Does not share any data with any other processes
– Cooperating Process: It can affect or be affected by the other processes executing
in the system.
• Cooperating Process
– Share variable, memory, buffer, code, resources
• Synchronization
– Several processes access and manipulate the same data concurrently
– Outcome of the execution depends on the particular order in which the access
takes place
– data consistency is must in co-operating processes
Producer() Consumer()
release() {
available = true;
}
Down(Semaphore S){
S.value= S.value -1; Up(Semaphore S){
if(S.value<0){ S.value = S.value +1;
put process(PCB) in suspended list sleep(); if(S.value <=0){
} select
else{ process from sleep list
return ; wakeup();
} }
} }
Semaphore s; // initialized to 1
do {
down(s);
// Critical Section
up(s);
// remainder section
} while (TRUE);
is
Teacher OS
is I/O
Students device
• Hardware Interrupts
– asynchronous entities
– typically employed to provide an effective means for a
system to react to outside stimuli.
• Software Interrupts (Exceptions)
– Has both synch and asynch
– Exceptions generated by processes.
– Caused by events that occur as result of executing an
instruction
• I/O interrupts
– hitting ctl-c at the keyboard
– arrival of a packet from a network
– arrival of a data sector from a disk
• Hard reset interrupt
– hitting the reset button
• Soft-reset interrupts
– hitting ctl-alt-delete on a PC
• Traps
– Intentional - system calls, breakpoint traps, special
instruction
– Returns control to “next” instruction
• Faults
– Unintentional but possibly recoverable - page faults.
– Either re-executes faulting instruction.
• Aborts
– unintentional and unrecoverable -parity error, machine
check.
– Aborts current program
• A notification of an event
– Event gains attention of the OS
– OS stops the current process, sending it a signal
– Signal handler executes to completion
– Application process resumes where it left off
• Different signals are identified by small integer ID’s
1
March 14 - 22, 2021 DAC , Operating System - Process 0
Receiving
1
March 14 - 22, 2021 DAC , Operating System - Process 1
Predefined Signals
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD
18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN
22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1
36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5
40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9
44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13
52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9
56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5
60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1
64) SIGRTMAX
1
March 14 - 22, 2021 DAC , Operating System - Process 2
Signal via Keyboard
1
March 14 - 22, 2021 DAC , Operating System - Process 3
Signal via Commands
1
March 14 - 22, 2021 DAC , Operating System - Process 4
Signal via function call – raise()
• Example
int ret = raise(SIGINT); /* Process commits suicide. */
assert(ret != 0); /* Shouldn't get here. */
1
March 14 - 22, 2021 DAC , Operating System - Process 5
#include<stdio.h>
#include<signal.h>
void main()
{
printf(" the process id is %d\n",getpid());
raise(SIGINT);
printf(" the parent process id is %d\n",getppid());
1
March 14 - 22, 2021 DAC , Operating System - Process 6
Signal via function call – kill()
• Example
pid_t iPid = getpid(); /* Process gets its id.*/
kill(iPid, SIGINT);
1
March 14 - 22, 2021 DAC , Operating System - Process 7
#include<stdio.h>
#include<signal.h>
void main()
{
int ret;
ret = fork();
if(ret == 0)
{
printf(" the process id is %d\n",getpid());
printf(" the parent process id is %d\n",getppid());
for(;;)
printf("Looping in child process\n");
}
else
{
kill(ret,SIGINT);
}
}
1
March 14 - 22, 2021 DAC , Operating System - Process 8
Signal via function call - signal
1
March 14 - 22, 2021 DAC , Operating System - Process 9
…
static FILE *psFile; /* Must be global. */
static void cleanup(int iSig) {
fclose(psFile);
remove("tmp.txt");
exit(EXIT_FAILURE);
}
int main(void) {
void (*pfRet)(int);
psFile = fopen("temp.txt", "w");
pfRet = signal(SIGINT, cleanup);
…
raise(SIGINT);
return 0; /* Never get here. */
}
2
March 14 - 22, 2021 DAC , Operating System - Process 0
Terminologies
• Signal generated
• Signal Delivered
• Lifetime
• Pending
• Signal catched
• Signal ignored
• Signal blocked
2
March 14 - 22, 2021 DAC , Operating System - Process 1
Symmetric MultiProcessor
• SMP – computer architecture where two or more identical processors can connect
to a single shared memory.
• What is thread?
– Is an independent /different stream of control that can execute its instructions
independently and can use the process resources
• One to One
– Windows NT, Linux
• Many to one
– Solaris Green Threads and GNU portable threads
• Many to Many
– Solaris prior to ver 9
– Windows 2000
• Independent Tasks
• Servers
• Repetitive tasks
• Asynchronous events
• pthread_create (thread,attr,start_routine,arg)
• pthread_exit (status)
• pthread_attr_init (attr)
• pthread_attr_destroy (attr)
• pthread_join (threadid,status)
• pthread_detach (threadid,status)
• pthread_mutex_init (mutex,attr)
• pthread_mutex_destroy (mutex)
• pthread_mutexattr_init (attr)
• pthread_mutexattr_destroy (attr)
• pthread_mutex_lock (mutex)
• pthread_mutex_trylock (mutex)
• pthread_mutex_unlock (mutex)
• Pipe
– A pipe is a data channel that is unidirectional
• File
– A file is a data record that may be stored on a disk or acquired on
demand by a file server.
• Signal
– signals are not used to transfer data but are used for remote
commands between processes
• Shared Memory
– memory that can be simultaneously accessed by multiple processes
• Message Queue
– Multiple processes can read and write data to the message queue
without being connected to each other
• Socket
– the endpoint for sending or receiving data in a network
ls | wc -l
Pipe
Byte stream:
ls stdout (fd1) Stdin (fd 0) wc
Unidirectional
#include<unistd.h>
int pipe(int pipedes[2]);
Send message
#include<unistd.h>
Receive message
#include<unistd.h>
ssize_t read(int fd, void *buf, size_t count)
Algorithm
Step 1 − Create a pipe.
Step 2 − Send a message to the pipe.
Step 3 − Retrieve the message from the pipe and write it to the
standard output.
Step 4 − Send another message to the pipe.
Step 5 − Retrieve the message from the pipe and write it to the
standard output.
Note − Retrieving messages can also be done after sending all
messages.
Mode:
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH |S_IFIFO
fd = open(FIFO_FILE, O_CREAT|O_RDWR);
• PIPE
– Between the processes which share the same file
descriptor table (normally the parent and child processes
or threads created by them)
• Named PIPE
– Don't have to start the reading/writing processes at the
same time
– Can control ownership and permissions
– Across different systems – If a common file system
available
}
March 14 - 22, 2022 2
DAC , Operating System – IPC
9
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main() {
int i , shmid ;
void *shared_memory;
char buff[100];
#include <sys/types.h> if ((msqid = msgget(key, msgflg )) < 0){ //Get the message
#include <sys/ipc.h> queue ID for the given key
#include <sys/msg.h> perror("msgget");
#include <stdio.h> exit(1);
#include <string.h> }
#include <stdlib.h> //Message Type
#define MAXSIZE 128 sbuf.mtype = 1;
struct msgbuf printf("Enter a message to add to message queue : ");
{ scanf("%[^\n]",sbuf.mtext);
long mtype; getchar();
char mtext[MAXSIZE]; buflen = strlen(sbuf.mtext) + 1 ;
}; if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
main() {
{ printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype,
int msqid; sbuf.mtext, buflen);
int msgflg = IPC_CREAT | 0666; perror("msgsnd");
key_t key; exit(1);
struct msgbuf sbuf; }
size_t buflen; else
key = 1234; printf("Message Sent\n");
exit(0);
March 14 - 22, 2022 } DAC , Operating System – IPC 3
8
Receiving Message
Best when small messages needs to be Can be used when frequency of writing
exchanged and reading is high