Académique Documents
Professionnel Documents
Culture Documents
3. Processes
2011/2012
Lus Moura Silva Email: luis@dei.uc.pt Departamento de Eng. Informtica Universidade de Coimbra
Disclaimer
This slides and notes are heavily based on the companion material of [Silberschatz05].The original material can be found at:
http://codex.cs.yale.edu/avi/os-book/os7/slide-dir/index.html
In some cases, material from [Stallings04] may also be used. The original material can be found at:
http://williamstallings.com/OS/OS5e.html
Whats a Process?
MS Word (1 process)
MS Excel (1 process)
Program = Binary Image (Executable) Process = The living Image of a program running
It includes information like: - Program Counter - Allocated Memory / Address Space - Identifier, Owner, Security Attributes, etc.
A process in Memory
4GB
Address Space
My Process
0
4
.data section global and static initialized variables to non-zero .bss section global and static non-initialized variables (or initialized to 0)
Variables that are automatically created and destroyed in methods It grows down
5
.stack segment
possible hole in address space! heap non-initialized static data initialized static data code (shared) (low address)
Process States
Notes: Waiting is also known as Blocked Processes in the Ready and Blocked states do not consume CPU! This is very important!
9
Process identifier Process state Program Counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information List of open files
10
11
12
Process Queues
Job queue
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 (also known as blocked queues)
13
14
Process Scheduler
Note that events do not necessarily correspond to I/O (more on this latter)
15
16
Even in Windows
Server Multitasking Operating Systems User Multitasking Operating Systems Batch Operating Systems Real-time Operating Systems Embedded Operating Systems Multimedia Operating Systems
17
Suspended States
In some operating systems, additional states are introduced for describing processes that have been swapped to disk
18
19
20
Types of Processes
I/O Bound
Spend more time doing I/O than computation (Live mostly in the blocked queue)
CPU Bound
Spend more time doing computation than I/O (Live mostly in the ready queue)
For a good utilization of computer resources its important to have a good mix of I/O bounded and CPU bounded processes: the long-term scheduler is responsible for this
21
Process Creation
Parent process create children processes, which in turn create other processes, forming a tree of processes
Process Tree in Solaris
22
The way resources are shared between parent and child processes varies widely among operating systems
Resource sharing
Parent and children share all resources Children share subset of parents resources Parent and children share no resources
Execution
Parent and children execute concurrently Parent waits until children terminate Children terminate if parent terminates (cascading termination)
23
Process creation in Unix is based on spawning child processes which inherit all the characteristics of their fathers
Original Process
Variables, program counter, open files, etc. Spawning a process is done using the fork() system call
After forking, each process will be executing having different variables and different state.
Original Process
The Program Counter will be pointing to the next instruction Changing a variable in the child program doesnt affect its father (and vice-versa)
24
Forking in Unix
#include #include #include #include <stdio.h> <unistd.h> <sys/wait.h> <sys/types.h>
int main() { pid_t id; id = fork(); if (id == 0) { printf("[%d] I'm the son!\n", getpid()); printf("[%d] My parent is: %d\n", getpid(), getppid()); } else { printf("[%d] I'm the father!\n", getpid()); wait(NULL); } return 0; }
25
Process Management
Each
process has an unique identifier (PID). Each process has a father, which is also identified (PPID).
pid_t pid_t pid_t
getpid(void); getppid(void);
Returns the PID of the current process. Returns the PID of the parent process.
fork(void); Creates a new process which inherits all its fathers state. It returns 0 on the child process and the childs PID in the original process.
pid_t
wait(int* status); Waits until a child process exits. The status of the child is set in status. (status is the return value of the process)
pid_t
waitpid(pid_t who, int* status, int options); Same as wait() but allows to wait for a particular child. In options, by using WNOHANG in options, allows for checking if a child has already exited without blocking. 0 in who means wait for any child.
26
Copy-on-Write
Nowadays, the memory of a process is organized in pages of typically 4KB After a fork(), the child process and the parent process share the same pages The operating system detects when a page is being written either by the parent process or the child process. When that happens, a copy is made on demand, before the write is allowed to proceed. This is called copy-on-write Thus, changing a variable on a child process doesnt affect its parent and vice-versa
27
For making system calls and traps fast, it is possible to jump directly to the kernel handler routine without remapping any memory
One that is specific of that process One that corresponds to the kernel and is shared by all processes
The user process does not have direct access to the kernel memory; it cannot read nor write that part of the address space Whenever a trap occurs, it enters in kernel mode and thus has access to the already mapped memory
28
1GB
0x40000000
3GB
Program Data
0x08048000 0x00000000
29
A process is only truly eliminated by the operating system when its father calls wait()/waitpid() on it.
This allows the parent to check things like the exit code of its sons
Zombie Process: One that has died and its parent has not acknowledged its death (by calling wait())
Be careful with this if your are designing servers. They are eating up resources!!
Orphan Process: One whose original parent has died. In that case, its parent becomes init (process 1).
30
void worker() { printf("[%d] Hi, I'm a worker process! Going to die...\n", getpid()); } int main() { for (int i=0; i<10; i++) { if (fork() == 0) { worker(); exit(0); } } printf("[%d] Big father is sleeping!\n", getpid()); sleep(10); return 0; }
31
Zombies (2)
32
34
35
36
Allow to substitute the current process executable image by another one. The substitution is complete! The functions that have a p make use of the environment PATH; The functions that have a v make use of a pointer to an array on parameters; The functions that have an l have the parameters passed separated by commas Make sure that the first parameter is the name of the program!
37
Example
The code, the stack, the heap, its all replaced by the new executable exec()
Original Code ls code
ls code
38
int main() { if (execlp("ls", "ls", "-a", NULL) == -1) perror("Error executing ls: "); else printf("This cannot happen!\n"); return 0; }
char* ls_param[] = { "ls", "-a", NULL }; if (execvp(ls_param[0], ls_param) == -1) perror("Error executing ls: ");
39
Normal completion Time limit exceeded Memory unavailable Bounds violation Protection error I/O failure Invalid instruction Operating system intervention (e.g. on deadlock) Parent terminates so child processes terminate Parent request
40
Reference
41