Homework1
System programming 2011Ď
Introduction
!
In this assignment
!
You have to modify a simple chat server to a multiplexing chat server
!
You don’t have to handle the network communication. We will handle that partĎ
2
Chat server
Chat serverĎ
<RD>Ď <RD>Ď
<RD>Ď
<WR>Ď
<WR>Ď <WR>Ď
Client AĎ Client BĎ
Client CĎ
3
We will provide
!
A simple Chat Server
! Handle only one request
! Handle only one msg Ď
5
File desĎ 0Ď 1Ď 2Ď 3Ď 4Ď …Ď …Ď 1022Ď 1023Ď
flagĎ 1Ď 1Ď 1Ď 0Ď 0Ď 0Ď 0Ď 0Ď 0Ď
requestP:Ď
The server.listen_fdĎ
6
7
File desĎ 0Ď 1Ď 2Ď 3Ď 4Ď …Ď …Ď 1022Ď 1023Ď
flagĎ 1Ď 1Ď 1Ď 1Ď 0Ď 0Ď 0Ď 0Ď 0Ď
requestP:Ď
The server.listen_fdĎ accept return fd =conn_fdĎ
9
DEMO TIMEĎ
Your Job is
!
A Multiplexing Chat Server
! Handle!mutiple requests simultaneously
10
What you should do?
!
Use select
! Select() allows a program to monitor multiple file
descriptors,waiting until one or more of the file descriptors become “ready” for some class of I/O operation
! If we have a nonblocking descriptor that we Want to read from and we call select with timeout value of 5 seconds,select will block for up to 5 seconds.Ď
11
Detail Spec
!
You can download the files from:
! http://www.csie.ntu.edu.tw/~pjcheng/course/sp2011/
assignments.html
! You can start up server using:
! $> gcc –wall sp_hw1_chatsvr.c
!
You can start up server using:
! $> ./a.out 6000
! 3000<port<30000
How Can I get full Score
!
Prevent from waiting for reading(2%)
!
Prevent from waiting for writing(2%)
!
message passing (2%)
!
Detect disconnection (1%) Ď
13
Example
!
Client A : <RD> !
!
!
Client B:
<WR>hello ! everyone ! !
hello everyone
Ď Client A’s screenĎ
14
Example
!
Client A : <RD> !
!
Client C : <RD> ! !
!
Client B:
<WR>hello ! everyone!
!
Ď
hello everyone
Ď Client A’s screenĎ
Client C’s screenĎ
15
Example
! Client A : <RD> ! !
! Client C :
<WR> it’s my life!
! Client B:
<WR>hello ! everyone!
!
! Client C:
I know !
!
Client A’s screenĎ hello
Everyone it’s my life I know
Ď
Example
!
Client A : <WR>hello ! (break connection)
Server Detect disconnection and out put msg
”someone logout” on server side
17
Pseudo code
18
Set_fd();
ret = select(nfds,& rfds, &wfds, &efds, &timeout);
If(ret>0) then check new condiction;
For i=4 to maxfd {
if(FD_ISSET(i,&rfds))handle_read();// may create a pool here.
if(FD_ISSET(i,&wfds))handle_write();//may use the data in pool.
}
Submission
19
! Course website: http://newslabx.csie.ntu.edu.tw/courses
! 1. Makefile
! 2. README.txt
! 3. sp_hw1_chatsvr.
! You should tar this three files
! And submission the tared file named
! SPHW1_STUDENT_ID.tar.gz ex: SPHW1_b98902000.tar.gz
! And the first lines of her sp_hw1_chatsvr.c would be /* b97902000林玲綾*/
! **please do not use .rar or other file type.**
Questions
!
Ptt2 :SysProgram
!
Email:[email protected]
! Title:[sp][hw1]question title
!
Come to lab302
Finally
!
Submit your program before 4/10 23:59
!
Demo your program after 4/10
!
Check your ptt2 frequently Ď
21