Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehBudy Hayley Telah diubah "9 tahun yang lalu
1
Pertemuan 6 Server dengan Multi Thread pada Linux
I Made Astawa
2
mendemonstrasikan program aplikasi client- server dengan multithread
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : mendemonstrasikan program aplikasi client- server dengan multithread
3
Beda multiproses vs multithread Kasus: Server dengan multithread
Outline Materi Pengertian program Pengertian proses Pengertian threads Beda multiproses vs multithread Kasus: Server dengan multithread
4
The Process Model Multiprogramming of four programs
Conceptual model of 4 independent, sequential processes 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
Each thread has its own stack
The Thread Model Each thread has its own stack
9
A word processor with three threads
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 <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);
12
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");
13
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 */
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 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 ?
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.