Please help me with the attached assignment.
Assign-2 What to Hand In: Include your source code file(s) (including .h) and a makefile (with a default target to make all, and a clean target). There is a bit of work to do here and you should begin right away. Trust me - you will not finish this if you leave it for the last week. Do yourself a huge favour and begin NOW. This is a great assignment. Why is it great you ask? It is great for two reasons: 1. you are going to learn many things: o how to use UNIX UDP IPC o increased detail regarding the joy (and pain) of programming concurrent processes o how to program using the client/server model o how to write a multi-threaded program using pthreads (which is representative of most other threads packages) o how to solve the critical section problem between threads 2. it is going to be fun o you are left to judge the truth of this statement Assignment Overview For this assignment you are going to create a simple "chat"-like facility that enables someone at one terminal (or Xterm) to communicate with someone at another terminal. The interface will not be pretty, but it will be functional. This facility will be called "s- talk" for simple-talk. To initiate an s-talk session two users will first agree on two things: o the machine that each will be running on o the port number (explained later) each will use Say that Fred and Barney want to talk. Fred is on machine "csil-cpu1" and will use port number 6060. Barney is on machine "csil-cpu3" and will use port number 6001. To initiate s-talk, Fred must type: s-talk 6060 csil-cpu3 6001 And Barney must type: s-talk 6001 csil-cpu1 6060. So, (in case you haven't figured it out) the general format is: s-talk [my port number] [remote machine name] [remote port number] The result will be that every line typed at each terminal will appear on BOTH terminals: it will appear character-by-character on the sender’s terminal as they type it, and it will appear on the receiver’s terminal once the sender presses enter. If you want to learn about "curses" and "cbreak" on your own, you can alter this slightly so that every character typed appears on both screens as it is typed rather than having to wait for each [return]. If you are interested look in the man pages under "curses" (this is NOT a requirement of the assignment). An s-talk session is terminated when either user enters the complete line of text which is just one ‘!’ and presses enter. Expected Process Structure For this Assignment This assignment will be done using pthreads, a kernel-level thread implementation for LINUX. As you may or may not know, pthreads allows you to create any number of threads inside one UNIX process. All threads running in the same UNIX process share memory and also have access to semaphores (mutexes) and the ability to use conditional signal/wait to synchronize their actions in relation to each each other. UNIX itself also allows you to create multiple processes. Communication between UNIX processes can be done using something called "datagram sockets" which use a protocol called UDP (universal datagram protocol). In this assignment, you will be dealing with processes/threads on two levels. First, you will have two UNIX processes. Each one is started by one of the people who want to talk (as a result of executing s-talk). Second, within each s-talk process a pthreads environment will be running four threads that you will write. Required threads (in each of the processes):
• One of the threads does nothing other than await input from the keyboard. • Another thread does nothing other than await a UDP datagram.
• There will also be a thread which prints characters to the screen.
• Finally a thread which sends data to the remote UNIX process over the network using UDP.
All four threads will share access to a list ADT (the one you wrote for assignment #1):
• The keyboard input thread, on receipt of input, adds the input to the list of messages that need to be sent to the remote s-talk client.
• The UDP output thread will take each message off this list and send it over the network to the remote client.
• The UDP input thread, on receipt of input from the remote s-talk client, will put the message onto the list of messages that need to be printed to the local screen.
• The screen output thread will take each message off this list and output it to the screen.
Clearly the lists that are shared between the threads will need to be carefully controlled to prevent concurrent access. This will be done by using mutexes and signalling/waiting on condition variables.
It would be easy to modify the process structure to have the keyboard input thread send its characters directly out over the network, and to have the UDP input thread print its characters directly to the screen. Please don't do it this way - I'd like you to get more experience writing client/server applications.
How do you go about starting?
There are several things you are going to have to figure out - and you’d best start NOW! I think you will appreciate all the knowledge you gain from doing this assignment, but there is a lot to learn - this is not one you can leave until the last few days. There will be no extensions.
First, try out some of the keyboard/screen I/O primitives supplied by UNIX without using pthreads. Check the section 2 man pages under "read", "write" and (optionally) "curses", "cbreak".
Next (and this is a big one) you'll need to experiment with UNIX UDP sockets. Do this by experimenting with code that sends a message (without pthreads) from one UNIX process to another.
Check the man pages for: • socket
• bind
• sendto
• recvfrom
• getaddrinfo Some of these will be complicated, but don't despair. You can ask the TAs and me questions in the discussion forum, though we won't answer any questions that can be easily obtained from the man pages. I also strongly suggest you use the web as a resource here. Try a search on "UNIX and SOCKET" and see what you come up with. If you find something good, please share it with others using the discussion forum. There are a couple of good web-pages that I will point you to: • Socket programming: http://beej.us/guide/bgnet/ • Pthreads documentation: https://computing.llnl.gov/tutorials/pthreads/ Finally, bring them all together in a pthreads application and two UNIX processes. Marking This will be handed in electronically. I'd like you to hand in the following items: o all your source code in .c and .h files o any separate documentation files you would like to hand in (e.g. a README file documenting any errors, like things you didn’t get working) o a makefile that compiles your .c files and links the objects to any necessary libraries. The makefile should produce the executable s-talk which behaves according to the description above, and should have a clean rule. o Do NOT hand in any executables or .o files (unless you choose to use the instructor-provided list.o implementation). Have fun and good luck. tut-3_20-3.pages CMPT 300: Sockets This tutorial focuses on an overview of s-talk and sockets. The four threads (keyboard/screen/send/recv) communicate with each other via shared lists. One S-Talk “send” thread communicates with another’s “recv” thread via UDP between two sockets. The URL in the assignment has a great discussion of UDP. It is strongly recommended that you read it. It explains why we use AF_INET and not the PF_INET you see on the man page for socket. A socket is a subclass of a file. A connection between two sockets is like a bi-directional pipe between two processes on (possibly) different machines. To create a socket use the following: s = socket(AF_INET, SOCK_DGRAM, 0); We need a way of “binding” the socket to an internet port that can be named outside the program so that a link can be made with the other s-talk program over the AF_INET protocol. Bind the socket to an IP:port number using: bind(s, (struct sockaddr *)psa_in, sizeof(struct sockaddr_in)); where s is the socket id returned by “socket”, psa_in is a pointer to a sockaddr_in struct (see /usr/include/sys/socket.h and /usr/include/netinet/in.h). Since we’re using an internet “address family” socket we use a sockaddr_in instead of a sockaddr. It needs to be initialized as follows: keyboard screen recv send kbd scrnrecv send S-Talk Shared List(s) Lists CMPT 300: Sockets struct sockaddr_in addr; addr.sin_family = AF_INET; sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; memset(&addr.sin_zero, ‘\0’, 8); “Htons” takes care of the difference between the integer storage format on the host and “network byte order”. “Port” can be anything above 1024 up to 65535. (0 to 1024 are reserved.) INADDR_ANY simply means “use the IP of the local machine”. Our bind call is now: bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); To send data or receive data: sendto(s, &msg, len, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr_in)); recvfrom(s, &msg, len, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr_in)); where len is the size of msg, and remote is another sockaddr_in value with the IP address and port of the receiving/sending socket. See the man pages for gethostbyname and inet_aton. Close the socket when you’re done: close(s);