Anda di halaman 1dari 112

Process and Thread

Affix Mareta, S.Pd., M.Eng.


Batch Operating System
Multiprogramming OS
Multitasking OS
Multiprocessing OS
Distributed OS
Process
Process Control Block
Process State
Process Scheduling
Context Switch
Difference Between Process and
Thread
Program seperti koran yang
diletakkan di atas meja. Tidak ada
gunanya.

Koran baru berguna saat dia dibaca,


tapi sebelum dia dibaca, dia tidak ada
gunanya.

Sebelum dieksekusi, program juga


tidak berguna.
Ketika saya mulai membaca berita
Olahraga di koran, maka saya
memberikan “nyawa” pada koran,
sehingga dia berguna.

Ketika program dieksekusi, maka


program pun mulai “bernyawa” dan
ada 1 thread yang mengontrol.
Ketika saya membaca berita
Olahraga, istri saya mengambil koran
yang saya baca dan membaca berita
Selebriti dan Film.

Ketika itulah terjadi 2 thread atau 2


“nyawa” dalam 1 program.
Masalah terjadi ketika saya dan istri
sama-sama ingin membaca bagian
Politik, maka saat itulah terjadi
konflik antara saya dan istri.

Seperti koran, program terdiri dari


beberapa thread. Dalam 1 program
juga bisa terjadi konflik antar thread,
karena mengakses data yang sama
atau bagian yang sama.
Masalah terjadi ketika saya dan istri
sama-sama ingin membaca bagian
Politik, maka saat itulah terjadi
konflik antara saya dan istri.

Seperti koran, program terdiri dari


beberapa thread. Dalam 1 program
juga bisa terjadi konflik antar thread,
karena mengakses data yang sama
atau bagian yang sama.
Di mana proses?

Proses yaitu gabungan dari program dan semua thread yang dieksekusi di dalam program.

Jadi, proses yaitu program yang dieksekusi, dan thread yaitu basic unit eksekusi dari sebuah proses.
Thread
Fork & Exec
INTRO

System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of
fork() is to create a new process, which becomes the child process of the caller. After a new child process is
created, both processes will execute the next instruction following the fork() system call.
Parent or child?
Therefore, we have to distinguish the parent from the child. This can be done by testing the returned
value of fork():

● If fork() returns a negative value, the creation of a child process was unsuccessful.
● fork() returns a zero to the newly created child process.
● fork() returns a positive value, the process ID of the child process, to the parent. The returned
process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover,
a process can use function getpid() to retrieve the process ID assigned to this process.
Parent or child?
Exec

The exec system call is used to execute a file which is residing in an active process. When exec is called the
previous executable file is replaced and new file is executed.

More precisely, we can say that using exec system call will replace the old file or program from the
process with a new file or program. The entire content of the process is replaced with a new program.
Thread Problems
Praktik
#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>
1. Tuliskan coding di samping.
2. Jika ada 4 fork, berapa kata “hello” yang
akan muncul?
int main(void)
3. Jika ada 8 fork, berapa kata “hello” yang
{ akan muncul?
4. Jelaskan mengapa kata “Bye”” bisa muncul
printf("Hello \n"); berkali-kali.

fork();

printf("bye\n");

return 0;

}
5. Ceritakan alur logika
pemrogaman di samping?
6. Jelaskan perbedaan fork_return,
jika ia lebih dari 0, sama dengan 0
atau lkurang dari 0!
6. Apa itu PID? Apakah di Task
Manager Windows kalian bisa
menemukan PID?

Anda mungkin juga menyukai