Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Pertemuan 6 Server dengan Multi Thread pada Linux I Made Astawa

Presentasi serupa


Presentasi berjudul: "Pertemuan 6 Server dengan Multi Thread pada Linux I Made Astawa"— Transcript presentasi:

1 Pertemuan 6 Server dengan Multi Thread pada Linux I Made Astawa

2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : mendemonstrasikan program aplikasi client- server dengan multithread

3 Outline Materi Pengertian program Pengertian proses Pengertian threads Beda multiproses vs multithread Kasus: Server dengan multithread

4 The Process Model a.Multiprogramming of four programs b.Conceptual model of 4 independent, sequential processes c.Only one program active at any instant

5 Process States Possible process states – running – blocked – ready Transitions between states shown

6 Threads The Thread Model (a) Three processes each with one thread (b) One process with three threads

7 The Thread Model Items shared by all threads in a process Items private to each thread

8 The Thread Model Each thread has its own stack

9 Thread Usage (1) A word processor with three threads

10 Pop-Up Threads Creation of a new thread when message arrives (a) before message arrives (b) after message arrives

11 CONTOH PROGRAM #include 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);

12 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"); return 0; } result=listen(listensock, 5); if(result < 0) { perror("serverthread1"); return 0; } CONTOH PROGRAM

13 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 */ }

14 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()); }

15 Latihan 1.Perhatikan contoh program diatas, bolehkan variabel newsocket diatas dibuat menjadi variabel global?, sehingga pada fungsi “thread_proc” variabel newsocket bisa digunakan ? 2.Setelah accept mengapa tidak perlu close(listensocket) ?, seperti pada server dengan fork() ? 3.Variabel int sock pada fungsi “thread_proc”, digunakan untuk menyimpan socket dari client yang connect dengan server. Jelaskan mengapa bisa demikian ?


Download ppt "Pertemuan 6 Server dengan Multi Thread pada Linux I Made Astawa"

Presentasi serupa


Iklan oleh Google