Operating System Multiple Access Chatting Program using Multithread Han Jun Bae Korea University http://forbes.tistory.com/64
Project purpose Main purpose of this Project Understand Thread Develop to linux through C programming Enable to test on one computer (IP – 127.0.0.1) client1 server client2 client3
Function of socket program Function of program Server program Relay to chatting message between client and client. Enable to network connect many clients at the same time using multithread. Every message should be delivered to every connected client. Cilent program Should be able to connect to the server when you input the server IP and PORT. This program is able to input the message and output the message received from server. You can programming it if you know how to use the network related socket function even if you don’t have the enough knowledge about network.
Configuration of server and client Purchase phone Assigned phone number Listening Pick up the phone Conversation End of conversation Purchase phone Make a call Conversation End of conversation Connection request Transmitting and receiving data Connection determination
Multithread through chatting program Client1 Server Client2 Read Read Read Write Write Write Received messages from the client is sent to all clients. Send message.
pthread #include <pthread.h> Compile in Linux To use the pthread function, you should be declare the header file. http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread.h.html Compile in Linux gcc –c [compile target].c gcc –pthread –o [output file name] [compile target].c -pthread command connects library to use thread. ./server.o ./client.o
pthread int pthread_create( pthread_t *th_id, const pthread_attr_t *attr, void* 함수명, void *arg ); pthread 생성 첫 번째 인자 : pthread 식별자로 thread가 성공적으로 생성되면 thread 식별값이 주어진다. 두 번째 인자 : pthread 속성(옵션), 기본적인 thread 속성을 사용할 경우 NULL 세 번째 인자 : pthread로 분기할 함수. 반환값이 void* 타입이고 매개변수도 void* 으로 선언된 함수만 가능하다. ex) void* handler (void* arg) { ... } 네 번째 인자 : 분기할 함수로 넘겨줄 인자값. 어떤 자료형을 넘겨줄 지 모르기 때문에 void형으로 넘겨주고 상황에 맞게 분기하는 함수내에서 원래의 자료형으로 캐스팅해서 사용하면 된다. 리턴 값 : 성공적으로 pthread가 생성될 경우 0 반환
pthread int pthread_join( pthread_t th_id, void** thread_return ); 특정 pthread가 종료될 때까지 기다리다가 특정 pthread가 종료시 자원 해제시켜준다. 첫 번째 인자 : 어떤 pthread를 기다릴 지 정하는 식별자 두 번째 인자 : pthread의 return 값, 포인트로 값을 받아오는 점을 주의할 것 int pthread_detach( pthread_t th_id ); th_id 식별자를 가지는 pthread가 부모 pthread로부터 독립한다. 즉 이렇게 독립된 pthread는 따로 pthread_join()이 없어도 종료시 자동으로 리소스 해제된다.
example
Project Due date : November 29(wednesday) Multiple access chatting program using multithread Both client and server should be able to send and receive messages to each other. Program should be contain concept of multithread. A code file [filename.c] : 60% ex) server.c, client.c Include detailed comments inside your code
Project A word / hwp document that describes your program : 40% File name is your student number. ex) 2015000001.doc You can use any word program. You should be explain your program in detail. All the files are compressed into one file (ex: 2011000001.zip) Submit your zip file to qwerty2901@korea.ac.kr If you have any question, please mail me or use bulletin board in laboratory homepage(http://it.korea.ac.kr) or visit to laboratory(engineering bldg #236)