phones off please processes parminder singh kang home: pkang email: [email protected]

25
Phones OFF Please Processes Parminder Singh Kang Home: www.cse.dmu.ac.uk/~pkang Email: [email protected]

Post on 22-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

Phones OFF Please

Processes

Parminder Singh Kang

Home: www.cse.dmu.ac.uk/~pkang

Email: [email protected]

Page 2: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

Processes:

• Most modern operating systems provide facilities for:

• Multiple concurrent processes, i.e. can run many processes (applications, system tasks) concurrently

• pseudo parallelism, i.e. processes are running in parallel or apparently in parallel on a machine with more processes than CPUs

• This is called multiprogramming or multiprocessing

• CPU execution time is shared among the processes

• This gives users virtual processors;

• Applications think they have their own CPU and memory

Page 3: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

1 Concept of Process and Program

• Process is program in execution.

•Process is more than the program code;

• includes current activity • content of processor’s registers.

• Process generally includes process stack and data section;

• Process Stack includes;

• method parameters, return address and local variables,

• Data section contains global variables.

• Code – executable code of application.

Page 4: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

•Registers:

• Registers are in the CPU, e.g. data, address and index registers

• Includes the program counter (PC)

Page 5: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

2 Process State 

• During process execution, it goes from one state to another.

• Process state represents current activity of process;

• New: Process is being created.

• Running: Instructions are being executed.

• Waiting: The process is waiting for some event to occur. Such as waiting for I/O event.

• Ready: process is ready for execution and it’s waiting to be assigned to a processor.

• Terminated: process has finished execution.

Page 6: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

New

Ready Running

Terminated

Waiting

AdmittedInterrupted Exit

Scheduler

Event Wait

Event Completed

Page 7: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

3 Supporting multiprocessing/multitasking 

• For multiprocessing/multitasking OS needs:

• Scheduling policy

• Maintain status of each process

• Process communication (IPC) and synchronization.

• Creation of new processes

• Removal of completed processes

• Kernel maintains a process table - one entry per process

• Process identifier – PID in UNIX

• Execution state

• Memory allocation

• Other resources (files etc), Ownership and accounting

Page 8: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

• Process states

• Running

• Ready (to run)

• Blocked, e.g. waiting for I/O

Page 9: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

4 Processes in UNIX: 

• Classical sequential processes;

• Processes runs a single program with a single thread of control. Can have multiple independent processes running.

• With no users processes are running - background system processes, daemons. e.g. print spooler, cron, email, etc.

• The first process created is init.

• Init forks off a login process for each terminal.

• All other processes are created with fork.

• When someone logs in, login forks off a shell.

• The shell forks off user processes as required.

• Shell waits for the child to terminate before carrying on.

Page 10: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

• How to identify user;

• identified by a uid.

• Every process gains the users uid.

• Root has a uid of 0.

• Processes with root's uid can make special system calls.

• e.g. disk space.

• program with the set uid bit on is executed the effective uid becomes the uid of the program's owner (root), not the uid of the user who called it.

This allows users to run a process with root privileges.

Page 11: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

4.1 Low-level process creation

• Function execlp () overlays the current process with the named program.

• e.g. to use ls -l to obtain a list of files in long format in C++:

• execlp("ls", "ls", "-l", NULL);

• Parameters are;

• Filename of the command (i.e. ls in the above example)

• Command and command line parameters to be passed to the child process (as in the command line ls -l).

• The execlp call overlays the existing program with the new one, starts it, and then exits (if execlp fails control returns to the parent, e.g. if the file to execute does not exist).

• execlp (and similar) is used when the parent process is to be terminated.

Page 12: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

4.2 New processes – Fork and Wait

• Need of Fork and Wait:

• Resuming execution of parent process when the child terminates.• Run concurrently with the child ( multiprocessing environment ).

• Under UNIX the function fork makes an exact copy of the process and executes it concurrently with the parent.

• How to identify?

• by Process Id.• fork returns the child process ID to the parent and zero to the child.• if fork fails -1 is returned and errno set.

 

• UNIX creates a new process with the fork system call 

pid = fork();

Page 13: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk
Page 14: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

 OS makes a clone (identical copy) of the processes:

• After the fork both processes execute in parallel

• Original process is the parent and pid is non zero, i.e. the PID of the child

• New process is the child and pid is zero.

• Both processes separate, i.e. have separate copies of global and local data, etc.

• The child and parent then execute in parallel; obviously if there is only one processor they are time-shared.

• Open files are shared between parent and child. Initially, the child will have a copy of the parents memory, but it will be a separate copy.

• Heavyweight processes, i.e. called heavyweight because that have separate stacks, heap, etc.

Page 15: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

e.g.

• A C++ program that forks and the parent prints "The parent, child pid " and the child prints "The child":

     #include <iostream.h>     #include <unistd.h>     int main(void)     {             int pid;             if((pid = fork()) < 0) // create new process                 cout << "Fork failed" << endl;             else                 if (pid == 0) cout << "The child" << endl;                 else cout << "The parent, child pid " << pid << endl;             return 0;     }

Page 16: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

• Once invoked the child can call execlp to carry out the required task.

• parent can wait for a child to terminate:

pid = wait(&status);

• Wait returns the process ID of the terminating child process.

• Parent may have many child processes?

• checked against the value returned by fork

• in the low-order eight bits the systems ideas of the child's exit status

• (0 for normal termination and non-zero for error conditions)

• next high-order eight bits the value returned by the main child's return from or call to exit.

Page 17: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

5 Threads 

• Most systems have lightweight processes – threads

• Lightweight because when new thread is created it uses the same address space as the parent.

•i.e. they share code and global variables – but have own local variables

• e.g. in Java

• Thread p = new Prod();

• Thread c = new Cons();

• p.start();

• c.start();

Page 18: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk
Page 19: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

6 Scheduling  6.1 Concept of Process Scheduling: 

• To maximize CPU utilization, there have some process running all the time.

• How to achieve it; process scheduling.

• In order to switch between various processes to server user and system requirements.

• Scheduling queues: Only one process can run at a time.

• Scheduling queues;

• Job Queue; Job queue represents all the processes in the system. As processes enter the system, they are put into job queue.

• Ready Queue; represents list of all processes, those are ready and waiting to execute.

Page 20: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

•For a process p there are several events that can occur;

• The process could issue an I/O request, and then can be placed in an I/O queue.

• The process could create a new sub process and wait for its termination.

• The process could be removed forcibly from the CPU as a interrupt and be put back in ready queue.

Page 21: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

Ready Queue

I/O Queue I/O Request

Time Slice Expired

Fork a Child

Wait for an Interrupt

CPU

I/O

Child

Executes

Interrupt

Occurs

Page 22: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

6.2 Process Scheduling issues:  

• Primary goal of process scheduling is to maximize CPU utilization.

• In order to achieve this goal, it is important that processes selected by scheduler are good mixture of I/O based and CPU based processes.

• Most often there are two types of schedulers are used;

• Long Term Scheduler or Job Scheduler; Selects processes from the process pool and loads them into main memory for execution.

• Short Term Scheduler or CPU Scheduler; Select among processes ready for executions, and allocate CPU to One of them.

• The main difference between both schedulers is the frequency of their execution; CPU scheduler run more often than long term scheduler.

• Unix only uses Short term Scheduler.

• How to maintain good mixture of processes according to change in memory requirements? (Medium term Scheduler)

Page 23: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

6.3 Round Robin Scheduling 

• Most O/S uses a round robin scheduler – each run able process is given a short burst of time (~ 0.1 sec) on CPU - at end of time O/S reschedules a new process.

• Most are pre-emptive, i.e. can be interrupted to resume later

• UNIX incorporates priorities on top of this

• Processes have different priorities;

• High priority processes run first.

• All processes with the same priority run in a round robin fashion.

• Periodically priorities are recalculated (looking at CPU usage, I/O usage, etc. – in general I/O bound processed increase in priority and CPU bound decrease)

Page 24: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

• Thus there is a stack of priority based round robin lists:

• Using a round robin scheduler the highest priority processes that can run is given a short burst of time

• At end of time the next process of same priority runs Etc.

• When all processes of this priority have run go back to 1

• Note:

• At any time a higher priority process may become run able and be given to the CPU

• If high priority tasks are blocked the scheduler works down priority list until it find one that can run

• How is process stopped?

• Voluntarily – when it makes a system call

• Involuntarily – when an interrupt occurs. 

Page 25: Phones OFF Please Processes Parminder Singh Kang Home: pkang Email: pkang@dmu.ac.uk

Clock

• Computer has a clock (counter) which counts down to zero.

• When it gets to zero it generates an interrupt and resets counter.

• Generates regular interrupts. Real Time Processes 

• When an event occurs process has to be started within critical time.

• Process should run to completion.

• Hard real time – essential system meets deadlines (e.g. process control)

• Soft real time – desirable system meets deadlines (e.g. multimedia)