You are on page 1of 28

Operating Systems (2INC0)

Process (02)
Active program execution

Dr. Geoffrey Nelissen


Courtesy of Dr. Tanir Ozcelebi

Interconnected
Resource-aware
Intelligent Systems
Process concept

• A program is a passive entity Your textbook uses the


• A process is a program in execution terms job and process
• Several processes can run the same program interchangeably.
• User processes run in user mode

• A process is defined by:


• program code
• stack
• Heap
• Data
• program counter
= Address of the next instruction to execute
• CPU registers content
= Data manipulated and execution context

Geoffrey Nelissen
Memory allocated to a process

• Text (program code), stack, heap and data are


memory segments allocated to a process.

• Stack (temporary data)


- e.g. local variables, function parameters, return
addresses (their size is known at compile time).

• Heap
- used to store data whose size may be unknown
before run time, e.g. used by a program that reads
a file whose size is unknown.

• Data (global variables)


- fixed size

Geoffrey Nelissen
Process Control Block (PCB) –
inside kernel
A process is identified by an ID (number) and a pointer to a PCB in
kernel space

PCB contains:
• Process state (see next slide) PCB
• Process number (ID)
• Program counter (needed for context switching)
• CPU registers (needed for context switching)
• Memory management info (values of base, limit registers)
• I/O status information (list of open files, I/O devices)
• Scheduling information (priority, scheduling parameters)
• Accounting information (e.g. CPU time used)
• and more…

Geoffrey Nelissen
Process states

• new: process is being created


• ready: process is waiting to be assigned to a CPU (before scheduler dispatch)
• running: instructions are being executed
• waiting: process is waiting for some event to occur
• terminated: process has finished execution

Only one process


can run on a CPU
at any time instant!
→ must schedule

Geoffrey Nelissen
Process scheduling
Ready queue and device (I/O) queues

job queue dispatch

Queues are generally stored as linked lists.

Geoffrey Nelissen
Process scheduling
Ready queue and device (I/O) queues

READY RUNNING

WAITING

Geoffrey Nelissen
Context switch

Context switch:
Saving the state of
a process whose
execution is to be
suspended, and
reloading this state
when the process
is to be resumed.

Overhead:
(typically) takes a
few usecs. →
depends on HW

Geoffrey Nelissen
Operating Systems (2INC0)

Process (02)
Process creation/termination

Dr. Geoffrey Nelissen


Courtesy of Dr. Tanir Ozcelebi

Interconnected
Resource-aware
Intelligent Systems
Process Creation

• Starting point is an already running process

• Parent process creates children processes,


which, in turn create other processes, forming
a tree of processes
• new processes given process identifiers (pid)

• Resource sharing options


• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources

Geoffrey Nelissen 10
Example: UNIX processes in
operation (Solaris example)
• Unique pid’s
File System
Management
• Parent-child relationships are
recorded
Memory
Management
• System services are processes
started by init (pid=1), by themselves
capable of starting new ones
Network Login

• init initiates dtlogin for login of new


users

• init initiates inetd (the internet


daemon) for network services
• e.g. ftp, telnet, rlogin etc.

Geoffrey Nelissen
Process Creation (cont’d)

• Execution options
• The parent and children execute concurrently
• The parent waits until children terminate

• Address space options


• The child is a copy of the parent (e.g., default in Linux)
• The child has a new program loaded into its address space
(e.g., Windows)

Geoffrey Nelissen 12
Example:
Using the Portable Operating System
Interface (POSIX) API
• IEEE standard on the API (it’s not an OS!)
• Goal: reduce portability effort for applications
• Many operating systems use the POSIX API (or a subset).

• Implementations may vary …


• … but a system supporting POSIX must provide
• a host language and compiler (often: C)
• programming interface definition files (e.g., C-header files)
• programming interface implementation binary or code (e.g., C-libraries)
• a run-time system (a platform: OS or the like)

Geoffrey Nelissen 13
POSIX processes (1003.1)

• POSIX 1003.1 API: fork() – exec() – exit() – wait()

• fork() function creates new process (duplicate address space of the


parent in another location)
• exec() function used after a fork to write over the process’ memory space
with a new program
• wait() function may be issued by the parent to wait until the execution of
the child is finished.

Geoffrey Nelissen 14
Example: create new process

P pid_t child;
......
child = fork();
if (child<0) /* error occurred */ {
perror (“fork”); exit (-1); }

if (child == 0) /* the child */ {


execlp (“/bin/ls”, “ls”, arg0, arg1, ..., NULL);
/* this place is reached only in case of error */
perror (“execlp”); exit (-1);
}
else /* the parent; child == process id of child */ {
/* do whatever you want, e.g., just return from
this routine */
}
......

Geoffrey Nelissen 15
Example: create new process
• fork() creates an identical copy
pid_t child;
of the caller in a separate
......
memory space; only the return
P child = fork();
value stored in variable ‘child’
if (child<0) /* error occurred */ {
differs between the two: 0 for
perror (“fork”); exit (-1); }
the child, and child-pid for the
parent
if (child == 0) /* the child */ {
execlp (“/bin/ls”, “ls”, arg0, arg1, ..., NULL);
/* this place is reached only in case of error */
perror (“execlp”); exit (-1);
}
else /* the parent; child == process id of child */ {
/* do whatever you want, e.g., just return from
this routine */
}
......

Geoffrey Nelissen 16
Example: create new process
• fork() creates an identical copy
pid_t child;
of the caller in a separate
......
memory space; only the return
child = fork();
value stored in variable ‘child’
C P if (child<0) /* error occurred */ {
differs between the two: 0 for
perror (“fork”); exit (-1); }
the child, and child-pid for the
parent
if (child == 0) /* the child */ {
execlp (“/bin/ls”, “ls”, arg0, arg1, ..., NULL);
/* this place is reached only in case of error */
perror (“execlp”); exit (-1);
}
else /* the parent; child == process id of child */ {
/* do whatever you want, e.g., just return from
this routine */
}
......

Geoffrey Nelissen 17
Example: create new process
• fork() creates an identical copy
pid_t child;
of the caller in a separate
......
memory space; only the return
child = fork();
value stored in variable ‘child’
if (child<0) /* error occurred */ {
differs between the two: 0 for
perror (“fork”); exit (-1); }
the child, and child-pid for the
parent
C P if (child == 0) /* the child */ {
execlp (“/bin/ls”, “ls”, arg0, arg1, ..., NULL);
/* this place is reached only in case of error */
perror (“execlp”); exit (-1);
}
else /* the parent; child == process id of child */ {
/* do whatever you want, e.g., just return from
this routine */
}
......

Geoffrey Nelissen 18
Example: create new process
• fork() creates an identical copy
pid_t child;
of the caller in a separate
......
memory space; only the return
child = fork();
value stored in variable ‘child’
if (child<0) /* error occurred */ {
differs between the two: 0 for
perror (“fork”); exit (-1); }
the child, and child-pid for the
parent
if (child == 0) /* the child */ {
C execlp (“/bin/ls”, “ls”, arg0, arg1, ..., NULL);
• execlp() overwrites the calling /* this place is reached only in case of error */
process with its argument (an perror (“execlp”); exit (-1);
executable). This means that }
code following execlp is never P else /* the parent; child == process id of child */ {
reached. /* do whatever you want, e.g., just return from
this routine */
• perror() is a generic error }
printing routine ......

Geoffrey Nelissen 19
Example: create new process
• fork() creates an identical copy
pid_t child;
of the caller in a separate
......
memory space; only the return
child = fork();
value stored in variable ‘child’
if (child<0) /* error occurred */ {
differs between the two: 0 for
perror (“fork”); exit (-1); }
the child, and child-pid for the
parent
if (child == 0) /* the child */ {
execlp (“/bin/ls”, “ls”, arg0, arg1, ..., NULL);
• execlp() overwrites the calling /* this place is reached only in case of error */
process with its argument (an perror (“execlp”); exit (-1);
executable). This means that }
code following execlp is never else /* the parent; child == process id of child */ {
reached. /* do whatever you want, e.g., just return from
P this routine */
• perror() is a generic error }
printing routine ......

Geoffrey Nelissen 20
Before and after a fork()

Parent process about to perform child = fork()

memory

child
Parent and child process after fork()
child is a literal copy of parent

memory

variable child of child process equals 0 variable child in parent points to child process

Geoffrey Nelissen 21
Process Termination

• Process may ask the operating system to delete itself (exit)…


• return status value from child to parent (via wait)
• the process’ resources are taken away by OS

• … or parent may terminate execution of children processes (abort)


• The child has exceeded allocated resources
• The task assigned to child is no longer required
• If the parent is exiting
− Some operating systems do not allow the child to continue if its parent
terminates, in which case all children are terminated - cascading termination
− Some operating systems attach orphans to a ‘grandfather’ process (e.g. init)

Geoffrey Nelissen 22
Termination of children:
fragment
parent:
• Use exit(status) to terminate …
pid_t child, terminated; int status;

• In some OSs, the parent must /* blocking wait */
wait for children to free while (child != wait (&status)) /* nothing */;
children’s resources
/* or polling wait */
terminated = (pid_t) 0;
• Functions wait(), waitpid() while (terminated != child) {
• wait() blocks until any child exit terminated = waitpid (child, &status,
• waitpid() blocks until a specific child WNOHANG);
changes its state /* other useful activities */
}
/* both cases: status == 23 */
• Alternative (not shown here): use
asynchronous notification signals child:
..... exit (23);

Geoffrey Nelissen 23
Operating Systems (2INC0)

Process (02)
Interprocess communication

Dr. Geoffrey Nelissen


Courtesy of Dr. Tanir Ozcelebi

Interconnected
Resource-aware
Intelligent Systems
Cooperating processes need
interprocess communication (IPC)

No shared Shared
address address
space: space:
useful for faster but
exchanging difficult to
small implement
amounts of
data
Message passing Shared memory

Geoffrey Nelissen 25
Message passing between
processes
• The easiest solution especially for distributed systems

• The producer(s) send(s) messages through a pipe or in a mailbox


• Can be blocking or non-blocking
• The consumer(s) read(s) messages
• Can be blocking or non-blocking
• If both send and receive are blocking then we call it a rendez-vous
• communicating processes synchronize on the transmission

• A temporary queue (buffer) can be associated to the communication


• Allows for asynchronous executions and/or applications with
multiple producers/consumers
• The producer may or may not be blocked when the buffer is full
− If non-blocking, the consumers see the most recent data and
may miss old ones(e.g., control systems)

Geoffrey Nelissen 26
Shared memory between
processes
• A memory segment is reserved in the kernel memory User Stack

• It is added to the space addressable in user mode


User heap
• No kernel involvement is required upon

process private
subsequent reading and writing User data

User text
(code)

Private part
process process of PCB
1 2
Kernel
stack
Shared space
includes kernel
functions, data,
kernel part of
Shared mem OS kernel PCB
memory

Geoffrey Nelissen 27
Major issue: interference

• mutual influence (good or bad):


Assumptions or knowledge that one process has about
the state, are disturbed by actions of another process

– good interference example: wait for another process to set a boolean to true
indicating delivery of a value in a variable (assume x := false is before x := true)
• x := false; { ¬x } while ¬x do skip od; “use y” || .... y := E; x := true .....

• Question: What happens if the right hand side is “x := true; y := E”?


– possible trace: (x := false) (x := true) (use y) (y := E)

– bad interference example: access a resource (e.g. a printer) after checking its
availability; in between the check and the use the resource is accessed by
another process
• if avail then { avail } avail := false; “use resource”; avail := true; fi || ...same

Geoffrey Nelissen 28

You might also like