BAB 4
SINKRONISASI
4.1. Tujuan Praktikum
Bab ini bertujuan untuk mengenalkan konsep sinkronisasi dalam
praktek sistem operasi Ubuntu.
4.3.3. Join
Sebuah join digunakan jika diinginkan suatu keadaan sebuah thread
baru akan berjalan setelah suatu thread lain selesai dijalankan. Sebuah
thread dapat juga menjalankan suatu rutin yang menciptakan banyak thread
baru di mana thread induk akan dijalankan lagi saat banyak thread baru
tersebut sudah selesai dijalankan.
4.4.1. Mutex
main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 pthread_mutex_t mutex1 =
6 PTHREAD_MUTEX_INITIALIZER;
7 int counter = 0;
8
9 void *increase() {
10 pthread_mutex_lock( &mutex1 );
11 int i;
12
13 for(i = 0; i < 99999; i++) {
14 counter++;
15 printf("Counter
value: %d\n",counter);
16 }
17
18 pthread_mutex_unlock( &mutex1 );
19 }
20
21 void *decrease() {
22 pthread_mutex_lock( &mutex1 );
23 int i;
24
25 for(i = 0; i < 99999; i++) {
26 counter--;
27 printf("Counter
value: %d\n",counter);
28 }
29
30 pthread_mutex_unlock( &mutex1 );
31 }
32
33 int main() {
34 int rc1, rc2;
35 pthread_t thread1, thread2;
36
37 /* Create independent threads each of
which will execute increase */
38 if( (rc1=pthread_create( &thread1, NULL,
&increase, NULL)) ) {
39 printf("Thread creation
failed: %d\n", rc1);
40 }
41
42 /* Create independent threads each of
which will execute decrease */
43 if( (rc2=pthread_create( &thread2, NULL,
&decrease, NULL)) ) {
44 printf("Thread creation
failed: %d\n", rc2);
45 }
46
47 /* Wait till threads are complete before
main continues. Unless we */
48 /* wait we run the risk of executing an
exit which will terminate */
49 /* the process and all threads before the
threads have completed. */
50 pthread_join( thread1, NULL);
51 pthread_join( thread2, NULL);
52
53 exit(EXIT_SUCCESS);
54 }
2. Hapus kode program pada baris 9, 15, 19, dan 25 yang berisi
perintah pthread_mutex_lock dan pthread_mutex_unlock.
Kemudian jalankan program tersebut sebanyak 10 kali. Apakah
semua output yang dihasilkan sama?
Jawab:
Tidak, output yang dihasilkan tidak sama seperti pada Gambar
7 dan 8 diketahui bahwa output1.txt dan output2.txt
memiliki perbedaan pada line 17025. Hal ini disebabkan oleh
perintah pthread_mutex_lock dan pthread_mutex_unlock
dihapus.
4.4.2. Join
main.c
1 #include <stdio.h>
2 #include <pthread.h>
3
4 pthread_mutex_t mutex1 =
5 PTHREAD_MUTEX_INITIALIZER;
6 int counter = 0;
7
8 void *thread_function(void *dummyPtr) {
9 pthread_mutex_lock( &mutex1 );
10 counter++;
11 pthread_mutex_unlock( &mutex1 );
12 }
13
14 int main() {
15 pthread_t thread_id[100];
16 int i, j;
17
18 for(i = 1; i <= 100; i++) {
19 printf("Create thread %d\n", i);
20 pthread_create( &thread_id[i], NULL,
thread_function, NULL );
21 }
22
23 for(j=1; j <= 100; j++) {
24 pthread_join( thread_id[j], NULL);
25 }
26
27 /* Now that all threads are complete I can
print the final result. */
28 /* Without the join I could be printing a
value before all the threads */
29 /* have been completed. */
30 printf("Final counter value: %d\n",
counter);
31 }
main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 int count = 0;
6 pthread_mutex_t count_mutex =
PTHREAD_MUTEX_INITIALIZER;
7 pthread_cond_t condition_var =
PTHREAD_COND_INITIALIZER;
8
9 // Write numbers 1-3 and 8-10 as permitted by
functionCount2()
10 void *functionCount1() {
11 for(;;) {
12 // Lock mutex and then wait for
signal to relase mutex
13 pthread_mutex_lock( &count_mutex );
14
15 // Wait while functionCount2()
operates on count
16 // mutex unlocked if condition
varialbe in functionCount2() signaled.
17 pthread_cond_wait( &condition_var,
&count_mutex );
18 count++;
19 printf("Counter value
functionCount1: %d\n",count);
20
pthread_mutex_unlock( &count_mutex );
21
22 if(count >= 10)
23 return(NULL);
24 }
25 }
26
27 // Write numbers 4-7
28 void *functionCount2() {
29 for(;;) {
30 pthread_mutex_lock( &count_mutex );
31
32 if( count < 3 || count > 6 ) {
33 // Condition of if statement
has been met.
34 // Signal to free waiting
thread by freeing the mutex.
35 // Note: functionCount1() is
now permitted to modify "count".
36
pthread_cond_signal( &condition_var );
37 } else {
38 count++;
39 printf("Counter value
functionCount2: %d\n",count);
40 }
41
pthread_mutex_unlock( &count_mutex );
42
43 if(count >= 10)
44 return(NULL);
45 }
46 }
47
48 int main() {
49 pthread_t thread1, thread2;
50 pthread_create( &thread1, NULL,
&functionCount1, NULL);
51 pthread_create( &thread2, NULL,
&functionCount2, NULL);
52 pthread_join( thread1, NULL);
53 pthread_join( thread2, NULL);
54 printf("Final count: %d\n",count);
55 exit(EXIT_SUCCESS);
56 }
4.5.1. Tugas 1
soal.c
1 #include<stdio.h>
2 #include<string.h>
3 #include<pthread.h>
4 #include<stdlib.h>
5 #include<unistd.h>
6
7 int counter = 0;
8 pthread_t tid[4];
9
10 void* doSomeThing(void *arg) {
11 printf("\n Thread %d started, please
wait...\n", counter);
12 counter++;
13 unsigned long i = 0;
14
15 for(i = 0; i < (0xFFFFFFFF); i++);
16
17 printf("\n Thread %d finished\n", counter);
18
19 return NULL;
20 }
21
22 int main(void) {
23 int i;
24
25 for(i = 0; i < 4; i++)
26 pthread_create(&(tid[i]), NULL,
&doSomeThing, NULL);
27
28 for(i = 0; i < 4; i++)
29 pthread_join(tid[i], NULL);
30 return 0;
31 }
3. Apa fungsi for pada baris 14? Berapa kali looping yang dilakukan
oleh perintah for tersebut?
Jawab:
Fungsi for pada baris 14 ini berperan sebagai salah satu
penyebab keempat thread memiliki nilai counter yang sama.
Dan for ini juga berfungsi untuk menjaga thread yang
terbentuk dalam kondisi running sambil memastikan thread
yang lain terbentuk terlebih dahulu sebelum looping selesai
dijalankan. Looping yang telah dilakukan oleh perintah for
tersebut sebenarnya sebanyak 4294967295 karena
0XFFFFFFFF adalah representasi heksadesimal dari nilai
4294967295, namun karena hanya terdapat 4 kali pencetakan
kalimat thread 4 finished dan sisanya adalah NULL.
4.5.2. Tugas 2
4.5.3. Tugas 3
soal.c
... ...
24 for(i = 0; i < 4; i++) {
25 pthread_create(&(tid[i]), NULL,
&doSomeThing, NULL);
26 pthread_join(tid[i], NULL);
27 }
... ...