Threaded Programming in Python

Slides:



Advertisements
Similar presentations
Chapter 15 Multithreading, Networks, and Client/Server Programming
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 26 Client Server Interaction Communication across a computer network requires a pair of application programs to cooperate. One application on one.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Experience with Processes and Monitors in Mesa
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Networks and Client/Server Applications Handling Multiple Clients Concurrently.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
2001 Networking Operating Systems (CO32010) 1. Operating Systems 2. Processes and scheduling 4.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Tutorial 2: Homework 1 and Project 1
Multithreading The objectives of this chapter are:
CSCD 330 Network Programming
CS703 - Advanced Operating Systems
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Processes and threads.
CPU SCHEDULING.
Process concept.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
PROCESS MANAGEMENT IN MACH
Networks and Client/Server Applications
Multi Threading.
Background on the need for Synchronization
Multithreaded Programming in Java
Chapter 3: Process Concept
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Lecture 21 Concurrency Introduction
Inter-Process Communication and Synchronization
Multithreading Chapter 23.
Chapter 6: CPU Scheduling
Multithreading.
Multithreading.
Multithreaded Programming
Threads Chapter 4.
Chapter 10 Operating Systems.
Concurrency: Mutual Exclusion and Process Synchronization
Threaded Programming in Python
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Chapter 3: Processes Process Concept Process Scheduling
Multithreading The objectives of this chapter are:
Processes August 10, 2019 OS:Processes.
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threaded Programming in Python CPE 401 / 601 Computer Network Systems Mehmet Hadi Gunes Adapted from Fundamentals of Python: From First Programs Through Data Structures

Objectives Describe what threads do and how they are manipulated in an application Code an algorithm to run as a thread Use conditions to solve a simple synchronization problem with threads Use IP addresses, ports, and sockets to create a simple client/server application on a network Decompose a server application with threads to handle client requests efficiently Restructure existing applications for deployment as client/server applications on a network

Threads In Python, a thread is an object like any other in that it can hold data, be run with methods, be stored in data structures, and be passed as parameters to methods A thread can also be executed as a process Before it can execute, a thread’s class must implement a run method During its lifetime, a thread can be in various states

Threads (continued)

Threads (continued) A thread remains inactive until start method runs Thread is placed in the ready queue Newly started thread’s run method is also activated A thread can lose access to the CPU: Time-out (process also known as time slicing) Sleep Block Wait Process of saving/restoring a thread’s state is called a context switch

Threads (continued) A thread’s run method is invoked automatically by start

Threads (continued) Most common way to create a thread is to define a class that extends the class threading.Thread

Sleeping Threads The function time.sleep puts a thread to sleep for the specified number of seconds

Sleeping Threads

Sleeping Threads

Producer, Consumer, and Synchronization Threads that interact by sharing data are said to have a producer/consumer relationship Example: an assembly line in a factory A producer must produce each item before a consumer consumes it Each item must be consumed before the producer produces the next item A consumer must consume each item just once We will simulate a producer/consumer relationship: Will share a single data cell with an integer

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization Threads sleep for random intervals

Producer, Consumer, and Synchronization Synchronization problems may arise: Consumer accesses the shared cell before the producer has written its first datum Producer then writes two consecutive data (1 and 2) before the consumer has accessed the cell again Consumer accesses data 2 twice Producer writes data 4 after consumer is finished Solution: synchronize producer/consumer threads States of shared cell: writeable or not writeable

Producer, Consumer, and Synchronization Solution (continued): Add two instance variables to SharedCell: a Boolean flag (_writeable) and an instance of threading.Condition A Condition maintains a lock on a resource

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization Pattern for accessing a resource with a lock: Run acquire on the condition. While it’s not OK to do the work Run wait on the condition. Do the work with the resource. Run notify on the condition. Run release on the condition.

Producer, Consumer, and Synchronization

Summary Threads allow the work of a single program to be distributed among several computational processes States: born, ready, executing, sleeping, and waiting After a thread is started, it goes to the end of the ready queue to be scheduled for a turn in the CPU A thread may give up CPU when timed out, sleeps, waits on a condition, or finishes its run method When a thread wakes up, is timed out, or is notified that it can stop waiting, it returns to the rear of the ready queue Thread synchronization problems can occur when two or more threads share data A server can handle several clients concurrently by assigning each client request to a separate handler thread