0% found this document useful (0 votes)
60 views

Sistemet Operative Processes: Lënda

1. A process is the active execution of a program and includes program instructions, data, stack, heap, and other context. 2. Processes go through various states like new, running, waiting, ready and terminated during execution. 3. The operating system uses process control blocks (PCBs) to store process state and context information to manage processes.

Uploaded by

Sara Lohaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Sistemet Operative Processes: Lënda

1. A process is the active execution of a program and includes program instructions, data, stack, heap, and other context. 2. Processes go through various states like new, running, waiting, ready and terminated during execution. 3. The operating system uses process control blocks (PCBs) to store process state and context information to manage processes.

Uploaded by

Sara Lohaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIVERSITETI I PRISHTINËS ”HASAN PRISHTINA”

Fakulteti i Inxhinierisë Elektrike dhe Kompjuterike

Lënda: Sistemet Operative

Processes
Artan Mazrekaj

Prishtinë, 2023

1
Process definition

• An operating system executes a variety of programs that run as a


process.
• Process is a basic term to understand the operation of an OS.
▪ Program is passive entity stored on disk (executable file);
▪ Process is active.
→ Program becomes process when executable file loaded into
memory.

2
Process definition (cont.)

• The term ‘process’ is different from the terms: ‘job’ or ‘program’.

A program in execution is called a process!!!

Process
Environment

3
Process definition (cont.)

Multiple parts:
 Text section (instructions) - also called program code - the executable code.

 Data section containing global and static variables.

 Heap section- containing memory dynamically allocated during program run time.

 Stack section - containing temporary data, function parameters, return addresses,

local variables.

Process in memory 4
Memory Layout of a C program

5
Process States

▪ As a process executes, it changes state.


 New: The process is being created

 Running: Instructions are being executed

 Waiting: The process is waiting for some event to occur

 Ready: The process is waiting to be assigned to a processor

 Terminated: The process has finished execution

Diagram of Process States

6
Life Cycle of a Process

1. A process when created is in a new state as a program/job.


2. Whenever a new job is entered into the system, it is stored in the job pool/queue
maintained in the hard disk.
3. When a job is selected to be brought into the main memory, it is called job scheduling.
4. When a job is loaded into the memory, it becomes a process and is stored in a waiting
queue where all processes wait for their turn to be sent to the CPU for execution.
This waiting queue is called ready queue.
5. The processes in ready queue are called process scheduling or CPU scheduling.
6. The selected process is sent for execution called process dispatching.
7. After getting the CPU time, the running process executes its full code and terminates.

7
Process Control Block (PCB)

▪ Information associated with each process (also called


Task Control Block).

▪ Process state – running, waiting, etc.


▪ Program counter – location of instruction to next
execute.
▪ CPU registers – contents of all process-centric registers.
▪ CPU scheduling information- priorities, scheduling queue
pointers.
▪ Memory-management information – memory allocated
to the process.
▪ Accounting information – CPU used, clock time elapsed
since start, time limits.
▪ I/O status information – I/O devices allocated to process,
list of open files.
8
Process Table and the PCB

• The processes are implemented by the means of process tables.


• The process tables store the ID of every process and, corresponding
to it, the pointer to its PCB.

9
Process Scheduling

Scheduling queues

• Device queue (I/O queue) - the list of processes waiting for a


particular I/O device.

10
CPU switch from process to process

▪ A context switch occurs when the CPU switches from one process to
another.

11
Context Switch

▪ When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new process
via a context switch.

▪ Context of a process represented in the PCB.

▪ Context switch time is overhead; the system does no useful work


while switching.
 The more complex the OS and the PCB ➔ the longer the context
switch.

12
Context Switch (cont.)

Context Switching Triggers

There are three major triggers for context switching:


▪ Multitasking
- In a multitasking environment, a process is switched out of the CPU
so another process can be run.

▪ Interrupt handling
- The hardware switches a part of the context when an interrupt
occurs. This happens automatically.

▪ User and kernel mode switching


- A context switch may take place when a transition between the
user mode and kernel mode is required in the OS.

13
Schedulers

Long-term Scheduler (Job Scheduler)


• This scheduler is invoked when there is a need to perform job
scheduling, that is, when a job from the job pool is selected to be sent
to the ready queue.
→ This type of scheduling does not happen very frequently because a process
needs some time in the system to be executed.

Short-term Scheduler (CPU Scheduler)


• Selects from among the processes that are ready to execute and
allocates the CPU to one of them.
• The short-term scheduler must select a new process for the CPU.
→ A process may execute for only a few milliseconds before waiting for an
I/O request.

14
Schedulers (cont.)

Medium-term Scheduler
• This scheduler is invoked when there is a need to swap out some
blocked processes.

15
Long-term & Medium-term & Short-term Schedulers

16
Long-term & Medium-term & Short-term Schedulers (cont.)

17
Dispatcher

• This operation starts when there is a need to schedule a process from the
ready queue.

• The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler.

The dispatcher’s function involves:


→ switching context
→ switching to user mode
→ jumping to the proper location in the user program to restart that
program.

18
Operations on Processes

▪ System must provide mechanisms for:

 Process creation

 Process termination

19
Process Creation

▪ Four principal events cause processes to be created:


1. System initialization.
2. Execution of a process-creation system call by a running process.
3. A user request to create a new process.
4. Initiation of a batch job.

▪ When an OS is booted, typically numerous processes are created.


- Some of these are foreground processes (processes that interact with
users and perform work for them).
- Others are background processes and are not associated with
particular users, but instead have some specific function.
→ Processes that stay in the background to handle some activity such
as e-mail, web pages, news, printing, and so on are called daemons.

20
Process Creation (cont.)

▪ Process identified and managed via a process identifier (pid), which is


typically an integer number.
Resource sharing options
▪ When a process creates a child process, that child process will need
certain resources (CPU time, memory, files, I/O devices) to accomplish
its task.
 A child process may be able to obtain its resources directly from the
OS, or it may be constrained to a subset of the resources of the parent
process.
 The parent may have to partition its resources among its children, or
it may be able to share some resources (such as memory or files)
among several of its children.

21
Process Creation (cont.)

When a process creates a new process (child process), two possibilities


for execution exist:
 Parent and children execute concurrently.

 Parent waits until children terminate.

There are also two address-space possibilities for the new process:
 Child duplicate of parent (it has the same program and data as the
parent).
 Child has a program loaded into it.

22
Process Creation (cont.)

▪ UNIX/Linux examples
 fork() system call creates new process.

 exec() system call used after a fork() to replace the


process’memory space with a new program.
 Parent process calls wait() for the child to terminate.

23
Process Hierarchies

▪ When a process creates another process, the parent process and child
process continue to be associated in certain ways.
▪ The child process can itself create more processes, forming a process
hierarchy (tree of processes).
▪ In UNIX, a process and all of its children and further descendants together
form a process group.
▪ When UNIX initializes itself when it is started, just after the computer is
booted, a special process, called init (“parent of all processes”), is present
in the boot image.
▪ When it starts running, it reads a file telling how many terminals there
are.

24
Process Hierarchies (cont.)

A Tree of Processes in Linux

▪ Windows has no concept of a process hierarchy!


▪ All processes are equal. The only hint of a process hierarchy is that when a
process is created, the parent is given a special token (called a handle)
that it can use to control the child.
25
Process Termination

▪ Sooner or later the new process will terminate, usually due to one of the
following conditions:
1. Normal exit (voluntary).
2. Error exit (voluntary).
3. Fatal error (involuntary).
4. Killed by another process (involuntary).

▪ Most processes terminate because they have done their work.


→ When a compiler has compiled the program given to it, the compiler
executes a system call to tell the OS that it is finished.

26
Process Termination (cont.)

▪ A process terminates when it finishes executing its final statement and


asks the OS to delete it by using the exit() system call:
 The process returns status value (typically an integer) to its waiting

parent process via the wait() system call.


 Process’resources (including physical and virtual memory, open files,
and I/O buffers) are deallocated by OS.

▪ Parent may terminate the execution of children processes using the


abort() system call.
Some reasons for doing so:
 Child has exceeded allocated resources.

 Task assigned to the child is no longer required.

 The parent is exiting, and the OS does not allow a child to continue if

its parent terminates.


27
Process Termination (cont.)

▪ Some OSes do not allow child to exists if its parent has terminated.
If a process terminates (either normally or abnormally), then all its
children must also be terminated.
→ This phenomenon, referred to as cascading termination.

▪ The parent process may wait for termination of a child process by using
the wait() system call.
 The wait() system call is passed a parameter that allows the parent to
obtain the exit status of the child.
 This system call also returns the PID of the terminated child so that
the parent can tell which of its children has terminated:
pid_t pid;
int status;
pid = wait(&status);
28
Process Termination (cont.)

▪ A process that has terminated, but whose parent has not yet called
wait(), is known as a zombie process.
→ All processes transition to this state when they terminate, but
generally they exist as zombies only briefly.
→ Once the parent calls wait(), the PID of the zombie process and its
entry in the process table are released.

▪ A process whose parent process no more exists i.e. either finished or


terminated without waiting (if a parent did not invoke wait()) for its
child process to terminate is called an orphan process.

29
Process Termination (cont.)

Zombie Process Orphan Process

int main() int main()


{ {
int pid = fork(); int pid = fork();
if (pid > 0) //procesi prind if (pid > 0) //procesi prind
{ {
sleep(50); exit(0);
} }
else if (pid == 0) //procesi femije else if (pid == 0) //procesi femije
{ {
exit(0); sleep(50);
} }
return 0; return 0;
} }

30
The slides are adapted from: Abraham Silberschatz, Peter Baer
Galvin, and Greg Gagne, “Operating System Concepts”, Tenth
Edition

31
Pyetje dhe Diskutime ?

32

You might also like