Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehSadam Panda Telah diubah "9 tahun yang lalu
1
Pertemuan 5 Server dengan Multi Proses pada Linux
I Made Astawa
2
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan program server yang bisa melayani banyak client dengan teknik multi proses
3
System call yang berhubungan dengan proses
Outline Materi Pengertian Proses Proses hirarki System call yang berhubungan dengan proses Contoh program server dengan multi proses
4
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)
5
Proses vs Thread
6
Pengertian Proses System Call : manajemen Proses Hirarki proses
fork(), waitpid(), execve(), exit(), getpid(), getppid(), dll Hirarki proses
7
Process Identifier: Pengertian Proses Process ID (PID)
Parent Process ID (PPID)
8
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());
9
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);
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
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
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);
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
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;
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
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
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
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);
19
Latihan Buat program dengan menggunakan system call fork(), agar terbentuk hirarki proses sbb:
20
Latihan Buat Program Client – Server dengan Flow Control sbb:
21
Latihan Buat Program Client – Server dengan Flow Control sbb:
22
Latihan Buat Program Client – Server dengan Flow Control sbb:
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.