Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Pertemuan 5 Server dengan Multi Proses pada Linux I Made Astawa

Presentasi serupa


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

1 Pertemuan 5 Server dengan Multi Proses pada Linux I Made Astawa

2 2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan program server yang bisa melayani banyak client dengan teknik multi proses

3 3 Outline Materi Pengertian Proses Proses hirarki System call yang berhubungan dengan proses Contoh program server dengan multi proses

4 4 Pengertian Proses Proses – Adalah program yang sedang dieksekusi – Setiap proses memilki: Executable program, data, stack Program counter, stack pointer, registers Address space (core image) Dll (lihat tabel dibawah)

5 5 Proses vs Thread

6 6 Pengertian Proses System Call : manajemen Proses – fork(), waitpid(), execve(), exit(), getpid(), getppid(), dll Hirarki proses

7 7 Pengertian Proses Process Identifier: – Process ID (PID) – Parent Process ID (PPID)

8 8 Contoh-1 int main(void) { pid_t child; int i; if((child=fork())==-1){ perror("fork"); exit(EXIT_FAILURE); } else if(child==0){ for(i=0; i<10; i++){ printf("\tChild PID =%d\n",getpid()); printf("\tChild PPID=%d\n",getppid()); sleep(1); } exit(EXIT_SUCCESS); } else{ for(i=0; i<10; i++){ printf("\tParent PID=%d\n",getpid()); printf("\tParent PPID=%d\n",getppid()); sleep(1); } exit(EXIT_SUCCESS); } #include

9 9 Contoh-2 #include void childproses() { int i; for(i=0; i<10; i++){ printf("\tChild PID =%d\n",getpid()); printf("\tChild PPID=%d\n",getppid()); sleep(1); } exit(EXIT_SUCCESS); }

10 10 Contoh-2 void parentproses() { int i; for(i=0; i<10; i++){ printf("\tParent PID=%d\n",getpid()); printf("\tParent PPID=%d\n",getppid()); sleep(1); }

11 11 Contoh-2 int main(void) { pid_t child; child=fork(); switch(child){ case -1: perror("fork"); exit(EXIT_FAILURE); case 0: childproses(); default: parentproses(); } exit(EXIT_SUCCESS); }

12 12 Contoh-3 void childproses() { puts("Child process..."); printf("\tChild PID =%d\n",getpid()); printf("\tChild PPID=%d\n",getppid()); exit(EXIT_SUCCESS); } void parentproses(pid_t child, int status) { waitpid(child,&status,0); puts("Parent Process..."); printf("\tParent PID=%d\n",getpid()); printf("\tParent PPID=%d\n",getppid()); printf("\tChild exited with %d\n",status); } #include

13 13 Contoh-3 int main(void) { pid_t child; int status; child=fork(); switch(child){ case -1: perror("fork"); exit(EXIT_FAILURE); case 0: childproses(); default: parentproses(child, status); } exit(EXIT_SUCCESS); }

14 14 Contoh-4 #include void childproses(); void parentproses(pid_t child, int status); int GlobalVar=10;

15 15 Contoh-4 int main(void) { pid_t child; int status; child=fork(); switch(child){ case -1: perror("fork"); exit(EXIT_FAILURE); case 0: childproses(); default: parentproses(child, status); } exit(EXIT_SUCCESS); }

16 16 Contoh-4 void childproses() { GlobalVar += 10; printf("Child: Globalvar=%d\n",GlobalVar); exit(EXIT_SUCCESS); } void parentproses(pid_t child, int status) { GlobalVar += 20; printf("Parent: Globalvar=%d\n",GlobalVar); waitpid(child,&status,0); }

17 17 DayTimeServer tanpa Fork() ………… //lihat code sebelumnya ………… for(;;){ sin_size = sizeof(struct sockaddr_in); connsd = accept(listensd, (struct sockaddr *) &cliaddr, &sin_size); if(connsd == -1){ perror("error accept"); exit(1); } ticks = time(NULL); snprintf(buff, sizeof(buff),"Date/Time: %.24s\r\n", ctime(&ticks)); write(connsd, buff, strlen(buff)); close(connsd); //sleep(10); }

18 18 DayTimeServer dgn Fork() for(;;){ sin_size = sizeof(struct sockaddr_in); connsd = accept(listensd, (struct sockaddr *) &cliaddr, &sin_size); if(connsd == -1){ perror("error accept"); exit(1); } if(!fork()){ close(listensd); ticks = time(NULL); snprintf(buff, sizeof(buff), "Date/Time: %.24s\n", ctime(&ticks)); write(connsd, buff, strlen(buff)); close(connsd); //sleep(10); exit(EXIT_SUCCESS); } close(connsd); }

19 19 Latihan Buat program dengan menggunakan system call fork(), agar terbentuk hirarki proses sbb:

20 20 Latihan Buat Program Client – Server dengan Flow Control sbb:

21 21 Latihan Buat Program Client – Server dengan Flow Control sbb:

22 22 Latihan Buat Program Client – Server dengan Flow Control sbb:


Download ppt "Pertemuan 5 Server dengan Multi Proses pada Linux I Made Astawa"

Presentasi serupa


Iklan oleh Google