Pertemuan 6 Server dengan Multi Thread Matakuliah : H0483 / Network Programming Tahun : 2008 Versi : 2.0 Pertemuan 6 Server dengan Multi Thread
mendemonstrasikan program aplikasi client- server dengan multithread Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : mendemonstrasikan program aplikasi client- server dengan multithread
Beda multiproses vs multithread Kasus: Server dengan multithread Outline Materi Pengertian program Pengertian proses Pengertian threads Beda multiproses vs multithread Kasus: Server dengan multithread
Multiprogramming of four programs The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential processes Only one program active at any instant
Possible process states running blocked ready Transitions between states shown
Threads The Thread Model (a) Three processes each with one thread (b) One process with three threads
The Thread Model Items shared by all threads in a process Items private to each thread
Each thread has its own stack The Thread Model Each thread has its own stack
A word processor with three threads Thread Usage (1) A word processor with three threads
Pop-Up Threads Creation of a new thread when message arrives (a) before message arrives (b) after message arrives
CONTOH PROGRAM #include <stdio.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <pthread.h> void* thread_proc(void *arg); int main(int argc, char *argv[]) { struct sockaddr_in sAddr; int listensock; int newsock; int result; pthread_t thread_id; int val; listensock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
CONTOH PROGRAM val = 1; result = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); if(result < 0){ perror("serverthread1"); return 0; } sAddr.sin_family = AF_INET; sAddr.sin_port = htons(1972); sAddr.sin_addr.s_addr = INADDR_ANY; result = bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr)); if(result < 0) { perror("serverthread1"); result=listen(listensock, 5); if(result < 0) { perror("serverthread1");
CONTOH PROGRAM while(1){ newsock = accept(listensock, NULL ,NULL); result = pthread_create(&thread_id, NULL, thread_proc, (void *) newsock); if(result != 0){ printf("Could not create thread.\n"); return 0; } pthread_detach(thread_id); // melepaskan/detach a thread sched_yield(); /*function shall force the running thread to relinquish the processor until it again becomes the head of its thread lis */
CONTOH PROGRAM void* thread_proc(void *arg) { int sock; char buffer[25]; int nread; printf("child thread %u with pid %i created.\n", pthread_self(), getpid()); sock = (int) arg; nread = recv(sock, buffer, 25, 0); buffer[nread] = '\0'; printf("%s\n", buffer); send(sock, buffer, nread, 0); close(sock); printf("child thread %u with pid %i finished.\n", pthread_self(), getpid()); }
Latihan Perhatikan contoh program diatas, bolehkan variabel newsocket diatas dibuat menjadi variabel global?, sehingga pada fungsi “thread_proc” variabel newsocket bisa digunakan ? Setelah accept mengapa tidak perlu close(listensocket) ?, seperti pada server dengan fork() ? Variabel int sock pada fungsi “thread_proc”, digunakan untuk menyimpan socket dari client yang connect dengan server. Jelaskan mengapa bisa demikian ?