Pertemuan 5 Server dengan Multi Proses pada Linux I Made Astawa deastawa@yahoo.com
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan program server yang bisa melayani banyak client dengan teknik multi proses
System call yang berhubungan dengan proses Outline Materi Pengertian Proses Proses hirarki System call yang berhubungan dengan proses Contoh program server dengan multi proses
Proses Adalah program yang sedang dieksekusi Setiap proses memilki: 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)
Proses vs Thread
Pengertian Proses System Call : manajemen Proses Hirarki proses fork(), waitpid(), execve(), exit(), getpid(), getppid(), dll Hirarki proses
Process Identifier: Pengertian Proses Process ID (PID) Parent Process ID (PPID)
Contoh-1 #include <unistd.h> #include <stdio.h> #include <stdlib.h> 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{ printf("\tParent PID=%d\n",getpid()); printf("\tParent PPID=%d\n",getppid());
Contoh-2 #include <unistd.h> #include <stdio.h> #include <stdlib.h> 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);
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); }
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);
Contoh-3 #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> 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);
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);
Contoh-4 #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> void childproses(); void parentproses(pid_t child, int status); int GlobalVar=10;
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);
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);
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);
DayTimeServer dgn Fork() 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);
Latihan Buat program dengan menggunakan system call fork(), agar terbentuk hirarki proses sbb:
Latihan Buat Program Client – Server dengan Flow Control sbb:
Latihan Buat Program Client – Server dengan Flow Control sbb:
Latihan Buat Program Client – Server dengan Flow Control sbb: