17. Buat tulisan ilmiah yang berisi penjelasan tentang kode program
singlethread yang telah dimodifikasi dengan aturan sebagai berikut:
JAWAB:
PENDAHULUAN
Thread adalah sebuah alur kontrol dari sebuah proses. Suatu proses yang
multithreaded mengandung beberapa perbedaan alur kontrol dengan ruang
alamat yang sama. Keuntungan dari multithreaded meliputi peningkatan respon
dari user, pembagian sumber daya proses, ekonomis, dan kemampuan untuk
mengambil keuntungan dari arsitektur multiprosesor.
User level thread adalah thread yang tampak oleh programmer dan tidak
diketahui oleh kernel. User level thread secara tipikal dikelola oleh sebuah library
thread di ruang user. Kernel level thread didukung dan dikelola oleh kernel
sistem operasi. Secara umum, user level thread lebih cepat dalam pembuatan
dan pengelolaan dari pada kernel thread. Ada tiga perbedaan tipe dari model
yang berhubungan dengan user dan kernel thread.
Model many to one: memetakan beberapa user level thread hanya ke satu
buah kernel thread.
Model one to one: memetakan setiap user thread ke dalam satu kernel
thread. Berakhir.
Model many to many: mengizinkan pengembang untuk membuat user
thread sebanyak mungkin, konkurensi tidak dapat tercapai karena hanya
satu thread yang dapat dijadualkan oleh kernel dalam satu waktu.
Thread merupakan unit dasar dari penggunaan CPU, yang terdiri
dari Thread_ID, program counter, register set, dan stack. Sebuah thread berbagi
code section, data section, dan sumber daya sistem operasi dengan Thread lain
yang dimiliki oleh proses yang sama. Thread juga sering disebut lightweight
process. Sebuah proses tradisional atau heavyweight process mempunyai thread
tunggal yang berfungsi sebagai pengendali.
Perbedaan antara proses dengan thread tunggal dengan proses dengan thread
yang banyak adalah proses dengan thread yang banyak dapat mengerjakan lebih
dari satu tugas pada satu satuan waktu.
Keuntungan
1 Tanggap: Multithreading mengizinkan program untuk berjalan terus walau
pun pada bagian program tersebut di block atau sedang dalam keadaan
menjalankan operasi yang lama/ panjang. Sebagai contoh, multithread web
browser dapat mengizinkan pengguna berinteraksi dengan suatu thread ketika
suatu gambar sedang diload oleh thread yang lain.
Khusus untuk C++ harus menggunakan header file dan dapat juga menggunakan
file header lainnya.
threadtiga.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 static int num_accts = 1024; // number of bank
accounts
5 static int num_trans = 10000; // number of
transactions
6 static int think_time = 50; // amount of "thinking
time"
7 struct acct_t {
8 int bal;
9 };
10 /*
11 * Pointer to accounts
12 */
13 struct acct_t *accts;
14 /*
15 * Method to perform a number of transactions.
16 * Parameter "dummy" is not used.
17 */
18 void *transact(void *dummy)
19 {
20 int i;
21 for (i = 0; i < num_trans; i++) {
22 // pick two random accounts
23 int acct_id_from = rand() % num_accts;
24 int acct_id_to = rand() % num_accts;
25 // pick a random amount
26 int amt = rand() % 100;
27 // try to transfer the money
28 if (accts[acct_id_from].bal > amt) {
29 accts[acct_id_from].bal -= amt;
30 accts[acct_id_to].bal += amt;
31 // "thinking time"... don't modify this code!!
32 amt *= think_time; while (amt--) ;
33 }
34 }
35 }
36 int main(int argc, char **argv)
37 {
38 // make sure the number of arguments is odd
(including the program name)
39 if (!(argc == 1 || argc == 3 || argc == 5 || argc ==
7)) {
40 fprintf(stderr, "usage: %s [-a <accts>] [-i
<transactions>] [-t <think-time>]\n", argv[0]);
41 exit(-1);
42 }
43 // look at each runtime argument and see which value
it's attempting to set
44 int i;
45 for (i = 1; i < argc; i++) {
46 if (!strcmp(argv[i], "-a")) {
47 num_accts = atoi(argv[i+1]);
48 i++;
49 } else if (!strcmp(argv[i], "-i")) {
50 num_trans = atoi(argv[i+1]);
51 i++;
52 } else if (!strcmp(argv[i], "-t")) {
53 think_time = atoi(argv[i+1]);
54 i++;
55 } else {
56 fprintf(stderr, "usage: %s [-a <accts>] [-i
<transactions>] [-t <think-time>]\n", argv[0]);
57 exit(-1);
58 }
59 }
60 // display the parameters that will be used for this
test run
61 fprintf(stderr, "%s: -a %d -i %d -t %d\n", argv[0],
num_accts, num_trans, think_time);
62 // initialize the random number generator
63 srand(1);
64 // create the bank accounts
65 accts = (struct acct_t *)malloc(num_accts *
sizeof(struct acct_t));
66 // initialize the bank accounts' values and keep
track of the total sum in all accounts
67 int original_sum = 0;
68 for (i = 0; i < num_accts; i++) {
69 accts[i].bal = rand() % 1000;
70 original_sum += accts[i].bal;
71 }
72 // call the transact function to do the transfers
73 transact(NULL);
74 // find the total sum of all accounts after the
transfers are done
75 int sum = 0;
76 for (i = 0; i < num_accts; i++) {
77 sum += accts[i].bal;
78 }
79 // if the sum is not equal to the original sum, then
we had a race condition!!
80 if (sum != original_sum) {
81 fprintf(stderr, "ERROR! original_sum = %d, sum
= %d\n", original_sum, sum);
82 }
83 else {
84 fprintf(stderr, "Values are still consistent\n");
85 }
86 return 0;
87 }
KESIMPULAN
REFERENSI
1. https://zegyjib.wordpress.com/matkul/oop2/oop2-multi-thread-c-java-
dan-c/
2. https://tintakopi.wordpress.com/2011/10/24/thread-multithread/