Threads & Networking C# offers facilities for multi threading and network programming an application roughly corresponds to a process, handled by the OS.

Slides:



Advertisements
Similar presentations

Advertisements

Categories of I/O Devices
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Lecture 4 Thread Concepts. Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Multithreading in Java Nelson Padua-Perez 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.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 19 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Ryerson University CPS Distributing Computing with Java.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick 1.
Introduction to Threads CS240 Programming in C. Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
Özgür Zeytinci Network Programming Microsoft.NET.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
1 Confidential Enterprise Solutions Group Process and Threads.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
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.
UDP Client-Server. The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you.
Threading Eriq Muhammad Adams J
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Laboratory - 4.  Threading Concept  Threading in.NET  Multi-Threaded Socket  Example.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
12/5/2015.net 1 system.net Contains any network functionallity you would need in c# Several sub namespaces exists to allow for more fined control –System.Net.Sockets.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Java Server Programming Web Interface for Java Programs.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSC 480 Software Engineering Lab 2 – Multi-Threading Oct 18, 2002.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
1 Network Communications A Brief Introduction. 2 Network Communications.
Big Picture Lab 4 Operating Systems C Andras Moritz
Threads in Java Two ways to start a thread
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Preemption Set timer interrupts But now, must synchronize Two tools:
Computing with C# and the .NET Framework
Distributed Systems - Comp 655
Multi-routing the '.Net' Gigax, Kevin Edward Torres, Francisco Javier
Units with – James tedder
Units with – James tedder
.Net Sockets.
First slide Rest of project 2 due next Friday Today:
Threads and Multithreading
Multi-routing the '.Net' Gigax, Kevin Edward Torres, Francisco Javier
Multiplexing/Demux.
Lecture 19 Threads CSE /6/2019.
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Presentation transcript:

Threads & Networking C# offers facilities for multi threading and network programming an application roughly corresponds to a process, handled by the OS time sharing simulates multi tasking inside an application : several execution threads

Multitasking P1 P2 P3 T3_1 T3_2 time P1 P2 P3 T31 T32 P1 P2 P3 T31 T32

Multithreading thread = 'light' process several threads belonging to a single application share the same memory space : efficient communication the main process is a thread : the main thread easy creation of a thread associated to a method : the method is ran independantly of the program

Multithreading the method is ran 'out of sequence' and sometimes need synchronization with other threads. Thread class implements _Thread interface public sealed class Thread : _Thread {…}

Creating a thread : example class test { static void run() // some interesting code inside } static void Main(string[] args) Thread th0 = new Thread(new ThreadStart(run)); th0.start();

class thApp { public static void countup() long i; for (i=1; i <=100;i++) System.Console.WriteLine(i.ToString()); } class test static void Main(string[] args) Thread th0 = new Thread(new ThreadStart(thApp.countup)); th0.start();

class thApp2 { public void hello() System.Console.WriteLine("hello world !"); } class test static void Main(string[] args) thApp2 myvar = new thApp2(); Thread th0 = new Thread(new ThreadStart(mavar.hello)); th0.start();

Synchronization class test { static void Main(string[] args) Thread th0 = new Thread(new threadStart(thApp.countup)); th0.start(); thApp2 myvar = new thApp2(); th0 = new Thread(new ThreadStart(myvar.hello)); // don't forget the pause }

Synchronization executing the program 3 times gives the following result : 1 2 3 … 22 hello world ! 23 24 99 100 1 2 3 4 hello world ! 5 6 7 … 99 100 1 2 3 … 22 23 24 99 100 hello world !

Synchronization to ensure synchronization, use : sleep() abort() join() interrupt()

locks & semaphores several threads sharing a resource critical section : exclusive execution use a lock to ensure exclusivity

locks & semaphores in each thread willing to access an exclusive resource : object access_grant=new object(); lock(access_grant) { critical section; }

locks & semaphores problem : access_grant is local to each thread : use a static object to share among threads class myThread { static object access_grant = new object(); public void myMethod() lock(access_grant) critical section; }

locks & semaphores class test { static void Main(string [] args) myThread [] progs = new myThread[3]; foreach (myThread m in progs) new Thread(new ThreadStart(m.myMethod)).Start(); }

locks & semaphores if a thread asks for access while already locked : thread is queued wrt the access_grant object. use the Monitor class to check access_grant status before entering critical section

Monitor class object access_grant = new object(); Monitor.Enter(access_grant); critical section; Monitor.Exit(access_grant); TryEnter method : if the resource is locked, do something else.

Monitor class if (Monitor.TryEnter(access_grant)) // true if resource is available { critical section code } else do something interesting anyway

Semaphore and Mutex to synchronize threads and processus :use the Semaphore and Mutex classes static Semaphore sem(0,n); // initial and max threads allowe to possess the semaphore sem.WaitOne() critical section sem.Release()

Semaphore and Mutex a Mutex is a Semaphore with initial count=0 ans a max count = 1 for more informations on semaphores, locks, monitors, see your OS documentation.

Working with processes a little break in theory how to list processes on the local computer use the System.Diagnostics classes and the Process class

Working with processes class test { static void Main(string [] args) Process [] locals = Process.GetProcesses(); foreach(Process p in locals) System.Console.Write(p.ProcessName); } // pause

Working with processes class test { static void Main(string [] args) Process appex = new Process(); appex.StartInfo.FileName = "path_to_exe"; appex.StartInfo.UseShellExecute = false; appex.StartInfo.RedirectStandardOutput = false; appex.Start(); // target application starts here // pause } Executing a standard application (any .exe) from a C# program

Networking made easy use System.Net and System.Net.Sockets C# : TcpListener and TcpClient classes server client 1 listen to connections trough a TcpListener 2 connection to server through a TcpClient 3 connection is accepted : dialog through a TcpClient 4 dialog through TcpClients

Sample server code class server { private TcpListener ear; private TcpClient cli_sock; public server() byte [] local_IP ={127,0,0,1}; ear = new TcpListener(new IPAdress(local_IP),8080); ear.start(); cli_sock = ear.AcceptTcpClient(); // blocking // now talk with the client }

Sample client code class client { private TcpClient sock; public client() sock = new TcpClient("server_name",8080); // now talk with the server if connected }

working with streams input and output streams to read/write with TcpClient objects StreamReader in=new StreamReader(sock.GetStream()); StreamWriter out=new StreamWriter(sock.getStream()); out.AutoFlush=true; read data with in.ReadLine(); write data with out.WriteLine();

networks and threads sockets (TcpClient objects) may send and receive information at any time : use a thread to run a method that receives information : while (connection is valid) { read data sent by the server-side socket; }

Communication network applications : using sockets and a communication protocol dedicated to the application; existing protocol : SMTP, FTP, HTTP, SOAP : Web development.