Module 4: Processes - Fordham University
Download
Report
Transcript Module 4: Processes - Fordham University
Chapter 3
Processes
Processes
Process
Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems
2
Objectives
To
introduce the notion of a process -- a
program in execution, which forms the
basis of all computation
To describe the various features of
processes, including scheduling, creation
termination, and communication
To describe communication in clientserver systems
3
Process Concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Process – a program in execution
A program is a sequence of instructions saved in disk, or
loaded into memory (your executable file, a.out, …)
A process execute a program in sequential fashion
4
Jump instruction provided to support branch, loop.
program counter points to next instruction to be
executed
Process in Memory
5
Process State
As a process executes, it changes state
6
new: being created
running: Instructions are being executed
waiting: waiting for some event to occur
ready: waiting to be assigned to a processor
terminated: has finished execution
Process Control Block (PCB)
Information associated with each process
Process state
Program counter:
CPU registers:
Amount of CPU time used …
I/O status information
7
Value of base and limit registers, page/segmentation tables (to be
studied later)
Accounting information
Process priority, …
Memory-management information
Contents of CPU registers
CPU scheduling information
address of next instruction to be executed
I/O device allocated, list of open files…
Process Control Block (PCB)
8
CPU Switch From Process to Process
9
P0, P1 are running concurrently, i.e.,
their executions are interleaved.
Context Switch
When CPU switches to another process, OS must save
the state/context of old process (PCB) and load saved
state for new process via a context switch
Context of a process represented in PCB
Context-switch time is overhead; system does no useful
work while switching
10
Time dependent on hardware support
Process Scheduling Queues
OS maintain multiple process queues for scheduling
purpose:
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main
memory, ready and waiting to execute
Device queues – set of processes waiting for an I/O
device
Processes migrate among the various queues
11
Ready Queue And I/O Device Queues
12
Queuing Diagram: A Process
13
Rectangles: queue
Ovals: resources that serve the queue or events being waited for
Schedulers
Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
controls the degree of multiprogramming
invoked very infrequently (seconds, minutes) (may be slow)
Goal: a good mix of I/O-bound and CPU-bound processes
=> efficient utilization of CPU and I/O
–
–
I/O-bound process – spends more time doing I/O than computations,
many short CPU bursts
CPU-bound process – spends more time doing computations; few
very long CPU bursts
Short-term scheduler (or CPU scheduler) – selects
which process should be executed next and allocates CPU
invoked very frequently (milliseconds) (must be fast)
14
Unix, Windows have no long-term scheduler
Medium Term Scheduling: swapping
Midterm-term scheduler:
to remove process from memory (and ready queue)
and reduce degree of multiprogramming; and later
reintroduce the process into memory to resume its
execution
15
Process Creation
Parent process create children processes, which, in
turn create other processes, forming a tree of
processes
16
process identified and managed via a process identifier
(pid)
A tree of processes on a typical Solaris
17
Child/Parent Process Relation
Child process needs resource (memory, files, I/O
devices)
Resource sharing between parent & child:
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
After parent creates a child process
Linux allows user to specify the degree of resource sharing
Parent continues to execute concurrently with the child
Parent waits until some or all of its children terminated
Address space
18
Child duplicate of parent: same program and data as parent
Child process loads a program into its address space
Process Creation: Unix example
fork system call creates new process
exec system call used after a fork to replace the
process’ memory space with a new program
To find out details of system calls, use man
command, e.g., man 2 wait
19
C Program Forking Separate Process
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
20
fork()
Only way to create a process
The child is a copy of the parent:
Often parent and child share text segment (code)
Never know whether parent or child will start executing first.
Parent waits.
Parent and child go their own way.
fork() may fail if it
Inherits the parent's data, heap and stack.
All file descriptors that are open in the parent are duplicated in the child
They also share the same file offset (Files opened after fork are not
shared).
if num. of processes exceeds user limit or total system limit.
Two uses (reasons) for fork()
21
Each can execute a different sections of code
One process can execute a different program.
Load a program: exec() family of functions
Replace current process image with a new process image
execl(), execv(), execle(), execlp(), execvp(), and execve().
Replace current process with a new program and start
execution. Brand new text, data, heap and stack
segments.
Only execve() is a system call.
Meaning of different letters:
22
l: needs a list of arguments.
v: needs an argv[] vector (l and v are mutually exclusive).
e: needs an envp[] array.
p: needs the PATH variable to find the executable file.
Example
/* example argument list; program name is arg0 */
const char *ps_argv{} = {"ps", "-ax", 0};
/* trivial example environment */
const chare *ps_envp[] = {"PATH=/bin:/usr/bin", "TERM=console", 0};
/* possible calls to exec */
execl("/bin/ps", "ps", "-as", 0);
execlp("ps", "ps", "-ax", 0); /* assumes ps on path */
execle("/bin/ps", "ps", "-as", 0, ps_envp); /* passes env */
execv("/bin/ps", ps_argv); /* passes args as vector */
execvp("ps", ps_argv);
execve("ps", ps_argv, ps_envp);
23
Process Termination
•
Process executes last statement and asks operating
system to delete it (exit)
–
–
–
Output data from child to parent (via wait)
Process’ resources are deallocated by operating system
Parent process must “wait” for child processes
–
–
24
Child processes terminated but not “waited for” become
“zombie” processes until the parent terminates (then these
zombies will become children of init…) or waits for it.
To make sure parent process obtain states info of the child
wait() system call (UNIX)
wait, waitpid - wait for process to change state
SYNOPSIS
–
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
pid: which child processes to wait for
status: if not NULL, wait(), waitpid() store status info. in the
integer it points to. (see manual for how to
25
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
pid_t cpid, w;
int status;
cpid = fork();
if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if (cpid == 0) { /* Code executed by child */
printf("Child PID is %ld\n", (long) getpid());
if (argc == 1) pause(); /* Wait for signals */
_exit(atoi(argv[1]));
}
26
else { /* Code executed by parent */
do {
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid"); exit(EXIT_FAILURE);
}
if (WIFEXITED(status))
printf("exited, status=%d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("killed by signal %d\n", WTERMSIG(status));
else if (WIFSTOPPED(status))
printf("stopped by signal %d\n", WSTOPSIG(status));
else if (WIFCONTINUED(status))
printf("continued\n");
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
} //end of else
} //end
27 of main
Process Termination (2)
Parent may terminate execution of children processes
(kill)
–
–
–
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
•
–
28
Some operating system do not allow child to continue if its parent
terminates
All children terminated - cascading termination
Case study: shell
How shell works?
Read a command line, parse it
Create a child process to run the command
Parent process waits for child process’s termination
Go back to 1.
1.
2.
3.
4.
Useful hints for using shell
To run a program in background, i.e., shell not waiting for its
termination
To allow a program to continue running even shell exits:
(standard output is saved to nohug.out)
29
nohup <program name> <program arguments> &
Linux Booting Procedure
Sirak Kaewjamnong
How computer startup?
Booting is a bootstrapping process that starts operating
systems when the user turns on a computer system
A boot sequence:
the sequence of operations computer performs when it is
switched on that load an operating system
31
Booting sequence
1.
2.
3.
4.
5.
6.
Turn on
CPU jump to address of BIOS (0xFFFF0)
BIOS runs POST (Power-On Self Test)
Find bootable devices
Loads and execute boot sector from Master Boot Record(MBR)
Load OS
32
BIOS (Basic Input/Output System)
BIOS: software code run by a computer when first powered on
Stored in predefined location in ROM (read-only memory)
The primary function of BIOS:
Power-On Self Test (POST): diagnostic tests to check memory/devices for
their presence and for correct operation
BIOS on board
BIOS on screen
33
Bootable Disk
Last step in BIOS:
Load boot record from the system’s boot disk (A: floppy disk as the
default, …)
A boot disk contains a boot record, also called Master Boot Record
(MBR) at its first sector
MBR is a 512-byte sector, located in the first sector on the disk
(sector 1 of cylinder 0, head 0)
After MBR is loaded into RAM, the BIOS yields control to it.
34
MBR (Master Boot Record)
First 446 bytes: primary
boot loader, which
contains both executable
code and error message
text
Next sixty-four bytes:
partition table, which
contains a record for
each of four partitions
Ends with two bytes
(magic number) used for
validation check of MBR
35
Extracting the MBR
To see the contents of MBR, use this command:
# dd if=/dev/hda of=mbr.bin bs=512 count=1
# od -xa mbr.bin
**The dd command, which needs to be run from root, reads the first 512
bytes from /dev/hda (the first Integrated Drive Electronics, or IDE drive)
and writes them to the mbr.bin file.
**The od command prints the binary file in hex and ASCII formats.
36
Boot loader
Boot loader could be more aptly called the kernel loader. The task at
this stage is to load the Linux kernel
GRUB and LILO are the most popular Linux boot loader.
37
Other boot loader (Several OS)
bootman
GRUB
LILO
NTLDR
XOSL
BootX
loadlin
Gujin
Boot Camp
Syslinux
GAG
38
GRUB: GRand Unified Bootloader
GRUB is an operating system independant boot loader
A multiboot software packet from GNU
Flexible command line interface
File system access
Support multiple executable format
Support diskless system
Download OS from network
Etc.
39
GRUB boot process
1.
The BIOS finds a bootable device (hard disk) and transfers control to the
master boot record
2.
The MBR contains GRUB stage 1. Given the small size of the MBR, Stage 1
just load the next stage of GRUB
3.
GRUB Stage 1.5 is located in the first 30 kilobytes of hard disk immediately
following the MBR. Stage 1.5 loads Stage 2.
4.
GRUB Stage 2 receives control, and displays to the user the GRUB boot
menu (where the user can manually specify the boot parameters).
5.
GRUB loads the user-selected (or default) kernel into memory and passes
control on to the kernel.
40
Example GRUB config file
41
LILO: LInux LOader
Not depend on a specific file system
Can boot from harddisk and floppy
Up to 16 different images
Must change LILO when kernel image file or config file is
changed
42
Kernel image
The kernel is the central part in most computer
operating systems because of its task, which is the
management of the system's resources and the
communication between hardware and software
components
Kernel is always store on memory until computer is
turned off
Kernel image is not an executable kernel, but a
compress kernel image
zImage size less than 512 KB
bzImage size greater than 512 KB
43
Init process
The first thing the kernel does
Initialize internal data structures, process manager
Create the first process (initial process, 0)
Initial process create process 1 to run init program
Init is the root/parent of all processes executing on Linux
The first processes that init starts is a script /etc/rc.d/rc.sysinit
Based on the appropriate run-level, scripts are executed to
start various processes to run the system and make it
functional
44
The Linux Init Processes
The init process is identified by process id "1“
Init is responsible for starting system processes as defined in the
/etc/inittab file
Init typically will start multiple instances of "getty" which waits for console
logins which spawn one's user shell process
Upon shutdown, init controls the sequence and processes for shutdown
45
System processes
Process ID
Description
0
The Scheduler
1
The init process
2
kflushd
3
kupdate
4
kpiod
5
kswapd
6
mdrecoveryd
46
Inittab file
The inittab file describes which processes are started at
bootup and during normal operation
/etc/init.d/boot
/etc/init.d/rc
The computer will be booted to the runlevel as defined by
the initdefault directive in the /etc/inittab file
id:5:initdefault:
47
Runlevels
A runlevel is a software configuration of the system which
allows only a selected group of processes to exist
The processes spawned by init for each of these runlevels
are defined in the /etc/inittab file
Init can be in one of eight runlevels: 0-6
48
Runlevels
Runlevel
Scripts Directory
(Red Hat/Fedora
Core)
State
0
/etc/rc.d/rc0.d/
shutdown/halt system
1
/etc/rc.d/rc1.d/
Single user mode
2
/etc/rc.d/rc2.d/
Multiuser with no network services exported
3
/etc/rc.d/rc3.d/
Default text/console only start. Full multiuser
4
/etc/rc.d/rc4.d/
Reserved for local use. Also X-windows (Slackware/BSD)
5
/etc/rc.d/rc5.d/
XDM X-windows GUI mode (Redhat/System V)
6
/etc/rc.d/rc6.d/
Reboot
s or S
Single user/Maintenance mode (Slackware)
M
Multiuser mode (Slackware)
49
rc#.d files
rc#.d files are the scripts for a given run level that run
during boot and shutdown
The scripts are found in the directory /etc/rc.d/rc#.d/
where the symbol # represents the run level
50
init.d
Deamon is a background process
init.d is a directory that admin can start/stop individual
demons by changing on it
/etc/rc.d/init.d/ (Red Hat/Fedora )
/etc/init.d/ (S.u.s.e.)
/etc/init.d/ (Debian)
51
Start/stop deamon
Admin can issuing the command and either the start, stop,
status, restart or reload option
i.e. to stop the web server:
cd /etc/rc.d/init.d/
(or /etc/init.d/ for S.u.s.e. and Debian)
httpd stop
52
References
http://en.wikipedia.org/
http://www-128.ibm.com/developerworks/linux/library/l-linuxboot/
http://yolinux.com/TUTORIALS/LinuxTutorialInitProcess.html
http://www.pycs.net/lateral/stories/23.html
http://www.secguru.com/files/linux_file_structure
http://www.comptechdoc.org/os/linux/commands/linux_crfilest.html
53
Interprocess Communication
54
Interprocess Communication
Processes within a system may be independent or
cooperating
Cooperating process can affect or be affected by other
processes, including sharing data
Cooperating processes need interprocess communication
(IPC)
Two models of IPC
55
Shared memory
Message passing
Communications Models
56
Cooperating Processes
•
•
•
Independent process cannot affect or be affected by the
execution of another process
Cooperating process can affect or be affected by the
execution of another process
Advantages of process cooperation
–
–
–
–
57
Information sharing
Computation speed-up
Modularity
Convenience
Producer-Consumer Problem
Paradigm for cooperating processes, producer process
produces information that is consumed by a consumer
process
58
unbounded-buffer places no practical limit on the size of the
buffer
bounded-buffer assumes that there is a fixed buffer size
Bounded-Buffer – Shared-Memory Solution
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Solution is correct, but can only use BUFFER_SIZE-1
elements
59
Bounded-Buffer – Producer
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
60
Bounded Buffer – Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
// remove an item from the buffer
item = buffer[out];
out = (out + 1) % BUFFER SIZE;
return item;
}
61
Interprocess Communication – Message
Passing
•
•
•
Mechanism for processes to communicate and to
synchronize their actions
Message system – processes communicate with each other
without resorting to shared variables
IPC facility provides two operations:
–
–
•
If P and Q wish to communicate, they need to:
–
–
•
send(message) – message size fixed or variable
receive(message)
establish a communication link between them
exchange messages via send/receive
Implementation of communication link
–
–
62
physical (e.g., shared memory, hardware bus)
logical (e.g., logical properties)
Implementation Questions
•
•
•
•
•
•
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of
communicating processes?
What is the capacity of a link?
Is the size of a message that the link can accommodate
fixed or variable?
Is a link unidirectional or bi-directional?
63
Direct Communication
Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process
Q
Properties of communication link
64
Links are established automatically
A link is associated with exactly one pair of
communicating processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bidirectional
Indirect Communication
Messages are directed and received from mailboxes
(also referred to as ports)
Properties of communication link
65
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication
links
Link may be unidirectional or bi-directional
Indirect Communication
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to
mailbox A
receive(A, message) – receive a message
from mailbox A
66
Indirect Communication
•
Mailbox sharing
–
–
–
•
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Who gets the message?
Solutions
–
–
–
67
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive
operation
Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.
Synchronization
Message passing may be either blocking or nonblocking
Blocking is considered synchronous
Non-blocking is considered asynchronous
68
Blocking send has the sender block until the
message is received
Blocking receive has the receiver block until a
message is available
Non-blocking send has the sender send the
message and continue
Non-blocking receive has the receiver receive a
valid message or null
Buffering
Queue of messages attached to the link;
implemented in one of three ways
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits
69
Examples of IPC Systems - POSIX
•
POSIX Shared Memory
Process first creates shared memory segment
segment id = shmget(IPC PRIVATE, size, S
IRUSR | S IWUSR);
– Process wanting access to that shared memory must attach to
it
shared memory = (char *) shmat(id, NULL, 0);
– Now the process could write to the shared memory
sprintf(shared memory, "Writing to shared
memory");
– When done a process can detach the shared memory from its
address space
shmdt(shared memory);
–
70
Examples of IPC Systems – Windows XP
•
Message-passing centric via local procedure call (LPC)
facility
–
–
–
Only works between processes on the same system
Uses ports (like mailboxes) to establish and maintain
communication channels
Communication works as follows:
•
•
•
•
71
The client opens a handle to the subsystem’s connection port object
The client sends a connection request
The server creates two private communication ports and returns the
handle to one of them to the client
The client and server use the corresponding port handle to send
messages or callbacks and to listen for replies
Local Procedure Calls in Windows XP
72
Communications in Client-Server
Systems
Sockets
Remote Procedure Calls
Remote Method Invocation (Java)
73
Sockets
A socket is defined as an endpoint for communication
Concatenation of IP address and port
The socket 161.25.19.8:1625 refers to port 1625 on
host 161.25.19.8
Communication consists between a pair of sockets
74
Socket Communication
75
Remote Procedure Calls
•
•
•
•
Remote procedure call (RPC) abstracts procedure calls
between processes on networked systems
Stubs – client-side proxy for the actual procedure on the
server
The client-side stub locates the server and marshalls the
parameters
The server-side stub receives this message, unpacks the
marshalled parameters, and peforms the procedure on
the server
76
Execution of RPC
77
Remote Method Invocation
Remote Method Invocation (RMI) is a Java mechanism
similar to RPCs
RMI allows a Java program on one machine to invoke
a method on a remote object
78
Marshalling Parameters
79