Last active
December 15, 2020 11:01
-
-
Save u-r-w/ab3418f54d3f7d4310c03ccd4d121408 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
// Compile : $ clang add.c -lpthread -w -o add | |
// atau $ gcc add.c -lpthread -w -o add | |
// https://stackoverflow.com/questions/24947446/pthread-create-how-do-i-get-the-returned-value-from-the-passed-function | |
#define TOTAL_CPU 8 | |
#define LOOP_MAX 800000000 // LOOP_MAX nilainya sebesar memory fisik yang tersedia, jika terlalu besar akan segfault | |
#define LOOP_THREAD LOOP_MAX/TOTAL_CPU | |
void* process( int64_t* data ) { // function process yang return valuenya bertipe void * | |
int64_t total = 0; // total adalah lokal variabel untuk fungsi ini | |
float a = 934934.4, b = 849854.5, c; | |
for ( int64_t i =0;i< LOOP_THREAD; i++ ) { | |
total = total + *(data+i); | |
for (int y=0; y<100; y++ ) // looping untuk menambah beban processor, supaya tidak cepat selesai | |
c = a / b; | |
} | |
printf( "process() total= %lu\n", total ); | |
return total; | |
} | |
int tambah_thread() { // fungsi main() adalah fungsi utama, akan me-return int | |
pthread_t th[ TOTAL_CPU ]; | |
void* temp[ TOTAL_CPU ]; // void adalah tipe data , = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; | |
int64_t total = 0; // hanya dikenal di fungsi main() | |
int64_t n = LOOP_MAX; | |
int64_t *data = malloc( n * sizeof(int64_t)); // pesan memory sepanjang LOOP_MAX | |
for( int64_t i=0; i<n; i++) { // isi array secara bururutan dari 0 sampai n | |
*(data+i) = i; | |
} | |
for ( int j=0; j< TOTAL_CPU; j++ ) { | |
// data + (10*j) menunjuk alamat data ke 10, 20, 30 | |
pthread_create( &th[j], NULL, process, data+ (LOOP_THREAD*j) ) ; | |
} | |
int64_t t[ TOTAL_CPU ]; // temporer | |
for ( int j=0; j< TOTAL_CPU; j++ ) { | |
pthread_join( th[j], &temp[j]); // ditaruh di sini bukan ditaruh di loop sebelumnya (deket pthread_create() ) supaya menjamin ptread_create telah selesai membuat thread seluruhnya | |
t[j] = (temp[j]); | |
printf( "Hasil %d %lu\n\n", j,t[j] ); | |
total = total + t[j]; | |
} | |
total = total + n; | |
free( data ); | |
printf( "%lu\n", total ); | |
return 0; | |
} | |
int tambah_sequential() { | |
int64_t n = LOOP_MAX; | |
int64_t total = 0; // hanya dikenal di fungsi main() | |
int64_t *data = malloc( n * sizeof(int64_t)); | |
for( int64_t i=0; i<n; i++) { | |
*(data+i) = i; | |
} | |
float a = 934934.4, b = 849854.5, c; | |
for( int64_t i=0; i<n; i++) { | |
total = total + *(data+i) ; | |
for (int y=0; y<100; y++ ) // looping untuk menambah beban processor, supaya tidak cepat selesai | |
c = a / b; | |
} | |
total = total + n; | |
printf( "Total %lu\n", total ); | |
} | |
int main(int argc, char *argv[]) { | |
if ( argc == 1 ) | |
tambah_sequential(); | |
else | |
tambah_thread(); | |
return 0; | |
} | |
/* | |
verifikasi menggunakan wolfram alpha : | |
https://www.wolframalpha.com/input/?i=1%2B2%2B3%2B...%2B800000000 | |
= 2.000.000.001.000.000.000 = 2 quintillion | |
2.000.000.001.000.000.000 | |
Pada window lain jalankan $ htop kemudian tampilkan dalam tree (F5), untuk menunjukkan hirarki dari proses yang sedang jalan, | |
di bawah terlihat bahwa add sebagai main (pid=26992) akan punya 8 child process yang dihasilkan oleh fungsi pthread_create(). | |
Dapat dibuktikan bahwa penggunaan thread akan mempercepat proses sebanyak jumlah core cpu yang ada | |
https://superuser.com/questions/809989/is-there-a-way-to-copy-text-in-htop | |
1 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] 5 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] | |
2 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] 6 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] | |
3 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] 7 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] | |
4 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] 8 [||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] | |
Mem[|||||||||||||||||||||||||||||||||||||||||||||6.79G/15.5G] Tasks: 103, 201 thr; 8 running | |
Swp[| 256K/2.00G] Load average: 2.75 1.88 0.98 | |
Uptime: 04:26:47 | |
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command | |
1 root 20 0 220M 9336 6656 S 0.0 0.1 0:02.40 /sbin/init splash | |
2620 ok 20 0 574M 48464 29064 S 0.0 0.3 0:57.89 ├─ /usr/bin/xfce4-terminal | |
21773 ok 20 0 25300 6216 3752 S 0.0 0.0 0:00.14 │ ├─ bash | |
26992 ok 20 0 6174M 6104M 1272 S 800. 38.4 2:07.12 │ │ └─ ./add 1 | |
27000 ok 20 0 6174M 6104M 1272 R 102. 38.4 0:15.49 │ │ ├─ ./add 1 | |
26999 ok 20 0 6174M 6104M 1272 R 100. 38.4 0:15.16 │ │ ├─ ./add 1 | |
26998 ok 20 0 6174M 6104M 1272 R 100. 38.4 0:15.31 │ │ ├─ ./add 1 | |
26997 ok 20 0 6174M 6104M 1272 R 101. 38.4 0:15.48 │ │ ├─ ./add 1 | |
26996 ok 20 0 6174M 6104M 1272 R 101. 38.4 0:15.48 │ │ ├─ ./add 1 | |
26995 ok 20 0 6174M 6104M 1272 R 100. 38.4 0:15.43 │ │ ├─ ./add 1 | |
26994 ok 20 0 6174M 6104M 1272 R 98.8 38.4 0:15.26 │ │ ├─ ./add 1 | |
26993 ok 20 0 6174M 6104M 1272 R 96.8 38.4 0:15.15 │ │ └─ ./add 1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment