Implementing Processes

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
1 (Review of Prerequisite Material). Processes are an abstraction of the operation of computers. So, to understand operating systems, one must have a.
6/9/2015B.Ramamurthy1 Process Description and Control B.Ramamurthy.
Process Description and Control
Process Description and Control Module 1.0. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Process Description and Control Chapter 3. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Processes CSCI 444/544 Operating Systems Fall 2008.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
CSCE 351: Operating System Kernels
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
Process Description and Control A process is sometimes called a task, it is a program in execution.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
Introduction to Processes CS Intoduction to Operating Systems.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
Chapter 41 Processes Chapter 4. 2 Processes  Multiprogramming operating systems are built around the concept of process (also called task).  A process.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Chapter 5 - Implementing Processes Process is the most fundamental object Process is a program executing in a virtual computer Process represented in the.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Processes 2 Introduction to Operating Systems: Module 4.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Multiprogramming. Readings r Chapter 2.1 of the textbook.
WORKING OF SCHEDULER IN OS
Processes and threads.
Chapter 5 - Implementing Processes
Process concept.
Protection of System Resources
Chapter 3 – Process Concepts
Chapter 3: Process Concept
Operating Systems: A Modern Perspective, Chapter 6
Parallel Systems Chapter 6 9/17/2018 Crowley OS Chap. 6.
The Client-Server Model
Processes Chapter 8 9/21/2018 Crowley OS Chap. 8.
The Hardware Interface
Direct Memory Access Disk and Network transfers: awkward timing:
Process Description and Control
Process Description and Control
Process Description and Control
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Process Description and Control
Process Description and Control
Process Description and Control
Process Description and Control
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix and Windows
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Process Description and Control
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Implementing Processes Chapter 5 9/17/2018 Crowley OS Chap. 5

Key concepts in chapter 5 Simple operating systems (SOS) Implementation of processes System initialization Process switching System call handling Waiting in the OS Operating systems as table and event managers 9/17/2018 Crowley OS Chap. 5

Implementing processes by interleaving the processor 9/17/2018 Crowley OS Chap. 5

System call interface of the Simple OS (SOS) CreateProcess(int firstBlock, int nBlocks) ExitProcess(int exitCode) CreateMessageQueue() SendMessage(int mqid, int *msg) ReceiveMessage(int mqid, int *msg) ReadDiskBlock(int block, char *buffer) WriteDiskBlock(int block, char *buffer) 9/17/2018 Crowley OS Chap. 5

SOS objects and operations 9/17/2018 Crowley OS Chap. 5

SOS architecture 9/17/2018 Crowley OS Chap. 5

System constants (1 of 2) // Boolean values enum { False=0, True=1 }; // hardware constants (determined by the hardware) const int DiskBlockSize = 4096; const int NumberOfRegisters = 32; // system constants (we can change these constants // to tune the operating system) const int SystemStackSize = 4096; // bytes const int ProcessSize = 512*1024; // bytes const int TimeQuantum = 100000; // 100000 microseconds = 100 milliseconds const int MessageSize = 8; // 8 words = 32 bytes const int InitialProcessDiskBlock = 4341; //disk block # const int EndOfFreeList = -1; 9/17/2018 Crowley OS Chap. 5

System constants (2 of 2) // system limits (we can change these) const int NumberOfProcesses = 20; const int NumberOfMessageQueues = 20; // The total number of message buffers const int NumberOfMessageBuffers = 100; // event handler offsets (determined by the hardware) const int SystemCallHandler = 0; const int TimerHandler = 4; const int DiskHandler = 8; const int ProgramErrorHandler = 12; // system call numbers (arbitrary numbers, // as long as they are all different) const int CreateProcessSystemCall = 1; const int ExitProcessSystemCall = 2; const int CreateMessageQueueSystemCall = 3; const int SendMessageSystemCall = 4; const int ReceiveMessageSystemCall = 5; const int DiskReadSystemCall = 6; const int DiskWriteSystemCall = 7; 9/17/2018 Crowley OS Chap. 5

Process global data struct SaveArea { int ia, psw, base, bound, reg[NumberOfRegisters]; }; enum ProcessState { Ready, Running, Blocked }; typedef int Pid; struct ProcessDescriptor { int slotAllocated; int timeLeft; // time left from the last time slice ProcessState state; // ready, running or blocked SaveArea sa; // register save area }; int current_process int SystemStack[SystemStackSize]; ProcessDescriptor pd[NumberOfProcesses]; // pd[0] is the system 9/17/2018 Crowley OS Chap. 5

Message global data typedef int MessageBuffer[MessageSize]; MessageBuffer message_buffer[NumberOfMessageBuffers]; int free_message_buffer; int message_queue_allocated[NumberOfMessageQueues]; Queue<int> * message_queue[NumberOfMessageQueues]; struct WaitQueueItem { Pid pid; char * buffer; }; Queue<WaitQueueItem *> * wait_queue[NumberOfMessageQueues]; 9/17/2018 Crowley OS Chap. 5

Interrupt vector area char * SystemCallVector = &SystemCallInterruptHandler; char * TimerVector = &TimerInterruptHandler; char * DiskVector = &DiskInterruptHandler; char * ProgramErrorVector = &ProgramErrorInterruptHandler 9/17/2018 Crowley OS Chap. 5

Disk global data int process_using_disk; struct DiskRequest { int command; int disk_block; char * buffer; int pid; }; Queue<DiskRequest *> disk_queue; 9/17/2018 Crowley OS Chap. 5

Message buffer allocation procedures int GetMessageBuffer( void ) { // get the head of the free list int msg_no = free_message_buffer; if( msg_no != EndOfFreeList ) { // follow the link to the next buffer free_message_buffer = message_buffer[msg_no][0]; } return msg_no; } void FreeMessageBuffer( int msg_no ) { message_buffer[msg_no][0] = free_message_buffer; free_message_buffer = msg_no; } 9/17/2018 Crowley OS Chap. 5

Process creation int CreateProcessSysProc(int first_block,int n_blocks) { int pid; for( pid = 1; pid < NumberOfProcesses; ++pid ) { if( pd[pid].slotAllocated ) break; } if( pid >= NumberOfProcesses ) { return -1; } pd[pid].slotAllocated = True; pd[pid].state = Ready; pd[pid].sa.base = pid * ProcessSize; pd[pid].sa.bound = ProcessSize; pd[pid].sa.psw = 3; // user mode, interrupts enabled pd[pid].sa.ia = 0; char * addr = (char *)(pd[pid].sa.base); for( i = 0; i < n_blocks; ++i ) { while( DiskBusy() ) ; IssueDiskRead( first_block + i, addr, 0/*no int*/); addr += DiskBlockSize; } return pid; } 9/17/2018 Crowley OS Chap. 5

Process states 9/17/2018 Crowley OS Chap. 5

Dispatcher void Dispatcher( void ) { current_process = SelectProcessToRun(); RunProcess( current_process ); } 9/17/2018 Crowley OS Chap. 5

Select a process to run int SelectProcessToRun( void ) { static int next_proc = NumberOfProcesses; if( current_process > 0 && pd[current_process].state == Ready && pd[current_process].timeLeft > 0 ) { pd[next_proc].state = Running; return current_process; } for( int i = 1; i < NumberOfProcesses; ++i ) { if( ++next_proc >= NumberOfProcesses ) next_proc = 1; if( pd[next_proc].slotAllocated && pd[next_proc].state == Ready ) { pd[next_proc].timeLeft = TimeQuantum; pd[next_proc].state = Running; return next_proc; } } return -1; } 9/17/2018 Crowley OS Chap. 5

Run a process void RunProcess( int pid ) { if( pid >= 0 ) { SaveArea * savearea = &(pd[pid].sa); int quantum = pd[pid].timeLeft; asm { load savearea+0,iia load savearea+4,ipsw load savearea+8,base load savearea+12,bound loadall savearea+16 load quantum,timer rti } } else { waitLoop: goto waitLoop; } } 9/17/2018 Crowley OS Chap. 5

The system stack All code needs a stack the compiler expects to have one for us by the running program We play tricks on the C++ compiler and fiddle with its stack 9/17/2018 Crowley OS Chap. 5

Timer interrupt handler void TimerInterruptHandler( void ) { if( current_process > 0 ) { SaveArea * savearea = &(pd[current_process].sa); asm { store iia,savearea+0 store ipsw,savearea+4 store base,savearea+8 store bound,savearea+12 storeall savearea+16 load SystemStack+SystemStackSize,r30 } } pd[current_process].timeLeft = 0; pd[current_process].state = Ready; Dispatcher(); } 9/17/2018 Crowley OS Chap. 5

System initialization int main( void ) { asm{ load SystemStack+SystemStackSize,r30 } asm{ load &SystemCallVector,iva } pd[0].slotAllocated = True; pd[0].state = Blocked; for( i = 1; i < NumberOfProcesses; ++i ) pd[i].slotAllocated = False; (void)CreateProcessSysProc( InitialProcessDiskBlock, 1 ); for( i = 0; i < (NumberOfMessageBuffers-1); ++i ) message_buffer[i][0] = i + 1; message_buffer[NumberOfMessageBuffers-1][0] = EndOfFreeList; free_message_buffer = 0; for( i = 0; i < NumberOfMessageQueues; ++i ) message_queue_allocated[i] = False; process_using_disk = 0; Dispatcher(); } 9/17/2018 Crowley OS Chap. 5

The initial process void main() { // start the two counting processes (void)CreateProcess( UserProcessA, UserProcessASize ); (void)CreateProcess( UserProcessB, UserProcessBSize ); // Nothing else for this process to do. // We haven't implemented a Wait system call, // so just exit. ExitProcess( 0 ); } 9/17/2018 Crowley OS Chap. 5

Initial process creates other processes 9/17/2018 Crowley OS Chap. 5

Process switching 9/17/2018 Crowley OS Chap. 5

Flow of control within a process 9/17/2018 Crowley OS Chap. 5

Process switching control flow 9/17/2018 Crowley OS Chap. 5

Flow of control during process switching (another view) 9/17/2018 Crowley OS Chap. 5

System call interrupt handler 9/17/2018 Crowley OS Chap. 5

System call interrupt handler (1 of 6) void SystemCallInterruptHandler( void ) { SaveArea * savearea = &(pd[current_process].sa); int saveTimer; asm { store timer,saveTimer load #0,timer store iia,savearea+0 store ipsw,savearea+4 store base,savearea+8 store bound,savearea+12 storeall savearea+16 load SystemStack+SystemStackSize,r30 } pd[current_process].timeLeft = saveTimer; pd[current_process].state = Ready; int system_call_number; asm { store r8,system_call_number } switch( system_call_number ) { 9/17/2018 Crowley OS Chap. 5

System call interrupt handler (2 of 6) case CreateProcessSystemCall: // get the system call arguments from the registers int block_number; asm { store r9,block_number } int number_of_blocks; asm { store r10,number_of_blocks } // put the return code in R1 pd[current_process].sa.reg[1] = CreateProcessSysProc( block_number,number_of_blocks); break; case ExitProcessSystemCall: char * return_code; asm { store r9,return_code } // we don't save the return code in this OS so // just free up the pd slot pd[current_process].slotAllocated = False; break; 9/17/2018 Crowley OS Chap. 5

System call interrupt handler (3 of 6) case CreateMessageQueueSystemCall: // find a free message queue int i; for( i = 0; i < NumberOfMessageQueues; ++i ) { if( !message_queue_allocated[i] ) { break; } } if( i >= NumberOfMessageQueues ) { // signal the error, message queue overflow // return a value that is invalid pd[current_process].sa.reg[1] = -1; break; } message_queue_allocated[i] = True; message_queue[i] = new Queue<int>; wait_queue[i] = new Queue<WaitQueueItem *>; pd[current_process].sa.reg[1] = i; break; 9/17/2018 Crowley OS Chap. 5

System call interrupt handler (4 of 6) case SendMessageSystemCall: int * user_msg; asm { store r9,user_msg } int to_q; asm { store r10,to_q } if( !message_queue_allocated[to_q] ) { pd[current_process].sa.reg[1] = -1; break; } int msg_no = GetMessageBuffer(); if( msg_no == EndOfFreeList ) { pd[current_process].sa.reg[1] = -2; break } CopyToSystemSpace( current_process, user_msg, message_buffer[msg_no], MessageSize ); if( !wait_queue[to_q].Empty() ) { WaitQueueItem item = wait_queue[to_q].Remove(); TransferMessage( msg_no, item.buffer ); pd[item.pid].state = Ready; } else message_queue[to_q].Insert( msg_no ); pd[current_process].sa.reg[1] = 0; break; 9/17/2018 Crowley OS Chap. 5

System call interrupt handler (5 of 6) case ReceiveMessageSystemCall: int * user_msg; asm { store r9,user_msg } int from_q; asm { store r10,from_q } // check for an invalid queue identifier if( !message_queue_allocated[from_q] ) { pd[current_process].sa.reg[1] = -1; break; } if( message_queue[from_q].Empty() ) { pd[current_process].state = Blocked; WaitQueueItem item; item.pid = current_process; item.buffer = user_msg; wait_queue[from_q].Insert( item ); } else { int msg_no = message_queue[from_q].Remove(); TransferMessage( msg_no, user_msg ); } pd[current_process].sa.reg[1] = 0; break; 9/17/2018 Crowley OS Chap. 5

System call interrupt handler (6 of 6) case DiskReadSystemCall: case DiskWriteSystemCall: char * buffer; asm { store r9,buffer } buffer += pd[current_process].sa.base; // convert to physical address int disk_block; asm { store r10,disk_block } DiskIO( system_call_number, disk_block, buffer ); pd[current_process].sa.reg[1] = 0; break; } Dispatcher(); } 9/17/2018 Crowley OS Chap. 5

Send and receive cctions 9/17/2018 Crowley OS Chap. 5

Transfer between system and user memory void CopyToSystemSpace( int pid, char * from, char * to, int len ) { from += pd[pid].sa.base; while( len-- > 0 ) *to++ = *from++; } void CopyFromSystemSpace( int pid, char * to, char * from, int len ) { to += pd[pid].sa.base; while( len-- > 0 ) *to++ = *from++; } 9/17/2018 Crowley OS Chap. 5

Program error interrupt handler void ProgramErrorInterruptHandler( void ) { asm { // stop the interval timer // and clear any pending timer interrupt load #0,timer // no need to save the processor state // // set up the stack load SystemStack+SystemStackSize,r30 } pd[current_process].slotAllocated = False; Dispatcher(); } 9/17/2018 Crowley OS Chap. 5

Disk I/O void DiskIO( int command, int disk_block, char * buffer ) { // Create a new disk request // and fill in the fields. DiskRequest * req = new DiskRequest; req->command = command; req->disk_block = disk_block; req->buffer = buffer; req->pid = current_process; // Then insert it on the queue. disk_queue.Insert( req ); pd[current_process].state = Blocked; // Wake up the disk scheduler if it is idle. ScheduleDisk(); } 9/17/2018 Crowley OS Chap. 5

Disk scheduling void ScheduleDisk( void ) { // If the disk is already busy if( DiskBusy() ) return; DiskRequest * req = disk_queue.Remove(); // no disk request to service so return. if( req == 0 ) return; // remember process waiting for the disk operation process_using_disk = req->pid; // issue read or write, disk interrupt enabled if( req->command == DiskReadSystemCall ) IssueDiskRead( req->disk_block, req->buffer, 1 ); else IssueDiskWrite( req->disk_block, req->buffer, 1 ); } 9/17/2018 Crowley OS Chap. 5

Disk interrupt handler void DiskInterruptHandler( void ) { if( current_process > 0 ) { SaveArea * savearea = &(pd[current_process].sa); int saveTimer; asm { store timer,saveTimer load #0,timer store iia,savearea+0 store ipsw,savearea+4 store base,savearea+8 store bound,savearea+12 storeall savearea+16 load SystemStack+SystemStackSize,r30 } pd[current_process].timeLeft = saveTimer; pd[current_process].state = Ready; } pd[process_using_disk].state = Ready; process_using_disk = 0; ScheduleDisk(); Dispatcher(); } 9/17/2018 Crowley OS Chap. 5

Disk interface implementation int DiskBusy( void ) { disk_status_reg stat = *Disk_status; return stat.busy; } void IssueDiskRead( int block_number, char * buffer, int enable_disk_interrupt ) { disk_control_reg control_reg; // assemble the necessary control word control_reg.command = 1; control_reg.disk_block = block_number; control_reg.interrupt_enabled = enable_disk_interrupt; // store the control words // in the disk control register *Disk_memory_addr = buffer; *Disk_control_reg = control_reg; } 9/17/2018 Crowley OS Chap. 5

Waiting for messages 9/17/2018 Crowley OS Chap. 5

Waiting inside a system call Some systems calls must wait E.g. ReceiveMessage, ReadDiskBlock The OS suspends the process and saves its state but how does the state of the OS processing the system call get saved? Special provision must be made for this 9/17/2018 Crowley OS Chap. 5

Suspending system calls Find a place to save the state that will be needed when the system call resumes usually this is in a waiting queue Arrange to be resumed when the event you are waiting for occurs the OS component that handles the event will also handle this duty 9/17/2018 Crowley OS Chap. 5

Disk read flow of control 9/17/2018 Crowley OS Chap. 5

Create process flow of control 9/17/2018 Crowley OS Chap. 5

Create message queue control flow 9/17/2018 Crowley OS Chap. 5

Send message flow of control 9/17/2018 Crowley OS Chap. 5

Interrupts in the OS The OS is not set up to handle this data save areas will be destroyed so we don’t allow interrupts in system code Chapter 6 shows how to handle this problem 9/17/2018 Crowley OS Chap. 5

The OS as an event and table manager Interrupt Data Updated Processing Done Timer pd Switch processes Disk pd Unblock process and start next I/O System Call Data Updated Processing Done Createprocess pd Initialize process table slot Exit pd Free process table slot CreateMsgQueue pd, message_queue Initialize message queue SendMessage pd, message_queue Queue or transfer message message_buffer ReceiveMessage pd, message_queue Block or transfer message message_buffer ReadDiskBlock pd, disk_queue Queue disk request WriteDiskBlock pd, disk_queue Queue disk request 9/17/2018 Crowley OS Chap. 5

Interrupt event handling 9/17/2018 Crowley OS Chap. 5

Typical process descriptor fields Process ID and name Memory allocated to the process Open files Process state User name and protection privileges Register save area CPU time user Pending software interrupts Parent process User ID 9/17/2018 Crowley OS Chap. 5

A batch operating system 9/17/2018 Crowley OS Chap. 5

Speeding up I/O (3 methods) 9/17/2018 Crowley OS Chap. 5

I/O overlap 9/17/2018 Crowley OS Chap. 5

TSRs in PCs 9/17/2018 Crowley OS Chap. 5