Lab 3 Design of Server Software - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Lab 3 Design of Server Software

Description:

Thanks for Prof. Y. W. Leung providing valuable materials by which ... int main(int argc, char *argv[]) { add(3); add(5); return 0; 11. 2. Concurrent Processing ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 24
Provided by: orc52
Category:
Tags: argv | design | lab | server | software

less

Transcript and Presenter's Notes

Title: Lab 3 Design of Server Software


1
Lab 3 Design of Server Software
  • Dr. Tony K. C. Chan

2
  • Acknowledgements
  • Thanks for Prof. Y. W. Leung providing valuable
    materials by which this teaching materials is
    prepared.

3
  • References
  • 1 D. E. Comer and D. L. Stevens,
    Internetworking with TCP/IP Volume III
    Client-Server Programming and Applications,
    Prentice-Hall, Inc, 1997.
  • 2 Beejs Guide to Network Programming Using
    Internet Sockets Online Available
    http//beej.us/guide/bgnet/
  • 3 Winsock Functions Online Available
    http//msdn2.microsoft.com/en-us/library/ms741394.
    aspx.

4
1. A Short Review
  • Four Basic Types of Servers

Iterative Connectionless
Iterative Connection-Oriented
Concurrent Connectionless
Concurrent Connection-Oriented
5
2. Concurrent Processing
  • 2.1 Process and Thread
  • We can use processes and threads to realize
    concurrency on a processor via time sharing.
  • Process
  • Process is a fundamental unit of computation.
    The OS has to store various information about
    each process.

6
2. Concurrent Processing
  • 2.1 Process and Thread, cont.
  • Thread
  • Windows provides a second form of concurrent
    execution known as threads of execution of
    threads.
  • Each thread must be associated with a single
    process.
  • A thread is executed independently of other
    threads.
  • All threads in a process share
  • Global variables and
  • Resources that the OS allocates to the process.
  • Each thread in a process has its own local
    variables.

7
2. Concurrent Processing
  • 2.2 Creating Threads in Windows
  • In Windows, a program calls _beginthread() to
    create a new thread to execute a specified
    function.
  • ? The program and the newly created thread are
    executed concurrently.
  • _beginthread() requires three parameters
  • 1st parameter start address of the function to
    be executed.
  • 2nd parameter stack size for the new thread (we
    can use 0 for our purpose).
  • 3rd parameter pointer to a list of parameters to
    be passed to the function if no parameter is
    passed, specify NULL.
  • For more details, read the help pages in MS
    Visual C

8
2. Concurrent Processing
  • 2.2 Creating Threads in Windows, cont.
  • A program can call _endthread() (without any
    parameter) to terminate a thread explicitly.
    However, when the function has completed
    execution, _endthread() is called automatically.
  • Remarks In MS visual C, remember to include
    the header file ltprocess.hgt and link to the
    library libcmt.lib.

9
2. Concurrent Processing
  • 2.2 Creating Threads in Windows, cont.
  • Example
  • We consider an add process as follows.
  • void add(int count)
  • int i, sum
  • sum0
  • for (i1 iltcount i)
  • printf("i d\n", i)
  • sum i
  • printf("The sum of i is d\n", sum)

10
2. Concurrent Processing
  • 2.2 Creating Threads in Windows, cont.
  • Example, cont.
  • What will be printed in the following codes?
  • include ltstdio.hgt
  • void add(int)
  • int main(int argc, char argv)
  • add(3)
  • add(5)
  • return 0

11
2. Concurrent Processing
  • 2.2 Creating Threads in Windows, cont.
  • Example, cont.
  • What will be printed in the following codes?
  • pragma comment( linker, "/defaultliblibcmt.lib"
    )
  • include ltstdio.hgt
  • include ltprocess.hgt
  • void add(int)
  • int main(int argc, char argv)
  • _beginthread(add, 0, (void )3) / Execute in
    a thread /
  • add(5) / Execute in the main process /
  • return 0

12
Exercises
  • 1. Write a multithread program using
    _beginthread()
  • 2. Write a concurrent program that starts five
    threads. Arrange for each thread to print a few
    lines of output and then halt. Is output from the
    threads intermixed?

13
3. Implementing Concurrent Server
  • A concurrent server executes a main thread and
    multiple service threads.
  • Main Thread (or Master Thread)
  • When a server program starts execution, the main
    thread is executed.
  • The main thread creates a socket and waits for
    clients requests.
  • When a clients request arrives, the main thread
    creates a new service thread to serve this
    client. Meanwhile, the main thread waits for
    another request.

14
3. Implementing Concurrent Server
  • Service Thread (or Slave Thread)
  • Each service thread is created to serve one
    client.
  • When the service is completed, the service thread
    is terminated.
  • If the server computer in serving N clients,
    there are one main thread and N service threads.

15
Exercises
  • 1. Write a concurrent connection-oriented server
    that provide ECHO service.
  • 2. Modify the server in 1 so that the server
    keeps a log of the time at which it creates each
    slave thread and the time at which the slave
    terminates.
  • 3. Build an iterative implementation of an ECHO
    server. Conduct an experiment to determine if a
    human can sense the difference in response time
    between the concurrent and the iterative versions.

16
4. Asynchronous I/O
  • An application calls the select(), asking the OS
    which socket becomes ready for I/O.
  • The call returns when at least one socket becomes
    ready or timer expires.
  • Then the application can perform I/O through the
    ready sockets.

17
4. Asynchronous I/O
  • select() uses the following structure to
    represent a set of socket descriptors to be
    handled
  • Typedef struct fd_set
  • u_int fd_count
  • SOCKET fd_arrayFD_SETSIZE
  • fd_set
  • where
  • fd_count is the number of socket descriptors in
    the set
  • fd_array is an array of socket descriptors and
    FD_SETSIZE is the maximum number of descriptors
    in a set

18
4. Asynchronous I/O
  • Some useful macros for asynchronous I/O(Let set
    be a set of socket descriptors fd_set set)
  • To initialize set to empty, execute
  • FD_ZERO(set)
  • To add the socket descriptor s to set, execute
  • FD_SET(s, set)
  • To remove s from set, execute
  • FD_CLR(s, set)
  • To test if s belongs to set, execute
  • FD_ISSET(s, set)
  • If s belongs to set, the return value is
    non-zero otherwise, zero.

19
4. AsynchronousI/O
  • The select function determinesthe status of one
    or moresockets, waiting if necessary,to perform
    synchronous I/O
  • Example
  • fd_set rfds/ Call select() to select
    socketsthat are ready for reading /select(0,
    rfds, (fd_set )0, (fd_set )0, (struct
    timeval )0)

20
5. Implementing Single-Thread Concurrent Server
  • Use asynchronous I/O and a single thread to
    provide apparent concurrency.
  • Example
  • SOCKET msock, s
  • fd_set afds, / set of all sockets created /
  • fd_set rfds / set of sockets having input I/O
    /
  • int i
  • / create master socket here /
  • FD_ZERO(afds)
  • FD_SET(msock, afds)
  • for () / an infinitive loop for handling
    the incoming requests /

21
5. Implementing Single-Thread Concurrent Server
  • Example, cont.
  • / codes inside the an infinitive loop /
  • memcpy(rfds, afds, sizeof(rfds))
  • select(0, rfds, (fd_set )0, (fd_set )0,
    (struct timeval )0)
  • if (FD_ISSET(msock, rfds))
  • / handle the request form the master socket /
  • for (i 0 ilt rfds.fd_count i)
  • s rfds.fd_arrayi
  • if (s ! msock FD_ISSET(s, rfds))
  • / handle the incoming message from the i-th
    socket in the set /

22
Exercises
  • 1. Write a ECHO service using asynchronous I/O.
  • 2. Conduct an experiment that proves the ECHO
    server in 1 can handle connections concurrently.

23
THE END
Write a Comment
User Comments (0)
About PowerShow.com