Process and Thread
Process and Thread
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?