Presentation is loading. Please wait.

Presentation is loading. Please wait.

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.

Similar presentations


Presentation on theme: "Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores."— Presentation transcript:

1 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 1 What happens when many clients want to contact the server at the same time ?  The iterative approach consist in “enqueuing” the requests (this is done automatically by the system) and serving them one by one :  Accept the next connection and build the socket  Read request  Process request (send the response)  This will inevitably mean that if there is a client with a small request (for example, a small file request) will have to wait until the large requests are over  If more than the allowed number of clients at socket queue request a service during this time they are rejected !!!  There may be also some delays in attending clients by waiting some information (client are asked to type in something)  There are many Disk I/O operations in a file serving scenario which are normally “slow”and do not require network traffic or CPU  So there is a lot of resources standing idle with this schema

2 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 2 The Concurrent Server  A concurrent server can attend many clients at the same time  While transferring a file, it can still keep listening for requests  Every time a request comes, a new parallel line of statements execution is started for attending this request. After this the server can “hear” again at the server socket for further requests  Different approaches have been developed to implement parallel lines of executions within a program  The operative system plays an important role

3 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 3 In concurrent servers there is a main program which listens for client requests A SERVER A CLIENT 4444

4 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 4 After a client is contacted, a new line of execution is created which will attend the request in parallel. A SERVER A CLIENT 4444

5 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 5 The main program will return to the listening loop while the first client is being attended, so it can accept another request A SERVER A CLIENT 4444

6 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 6 … and create another parallel line of execution for attending the client A SERVER A CLIENT 4444

7 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 7 … so a request of a third client can be immediately be accepted A SERVER A CLIENT 4444

8 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 8 … thus having the three clients being attended in parallel A SERVER A CLIENT 4444

9 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 9 The Concurrent Server Algorithm Master program of the server 1. Create a server socket 2. Wait for and accept a client’s request 3. Create a parallel line of execution to attend the requirement of the new client 4. Goto 2. Slave (parallel) process : 1. Receive the parameters of the communication established by the master (socket or input/output data streams) 2. Attend client’s request (read filename, transfer data) 3. Return (disappear !)

10 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 10 Parallel Lines of execution  If there is only 1 CPU, why create parallel processes?  It often makes server programming easier.  Because there are more CPU involved !!!!!  The concept of parallel processes implemented at the S.O level appeared with UNIX and C.  In C a new process can be created by executing the fork() statement  int i = fork() creates an exact copy of the running process. Both continue executing the same program with the same variables.  For the “father” the value of i will be the id of the created process. For the child process this value will be 0  When programming concurrent servers the father will be the main process accepting requests and the child will process the client’s request

11 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 11 A simplified example of the use of for for implementing concurrent servers main() { int pid, msock, ssock; msock = passivesock(port, “tcp”, qlen); /* see chapter 10.4 from Internetworking with tcp/ip from Douglas Commer for the implementation */ while(1) { ssock = accept(msock, &fsin, &alen); pid = fork(); if (pid == 0) { process client’s request; return; }

12 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 12 Problems with fork() in UNIX  The creation of a new process is a time costly procedure  In some texts it is suggested to create a set of processes at the beginning which will be activated later by the main program. When a client arrives the parameters of the communication are passed vi pipes to child.  The new created process duplicates all the variables !!!  It is not easy to manage the processes which ended in an abnormal way. They keep the resources !!!.  Sometimes is preferred to use the “select” statement: it basically selects from an array of sockets the first one which has available data to read.

13 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 13 In JAVA is better to use Threads  A thread is a sequence or a flow of instructions which are executed in parallel to the main sequence of the program. It has a start and an end.  A thread can only be created and lives inside an already running process. A process can start as many threads as necessary.  Because of this, the main program has a better control of the threads it started. They can be created, started, suspended, or reactivated by the program.  Threads are implemented as methods of a certain class. When this method (normally called run) is activated, it starts to run in parallel to the method that called it.  As any other method, it can define its own set of variables

14 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 14 Using threads to implement concurrent servers Main (master) program: Create ServerSocket ss; While(true) { Socket s = ss.accept(); Create new thread with the socket s as parameter; start executing thread; } Define a thread class with a parallel method (run) which implements the processing of the clients request

15 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 15 Implementation of Threads  One way (perhaps the most clear one) is to define a new class which is an extension of the Thread class and overwrite its run() method.  Thread is an existing class  This thread class has a run method (originally with no statements) which will be executed in parallel when activated.  In order to execute this method a program must create an object of the extended class and apply the start() method to it.  The definition of the new Thread class should look like this:  public class MyThread extends Thread {  And somewhere should appear:  public void run() { //instructions to be executed in parallel }

16 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 16 Example  The class ActiveClock is a chronometer which can be started, stopped, or resumed  It is an extension of the Thread class, with a run() method which increments the “time” almost every second  The main program will start two of this objects and display a controlling console  ShowClocks is another thread which shows the “time” of two clocks almost every second

17 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 17 This is NOT the way threads should be controlled !!!!  Threads should not be controlled externally by a process which does not know in which state the thread is when it is stopped  The external process just give a signal to the thread it should stop as soon as possible  Before stopping, the thread should come to a coherent state and liberate all the resources is has been using which may be shareable

18 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 18 Sometimes you cannot program the server as an extension of a thread  For example, if the server has to implement a graphical interface it should be declared as an extension of a Frame, if it is programmed as an Applet it must extend the Applet class  The we can use the interface Runnable, which means the Server will also be seen as a Runnable class and has to implement the run method run().  To initiate a parallel execution of the run method a Thread object must be created. In the creation a pointer to the server object (this) should be passed as parameter. With this, the Thread object will have access to the run method.  The run method implemented in the server class will be executed by performing start() on the new created Thread object.

19 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 19 Example with interface Runnable  See and execute the program NoSincron.java  Note that the Threads created in this way will have access to all resources of the master.  With the other form of creating threads, resources from the master object can be made available by passing a point to itself on creation of the thread

20 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 20 Critical regions and semaphores in Java  Java provides basically two methods for controlling concurrent access to resources  You can declare a whole method as a critical region (see sincron1)  You can use the semaphore of any object (see sincron2)  You can also use the wait and notify to synchronize 2 or more threads (see sincron3)

21 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 21 Implementing a concurrent file server (as an extension of Thread)  Define a new Class as an extension of the Thread class which has a socket as Object variable.  Program a constructor which receives a socket as parameter.  Program the run methods to attend the client connected at the other extreme of the socket.  Program a main method which consists of an infinite while. Inside the while program the acceptance of a new client, the creation of a new thread with the socket obtained, and the starting of the thread execution. MultiFileServer MultiFileThread

22 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 22 Broadcasting a text to many clients Hello

23 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 23 The server: receiving a new client 4444 Client contacts server at 4444

24 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 24 The server: receiving a new client 4444 A new socket is placed and the output channel is opened They are kept in an array

25 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 25 The server: Broadcasting a message When a message is entered the server will distribute it to the connected clients BroadcastCliente BraodcastServerNF Type a message

26 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 26 It is now easy to extend this to a chat system Hello

27 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 27 Conditions for implementing a chat system  Server must be listening to requests of new clients AND to messages which are sent by already connected  Client must be listening to messages from the server AND to the keyboard for messages the user wants to transmit.  We need in the server 2 server sockets (and a different thread attending each one)  We need at the client a server socket and a thread to attend it

28 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 28 A Chat based on TCP/IP  Server: 2 approaches  A thread for listening to clients which want to join the chat session, another thread for receiving messages which should be distributed to all participants, and a last one for listening to disconnecting requirements.  A single server listening to all messages. At the beginning of the message there is a command string telling which procedure has the server to perform (and afterwards the required parameters).  Clientes: must behave accordingly  a thread for sending messages and another to receive messages from the server.  Different clients are needed for the different protocols

29 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 29 A concurrent web server  This will be implemented only for HTML files and classes (like servlets), but it is easily extensible for attending different types of requirements  For each client a new thread is created  The process is according to the type of the request HttpProcessor processRequest() HttpFile browser Httpd (server) thread HttpInputStream HttpOutputStream HttpClass HttpException Echo HttpClassProcessor

30 Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 30 Implementing status in a web server  The server must keep tracking of the different users that contact it  The client must submit this information  The Echo2 class keeps track of a “shopping cart” for different users  The client must submit a request like: user=username&product=productcode&qtty=number


Download ppt "Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores."

Similar presentations


Ads by Google