Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter three.  An operating system executes a variety of programs:  A batch system executes jobs.  A time-shared systems has user programs or tasks.

Similar presentations


Presentation on theme: "Chapter three.  An operating system executes a variety of programs:  A batch system executes jobs.  A time-shared systems has user programs or tasks."— Presentation transcript:

1 Chapter three

2  An operating system executes a variety of programs:  A batch system executes jobs.  A time-shared systems has user programs or tasks.  A single user system, such as MS windows, a user may able to run several programs at one time, such as MS-word, a web browser, and an email.  All these activities are called processes.  The terms job and process are used interchangeably 2

3  A process is a program in execution.  A process is more than the program code (text section), it includes the current activity represented by the value of the program counter and the contents of the processor's registers.  A process also includes the process stack, which contains temporary data, such as function parameters and local variables.  In addition, a process contains a data section, which contains global variables.  A process may also include a heap, which is memory that’s dynamically allocated during process run time.  The structure of a process in memory is: 3

4 4

5  A process is not same as program.  A program is a passive entity (executable file), because it is a file containing a list of instructions stored on disk while a process is an active entity (program in execution), with a program counter specifying the next instruction to execute.  Many people can run the same program; and each copy of this program corresponds to a distinct process.  A program becomes a process when it is loaded into memory for execution. 5

6  Two common techniques for loading executable files are double clicking an icon representing the executable file and entering the name of the executable file on the command line.  Although two processes many be associated with the same program, they nevertheless considered two separate execution sequences. For instance, several users may be running different copies of the mail program, or the same user may invoke many copies of the web browser. Each of theses is a separate processes with the same text section, but the data, heap, and stack sections vary. 6

7  Example:  main()  { int i, prod=1;  for (i=0;i<100;i++)  prod*=i}  The above code is a program containing one multiplication statement (passive). However, when it’s loaded into memory for execution, it becomes a process (active), which will execute 100 multiplication statements, one at time through the for loop. 7

8  As a process executes, it changes state  new : The process is being created.  running : Instructions are being executed.  waiting : The process is waiting for some event to occur, such as I/O completion or reception of signal.  ready : The process is waiting to be assigned to a processor.  terminated : The process has finished execution. 8

9 9 Only one process can be running on any processor at any instant. Many processors may be ready and waiting.

10  In OS, each process is represented by a process control block or task control block.  A PCB is a data structure that physically represents a process in the memory of a computer system.  It contains information associated with a specific process:  Process state.  Program counter.  CPU registers.  CPU scheduling information.  Memory-management information.  Accounting information.  I/O status information. 10

11  Process state: may be new, ready, running, waiting, or terminated.  Program counter: indicates the address of the next instruction to be executed for this process.  CPU registers: they include index registers, stack pointers, and general purpose registers, and condition code information. Along with the program counter, this information must be saved when an interrupt occurs, to allow the process to be continued correctly.  CPU scheduling information: includes priority and pointers to scheduling queues. 11

12  Memory-management information: includes the value of the base and limit registers, page tables, or segment tables.  Accounting information: includes the amount of cpu and real time used and process numbers.  I/O status information: includes the list of I/O devices allocated to the process, a list of open files.  The PCB serves as the respiratory for any information that may vary from process to process.  The content, format, and name of this structure will vary, but some characteristics will be similar from system to system (Assignment: implement PCB). 12

13 13

14 14

15 Trace of Process 15

16  Switching from one process to another in a system requires saving the state of the old one and loading the saved for the new process. This task is known as context switching.  The context of a process is represented in the PCB of a process that includes various information.  When a context switch occurs, the kernel saves the context of old process in its PCB and loads the context of new process that is currently to run.  The system is idle while switching.  Switching is required when an interrupt occurs. 16

17 17

18

19  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. 19

20  As processes enter the system, they are put into a job queue.  The processes that are residing in main memory and are ready and waiting to execute are kept on a list called ready queue.  The processes waiting for a particular I/O device are kept on a list called device queue.

21 21

22 22

23  A new process is initially put in the ready queue. It waits there until it is selected for execution, or it’s dispatched.  Once the process is allocated the CPU and is executing, one of several events could occur:  The process could issue an I/O request and then be placed in an I/O queue.  The process could create a new subprocess and wait for the subprocess’s termination.  The process could be removed forcibly from the CPU as a result of an interrupt, and be put back in the ready queue.

24  In the first two cases, the process switches from the waiting state to the ready state and is then put back in the ready queue.  A process continues this cycle until it terminates, at which time it’s removed from all queues and has its PCB and resources deallocated.

25  The operating system must select, for scheduling purposes, processes from these queues in some fashion. The selection process is carried out by the appropriate scheduler.  Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue.  Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.

26  Short-term scheduler is invoked very frequently (milliseconds)  (must be fast).  Long-term scheduler is invoked very infrequently (seconds, minutes)  (may be slow). 26

27 27

28 28

29  The long-term scheduler controls the degree of multiprogramming (the number of processes in memory).  Processes can be described as either:  I/O-bound process – spends more time doing I/O than computations.  CPU-bound process – spends more time doing computations and generates infrequently I/O requests.  The long-term scheduler select a good process mix of I/O bound and CPU bound. 29

30  If all processes are I/O bound, the ready queue will always be empty, and the short-term scheduler will have little to do.  If all processes are cpu bound, the I/O waiting queue will almost always be empty, devices will go unused, and again the system will be unbalanced. The system with the best performance will have a combination of cpu bound and I/O bound processes. 30

31  Microsoft Windows is a time-sharing system, hence every process is put in memory for the short-term scheduler. Therefore MS windows have no long-term scheduler. 31

32  In most system, the processes can execute concurrently, and they may be created and deleted dynamically. Thus systems must provide a mechanism for process creation and termination. 32

33  If a user requests that a file be printed, the OS can create a process that will manage the printing.  A process is created when a new user attempts to log on.  Created by OS to provide a service  a process to control printing.  a process to control network connection. 33

34  When a new process is to be added to those currently being managed, the OS builds the data structures that are used to manage the process and allocates address space in main memory to the process.  A process may create several processes, via a create- process system call, during its time of execution  The creating process is called a parent process and the new processes are called the children of that process.  Each of these new processes may further create other process, forming a tree of processes. 34

35  OSes, such as windows and Unix identify processes according to a unique process identifier (pid), which is a integer number.  A process needs certain resources, such as CPU, memory, or I/O devices to accomplish its task.  When a process creates a subprocess, it obtains its resources form the OS, or it obtains a subset of the resources of its parent. 35

36  The parent may partition its resources among its children, or it may share some resources, such as memory or files.  The sharing is better, to allow child to create subprocesses.  For Execution  Parent and children execute concurrently, then  Parent waits until children terminate. 36

37  Address space  Child duplicate of parent, then  Child has a program loaded into it. 37

38  An UNIX example:  int main()  { int pid;  pid=fork();// fork a child process  if(pid<0) {cout<<“fork failed”; return 1;}  // child process  else if(pid==0){execlp(“/bin/ls”,”ls”,NULL);}  else {// parent process  wait(NULL); cout<<“child complete”;}  return 0;  } 38

39  A new process is created by the fork() system call.  The new process (child) duplicates the parent process. Therefore, there are two processes running the same c++ program with the same data.  Both processes (parent and child) continue execution concurrently at the fork(), with one difference: the return code (pid) for fork() is zero for new process (child), but nonzero for parent program. 39

40  In a child program, execlp() system call replaces the process’s memory space (program copy of child) with a new program. execlp() loads a binary file into memory and destroys the memory image of the program containing execlp() (parent’s program). Therefore, the new process has a new program loaded into memory.  The parent can create more children; or if it has nothing to do while the child runs, it can call wait() system call to move itself to the ready queue until the termination of all the children. 40

41 41

42  When the child completes, exit() system call is invoked, and the parent resumes from the calling of wait(). 42

43  A process terminates when it finishes executing its final instruction and asks the OS to delete it by calling exit() system call  The child process returns the output value to its parent via a wait() system call.  All the resources are deallocated by the OS.  Only the parent process can terminate its children processes by calling abort() system call. Hence, the parent must know the pids of all its children at the time of termination. Thus, when a new child process is created, its identity is passed to its parent. 43

44  A parent may terminate one of its children for:  Exceeding usage of resources.  The task assigned is completed or no longer required.  The parent want to exist from the system, and the OS doesn’t allow a child to continue if it’s parent terminates. In these systems, if a process terminates, then all its children must also be terminated. It’s called cascading termination. 44

45  Processes executing concurrently within a system may be independent or cooperating.  Independent process cannot affect or be affected by the execution of another process.  Cooperating process can affect or be affected by other processes.  Any process sharing data with other processes is a cooperating process. 45

46  Reasons for cooperating processes:  Information sharing: several users interested in same information.  Computation speedup: a task is broken into subtasks to run faster (needs multi processing elements).  Modularity: function separation processes or threads.  Convenience:user work on many tasks in parallel. 46

47  Cooperating processes need interprocess communication ( IPC ) mechanism to:  Allow exchange data and information  Two models of IPC  Shared memory.  Message passing. 47

48 A region of memory that’s shared by cooperating processes is established by one of them. Processes can exchange information by reading and writing data to the shared region. 48

49  Mechanism for processes to communicate without shared memory.  Like chat programs.  IPC facility provides two operations:  send ( message ) – message size fixed or variable.  receive ( message ). 49

50 50

51  Sockets  Remote Procedure Calls  Remote Method Invocation (Java) 51

52

53  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 53

54 54

55  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

56

57  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

58


Download ppt "Chapter three.  An operating system executes a variety of programs:  A batch system executes jobs.  A time-shared systems has user programs or tasks."

Similar presentations


Ads by Google