OperatingSystemLectures - Ferdowsi University of Mashhad

Download Report

Transcript OperatingSystemLectures - Ferdowsi University of Mashhad

Operating System Concepts and Techniques
Lecture 3
Process
M. Naghibzadeh
Reference
M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011.
To order: www.iUniverse.com, www.barnesandnoble.com, or www.amazon.com
Process
Process: born to run programs
Inside a computer there is a little
society of processes; Processes are
born, live, and die
Operating system is the governing body
of processes
It must keep information about
processes, called process attributes, to
be able to manage them
2
Some attributes
Process identification information
Process ID
Parent process
User identifier
Group identifier
Process state information
Process-visible registers
Location counter
Condition code flags
Stack pointers
Process control information
Status
Priority
Process start time
CPU time used
Children’s CPU time used
Scheduling parameters
Links
Process privileges
Memory pointers
Devices assigned to the process
Open files
Flags
3
Process Control Block (PCB)
Process attributes are kept in an
operating system structure called PCB;
One for each process
There is one process table in the system
Each row is for one process; it either has all
PBC information of the process or some of
it and a pointer to the PCB; the latter is our
convention
When process is terminated its PCB is
removed

4
Process table
Process properties
State
Properties of P1
blocked
Properties of P2
ready
Properties of P3
blocked
Properties of P4
running
Properties of P5
ready
Properties of P6
ready
Properties of P7
blocked
Properties of P8
ready
Properties of P9
ready
Properties of P10
blocked
Link
Link to PCB
Front of ready queue
Rear of ready queue
5
System calls
Kernel is robust, fault-free, and efficient
It includes numerous useful procedures
Some we can call from application
programs, these are called system calls,
supervisor calls, or kernel services
Kernel is protected from outside world,
entering and leaving is via an strict
controlled mechanism.
6
Breaking into kernel
…
call a kernel service from an
application program
….
A
B
Other
layers of
the
operating
system
Kernel Barrier
A kernel service
Although a kernel routine is
used by an application
program, it is like taking a
prisoner to an outside
hospital for medical services
while handcuffed and
accompanied by two guards
Kernel
Kernel barrier is busted twice as shown at points A and B
7
Some system calls
Process Management
File System
pid = fork()
fd = create (name, mode)
pid = waitpid (pid, &static, options)
s = execve (name, argv, envp)
exit (status)
fd = open (file, how)
s = close (fd)
n = read (fd, buffer, nbytes)
s = kill (pid, sig)
pause ()
wait (&status)
n = write (fp, buffer, nbytes)
pos = lseek (fd, offset, whence)
alarm (sec)
s = stat (name, &buf)
Interprocess Communication
s = mkdir (path, mode)
qid = msgget(queueid, rights)
s = rmdir (path)
msgsnd(qid, &mymsg, size, flags)
s = unlink (name)
msgrcv(qid, &mymsg, size, type, flags)
semid = semget(key, nsems, rights)
semop( semid, operation)
s = chdir (name)
s = chmode (name, mode)
8
Unix Process State Transition Model
The corresponding
state in the 3-stae
model
UNIX States
Process birth
Created
Main memory
assigned
Preempted
Imaginary
line
A process is
picked to
run
Back to
running
Ready to Run in
Memory
Not enough
main memory
Memory shortage
swap out
Swap in
Ready to Run
Swapped
Sleep
condition
ended
Running obstacle is
vanished
Asleep
Memory needed,
swap out
Sleep Swapped
In Memory
Preempt
User Running
Ready
Wait/
Blocked
Needs I/O or circumstance
Return
Kernel Running
System call
or interrupt
Interrupt, Interrupt
return
Running
Exit, but not disappeared
Zombie
9
Why are Processes Created?
Processes are the means of running
programs to perform tasks
Processes are created from executable
files
10
When is a Process Created?
At computer start or restart, by OS
Explicit request by a computer user



double clicking on an executable file
opening an executable program file
issuing a command to run a program, etc.
In response to a request from a running
process to create a child process
11
Process creation/termination
Why are processes created?
When is a process created?
What is done to create a process?
What are the tools for creating a process?
What are common in parent and child
processes?
What are different between parent and child
processes?
How is a process terminated?
12
What is done to create a process?
Ensure the degree of multiprogramming does not been exceeds
its maximum
Generate a unique process ID
Use one row of Process table
Allocate space for the PCB and initialize proper fields; then make
the connection between the corresponding row of the process
table and this PCB
Allocate space for process context
Allocate space for other preliminary structures such as stacks
and stack pointers and initialize the related fields in PCB
Put the process in one of the queues, usually ready queue
13
Process creation using
fork()
void main(void)
{
int pid;
int retval;
int status;
// Pointer to the value returned by the child process
pid = fork(); // It is assume that, there is no obstacle in creating
// the child process
if (pid != 0) // This is the parent process
{
proc1; // Parent process will continue running procedure proc1
wait (&status); // Wait until the child process is terminated
}
else
{
proc2; // Child process will continue running procedure proc2
status = …; // Provide a valid value for the returned status to parent process
exit (status);
}
}
14
How is a Process Terminated?
When its execution is completed it is logically
terminated


For the physical termination of a process, all memory
occupied as process images and stacks are first freed. Then,
the process control block is freed and the process is removed
from the process table
Logical termination of a process takes less processing time
than its physical termination. For logical termination it
suffices to change the terminating process’s state to
terminated and to actually add this process to the set of
terminated processes.
It could be killed by OS or user
15
Summary
Processes are one of the central issues of every operating system
Processes are created to run programs
Process state transition diagram shows the living states of process and
its transactions
The request to create a process could come from a user, an application
process, or the operating system itself
The same requesters could order the destruction of a process or it could
be terminated automatically when its duty is finished or when a fatal
fault has occurred
A process that requests the creation of another process becomes the
parent process and the created process is called the child process
A child process goes its own way right after it is created
As UNIX is a process-based operating system, this chapter was able to
use actual examples from this operating system.
16
Find out
The purpose of system calls
The purpose of the transitions from kernel to
itself
Complete set of Unix system calls
The deficiencies of process
How we can create a child process to run a
different program (not a different procedure)
Whether Linux is a process-based operating
system or a thread-based one
17
Any questions?
18