Asynchronous programming is used anywhere where your application interacts with other external entities - be it other threads, other processes. This world by its nature is Asynchronous. Asynchronous literally means - anything can happen anytime - unpredictable. How do you write software through Asynchronous programming? That's what we shall be going to learn in this course. Big software projects of reasonable sizes are Asynchronous. Software projects leave their synchronous boundaries the moment it starts integrating and communicating with external entities or go multithreaded.
Asynchronous programming is widely used in Distributed Systems, Networking Protocols, Distributes Databases, etc.
The prerequisite of this Course :
Know Thread Synchronization Concepts - Mutexes and Condition Variables ( any thread library, but we will be using pthreads )
C or C++ in preferable, but if your fav language is any other mainstream language then also it is ok. Borrow the concepts and implement it in your fav language.
Comfortable with basic Data structures such as Linked-List.
Zeal to learn and excel - beyond what Crowd is doing ( DS/ALGO/CP )
The end product of this Course shall be a mini library yet very powerful library which allows you do develop software through Asynchronous programming.
Course Difficulty Level :
The Course is level is Intermediate to Advanced. Very beginners pls refrain from enrolling into the Course.
Software Designing Problems to Address in this Course :
Problem 1. Simulating Concurrency in Single-Threaded Programs
problem 2. Reducing Multi-threaded Programs to Single Threaded
Problem 3. Work Deferrals
Problem 4. Asynchronous Communication
Problem 5. Queuing and Processing Incoming Network Packets
1) The document discusses parallelizing an application by dividing the work across multiple processors. It provides a simple example of summing 10^12 real numbers between 0 and 1 in parallel.
2) The example shows dividing the work of computing the sum across 8 processors. Each processor generates a portion of the input numbers and computes a partial sum, which are then combined.
3) The key steps of parallelizing an existing application are profiling it to find the most time-consuming part, designing a parallel method for that part, implementing it using a programming model like MPI, and measuring/tuning performance.
This document outlines a project to measure the performance of multithreaded Java programs on different systems. It discusses benchmarking three workloads - calculating trapezoidal rule, sorting arrays, and computing Fibonacci numbers - using different numbers of threads. Performance is measured on Intel Core 2 Duo and Intel Centrino Duo systems running Windows Vista and Ubuntu. The results show that for CPU-intensive tasks, using the optimal number of threads can improve performance, but adding excessive threads harms performance due to overhead. Multithreading is most effective for I/O-bound tasks.
MERIMeeting du 27 mai 2014 - Parallel ProgrammingOlivier NAVARRE
This document summarizes a presentation on parallel programming in .NET. It introduces parallel programming and why it is useful for performing work faster and creating responsive user interfaces. It discusses common issues like deadlocks and race conditions. It then covers different techniques for parallel programming in C#, including using threads, thread pools, tasks, Parallel class, async programming, and PLINQ. It also discusses synchronization techniques like locks, mutexes, and signaling with wait/pulse and barriers. Finally, it discusses storing shared items and provides an overview of thread-safe collection classes in .NET.
This document provides a brief overview of concurrency concepts in Python, focusing on threading. It defines processes and threads, explaining that threads run within a process and can share resources, while processes are independent and communicate via interprocess communication. It discusses why to use concurrency for performance, responsiveness, and non-blocking behavior. It also covers the Global Interpreter Lock in Python and its implications for threading, and provides examples of creating and running threads.
The document discusses parallel programming and message passing as a parallel programming model. It provides examples of using MPI (Message Passing Interface) and MapReduce frameworks for parallel programming. Some key applications discussed are financial risk assessment, molecular dynamics simulations, rendering animation, and web indexing. Challenges with parallel programming include potential slowdown due to overhead and limitations of parallel speedup based on sequential fractions of programs.
Migration To Multi Core - Parallel Programming ModelsZvi Avraham
The document discusses multi-core and many-core processors and parallel programming models. It provides an overview of hardware trends including increasing numbers of cores in CPUs and GPUs. It also covers parallel programming approaches like shared memory, message passing, data parallelism and task parallelism. Specific APIs discussed include Win32 threads, OpenMP, and Intel TBB.
1. The document discusses asynchrony and the Monix library for Scala. It introduces the concept of asynchronous computations using the type Async and describes how Monix implements asynchronous tasks using the Task type. It shows examples of composing Task values to run asynchronous operations sequentially or in parallel.
The document discusses programming concepts including programming languages, switch case statements, and looping. It provides examples of how to write code using switch case statements and different types of loops (for, while, do-while). The examples demonstrate how to get user input, perform calculations, and repeat blocks of code multiple times.
1) The document discusses parallelizing an application by dividing the work across multiple processors. It provides a simple example of summing 10^12 real numbers between 0 and 1 in parallel.
2) The example shows dividing the work of computing the sum across 8 processors. Each processor generates a portion of the input numbers and computes a partial sum, which are then combined.
3) The key steps of parallelizing an existing application are profiling it to find the most time-consuming part, designing a parallel method for that part, implementing it using a programming model like MPI, and measuring/tuning performance.
This document outlines a project to measure the performance of multithreaded Java programs on different systems. It discusses benchmarking three workloads - calculating trapezoidal rule, sorting arrays, and computing Fibonacci numbers - using different numbers of threads. Performance is measured on Intel Core 2 Duo and Intel Centrino Duo systems running Windows Vista and Ubuntu. The results show that for CPU-intensive tasks, using the optimal number of threads can improve performance, but adding excessive threads harms performance due to overhead. Multithreading is most effective for I/O-bound tasks.
MERIMeeting du 27 mai 2014 - Parallel ProgrammingOlivier NAVARRE
This document summarizes a presentation on parallel programming in .NET. It introduces parallel programming and why it is useful for performing work faster and creating responsive user interfaces. It discusses common issues like deadlocks and race conditions. It then covers different techniques for parallel programming in C#, including using threads, thread pools, tasks, Parallel class, async programming, and PLINQ. It also discusses synchronization techniques like locks, mutexes, and signaling with wait/pulse and barriers. Finally, it discusses storing shared items and provides an overview of thread-safe collection classes in .NET.
This document provides a brief overview of concurrency concepts in Python, focusing on threading. It defines processes and threads, explaining that threads run within a process and can share resources, while processes are independent and communicate via interprocess communication. It discusses why to use concurrency for performance, responsiveness, and non-blocking behavior. It also covers the Global Interpreter Lock in Python and its implications for threading, and provides examples of creating and running threads.
The document discusses parallel programming and message passing as a parallel programming model. It provides examples of using MPI (Message Passing Interface) and MapReduce frameworks for parallel programming. Some key applications discussed are financial risk assessment, molecular dynamics simulations, rendering animation, and web indexing. Challenges with parallel programming include potential slowdown due to overhead and limitations of parallel speedup based on sequential fractions of programs.
Migration To Multi Core - Parallel Programming ModelsZvi Avraham
The document discusses multi-core and many-core processors and parallel programming models. It provides an overview of hardware trends including increasing numbers of cores in CPUs and GPUs. It also covers parallel programming approaches like shared memory, message passing, data parallelism and task parallelism. Specific APIs discussed include Win32 threads, OpenMP, and Intel TBB.
1. The document discusses asynchrony and the Monix library for Scala. It introduces the concept of asynchronous computations using the type Async and describes how Monix implements asynchronous tasks using the Task type. It shows examples of composing Task values to run asynchronous operations sequentially or in parallel.
The document discusses programming concepts including programming languages, switch case statements, and looping. It provides examples of how to write code using switch case statements and different types of loops (for, while, do-while). The examples demonstrate how to get user input, perform calculations, and repeat blocks of code multiple times.
Declare Your Language: What is a Compiler?Eelco Visser
The document provides an overview of the course on compiler construction, including information on the course organization, website, assignments, and deadlines, as well as introducing the concept of what a compiler is and the different types of compilers. It also discusses how linguistic abstractions can be used to build domain-specific languages and language workbenches that support the design and implementation of programming languages through the use of declarative meta-languages.
Here is the sequence of println statements that will generate the given output:
System.out.println("This program prints the first lines");
System.out.println("of the song 'slots'.");
System.out.println("'She lives in a trailer'");
System.out.println("'On the o");
Switch case statements provide an alternative to long if/else statements when comparing a variable to multiple integral values. The basic format compares the variable to case values, executing code for a matching case. A default case handles non-matching values. Loops allow code to repeat, with for, while, and do-while loops. For loops initialize/update a variable and check a condition each repetition. While loops check a condition and repeat until it's false. Loops are useful for repeating tasks like displaying lists of data.
This document discusses MPI (Message Passing Interface) and OpenMP for parallel programming. MPI is a standard for message passing parallel programs that requires explicit communication between processes. It provides functions for point-to-point and collective communication. OpenMP is a specification for shared memory parallel programming that uses compiler directives to parallelize loops and sections of code. It provides constructs for work sharing, synchronization, and managing shared memory between threads. The document compares the two approaches and provides examples of simple MPI and OpenMP programs.
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
This document discusses the evolution of asynchronous programming on the .NET platform. It covers early approaches using Begin/End methods and event-based patterns in .NET 1.0. It then discusses improvements with yield in .NET 2.0/C# 2.0 and async workflows in F# for .NET 3.0. It also covers the Reactive Framework for .NET 4.0 which treats asynchronous computations as push-based collections. Finally, it discusses potential future improvements in C# vNext.
PyCon Canada 2019 - Introduction to Asynchronous ProgrammingJuti Noppornpitak
Since Python 3.5 and PEP 492, we have been able to write asynchronous programs in an easy and Pythonic way without external libraries. Even so, it is still difficult to understand what asynchronous programming is all about and when we, Python developers, should consider using it. This talk will give you a gentle introduction to the world of asynchronous programming, focusing mostly on the core concept of async programming, how it works, and what its applications are, in order to provide a good foundation to Python developers on the topic. On top of that, we will explore a small code example (mostly involving the built-in asyncio) and briefly exam the source code of CPython to find out how it works. This talk will also give you some brief comparison of threading.Thread and ThreadPoolExecutor.
Async/await and the Task model are the main features of C# 5/.NET 4.5. While asynchronous programming can be done in most languages, all have different specifics, usage and trade-offs. You will see, in this presentation, what is the asynchronous Task model in .NET and why it matters for web apps.
The document provides an agenda for a presentation on the Task Parallel Library (TPL) and async/await in .NET. The presentation covers topics like threads and blocking vs non-blocking code, asynchronous programming models before async/await, the lifecycle of async operations, common misconceptions about async/await, differences between CPU-bound and I/O-bound work, exception handling, progress/cancellation, unit testing, combinators like WhenAll and WhenAny, and tips/tricks for async programming.
The document discusses parallel programming in .NET. It covers two main strategies for parallelism - data parallelism and task parallelism. For data parallelism, it describes using Parallel.For to partition work over collections. For task parallelism, it discusses using the Task Parallel Library to create and run independent tasks concurrently, allowing work to be distributed across multiple processors. It provides examples of creating tasks implicitly with Parallel.Invoke and explicitly by instantiating Task objects and passing delegates.
This document provides a course syllabus for a Java training course. The syllabus outlines topics that will be covered including an overview of object-oriented programming in Java, important Java concepts like static, final, interface and abstract classes, exception handling, collections, generics, threads, JDBC, and J2EE technologies like JSP, Servlets, Struts and XML. It also discusses fees structure for the course and notes it will take place on Saturdays, Sundays and weekdays, with registration fees of Rs. 1000 and remaining Rs. 4000 to be paid during classes.
Parallel computing uses multiple processors simultaneously to solve computational problems faster. It allows solving larger problems or more problems in less time. Shared memory parallel programming with tools like OpenMP and pthreads is used for multicore processors that share memory. Distributed memory parallel programming with MPI is used for large clusters with separate processor memories. GPU programming with CUDA is also widely used to leverage graphics hardware for parallel tasks like SIMD. The key challenges in parallel programming are load balancing, communication overhead, and synchronization between processors.
computer notes - Inter process communicationecomputernotes
Processes execute to accomplish specified computations. An interesting and innovative
way to use a computer system is to spread a given computation over several processes.
The need for such communicating processes arises in parallel and distributed processing
contexts.
This document discusses processes and their interaction in operating systems. It defines a process as a program in execution that includes code, data, threads of execution, and resources. It describes how operating systems virtualize CPUs and memory to allow multiple processes to run concurrently. It discusses different types of process interactions including competition over shared resources, which can cause race conditions, and cooperation through synchronization and message passing. It presents several algorithms to solve the critical section problem of mutual exclusion when processes access shared resources.
This document provides an overview of parallel computing. It discusses why parallel computation is needed due to limitations in increasing processor speed. It then covers various parallel platforms including shared and distributed memory systems. It describes different parallel programming models and paradigms including MPI, OpenMP, Pthreads, CUDA and more. It also discusses key concepts like load balancing, domain decomposition, and synchronization which are important for parallel programming.
(1) c sharp introduction_basics_dot_netNico Ludwig
This document provides an introduction to parsing an update log using different programming languages, including C#, Visual Basic, C++/CLI, F#, and others. It describes the problem of parsing a software update log file to retrieve the latest updates for different applications. It then shows sample code solutions in each language and provides brief descriptions and context about each language.
This document discusses parallelization and multithreading techniques in .NET. It covers multithreading using AsyncEnumerator, which simplifies asynchronous programming. It also covers the Parallel Extensions to .NET Framework, including the Task Parallel Library for implicit parallelism using Parallel.For and Parallel.ForEach, and Parallel LINQ (PLINQ) for parallel querying of data sources. The document provides examples and discusses concepts like cancellation, exceptions, and thread safety.
The document provides an introduction to Java programming concepts including:
- What computer languages and programs are and how they allow computers to complete tasks.
- How high-level languages like Java make programming easier than machine or assembly languages.
- The basic structure of a Java program including classes, methods, and statements.
- How to compile and run a simple "Hello World" Java program from the command line.
- Key Java concepts like strings, print statements, and comments that allow programmers to output text.
Lesson 1 - Introduction to Computer Programming.pptxNeil Mutia
This document provides an introduction to computer programming. It defines computer programming as giving a set of instructions to a computer to perform a specific task. Programming involves encoding an algorithm into a programming language that can be executed by a computer. Analogously, a recipe provides instructions for a person to follow. The document outlines some key programming terminology like compilers, bugs, and debugging. It also presents examples of computer programs and sample salaries. Fundamental programming concepts of variables, data types, control structures and subroutines are introduced.
Declare Your Language: What is a Compiler?Eelco Visser
The document provides an overview of the course on compiler construction, including information on the course organization, website, assignments, and deadlines, as well as introducing the concept of what a compiler is and the different types of compilers. It also discusses how linguistic abstractions can be used to build domain-specific languages and language workbenches that support the design and implementation of programming languages through the use of declarative meta-languages.
Here is the sequence of println statements that will generate the given output:
System.out.println("This program prints the first lines");
System.out.println("of the song 'slots'.");
System.out.println("'She lives in a trailer'");
System.out.println("'On the o");
Switch case statements provide an alternative to long if/else statements when comparing a variable to multiple integral values. The basic format compares the variable to case values, executing code for a matching case. A default case handles non-matching values. Loops allow code to repeat, with for, while, and do-while loops. For loops initialize/update a variable and check a condition each repetition. While loops check a condition and repeat until it's false. Loops are useful for repeating tasks like displaying lists of data.
This document discusses MPI (Message Passing Interface) and OpenMP for parallel programming. MPI is a standard for message passing parallel programs that requires explicit communication between processes. It provides functions for point-to-point and collective communication. OpenMP is a specification for shared memory parallel programming that uses compiler directives to parallelize loops and sections of code. It provides constructs for work sharing, synchronization, and managing shared memory between threads. The document compares the two approaches and provides examples of simple MPI and OpenMP programs.
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
This document discusses the evolution of asynchronous programming on the .NET platform. It covers early approaches using Begin/End methods and event-based patterns in .NET 1.0. It then discusses improvements with yield in .NET 2.0/C# 2.0 and async workflows in F# for .NET 3.0. It also covers the Reactive Framework for .NET 4.0 which treats asynchronous computations as push-based collections. Finally, it discusses potential future improvements in C# vNext.
PyCon Canada 2019 - Introduction to Asynchronous ProgrammingJuti Noppornpitak
Since Python 3.5 and PEP 492, we have been able to write asynchronous programs in an easy and Pythonic way without external libraries. Even so, it is still difficult to understand what asynchronous programming is all about and when we, Python developers, should consider using it. This talk will give you a gentle introduction to the world of asynchronous programming, focusing mostly on the core concept of async programming, how it works, and what its applications are, in order to provide a good foundation to Python developers on the topic. On top of that, we will explore a small code example (mostly involving the built-in asyncio) and briefly exam the source code of CPython to find out how it works. This talk will also give you some brief comparison of threading.Thread and ThreadPoolExecutor.
Async/await and the Task model are the main features of C# 5/.NET 4.5. While asynchronous programming can be done in most languages, all have different specifics, usage and trade-offs. You will see, in this presentation, what is the asynchronous Task model in .NET and why it matters for web apps.
The document provides an agenda for a presentation on the Task Parallel Library (TPL) and async/await in .NET. The presentation covers topics like threads and blocking vs non-blocking code, asynchronous programming models before async/await, the lifecycle of async operations, common misconceptions about async/await, differences between CPU-bound and I/O-bound work, exception handling, progress/cancellation, unit testing, combinators like WhenAll and WhenAny, and tips/tricks for async programming.
The document discusses parallel programming in .NET. It covers two main strategies for parallelism - data parallelism and task parallelism. For data parallelism, it describes using Parallel.For to partition work over collections. For task parallelism, it discusses using the Task Parallel Library to create and run independent tasks concurrently, allowing work to be distributed across multiple processors. It provides examples of creating tasks implicitly with Parallel.Invoke and explicitly by instantiating Task objects and passing delegates.
This document provides a course syllabus for a Java training course. The syllabus outlines topics that will be covered including an overview of object-oriented programming in Java, important Java concepts like static, final, interface and abstract classes, exception handling, collections, generics, threads, JDBC, and J2EE technologies like JSP, Servlets, Struts and XML. It also discusses fees structure for the course and notes it will take place on Saturdays, Sundays and weekdays, with registration fees of Rs. 1000 and remaining Rs. 4000 to be paid during classes.
Parallel computing uses multiple processors simultaneously to solve computational problems faster. It allows solving larger problems or more problems in less time. Shared memory parallel programming with tools like OpenMP and pthreads is used for multicore processors that share memory. Distributed memory parallel programming with MPI is used for large clusters with separate processor memories. GPU programming with CUDA is also widely used to leverage graphics hardware for parallel tasks like SIMD. The key challenges in parallel programming are load balancing, communication overhead, and synchronization between processors.
computer notes - Inter process communicationecomputernotes
Processes execute to accomplish specified computations. An interesting and innovative
way to use a computer system is to spread a given computation over several processes.
The need for such communicating processes arises in parallel and distributed processing
contexts.
This document discusses processes and their interaction in operating systems. It defines a process as a program in execution that includes code, data, threads of execution, and resources. It describes how operating systems virtualize CPUs and memory to allow multiple processes to run concurrently. It discusses different types of process interactions including competition over shared resources, which can cause race conditions, and cooperation through synchronization and message passing. It presents several algorithms to solve the critical section problem of mutual exclusion when processes access shared resources.
This document provides an overview of parallel computing. It discusses why parallel computation is needed due to limitations in increasing processor speed. It then covers various parallel platforms including shared and distributed memory systems. It describes different parallel programming models and paradigms including MPI, OpenMP, Pthreads, CUDA and more. It also discusses key concepts like load balancing, domain decomposition, and synchronization which are important for parallel programming.
(1) c sharp introduction_basics_dot_netNico Ludwig
This document provides an introduction to parsing an update log using different programming languages, including C#, Visual Basic, C++/CLI, F#, and others. It describes the problem of parsing a software update log file to retrieve the latest updates for different applications. It then shows sample code solutions in each language and provides brief descriptions and context about each language.
This document discusses parallelization and multithreading techniques in .NET. It covers multithreading using AsyncEnumerator, which simplifies asynchronous programming. It also covers the Parallel Extensions to .NET Framework, including the Task Parallel Library for implicit parallelism using Parallel.For and Parallel.ForEach, and Parallel LINQ (PLINQ) for parallel querying of data sources. The document provides examples and discusses concepts like cancellation, exceptions, and thread safety.
The document provides an introduction to Java programming concepts including:
- What computer languages and programs are and how they allow computers to complete tasks.
- How high-level languages like Java make programming easier than machine or assembly languages.
- The basic structure of a Java program including classes, methods, and statements.
- How to compile and run a simple "Hello World" Java program from the command line.
- Key Java concepts like strings, print statements, and comments that allow programmers to output text.
Lesson 1 - Introduction to Computer Programming.pptxNeil Mutia
This document provides an introduction to computer programming. It defines computer programming as giving a set of instructions to a computer to perform a specific task. Programming involves encoding an algorithm into a programming language that can be executed by a computer. Analogously, a recipe provides instructions for a person to follow. The document outlines some key programming terminology like compilers, bugs, and debugging. It also presents examples of computer programs and sample salaries. Fundamental programming concepts of variables, data types, control structures and subroutines are introduced.
Similar to AsynchronousProgrammingDesignPatterns.pptx (20)
Macroeconomics- Movie Location
This will be used as part of your Personal Professional Portfolio once graded.
Objective:
Prepare a presentation or a paper using research, basic comparative analysis, data organization and application of economic information. You will make an informed assessment of an economic climate outside of the United States to accomplish an entertainment industry objective.
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
Thinking of getting a dog? Be aware that breeds like Pit Bulls, Rottweilers, and German Shepherds can be loyal and dangerous. Proper training and socialization are crucial to preventing aggressive behaviors. Ensure safety by understanding their needs and always supervising interactions. Stay safe, and enjoy your furry friends!
This presentation was provided by Steph Pollock of The American Psychological Association’s Journals Program, and Damita Snow, of The American Society of Civil Engineers (ASCE), for the initial session of NISO's 2024 Training Series "DEIA in the Scholarly Landscape." Session One: 'Setting Expectations: a DEIA Primer,' was held June 6, 2024.
How to Build a Module in Odoo 17 Using the Scaffold MethodCeline George
Odoo provides an option for creating a module by using a single line command. By using this command the user can make a whole structure of a module. It is very easy for a beginner to make a module. There is no need to make each file manually. This slide will show how to create a module using the scaffold method.
How to Fix the Import Error in the Odoo 17Celine George
An import error occurs when a program fails to import a module or library, disrupting its execution. In languages like Python, this issue arises when the specified module cannot be found or accessed, hindering the program's functionality. Resolving import errors is crucial for maintaining smooth software operation and uninterrupted development processes.
हिंदी वर्णमाला पीपीटी, hindi alphabet PPT presentation, hindi varnamala PPT, Hindi Varnamala pdf, हिंदी स्वर, हिंदी व्यंजन, sikhiye hindi varnmala, dr. mulla adam ali, hindi language and literature, hindi alphabet with drawing, hindi alphabet pdf, hindi varnamala for childrens, hindi language, hindi varnamala practice for kids, https://www.drmullaadamali.com
The simplified electron and muon model, Oscillating Spacetime: The Foundation...RitikBhardwaj56
Discover the Simplified Electron and Muon Model: A New Wave-Based Approach to Understanding Particles delves into a groundbreaking theory that presents electrons and muons as rotating soliton waves within oscillating spacetime. Geared towards students, researchers, and science buffs, this book breaks down complex ideas into simple explanations. It covers topics such as electron waves, temporal dynamics, and the implications of this model on particle physics. With clear illustrations and easy-to-follow explanations, readers will gain a new outlook on the universe's fundamental nature.
Assessment and Planning in Educational technology.pptxKavitha Krishnan
In an education system, it is understood that assessment is only for the students, but on the other hand, the Assessment of teachers is also an important aspect of the education system that ensures teachers are providing high-quality instruction to students. The assessment process can be used to provide feedback and support for professional development, to inform decisions about teacher retention or promotion, or to evaluate teacher effectiveness for accountability purposes.
বাংলাদেশের অর্থনৈতিক সমীক্ষা ২০২৪ [Bangladesh Economic Review 2024 Bangla.pdf] কম্পিউটার , ট্যাব ও স্মার্ট ফোন ভার্সন সহ সম্পূর্ণ বাংলা ই-বুক বা pdf বই " সুচিপত্র ...বুকমার্ক মেনু 🔖 ও হাইপার লিংক মেনু 📝👆 যুক্ত ..
আমাদের সবার জন্য খুব খুব গুরুত্বপূর্ণ একটি বই ..বিসিএস, ব্যাংক, ইউনিভার্সিটি ভর্তি ও যে কোন প্রতিযোগিতা মূলক পরীক্ষার জন্য এর খুব ইম্পরট্যান্ট একটি বিষয় ...তাছাড়া বাংলাদেশের সাম্প্রতিক যে কোন ডাটা বা তথ্য এই বইতে পাবেন ...
তাই একজন নাগরিক হিসাবে এই তথ্য গুলো আপনার জানা প্রয়োজন ...।
বিসিএস ও ব্যাংক এর লিখিত পরীক্ষা ...+এছাড়া মাধ্যমিক ও উচ্চমাধ্যমিকের স্টুডেন্টদের জন্য অনেক কাজে আসবে ...
1. Non-Juniper
Programming Language : C/C++ ( but concepts are language agnostic )
Pre-requisites : Thread Synch ( Mutex and Condition Variables ), Socket programming basics
Website : www.csepracticals.com
2. Non-Juniper
In this Course, we shall be going to understand Asynchronous Programming, and problem it solves through a Demo Project !
Pre-requites :
Multithreading, Thread Sync, Mutex, Condition Variables
C/C++ Or any main-stream programming language
Table of Contents :
1. Understanding sync and async programming paradigm
2. Understanding sync and async communication
3. Goals of Asynchronous Programming
4. Defining the project to practice Asynchronous Programming Concepts
5. Asynchronous Programming through Dispatch Queue/EventLoop
1. Design
2. Implementation
3. Implement Various Asynchronous Design Patterns
After doing this Course :
Understand where to apply Async
Programming techniques and why
Say good-byte to forced Multi-threading
Say good-bye to forced locking
Build scalable system soft-wares
Implement Async Communication
3. Non-Juniper
Asynchronous Programming Design Patterns Async Vs Sync Example
Till now, most of the code you have written so far is sync code
All codes that you write while practicing CP is sync code
Sync programming means – performs steps in known definite order
Measure ingredients
Mix flour, eggs,
Sugar
Heat Oven to
the right temp
Bake the cake
Wait for cake to
ready
Prepare the cake
Synchronous
way
Measure ingredients
Mix flour, eggs,
Sugar
Heat Oven to
the right temp
Bake the cake
Send notif that
cake is ready
Prepare the cake
ASynchronous
way
Eat Eat
• Do independent tasks in parallel
• No waiting, do something else
• Asynchronous doesn’t means
multithreading, but closely tied
to it
4. Non-Juniper
Asynchronous Programming Design Patterns Async Vs Sync communication
Conversation through telephone – Synchronous communication
You hear what other is saying
You wait until the other side has completed his sentence
When one speak, other one actively hear
While you speak or hear, you don’t do anything else
Conversation through email or chat – Asynchronous Communication
You don’t wait for other’s response
You shoot your email/msg without knowing if other one is in ready to recv/read state
You don’t wait for the reply ( at-least instantly )
You are notified by notification that reply has recvd
Asynchronous communication means communication which happens ‘out of sync’
It is almost a bad software design if you need to get blocked for some reason ! However, blocking is justified in few scenarios
like select( ) / fgets( ) etc
5. Non-Juniper
Asynchronous Programming Design Patterns Asynchronous Vs Multi-Threading Programming
They tend to appear same , but they are not
Multithreading is one form of asynchronous programming
Asynchronous programming can be done with single thread as well as with multiple threads
Multi-Threading is about Workers who works in parallel
Asynchronous is all about set of tasks to be finished, for example an application may have :
Task 1 : Network 10 pkts in Queue to be processed
Task 2 : Timer Callback pending to execute
Task 3 : A Network pkt to send to remote machine
Task 4 : A CLI input from user to be processed
Asynchronous
Programming
Multithreading
Programming
Single Threaded
Programming
How would application going to finish pending tasks ?
In what order ?
Parallel or sequentially ?
While executing current task, more task may Queue in
7. Non-Juniper
Asynchronous Programming Design Patterns Asynchronous Vs Multi-Threading Programming
ASynchronous
Single Threaded Sum[0,3] Mul[0,5] Sum[4,6] Mul[6,9] Sum[7,9] -
Thread 1
1 2 3 4 5 6 7 8 9 10
Task 1 - Calculate sum of all numbers
Task 2 – Calculate multiplication of all numbers
Sum[0,9] Mul[0,9]
Thread 1
Synchronous
Single Threaded
Sum[0,9]
Mul[0,9]
Thread 1
Synchronous
Multi Threaded
Thread 2
ASynchronous
Multi Threaded
Sum[0,3] Sum[4,6] Sum[7,9]
Thread 1
Mul[0,5] Mul[6,9]
Thread 2
8. Non-Juniper
Asynchronous Programming Design Patterns Course Layout
I have developed this Course on a Fast-Track
I would try to enforce learning by doing rather than splitting out theeeeoooooorrryyyyyyy
We will straightaway discuss one problem statement where asynchronous programming is essential
Then we built asynchronous solution to the problem called – Event Loop Or Dispatch Queue
We apply the event loop async solution to solve various other problems ( Design Patterns )
I will use C to demonstrate the concepts, but concepts are language agnostic
After doing this Course, you would be in a position to design asynchronous systems
Pre-requisites :
You must at-least know how to implement producer-consumer problem
If not, then pls learn thready Synchronization basics before attempting this course
No More Theory,
Let’s Get our Hands
Dirty
10. Non-Juniper
Asynchronous Programming Introducing Event Loops
Event loop is an async programming structure which schedule the computation to be formed in future ( Work Deferral )
Let us understand how event loop works – Design and Implementation
Event loop is also called as Event dispatcher
Once we finish the implementation of event loop, I will show you how to put event loop to implement different types
of Asynchronous programming model
The output of this section is : Event Loop Library
11. Non-Juniper
F1, arg1 F2, arg2 F3, arg3 F4, arg4 F1, arg5
Appln
Thread(s)
task_create_new_job
(argx, Fnx);
Event Loop is a separate independent thread acting on its task array
Event Loop works like a machine, picking up Fn + Arg ( Computation )
from its task array and placing it on execution stack
It serializes all computations, and discharge them one by one
It continues to do so until the task array is empty
It resumes again if some application thread(s) submits new Computation to task array
Task Array
Asynchronous Programming Event Loop internal Functioning
12. Non-Juniper
Appln
Thread(s)
task_create_new_job
(arg, foo);
Task Array
foo ( arg ) {
. . .
printf(“this ”);
task_t *bar_task =
task_create_new_job(arg1, bar);
printf(“is my fav ”);
. . .
}
o/P : this is my fav
bar( ) {
printf (“bar”);
}
Asynchronous Programming Event Loop internal Functioning
1
Foo, arg
2
3
5
bar, arg1
6
7
8
Appln submit new
C to EL
C is Queued in
Task Array as Task
EL is signaled
4 Fst task is removed
From task array
Task is
triggered
Ist Task is
Completed
Task Created
New Task
IInd task is removed
From task array
9
Task is
triggered
10
IInd Task is
Completed
bar
11
EL suspends again
C – Computation
EL – Event Loop Thread
13. Non-Juniper
Asynchronous Programming Event Loop Design and Implementation
Event Loop
Thread in Suspended State
Application Submit
Computation to Event Loop
Thread
EL Thread is Signaled (
Resume Execution )
While Task Array is Empty
Dequeue Computation from
Task Array and Triggers it
N
EV_DIS_IDLE
EV_DIS_TASK_FIN_WAIT
Y
14. Non-Juniper
Asynchronous Programming Design Patterns Code Access
You can do this Course on any POSIX compliant Operating system – Linux Or MAC-OS
I will execute and run all my codes on Linux
Compiler used – gcc
Programming language – C/C++ , if you are doing this course in other language, write equivalent code
You are advised to use github for all your codes management
30 min tutorial from youtube, that suffice
My Setup : Visual studio Code IDE, Ubuntu 20.04 LTS, VMWare Work-Station Player
Code Access : https://github.com/sachinites/AsyncProgramming
Create your github account and Fork this repository
15. Non-Juniper
Asynchronous Programming Event Loop Design and Implementation Data Structures
Event Loop
Thread in Suspended State
Application Submit
Computation to Event Loop
Thread
EL Thread is Signaled (
Resume Execution )
While Task Array is Empty
Dequeue Computation from
Task Array and Triggers it
N
EV_DIS_IDLE
EV_DIS_TASK_FIN_WAIT
Computation = Fn + Arg
typedef void (*event_cbk)(void *);
struct task_{
event_cbk ev_cbk;
void *arg;
struct task_ *next;
struct task_ *prev;
} ;
struct event_loop_{
struct task_ *task_array_head;
pthread_mutex_t ev_loop_mutex;
EV_LOOP_STATE ev_loop_state;
pthread_cond_t ev_loop_cv;
pthread_t *thread;
task_t *current_task;
};
typedef enum {
EV_DIS_IDLE,
EV_DIS_TASK_FIN_WAIT,
} EV_DISPATCHER_STATE;
Y
event_loop.h
16. Non-Juniper
Asynchronous Programming Event Loop Design and Implementation APIs
void
event_loop_init(event_loop_t *el);
• Initialize the member of event loop
void
event_loop_run(event_loop_t *el);
• Run the event loop thread
18. Non-Juniper
Asynchronous Programming Event Loop Design and Implementation APIs
static void *
event_loop_thread(void *arg) {
while(1) {
printf (“%s() runningn”, __FUNCTION__);
sleep(2);
}
return NULL;
}
event loop task
array
Application
Thread
Event Loop
Thread
Therefore, EnQueue/Dequeue operation on EL task array are C.S , and must be protected by EL mutex
Algorithm :
1. Lock the EL mutex
2. Fetch the task from task array
3. If task not found, suspend
4. If task found, fire the task
5. Unlock the EL Mutex
Repeat above 4 steps for forever
19. Non-Juniper
Asynchronous Programming Event Loop Design and Implementation APIs
task_t *
task_create_new_job (event_loop_t *el, event_cbk cbk, void *arg);
• Create a new task_t object, and initialise it
• Lock the EL Mutex
• Add a task to the EL’s task array
• If EL is in IDLE state, signal it
• If EL is already BUSY, We are done
• Unlock EL Mutex
void event_dispatcher_schedule_task (event_loop_t *el , task_t *task);
event loop task
array
Application
Thread
Event Loop
Thread
Task Submission to Event Loop Task Array
21. Non-Juniper
Asynchronous Programming Event Loop Lock Free
Application which uses Event Loops are lock free ( as long as Application logic is executed through Tasks only )
First, we need to understand why we need locks in multi-threaded environment, at the most fundamental level
22. Non-Juniper
Why we need locking in Multi-Threaded Environment
To put in one sentence, we need locking because threads do context switching at arbitrary points in a program
We want that other threads ever sees the data structures in in-consistent state
Hence, lock the DS !
23. Non-Juniper
Asynchronous Programming Event Loop Lock Free
Application which uses Event Loops are lock free ( as long as Application logic is executed through Tasks only )
And the reason is Obvious, if only event loop thread were to execute application code through tasks, then there is only
one and only one thread in the system – event loop thread
So , no Locks because the system is uni-threaded.
But in this uni-threaded system also, Tasks switches from T1 to T2 ( upload/download example ). So, does it mean, we would need
locks/synchronization since something similar to context switching is there – task switching ?
Ans :
Because Tasks switching happens in controlled fashion, unlike threads which Context Switches in uncontrolled
manner ( at any arbitrary point ), we still don’t need any locks.
When tasks terminate, it is programmer responsibility to not to leave application data structures in in-consistent state.
Why would any programmer return from a task while he is in middle of deleting a node from a DLL ?
The New task fired by event loop would always find application data structures in consistent state.
24. Non-Juniper
Asynchronous Programming Event Loop There is no Parallelism
Concurrency , Parallelism ? Do you understand the difference ? ( Refer Appendix B )
On multi-core systems, Independent threads can execute in parallel on multiple Cores
Event loop is a single threaded system, there is only one thread – Event loop thread
Tasks executes one after the other ( serially ), and not in parallel by event loop thread ( sum( ) / mul( ) example )
If tasks schedule itself again and again, it appear tasks are executing concurrently ( again, upload/download example )
Event Loop provides concurrency but no parallelism even on multi-processor systems
25. Non-Juniper
Asynchronous Programming Event Loop What applications must use Event Loops
Applications which should be designed single threaded
No parallelism
Lots of overlapping work
Too much contention if designed multithreaded
Have listener threads listening for I/O events
Need to defer work ( later )
Running on a single Core environment ( Embedded devices )
Communication across different independent components of the single software process
28. Non-Juniper
Asynchronous Programming Design Patterns Serializing Multi-threaded Flows
We have a multi-threaded program
We want to reduce multi-threaded program to a logical single threaded program
But, If we wanted a single threaded program, why did you write a multithreaded programming in the first place ?
And, now what is the need to reduce a multi-threaded program to single threaded ?
And Why am I even Studying this, simply refactor your application code such that you don’t launch multiple threads,
problem solved !
And Is it even possible ?
T1 T2 T3 T
Multi-threaded program Single-threaded program
29. Non-Juniper
Overlapping & Non-Overlapping Work
If Thread T1 is doing work W1 and Thread T2 doing work W2 , then W1 and W2 is said to be overlapping
if W1 and W2 operates on same Data
Eg : W1 - sorting the array A in Ascending order
W2 - sorting the array A in Descending order
Since Array is a common data on which Thread T1 and T2 are operating, therefore, W1 and W2 are
overlapping work
• If Threads access the same Data structures of a process , say, some global variable then work done by two
threads are overlapping work
Application
Overlapping work
Tend to be Single Threaded
Non-Overlapping work
Tend to be Multi Threaded
30. Non-Juniper
Overlapping & Non-Overlapping Application Example
Single Threaded Application Design Example
Networking protocols
Modification of one DS trigger Modification of other DS
Modification often Trigger computation
OSI Layer Implementation
Processing is done sequentially ( from top to bottom
Or bottom to top )
31. Non-Juniper
Asynchronous Programming Design Patterns Is Multi-Threaded Always Superior ?
Application
A
Single Threaded Multi Threaded
Is Multi-Threaded Always Superior to Single Threaded ?
Does Multi-Threaded appln always perform better as compared to Single Threaded ?
More the degree of isolated Data Structures and independent responsibilities, more the application tend to be multi-threaded
> So that threads can be assigned independent duties
> Threads operate on isolated data, not interfering with one another ( wait n signaling )
• Cars (threads) tend to move slow
• Road (shared resource ) is congested
• Cars move only when other
nearby cars create room
• You will cover 5km in 1 hr
• Ahh ! Resource exclusive for you !
• No Competitor to access same resource
• Move faster
• You will cover 5km in 10 min
32. Non-Juniper
Multithreaded and Single Threaded Applications
Abhi wants to design a new application; he is confused whether he should design an application as Single threaded or
multithreaded
Choosing the wrong thread model shall be devastating
Application
Too big in size
Wrong thread sync, deadlocks
Overlapping Data Structures
Too much locking & unlocking
Not clear demarcation of logic/processing
No Scope of parallelism
Sequential tasks
Single
threaded
Moderate size
• Proper thread sync , manageable
Isolated Data Structures
• Independent threads can perform CRUDs
clear demarcation of logic/processing
• Every thread tied to its specific responsibility
Parallel tasks
Multi
threaded
33. Non-Juniper
Asynchronous Programming Design Patterns Listener Threads
We need to understand how threads can be used by the application to listen for external events
Refer to Appendix C. Complete this section before jumping to next video.
34. Non-Juniper
It is a common scenario that an application needs to constantly listen to external events
Those external events can arrive anytime, and application needs to process those events
Application May Use Thread(s) to listen on those external events
Process STP has an overlapping property, meaning, it is a single threaded application by its design
Process STP
foo( )
bar( )
pkt_process( )
ipc_process( )
cli_handler( )
kernel_events( )
timer_event( )
Network
Pkt listener
thread
User input
listener
thread
User
Kernel events
listener
thread
Kernel
Process
communication
thread
Process P1
Using listener threads , process can
listen to all events at the same time,
When event arrive, process them !
Problems here :
• Process STP tends to move towards multi-threaded
design, against its base design and arch
• Forced locking
• Contention
timer
thread
Asynchronous Programming Design Patterns Listener Threads
35. Non-Juniper
Process STP
foo( )
bar( )
pkt_process( )
ipc_process( )
cli_handler( )
kernel_events( )
timer_events( )
Network
Pkt listener
thread
User input
listener
thread
User
Kernel events
listener
thread
Kernel
Process
communication
thread
Process P1
All listener threads + Timer thread Queue up
their computation to Event loop’s task array
Event loop then runs, and fires all Queued
Computations serially
End Result : Application logic executes only in
Context of EL thread
Result :
• Listener threads exist, but concurrency do not
• Process STP runs in the context of one thread only – Event
Loop thread ( Single threaded Design maintained )
• No locking, no Contention
Task Array
timer
thread
Asynchronous Programming Design Patterns Listener Threads Serializing Computation using Event Loop
36. Non-Juniper
Asynchronous Programming Design Patterns Getting Started with Sample Project
Assignment : https://github.com/sachinites/asyncprogramming/
dir : AsyncProject
Soln dir : AsyncProjectSoln
stp.c
pkt_process( )
Network
Pkt listener
thread
Pkt listener
thread
User input
listener
thread ( main
thread ) User
rt_table
Routing Table Files :
rt.c
rt.h
Routing table represents the data structures possessed by STP process
Routing Table can be concurrently updated by :
User in the context of main thread
By Network in the context of Pkt listener thread
STP is suppose to be single threaded, but it violates the assumption
STP is thread unsafe, error prone
uses
Dest/Mask Gateway OIF
122.1.1.1/32 10.1.1.1 Eth0
130.1.2.3/32 10.1.2.1 eth1
Sample routing table
*CUD – Create Update Delete
37. Non-Juniper
Asynchronous Programming Design Patterns Event Loop Integration
stp.c
pkt_process( )
cli_handler( )
Network
Pkt listener
thread
Pkt listener
thread
User input
listener
thread ( main
thread ) User
rt_table
Task Array
Event loop must be the only execution unit which executes STP’s instructions
Listener/UI threads submit their computation to Event Loop instead of executing appln logic directly
Consequently, event Loop triggers all computations on behalf of listener threads
Thus, STP is reduced to logically single threaded process
Listener threads have just one purpose – to listen for events in blocking state, and not to provide
concurrency ( reason for thread invention )
Next : Integrating Timers – Another tool to do Async Programming
stp_update_routing_table( )
Solution Design
38. Non-Juniper
stp.c
pkt_process( )
cli_handler( )
Network
Pkt listener
thread
Pkt listener
thread
User input
listener
thread ( main
thread ) User
rt_table
Task Array
Goal : stp_update_routing_table ( ) must be invoked by event Loop thread instead by listener threads, Obviously all STP’s listener
threads must handover their respective computation to event loop thread
STP process must have Event Loop object and initialize it using stp_init_el( )
Create new data Structure el_rt_table_update_data_t to wrap up all arguments in one single object
Write a new API which create a new task and submit the task to Event Loop task array
task_t *el_stp_update_routing_table(rt_table_t *rt_table, int cmd_code, rt_table_entry_t *rt_entry ) ;
Changes in application code stp.c to reroute the computation from direct to via Event Loop :
Instead of calling stp_update_routing_table, call new API el_stp_update_routing_table
Asynchronous Programming Design Patterns Event Loop Integration Steps
This is a common pattern
Regarding how an appln
Submits computation/data
to a general library
el_stp_update_routing_table( )
stp_update_routing_table( )
40. Non-Juniper
Asynchronous Programming Design Patterns Timers
Timers are one of the important elements of programming which contribute to Async programming
Timer allows us to do Timer driven programming
Schedule computation to be performed in future, one shot or periodically
In this section , Let us first learn Posix timer APIs, and how to launch a new timer
Then we will analyse the unwanted concurrency problem introduced by Timers in STP project
We shall integrate Timers With Event Loop Library so that our STP process stays logically single threaded program
Appendix D to learn Posix timers
From next Video, I assume you already know how to create/delete Posix timers
If you already have any other timer library which you have been using on Linux platform, feel free to use it and skip Appendix D
41. Non-Juniper
Asynchronous Programming Design Patterns Timers STP Project
Homework :
• Routing table entries which are learned by Network should have expiration time of 30 sec
• Entries which are created by User should not have any expiration time
• First Timers to work with ‘Timers’ –
• Opportunity to learn working with timers
• Conceptually same concept irrespective of timer library
42. Non-Juniper
Asynchronous Programming Design Patterns Timers Implement Timer Expiration
rt.h changes :
#define RT_ENTRY_EXP_TIMER 30
Define new members in below structures :
struct rt_table_entry_ {
…
Timer_t *exp_timer;
int exp_timer_msec;
} ;
int /*0 on success, -1 on failure*/
rt_insert_new_entry(rt_table_t *rt,
char *dest, char mask,
char *gw, char *oif,
int exp_timer_in_millisec); << new arg, if 0 do not start the timer, else start the timer
43. Non-Juniper
Asynchronous Programming Design Patterns Timers STP Project
Homework :
• Routing table entries which are learned by Network should have expiration time of 30 sec
• Entries which are created by User should not have any expiration time
• First Timers to work with ‘Timers’ –
• Opportunity to learn working with timers
• Conceptually same concept irrespective of timer library
44. Non-Juniper
Process STP
foo( )
bar( )
pkt_process( )
ipc_process( )
cli_handler( )
kernel_events( )
timer_event( )
Network
Pkt listener
thread
User input
listener
thread
User
Kernel events
listener
thread
Kernel
Process
communication
thread
Process P1
Timers Have Introduced
• Unwanted Concurrency
• Rt table entry is deleted by its timer thread
• User might be updating rt table through CLI
• While at the same time , pkt listener thread might be
Updating routing table
timer
thread
Asynchronous Programming Design Patterns Timers STP Project Unwanted Concurrency
Solution :
• Handover Timer Events to Event Loop thread !
• Let Event Loop Thread delete the RT Table Entry
• Again – All external threads work is serialized by
Event Loop thread
• External Threads – Main thread , Pkt Listener
thread, Timer threads for each RT entry
45. Non-Juniper
Asynchronous Programming Event Loops Summary
Event loop is an async programming structure which schedule the computation to be formed in future ( Work Deferral )
Event loop Dequeues the Computation out of Queue and Triggers them ( place it on execution stack )
Event loop is a powerful programming structure which helps avoid forced multithreading/locking
Event loop helps in realizing concurrency in a single threaded programs ( Next Section )
Event loop works like a in-charge Or prime authority of the application code, no other thread is allowed to execute
Application code but the event loop only
Other threads of the application ( Timers, Socket Threads , UI Threads ) need to place the request for computation to
Event loop
It is Event loop thread which then triggers the computation request submitted by several other threads
Event loop is also called as Event dispatcher
47. Non-Juniper
Asynchronous Programming Design Patterns Preemption
We have already discussed an example of Preempting the ongoing current task and scheduling it again In a event loop
Preemption is required so that same job do not occupy the entire CPU for a very long time.
Multiple EL jobs/Taks should get fair chance to get enough CPUs so that “Progression” is observed
Same is used by OS’s scheduler when it schedules processes using context-switching
So, now we will going to observer two levels of Scheduling :
Level1 : OS scheduling the processes, including our stp.c
Level2 : stp.c scheduling EL tasks ( EL tasks are invisible to underlying OS )
Lets do it formally now
48. Non-Juniper
Asynchronous Programming Design Patterns Preemption Context Save
For the task to get preempted and rescheduled again, the task must resume from the same state where it had
left
Application must save the task state before preempting it, and restore it when task is scheduled again
This is similar to Context Switching done by OS in the context of thread switching
Analogy : Book Reading ….
Here task state means – Remembering how much work has been completed by the task
Let us reinvent the “printing routing table” function – but lets implement it using preemption
In every Preemption & Reschedule, lets print 10 entries incrementally from Routing table
49. Non-Juniper
Asynchronous Programming Design Patterns Assignment
Before we proceed further, let do one assignment
Introduce a new option in STP main menu :
6. Serialize and send Rt Entry
7. Exit
New Option 6 must do the following :
Ask from user the Route Credentials : Destination & Mask = 32
Ask Destination Port Number 50000
Create a new task :
Task must search RT entry in RT table, if found send it across network using UDP to 127.0.0.1:50000
To encode RT entry in a msg to be sent, use the same msg layout as used in udp_client.c with code ROUTE_CREATE
Run an instance of stp.exe in another terminal window, and start a pkt listener thread listening on port number 50000
Now, Run another instance of stp.exe and choose option 2 to create a new rt entry followed by option 6
Link to the soln is shared with the video.
51. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization
Tasks may have priority associated with them which represents how important the task is !
Important task must be given CPU earlier and more CPU time as compared to other non-important tasks
Some tasks can tolerate delay in getting CPU time, hence, must be of least priority
Event Loop thread must choose Task with higher priority over task with Lower priority to schedule them on CPU
Task with priority Pj must be chosen by EL thread for execution only and only when there are no task scheduled with priority P such that P > Pj
Event Loop Thread must maintain a EL queue one for each priority level
EL thread must finish all task scheduled with higher priority first
Enhance the below API to include priority of the task as new parameter
task_t *
task_create_new_job(event_loop_t *el, event_cbk cbk, void *arg, TASK_PRIORITY_T task_priority);
TASK_PRIORITY Value Task Example
TASK_PRIORITY_HIGH 0 Packet Processing, Show commands,
Pkt sending
TASK_PRIORITY_MEDIUM 1 Config Commands, Algorithmic Computation
TASK_PRIOTITY_LOW
2 Object Deletion, Garbage Collection
52. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization
Tasks may have priority associated with them which represents how important the task is !
Important task must be given CPU earlier and more CPU time as compared to other non-important tasks
Some tasks can tolerate delay in getting CPU time, hence, must be of least priority
Event Loop thread must choose Task with higher priority over task with Lower priority to schedule them on CPU
Task with priority Pj must be chosen by EL thread for execution only and only when there are no task scheduled with priority P such that P > Pj
Event Loop Thread must maintain a EL queue one for each priority level
EL thread must finish all task scheduled with higher priority first
TASK_PRIORITY Value Task Example
TASK_PRIORITY_HIGH 0 Packet Processing, Show commands,
Pkt sending
TASK_PRIORITY_MEDIUM 1 Config Commands, Algorithmic Computation
TASK_PRIOTITY_LOW
2 Object Deletion, Garbage Collection
53. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization Code Changes
Enhance the below API to include priority of the task as new parameter
task_t *
task_create_new_job(event_loop_t *el, event_cbk cbk, void *arg,
TASK_PRIORITY_T task_priority);
Enhance the below API such that, task with higher priority is returned first
static task_t *
event_loop_get_next_task_to_run(event_loop_t *el);
54. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization Problems Addressed
1. Making show command highest priority task, User response time is enhanced
2. Making packet processing as highest priority task helps to process incoming network packets in a time bound
manner
3. Making delete-tasks as lowest priority task, Premature Deletion of objects is addressed
55. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization Problems Addressed
1. Making show command highest priority task, User response time is enhanced
Let there be Algorithm A, which executes with MEDIUM priority and with preemption. Let A takes total of 5 sec
to finish execution, preempts after every 1 sec
A1 A2 A3 show A4 A5
lag
show_task
A_task
show_task – Medium, Algorithm A priority - HIGH
A1 A2 A3 A4 A5 show
lag
User trigger
Show command
show_task
EL_Q
EL_Q
User trigger
Show command
show_task
EL_Q
A_task
show_task
EL_Q
56. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization Problems Addressed
2. Making packet processing as highest priority task helps to process incoming network packets in a time bound manner
Client Server
HTTP Req
Server uses Event Loop scheduling such that :
Algorithm A is HIGH Priority and HTTP Req Pkt processing task is Medium Priority
A1 A2 A3 A4 A5 http_pkt_processing
_task
lag
HTTP_
processing_task
HTTP Res
Client will experience delayed response from server
Soln : Make HTTP pkt processing task a high priority
And Algorithm A lower priority
HTTP Req pkt
arrives
HTTP_processing_task
A_task
EL_Q EL_Q
57. Non-Juniper
Asynchronous Programming Design Patterns Task Prioritization Problems Addressed
3. Making delete-tasks as lowest priority task, Premature Deletion of objects is addressed
Discussed in next section
59. Non-Juniper
Asynchronous Programming Design Patterns Handling Premature Deletion
Premature Deletion means, deleting a Data Object from the system when there are Tasks scheduled to be operating on the object
The problem of premature deletion is inherent to any environment which is concurrent
Be it, single threaded concurrent environment which is in-charge of event-loop Or
multi threaded concurrent environment achieved via multi-threading
60. Non-Juniper
Asynchronous Programming Design Patterns Understanding Premature Deletion
Following event happens in STP process exactly in same sequence :
1. A new RT table entry is malloc’d, PTR is pointer to it
2. This RT table entry is inserted into RT table
3. An Expiry timer is triggered for this rt entry
4. A task TS is scheduled on this rt entry which serialize it and send over network to remote machine
5. Expiry timer Fires :
1. Detach rt entry from RT table
2. Free this RT table entry – free(PTR)
6. TS is triggered, it perform its operation on this PTR
7. STP crashes because pointer to rt_table entry PTR is a dangling pointer
T1 T2 T3
D D D
free(D)
Dangling ptr Dangling ptr
Premature Deletion means, deleting a Data Object from the system when there are Tasks scheduled to be operating on the object
61. Non-Juniper
Asynchronous Programming Design Patterns Handling Premature Deletion Solution
Solution :
Problem to Premature deletion is resolved when :
Deletion (free) of Data object D is postponed until all Task scheduled to operate on it has also finished their respective operation on it
Let their be Three Tasks T1 , T2 and T3 , Where T3 task is a delete task.
Event-loop thread must schedule Task T3 only when Task T1 and T2 have finished their respective operation on D
This would ensure no other task in the system ever sees D through Dangling Pointer
Hence, Simple Solution is :
Always delete objects through Separate Tasks, and not directly
Delete Task must be of least Priority Pd
Any Non-Delete Task Must have priority P > Pd
T1 T2 T3
D D D
free(D)
62. Non-Juniper
Asynchronous Programming Design Patterns Handling Premature Deletion Solution in STP
Changes in STP process :
Delete Single Rt Table Entry option # 4
• Create a new Taks with Priority LOW
• In Task CBK fn :
• Detach incoming pointers to rt_entry
• Detach rt_entry from container Data Structures
• Release rt_entry resources
• free rt_entry
• Return EL_FINISH
Introduce a new Option # 7
Delete rt table : void stp_el_delete_rt_table ( rt_table_t *rt_table)
• Create a new Taks with Priority LOW
• In Task CBK fn :
• Detach incoming pointers to rt_table
• Detach rt_table from container Data Structures ( Imp )
• Release rt_table resources
• Preempt after deletion of 10 entries, Return EL_CONTINUE
• Return EL_FINISH if all entries are deleted
64. Non-Juniper
Asynchronous Programming Design Patterns Avoid Redundant work
Real Life Examples :
> Your mother ask you to mob the floor 5 times in a day, you mob it 5 times
You end up cleaning the same floor unnecessarily 4 times
> Your mother ask you to mob the floor 5 times in a day, but you obey only the last order and mob the floor on last call
Intelligent !!
Why to do same work multiple times, when the work produces the same result (Or result of doing it again overwrite
the result of previous work) no matter how many times you do it. Its better to do it only on last call
65. Non-Juniper
foo (){
. . .
. . .
if (C1) algo1();
. . .
. . .
if (C2) algo1();
. . .
. . .
}
Second invocation of algo1()
makes the prev invocation of algo1()
redundant
foo (){
bool call_algo1 = FALSE;
. . .
. . .
if (C1) call_algo1 = TRUE;
. . .
. . .
if (C2) call_algo1 = TRUE;
. . .
. . .
if (call_algo1) algo1();
}
Better
Approach
Call algo1 only once !
But requires tracking using local bool variable
Will not work if foo() is deeply nested fn and
Call to algo1 is decided in deep nested functions
also
Best Approach
foo ( ) {
. . .
if (C1) {
if (!algo_task) {
algo_task = task_create_new_job(el, algo1, arg)
}
}
. . .
if (C2) {
if (!algo_task) {
algo_task = task_create_new_job(el, algo1, arg)
}
}
}
Asynchronous Program
Call algo1 only once !
Requires tracking using global algo_task variable
Will work in nested fn call cases !
Asynchronous Programming Design Patterns Avoid Redundant work
66. Non-Juniper
Programming Language : C/C++ ( but concepts are language agnostic )
Pre-requisites : Thread Synch ( Mutex and Condition Variables ), Socket programming basics
Website : www.csepracticals.com