NP HW2: Remote Working Ground (RWG)
In this network programming homework, we are asked to build a remote working ground (RWG), which is actually a combination of hw1 and chat & public pipe function.
According to the spec(hw2Spec.txt
), we need to write two programs:
- Use the single-process concurrent paradigm.
- Use the concurrent connection-oriented paradigm with shared memory.
For more details, please refer to hw2Spec.txt
in the repository (cyhuang1230/NetworkProgramming
).
There are some notable ideas I came up with and described as follows.
(I will start with version II since I wrote that first :P)
For concurrent version:
- Keep track of client’s data (e.g.
sockfd
,pid
…) so that we can send msg to each client. - Store client data & messages in shared memory so let every child can read.
- Create additional
MAX_USER
(# of user) blocks of shm to store direct msg (or broadcst msg). - There’s no way server can be closed properly (must end by
Ctrl+C
), so, in order not to leave any shm unrelaesed, we won’tshmget
until the first client is connected, and free shm when all clients are disconnected.
→ To keep track of the number of children,sa_handler
needs to be implemented separately.
→ Furthermore, MUST resetsa_handler
in the child process so that won’t mistakenly detach shm later whenexec
. - Use signal
SIGUSR1
to notify other process there’s a new msg to write. - Semaphore on public pipes to prevent concurrent issue. (Same create/release idea as shm.)
- Use
FIFO
, a.k.aNamed pipe
, to implement public pipe. [No need to store in shm]. - Implement a generic function to send msg to a specific user.
Function hierarchy:
→sendRawMsgToClient
→tell
→sendRawMsgToClient
→broadcastRawMsg
→yell
/name
/who
For single-process version:
- Basically same structure as ver. II, but as a single-process program, we don’t need any shm or semaphore, which makes it much easier, yayyy!.
- Only involves one huge adjustment.
Consider the following input:
[Client 1] % ls |1
[Client 2] % ls |1
[Client 1] % cat
[Client 2] (empty) <- stucked, prompt won't display
This error is caused by the way we decide input.
→ We can no longer expect the whole input with numbered-pipe
can be got in one while loop.
→ Instead, we have to check input line by line, and keep track of input-related information.
Demo video: (client is not included in the repo)
Source → cyhuang1230/NetworkProgramming
For ver. I (Single process):
Please look for NP_project2_Single.cpp
.
To try: make hw2s
and run ./np_hw2s
.
*Try with debug message: make hw2_debugs
and run ./np_hw2s
.
For ver. II (Concurrent):
Please look for NP_project2_Concurrent.cpp
.
To try: make hw2
and run ./np_hw2
.
*Try with debug message: make hw2_debug
and run ./np_hw2
.
*Debug version is complied with -g
, which may use gdb np_hw2(s)
to debug.