OS02 Slide Deck 2020
OS02 Slide Deck 2020
Process (02)
Active program execution
Interconnected
Resource-aware
Intelligent Systems
Process concept
Geoffrey Nelissen
Memory allocated to a process
• 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.
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
Geoffrey Nelissen
Process scheduling
Ready queue and device (I/O) queues
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
Interconnected
Resource-aware
Intelligent Systems
Process Creation
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
Geoffrey Nelissen
Process Creation (cont’d)
• Execution options
• The parent and children execute concurrently
• The parent waits until children terminate
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).
Geoffrey Nelissen 13
POSIX processes (1003.1)
Geoffrey Nelissen 14
Example: create new process
P pid_t child;
......
child = fork();
if (child<0) /* error occurred */ {
perror (“fork”); exit (-1); }
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()
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
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
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
Geoffrey Nelissen 26
Shared memory between
processes
• A memory segment is reserved in the kernel memory User Stack
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
– 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 .....
– 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