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

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.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
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
Windows Programming Using C# Threading. 2 Contents Threading Thread class Interlocked class Monitor class Semaphore class Thread Pools.
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.
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.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured 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.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Embedded Computer - Definition When a microcomputer is part of a larger product, it is said to be an embedded computer. The embedded computer retrieves.
CE Operating Systems Lecture 8 Process Scheduling continued and an introduction to process synchronisation.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
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 / Concurrency
Threaded Programming in Python
Processes and threads.
Process concept.
Process Management Process Concept Why only the global variables?
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
Multithreading Chapter 23.
Multithreading.
Multithreading.
Concurrency: Mutual Exclusion and Process Synchronization
Threaded Programming in Python
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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

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)

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

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

Threads sleep for random intervals

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

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