Tugas Bab 4.1
Tugas Bab 4.1
4.4.1 Mutex
main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
6 int counter = 0;
7
8 void *increase() {
9 pthread_mutex_lock( &mutex1 );
10 int i;
11 for(i=0; i<99999; i++) {
12 counter++;
13 printf("Counter value: %d\n",counter);
14 }
15 pthread_mutex_unlock( &mutex1 );
16 }
17
18 void *decrease() {
19 pthread_mutex_lock( &mutex1 );
20 int i;
21 for(i=0; i<99999; i++) {
22 counter--;
23 printf("Counter value: %d\n",counter);
24 }
25 pthread_mutex_unlock( &mutex1 );
26 }
27
28 main() {
29 int rc1, rc2;
30 pthread_t thread1, thread2;
31
32 /* Create independent threads each of which will execute increase */
33 if( (rc1=pthread_create( &thread1, NULL, &increase, NULL)) ) {
34 printf("Thread creation failed: %d\n", rc1);
35 }
36
37 /* Create independent threads each of which will execute decrease */
38 if( (rc2=pthread_create( &thread2, NULL, &decrease, NULL)) ) {
39 printf("Thread creation failed: %d\n", rc2);
40 }
41
42 /* Wait till threads are complete before main continues. Unless we */
43 /* wait we run the risk of executing an exit which will terminate */
44 /* the process and all threads before the threads have completed. */
45 pthread_join( thread1, NULL);
46 pthread_join( thread2, NULL);
47
48 exit(EXIT_SUCCESS);
49 }
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
21 pthread_mutex_unlock( &count_mutex );
22
23 if(count >= 10)
24 return(NULL);
25 }
26 }
27
28 // Write numbers 4-7
29 void *functionCount2(){
30 for(;;) {
31 pthread_mutex_lock( &count_mutex );
32
33 if( count < 3 || count > 6 ) {
34 // Condition of if statement has been met.
35 // Signal to free waiting thread by freeing the mutex.
36 // Note: functionCount1() is now permitted to modify "count".
37 pthread_cond_signal( &condition_var );
38 } else {
39 count++;
40 printf("Counter value functionCount2: %d\n",count);
41 }
42
43 pthread_mutex_unlock( &count_mutex );
44
45 if(count >= 10)
46 return(NULL);
47 }
48 }
49
50 main(){
51 pthread_t thread1, thread2;
52 pthread_create( &thread1, NULL, &functionCount1, NULL);
53 pthread_create( &thread2, NULL, &functionCount2, NULL);
54 pthread_join( thread1, NULL);
55 pthread_join( thread2, NULL);
56 printf("Final count: %d\n",count);
57 exit(EXIT_SUCCESS);
58 }
Berdasarkan output dari program tersebut, jawablah pertanyaan berikut:
Tugas 1
Jalankan kode program berikut!
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
14 unsigned long i = 0;
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 for(i = 0; i < 4; i++)
25 pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
26
27 for(i = 0; i < 4; i++)
28 pthread_join(tid[i], NULL);
29
30 return 0;
31 }
Bila anda berhasil menjalankan kode program tersebut, maka anda akan
mendapati output seperti pada gambar di bawah ini.
Jawablah pertanyaan berikut ini:
3. Apa fungsi for pada baris 14? Berapa kali looping yang
dilakukan oleh perintah for tersebut?
Tugas 2
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 }
... ...
Jawablah pertanyaan berikut ini: