Anda di halaman 1dari 7

BAB II

THREAD
2.1 DASAR TEORI
Thread adalah unit dasar dari CPU utilization. Thread terdiri dari thread ID,
program counter, register set, dan stack. Beberapa Thread yang berada dalam
proses yang sama saling berbagi code section, data section, dan operating-system
resources (files dan signals).

Gambar 3.1 Perbedaan single-threaded process dengan multithreaded process


(Sumber: Operating System Concepts, 9th Edition)

2.1.1

Pthreads

Gambar 3.2 Pthread


(Sumber: https://computing.llnl.gov/tutorials/pthreads/#Joining)

Pthreads merujuk pada POSIX standard (IEEE 1003.1c) yang mendefinisikan


API untuk pembuatan dan sinkronisasi thread. Pthreads merupakan spesifikasi
perilaku
thread,
bukan
implementasi.
Pembuat
sistem
operasi
boleh
mengimplementasikan spesifikasi Pthreads ini dengan berbagai cara dengan bebas.
Contoh sistem operasi mengimplementasikan spesifikasi Pthreads adalah UNIX-type
Modul Praktikum Sistem Operasi part 2 |
1

systems (Linux, Mac OS X, dan Solaris). Program teori-3.1.c berikut ini adalah
contoh penggunaan Pthread pada sistem operasi Linux (Sumber: Operating System
Concepts, 9th Edition).
teori-3.1.c
1 #include<pthread.h>
2 #include<stdio.h>
3
4 int sum; /* this data is shared by the thread(s) */
5 void *runner(void *param); /* threads call this function */
6
7 int main(int argc, char *argv[])
8 {
9
pthread_t tid; /* the thread identifier */
10
pthread_attr_t attr; /* set of thread attributes */
11
12
if (argc != 2) {
13
fprintf(stderr,"usage: latihan-3.1 <integer value>\n");
14
return -1;
15
}
16
if (atoi(argv[1]) < 0) {
17
fprintf(stderr,"%d must be >= 0\n",atoi(argv[1]));
18
return -1;
19
}
20
21
/* get the default attributes */
22
pthread_attr_init(&attr);
23
/* create the thread */
24
pthread_create(&tid,&attr,runner,argv[1]);
25
/* wait for the thread to exit */
26
pthread_join(tid,NULL);
27
28
printf("sum = %d\n",sum);
29
30 }
31
32 /* The thread will begin control in this function */
33 void *runner(void *param)
34 {
int i, upper = atoi(param);
35
sum = 0;
36
37
38
for (i = 1; i <= upper; i++)
39
sum += i;
40
41
pthread_exit(0);
}

Berikut adalah proses compile dan output dari program teori-3.1.c diatas

2.1.2

Java Threads

Thread adalah model fundamental dari eksekusi program dalam program


Java. Bahasa pemrograman Java menyediakan banyak fasilitas untuk pembuatan
Modul Praktikum Sistem Operasi part 2 |
2

dan manajemen thread. Semua program Java minimal terdiri dari satu thread. Java
thread tersedia di seluruh sistem operasi, seperti Windows, Linux dan Mac OS X,
yang menyediakan Java Virtual Machine (JVM). Java Thread API juga tersedia untuk
aplikasi Android. Program DisplaySum.java berikut ini adalah contoh penggunaan Java
Thread pada sistem operasi Windows.
DisplaySum.java
1 public class DisplaySum implements Runnable {
2
private int param;
3
public DisplaySum(int param) {
4
this.param = param;
5
}
6
public void run() {
7
int sum = 0;
8
for (int i = 1; i <= this.param; i++) {
9
sum += i;
10
}
11
System.out.println("sum = " + sum);
12
}
13
public static void main(String[] args) {
14
try {
15
Runnable getSumRun = new DisplaySum(Integer.valueOf(args[0]));
16
Thread getSumThread = new Thread(getSumRun);
17
getSumThread.start();
18
getSumThread.join();
19
} catch (InterruptedException ex) {
20
System.err.println(ex.getMessage());
21
}
22
}
23 }

Berikut adalah proses compile dan output dari program DisplaySum.java diatas

2.2 MATERI PRAKTIKUM


2.2.1

Thread ID

Lakukan percobaan sesuai dengan panduan berikut ini:


1. Login ke sistem GNU/Linux kemudian buka terminal.
2. Ketikkan program materi-2.1.c menggunakan editor favorit anda.
materi-2.1.c
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 time_t current_time;
7
8 void *hello(void *param) {
9
pthread_t self_id;

Modul Praktikum Sistem Operasi part 2 |


3

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

self_id = pthread_self();
int i = 0;
while(i < 3){
sleep(1);
current_time = time(NULL);
printf("Thread %lu say hello at %s", self_id, ctime(&current_time));
i++;
}
printf("Thread %lu finished\n",self_id);
return &("joined");
}
main() {
pthread_t thread_id;
int ret;
void *res;
ret=pthread_create(&thread_id,NULL,&hello,NULL);
printf("Created thread %lu \n", thread_id);
pthread_join(thread_id,&res);
printf("Thread %lu %s \n",thread_id,(char *)res);
}

3. Compile program dengan perintah: gcc -pthread -o materi-2.1 materi-2.1.c


4. Jalankan dengan perintah: ./materi-2.1 sebanyak 3 kali
5. Simpan tampilan output dan amati hasil ketiga output dari program materi2.1.c

6. Apakah ketiga
perbedaannya!
2.2.2

output

tersebut

sama?

Bila

berbeda

jelaskan

letak

Multithread

Lakukan percobaan sesuai dengan panduan berikut ini:


1. Login ke sistem GNU/Linux kemudian buka terminal.
2. Ketikkan program materi-3.2.c menggunakan editor favorit anda.
materi-2.2.c
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 time_t current_time;
7
8 void *hello(void *param) {
9
pthread_t self_id;
10
self_id = pthread_self();
11
int delay = atoi(param);
12
int i = 0;
13
while(i < 3){
14
sleep(delay);
15
current_time = time(NULL);
16
printf("Thread %lu say hello at %s", self_id, ctime(&current_time));
17
i++;
18
}
19
printf("Thread %lu finished\n",self_id);
20
return &("joined");
21 }

Modul Praktikum Sistem Operasi part 2 |


4

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

main() {
pthread_t thread_id_1, thread_id_2, thread_id_3;
int ret_1, ret_2, ret_3;
void *res_1, *res_2, *res_3;
ret_1=pthread_create(&thread_id_1,NULL,&hello,"1");
printf("Created thread %lu \n", thread_id_1);
ret_2=pthread_create(&thread_id_2,NULL,&hello,"2");
printf("Created thread %lu \n", thread_id_2);
ret_3=pthread_create(&thread_id_3,NULL,&hello,"3");
printf("Created thread %lu \n", thread_id_3);
pthread_join(thread_id_1,&res_1);
printf("thread %lu %s \n",thread_id_1,(char *)res_1);
pthread_join(thread_id_2,&res_2);
printf("thread %lu %s \n",thread_id_2,(char *)res_2);
pthread_join(thread_id_3,&res_3);
printf("thread %lu %s \n",thread_id_3,(char *)res_3);
}

3.
4.
5.
6.

Compile program dengan perintah: gcc -pthread -o materi-2.2 materi-2.2.c


Jalankan dengan perintah: ./materi-2.2
Simpan tampilan output dan amati hasil output dari program materi-2.2.c
Apakah ketiga thread pada program tersebut selesai bersamaan? Bila tidak,
thread manakah yang selesai terlebih dahulu? Jelaskan penyebabnya!
7. Ubah baris ke 28, 30, dan 32 menjadi seperti potongan program berikut, lalu
simpan dengan nama materi-2.3.c
materi-2.3.c
.. ...
.
ret_1=pthread_create(&thread_id_1,NULL,&hello,"3");
28 ...
..
ret_2=pthread_create(&thread_id_2,NULL,&hello,"2");
. ...
30
ret_3=pthread_create(&thread_id_3,NULL,&hello,"1");
.. ...
.
32
..
.

8. Compile program dengan perintah: gcc -pthread -o materi-2.3 materi-2.3.c


9. Jalankan dengan perintah: ./materi-2.3
10.Simpan tampilan output dan amati hasil output dari program materi-2.3.c
11.Apakah ketiga thread pada program tersebut selesai bersamaan? Bila tidak,
thread manakah yang selesai terlebih dahulu? Jelaskan penyebabnya!
2.2.3

Shared Variable

Lakukan percobaan sesuai dengan panduan berikut ini:


1. Login ke sistem GNU/Linux kemudian buka terminal.
2. Ketikkan program materi-2.4.c menggunakan editor favorit anda.
materi-2.4.c

Modul Praktikum Sistem Operasi part 2 |


5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include
#include
#include
#include

<stdio.h>
<pthread.h>
<stdlib.h>
<time.h>

time_t current_time;
int sharevar;
void *counter(void *param) {
int i = 0;
while(i < 10){
sleep(1);
sharevar += 1;
i++;
}
current_time = time(NULL);
printf("\nCounter thread finished at %s", ctime(&current_time));
return NULL;
}
void *printer(void *param) {
int i = 0;
while(i < 10){
sleep(2);
current_time = time(NULL);
printf("\n%s", ctime(&current_time));
printf("sharevar = %i\n", sharevar);
i++;
}
current_time = time(NULL);
printf("\nPrinter thread finished at %s", ctime(&current_time));
return NULL;
}
main() {
pthread_t tid_counter, tid_printer;
int ret_counter, ret_printer;
void *res_counter, *res_printer;
ret_counter=pthread_create(&tid_counter,NULL,&counter,NULL);
current_time = time(NULL);
printf("\nCounter thread created at %s", ctime(&current_time));
ret_counter=pthread_create(&tid_printer,NULL,&printer,NULL);
current_time = time(NULL);
printf("\nPrinter thread created at %s", ctime(&current_time));
pthread_join(tid_counter,&res_counter);
pthread_join(tid_printer,&res_printer);
}

3.
4.
5.
6.

Compile program dengan perintah: gcc -pthread -o materi-2.4 materi-2.4.c


Jalankan dengan perintah: ./materi-2.4
Simpan tampilan output dan amati hasil output dari program materi-2.4.c
Apakah kedua thread pada program tersebut selesai bersamaan? Bila tidak,
thread manakah yang selesai terlebih dahulu? Jelaskan dampaknya!
7. Ubah baris ke 12 dan 24 menjadi seperti potongan program berikut, lalu
simpan dengan nama materi-2.5.c
materi-2.5.c

Modul Praktikum Sistem Operasi part 2 |


6

..
.
12
..
.
24
..
.

...
sleep(2);
...
sleep(1);
...

8. Compile program dengan perintah: gcc -pthread -o materi-2.5 materi-2.5.c


9. Jalankan dengan perintah: ./materi-2.5
10.Simpan tampilan output dan amati hasil output dari program materi-2.5.c
11.Apakah kedua thread pada program tersebut selesai bersamaan? Bila tidak,
thread manakah yang selesai terlebih dahulu? Jelaskan dampaknya!

2.3 TUGAS PRAKTIKUM


Kerjakan soal-soal berikut ini!
1. Jelaskan perbedaan dari pemrograman sekuensial dan pemrograman
multithread!
2. Jelaskan keungggulan dan kelemahan dari:
a. pemrograman sekuensial
b. pemrograman multithread
3. Apakah penggunaan thread dapat mempercepat waktu komputasi program
dalam menyelesaikan sebuah kasus / permasalahan? Bila iya, kasus /
permasalahan seperti apakah yang pengerjaannya dapat lebih optimal bila
menggunakan thread? Buktikan dengan membuat program sederhana dalam
bahasa C atau JAVA!

Modul Praktikum Sistem Operasi part 2 |


7

Anda mungkin juga menyukai