operating systems
DESCRIPTION
Operating Systems. Agenda of Today. Review PCB Contents of PCB Context Switching Schedulers Types of Schedulers Operations on Processes Creation of a process Termination of a process. Process Control Block (PCB). Information associated with each process. Process state Program counter - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/1.jpg)
Operating Systems
![Page 2: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/2.jpg)
Silberschatz, Galvin and Gagne 20024.2
Agenda of Today
Review PCB Contents of PCB Context Switching Schedulers Types of Schedulers Operations on Processes
Creation of a process Termination of a process
Operating System Concepts
![Page 3: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/3.jpg)
Silberschatz, Galvin and Gagne 20024.3Operating System Concepts
Process Control Block (PCB)
Information associated with each process.
Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information
![Page 4: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/4.jpg)
Silberschatz, Galvin and Gagne 20024.4Operating System Concepts
CPU Switch From Process to Process
![Page 5: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/5.jpg)
Silberschatz, Galvin and Gagne 20024.5CMP320 PUCIT Arif Butt
Context Switch
Steps involved in a full Process switch are:
Save context of currently running process (including PC and
other registers)
Move this PCB to an appropriate Queue
Select another process for execution (Kernel Schedules)
Update PCB of selected process
Update memory management data structures
Restore the context of the process
04/21/23
![Page 6: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/6.jpg)
Silberschatz, Galvin and Gagne 20024.6Operating System Concepts
Schedulers
“Scheduling is a matter of managing queues to minimize queuing delay and to optimize performance in a queuing environment”.
Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue.
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.
![Page 7: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/7.jpg)
Silberschatz, Galvin and Gagne 20024.7Operating System Concepts
Addition of Medium Term Scheduling
![Page 8: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/8.jpg)
Silberschatz, Galvin and Gagne 20024.8Operating System Concepts
Schedulers (Cont.) Long-term scheduler (or job scheduler) – selects processes from the
job pool (processes spooled on hard disk) to be brought into the ready queue (inside main memory)
Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow).
The long-term scheduler controls the degree of multiprogramming. Must select a good mix of I/O bound and CPU bound processes.
Short-term scheduler is invoked very frequently (milliseconds) (must be fast). Select processes from ready queue and execute them.
Medium-term scheduler handles swapping. Swap-out: When cpu is idle: all processes are blocked and another new job
is waiting while memory is full. Then CPU suspend a process from blocked queue and swap it out to the disk. Then swap-in another process. This adds another state in the state diagram.
![Page 9: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/9.jpg)
Silberschatz, Galvin and Gagne 20024.9Operating System Concepts
Process Creation
Parent process create children processes, which, in turn create other processes, forming a tree of processes.
Resource sharing Parent and children share all resources. Children share subset of parent’s resources. Parent and child share no resources.
Execution Parent and children execute concurrently. Parent waits until children terminate.
![Page 10: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/10.jpg)
Silberschatz, Galvin and Gagne 20024.10Operating System Concepts
Process Creation (Cont.) Address space
Child duplicate of parent. Child has a program loaded into it. Child same as parent: Exp. HTTP server processing clients through
multiple clones
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.
Important process-related UNIX/Linux system calls
fork()
exit()
wait()
exec()
![Page 11: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/11.jpg)
Silberschatz, Galvin and Gagne 20024.11Operating System Concepts
Processes Tree on a UNIX System
Daemon: System processes running in background.
![Page 12: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/12.jpg)
Silberschatz, Galvin and Gagne 20024.12Operating System Concepts
Process Termination
Process executes last statement and asks the operating system to decide it (exit). Output data from child to parent Process’ resources are de-allocated by operating system. Exit (0): normal or (1) for less space etc.
Parent may terminate execution of children processes (abort). Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting.
Operating system does not allow child to continue if its parent terminates.
Cascading termination.
![Page 13: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/13.jpg)
Silberschatz, Galvin and Gagne 20024.13
Process Termination
• A process may terminate due to following reasons:
• Normal completion• Memory unavailable• Protection error• Mathematical error• I/O failure• Cascading termination (by OS)• Operator intervention
Operating System Concepts
![Page 14: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/14.jpg)
Silberschatz, Galvin and Gagne 20024.14
System Call - fork() When the fork system call is executed, a new process is
created which consists of a copy of the address space of the
parent
An exact copy of the parent program is created
This mechanism allows the parent process to communicate
easily with the child process.
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>pid_t fork(void);
1404/21/23
![Page 15: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/15.jpg)
Silberschatz, Galvin and Gagne 20024.15
System Call - fork() ... On success: (Child process is created)
The return code for fork is zero for the child process
The child process ID is returned to the parent process
Both processes continue execution at the instruction after
the fork call
On failure: (No child process is created)
A -1 is returned to the parent process
Variable errno is set appropriately to indicate the reason of
failure
1504/21/23
![Page 16: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/16.jpg)
Silberschatz, Galvin and Gagne 20024.16
PID: 597
16
1. //fork1.c2. int main()3. {4. int i = 54, cpid = -1;5. cpid = fork();6. if (cpid == -1)7. {8. printf (“\nFork failed\n”);9. exit (1);10. }11. if (cpid == 0) //child code12. printf (“\n Hello I am child \n”);13. else //parent code14. printf (“\n Hello I am parent \
n”);15. }
•Parent forks
04/21/23
Parent
i = 54cpid = -1
DATA
Using fork() & exit() system call
![Page 17: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/17.jpg)
Silberschatz, Galvin and Gagne 20024.17
PID: 597
17
1. //fork1.c2. int main()3. {4. int i = 54, cpid = -1;5. cpid = fork();6. if (cpid == -1)7. {8. printf (“\nFork failed\n”);9. exit (1);10. }11. if (cpid == 0) //child code12. printf (“\n Hello I am child \
n”);13. else //parent code14. printf (“\n Hello I am parent \
n”);15. }
PID: 6321. //fork1.c2. int main()3. {4. int i = 54, cpid = -1;5. cpid = fork();6. if (cpid == -1)7. {8. printf (“\nFork failed\n”);9. exit (1);10. }11. if (cpid == 0) //child code12. printf (“\n Hello I am child \
n”);13. else //parent code14. printf (“\n Hello I am parent \
n”);15. }
Using fork() & exit() system call
04/21/23
Parent Child
i = 54cpid = 632
DATAi = 54
cpid = 0
DATA
• After fork parent and child are identical except for the return value of fork (and of course the PIDs).
• Because data are different therefore program execution differs.
• When both will execute line 11, parent will now execute line 12 while child will execute line 14.
![Page 18: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/18.jpg)
Silberschatz, Galvin and Gagne 20024.18
fork() – Child inherits from the Parent
The child process inherits the following attributes
form the parent:
Environment
Open File Descriptor table
Signal handling settings
Current working directory
Root directory
File mode creation mask (umask)
18CMP320 PUCIT Arif Butt
04/21/23
![Page 19: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/19.jpg)
Silberschatz, Galvin and Gagne 20024.19
fork() – Child Differs from the Parent
The child process differs from the parent
process:
Different process ID (PID).
Different parent process ID (PPID).
19CMP320 PUCIT Arif Butt
04/21/23
![Page 20: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/20.jpg)
Silberschatz, Galvin and Gagne 20024.20
fork() – Reasons for Failure
Maximum number of processes allowed to
execute under one user has exceeded
Maximum number of processes allowed on
the system has exceeded
Not enough swap space
20CMP320 PUCIT Arif Butt
04/21/23
![Page 21: Operating Systems](https://reader036.vdocuments.mx/reader036/viewer/2022070416/568150aa550346895dbebf92/html5/thumbnails/21.jpg)
Silberschatz, Galvin and Gagne 20024.21
Unix Commands related to Processes
Operating System Concepts
Command Description# ps [-aelu] Display status of processes
# top Display information about top 20 processes
# vim file1.txt & & cause Running a command in back ground
CTRL + Z Suspend a foreground process to go back to the shell
# jobs Display status of suspended and background processes
# bg %job_id Put a process in background
# fg %job_id Move background process to the foreground
CTRL + C Kill foreground process
# kill [-signal_no] proc_list Send the signal for signal_no to processes whose PIDs or jobIDs are specified in proc_list. jobIDs must start with %.