Create two programs (separate .c files), as described below, to allow one-way, asynchronous communication using a socket, P-threads, and a semaphore. The server will create two child threads, each of which will modify a global message buffer using strings sent from two client instances through the socket. Include an example of the programs’ output in your report.
- Server description: One program is a server that creates the global message buffer, socket, semaphore, and child threads. It also sets the original message. It should have exactly one function that is executed on each child thread, described below.
- Server child threads: The function executed in each child thread should call “accept” for the socket and then repeatedly read new strings from the socket until “quit” is received. For each new string that is read (except “quit”), the function should use “sprintf” to write that string to the global message buffer, protecting this critical section operation with the semaphore.
- Server parent thread: The parent thread of the server should allow the user to repeatedly choose two options: quit or print the current value of the message buffer.
- Client description: The second .c program is a client that connects to the socket. It allows the user to repeatedly choose two options: quit or enter/send a new message to the server. You should run two instances of the client, each in a separate terminal shell.
- Execution order: The server and client should correctly respond to every possible execution order. A typical order would be: server starts, clients start, clients quit, server quits. Below are requirements for other possibilities:
- Client starts before server: There will be no available socket available, and it should terminatewith an error message.
- Server quits before clients: If the server user chooses to quit before the clients quit, the servershould not terminate until after the clients close their socket connections. Simply use pthread_join() for each thread before closing the primary socket file descriptor and destroying the semaphore.Note that getting string input immediately after numerical input can be a problem. See the comment about this in problem #2 above. Below is an example of how your output might look. You can run the server and clients in separate terminal shells. User input is shown in bold. Normally, the text would appear without any gaps, but each of the three users could be entering text at any time. However, the following output is shown with gaps between text to demonstrate time delays between user actions.


0 comments