Assembly Language Co-Routines

Slides:



Advertisements
Similar presentations
Chapter 3 Process Description and Control
Advertisements

(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
PZ11B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11B - Parallel execution Programming Language Design.
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.
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.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
CSCE 351: Operating System Kernels
Run time vs. Compile time
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
Computer System Overview Chapter 1. Basic computer structure CPU Memory memory bus I/O bus diskNet interface.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Using Two Queues. Using Multiple Queues Suspended Processes Processor is faster than I/O so all processes could be waiting for I/O Processor is faster.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
System Calls 1.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Memory Management 3 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Process by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Chapter 4 Memory Management Virtual Memory.
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Processes and Process Control 1. Processes and Process Control 2. Definitions of a Process 3. Systems state vs. Process State 4. A 2 State Process Model.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
6-1 Infineon 167 Interrupts The C167CS provides 56 separate interrupt sources that may be assigned to 16 priority levels. The C167CS uses a vectored interrupt.
ECE291 Computer Engineering II Lecture 15 Dr. Zbigniew Kalbarczyk University of Illinois at Urbana- Champaign.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Department of Computer Science and Software Engineering
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department.
1 Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
What is a Process ? A program in execution.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Embedded Computer - Definition When a microcomputer is part of a larger product, it is said to be an embedded computer. The embedded computer retrieves.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Processes and threads.
Operating Systems (CS 340 D)
Protection of System Resources
CS 3305 System Calls Lecture 7.
Operating Systems (CS 340 D)
Chapter 9 :: Subroutines and Control Abstraction
Application Binary Interface (ABI)
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CS 143A Quiz 1 Solution.
Module IV Memory Organization.
Process Description and Control
Lecture Topics: 11/1 General Operating System Concepts Processes
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 3: Processes.
CS510 Operating System Foundations
Process.
CS703 – Advanced Operating Systems
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Assembly Language Co-Routines

Process A process is a program in execution. Multiprogramming/Multitasking  Operating systems switches from one process to another. Each process has its own Instruction pointer Stack pointer Register values Pointers to Code Segment Data Segment Stack Segment Three Processes A, B, and C A. B. C. Create

Threads A process has Resources: address space, open files, accounting information, etc. A thread of control: instruction pointer, register contents, stack. Multithreading scheme permits multiple threads of control to execute within one process. Threads in the same process share a lot of data, such as code and data segments. Switching between threads with the same process is much less expensive than switching between thread in separate processes as they cause threads in the same process share so much state, between separate processes.

Control of the CPU Multitasking operating systems swap control of the CPU between several code section back and forth while executing. In two approaches: I. Preemptive: Several processes or threads take turns executing with the task switch occurring independently of the executing code. The Operating System takes responsibility for interrupting one task and transferring control to other task. II. Cooperative Blocks of code explicitly pass control between one another. The program manages the control flow usually using co-routines

Routines Routine B Routine A Routine B Routine A Resume B call B Entry Entry Entry Entry Resume B call B Resume A Routine B Routine A Routine B Routine A Resume B Resume A return Return Return Return Return

Co-Routines Initial resume (call) of X: create activation for X resume execution at entry point resume Y : suspend current activation Resume which activation of Y? resume ?  return anonymous resume “terminated” activation Call  create & resume

A Use Case Coroutines are quite useful for games where the “players” take turns, following different strategies. The first player executes some code to make its first move, then resumes the second player and allows it to make a move. After the second player makes its move, it resumes the first process and gives the first player its second move, picking up immediately after its resume. This transfer of control bounces back and forth until one player wins. Coroutines are also useful to implement state machine

Implementation A resume is effectively a call and a return instruction all rolled into one operation. From the point of view of the routine executing the resume, the resume operation is equivalent to a procedure call From the point of view of the processing being called (callee), the resume operation is equivalent to a return operation. When the second routine resumes the first, control resumes not at the be ginning of the first process, but immediately after the last resume operation from that coroutine.

Implementation Coroutines originated as an assembly-language technique, but are supported in some high-level languages. Most popular programming languages do not have direct support for coroutines within the language or their standard libraries. This due to The limitations of stack-based subroutine implementation. The standard call mechanism Coroutines needs an initialization process, which is different from the typical resume. When a program begins execution The main coroutine takes control and uses the stack associated with the entire program. Each process have its own stack, which size depends on its coroutine

Implementation For each coroutines we define a structure that stores Pointer to a code Pointer to a stack Content of used flags and registers We define a global array that stores that information of all the co-routines in the application. Initialize all the co-rountines

Co-routine initialization

Co-routine initialization

Passing Parameters Passing parameters to a coroutine is difficult Typically the stack is used to pass parameters and coroutines all use different stacks. The parameters one coroutine passes to another won’t be on the correct stack when the second coroutine continues execution. Typical coroutine has several entry points It is often necessary to communicate information between coroutines and one could do that by Global variables Registers Pass the address of a block of parameters vi a register

Resume Resume Save the state of the current co-routine Resume the state of the next co-routine (its reference in EBX)

Resume