0% found this document useful (0 votes)
35 views17 pages

OS Class 3 Slides

The document discusses the concept of a process in an operating system, describing a process as a program in execution that contains code, data, and stack sections in memory. It explains that a process is created when a program is loaded into memory and executed via events like mouse clicks or command line entries, and that one program can create multiple processes through functions like fork(). The document also outlines how processes communicate with the operating system through system calls that cause a context switch between user and kernel mode.

Uploaded by

2229029
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)
35 views17 pages

OS Class 3 Slides

The document discusses the concept of a process in an operating system, describing a process as a program in execution that contains code, data, and stack sections in memory. It explains that a process is created when a program is loaded into memory and executed via events like mouse clicks or command line entries, and that one program can create multiple processes through functions like fork(). The document also outlines how processes communicate with the operating system through system calls that cause a context switch between user and kernel mode.

Uploaded by

2229029
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/ 17

Operating System

School of Computer Engineering

Kalinga Institute of Industrial Technology

1 / 17
Program to Process

#include <stdio.h>
int main () {

char str [ ] = "Hello World! \n";


printf ("%s", str);
}

$ gcc hello.c

$ ./a.out
Executable
Process
(a.out)

Figure: Program to Process

2 / 17
Process Concept

An OS executes a variety of programs:


− Batch system - jobs
− Time-shared system – user programs or tasks

Textbook uses the terms job and process almost interchangeably

Process – A program in execution;

Multiple Parts
− the program code, also called text section
− Current activity including program counter, processor registers
− Stack containing temporary data
.. Function parameters, return addresses, local variables
− Data section containing global variables
− Heap containing memory dynamically allocated during run time

3 / 17
Process Concept(Contd ..)

Program is passive entity stored on disk (executable file), process is


active
− Program becomes process when executable file loaded into memory

Execution of program started via GUI mouse clicks, command line


entry of its name, etc

One program can be several processes

4 / 17
Process in Memory

Figure: Process in Memory


5 / 17
Process and Memory Map
MAX_SIZE
#include <stdio.h>
#include <stdlib.h>
Stack
int calls ;
void fact (int a, int *b)
{
calls ++ ;
if (a == 1) return ;
*b = *b * a ;
fact(a-1, b) ;
Heap
}
int main () {
int n, *m;
Data
scanf("%d", &n) ;
(global and static)
m = (int *)malloc(sizeof(int)) ;
*m = 1 ;
fact (n, m) ;
Text
printf ("Factorial (%d) is %d\n", n, *m) ;
(Instruction)
free (m) ;
} 0

Figure: Program and Memory Map

6 / 17
Beyond the Program Memory Map

OXFE00000
Kernel
(text, data, device
Kernel Process can access

memory

User Process can access


MAX_SIZE
Stack

Heap
Data
(global and static)
Text
(Instruction)
0

Figure: Beyond the program Memory Map

7 / 17
Processes share the same Kernel but separate User Space
OXFE00000
OXFE00000
Kernel
Kernel
(text, data, device
(text, data, device
memory
Kernel Process can access

memory

Kernel Process can access


MAX_SIZE
User Process can access

Stack MAX_SIZE

User Process can access


Stack

Heap
Heap
Data
Data
Text
Text
(Instruction)
0 (Instruction) 0

Process 1 Process 2

Figure: Share same Kernel space but separate User space

How does the User Program invoke the Operating System ?


8 / 17
Communicating with the OS (System Calls)

Kernel
System call invokes a function in
the kernel using the trap
This causes
− Processor to shift from user
System Calls mode to priviledged mode or
kernel mode
On the completion of the system
User call, execution gets transferred
back to the user space

9 / 17
Example (Write System Call

printf ("%s", str);

User libc invocation


Space
write (STDOUT)

trap

Trap Handler

Kernel
Space
Implementation
of write system
call

Figure: Example of System Call

10 / 17
Process Creation

Parent process create children processes, which, in turn create other


processes, forming a tree of processes
Generally, process identified and managed via a process identifier (pid)
Resource sharing options
− Parent and children share all resources
− Children share subset of parent’s resources
− Parent and child share no resources
Execution options
− Parent and children execute concurrently
− Parent waits until children terminate

11 / 17
Tree of Processes in Linux

Figure: Tree of Processes


12 / 17
Process Creation (Contd ..)
Address Space
Child duplicate of parent
Child has a program loaded into it

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

Figure 13 / 17
Process Termination

Process executes last statement and then asks the operating system
to delete it using the exit() system call
− Returns status data from child to parent (via wait())
− Process’ resources are deallocated by operating system

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 child is no longer required
− The parent is exiting and the operating systems does not allow a child
to continue if its parent terminates

14 / 17
Process State

As a process executes, it changes state

New: The process is being created

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

Running: Instructions are being executed

Waiting: The process is waiting for some event to occur

Terminated: The process has finished execution

15 / 17
Diagram of Process State

Figure: Process State Diagram

16 / 17
Process Control Block (PCB)

Process state: Running, waiting, etc.


Program counter: Location of instruction to
next execute
content of CPU register
CPU Sceduling Informations: priorities,
scheduling queue pointers
Memory-management information: Base and
limit registers, page tables
Accounting information: CPU used, clock
time elapsed since start, time limits, pid
I/O Status information: I/O devices, allocated
to process, list of open files

17 / 17

You might also like