Anda di halaman 1dari 4

Tanggal 5 April 2017

Semester 4
Matakuliah CE321324 System Programming
Minggu ke/Session 9/2
Topik Thread
Aktifitas Write a code
Waktu pengerjaan 1 Sessions
Setoran Source Code
Batas akhir penyerahan The end of this session
Tempat penyerahan TA
Tujuan Knowing how the implementation of POSIX the thread using
the C programming language in a linux environment

Amati dan analisis kode program yang diberikan pada praktikum ini. Anda harus
memahami apa yang dilakukan oleh program ketika program tersebut dieksekusi.

A. Checking apakah platform linux mendukung thread

1. Program berikut akan mencek apakah platform linux Anda mendukung thread. Tuliskan
program dibawah ini dan berikan nama thread1.c,
/* Filename: thread1.c */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
printf("POSIX version is set to %ld\n", _POSIX_VERSION);
if (_POSIX_VERSION < 199506L) {
if (_POSIX_C_SOURCE >= 199506L) {
printf("Sorry, you system does not support POSIX1003.1c threads\n");
}
else {
printf("Try again with -D_POSIX_C_SOURCE=199506L\n");
}
}
else {
printf("Your system supports POSIX1003.1c threads,\n");
#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
printf("including support for priority scheduling\n");
#else
printf("but does not support priority scheduling\n");
#endif
}
exit(EXIT_SUCCESS);
}

2. Compile dan run program tersebut. Amati output program tersebut. Output program tersebut akan
terlihat seperti berikut ini:

root@someserver:/home/spc/07_thread# ./thread1
POSIX version is set to 200809
Your system supports POSIX1003.1c threads,
including support for priority scheduling

Program tersebut akan mengecek nilai _POSIX_VERSION.

B. Meng-create sebuah thread didalam proses

Protoype fungsi untuk mengcreate thread ini adalah :

#include <pthread.h>
int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void
*), void

10-Thread 4/4/2017 10:25 PM Page 1 of 4


Argumen 1 adalah pointer ke pthread_t. Ketika sebuah thread dicreat, sebuah identifier ditulis pada
variabel yang diacu oleh pointer ini. Identifier ini akan digunakan untuk mengacu ke thread ini.
Argumen 2 adalah sekumpulan atribut thread, dapat diset sebagai NULL.
Argumen 3 adalah fungsi yang akan dieksekusi oleh pada thread ini.
Argumen 4 adalah argumen yang akan dikirimkan pada fungsi ini.

3. Create thread didalam sebuah proses. Tuliskan kode program dibawah ini dan berinama thread2.c.

/* Filename : thread2.c */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void *thread_function(void *arg);

char message[] = "Hello World";

int main() {
int res;
pthread_t a_thread;
void *thread_result;

res = pthread_create (&a_thread, NULL, thread_function, (void*)message);

if(res != 0) {
perror("Thread creation failed\n");
exit(EXIT_FAILURE);
}

printf("Waiting for thread to finish....\n");

res = pthread_join(a_thread, &thread_result);

if(res != 0){
perror("Thread join failed");
exit(EXIT_FAILURE);
}

printf("Thread joined, it returned %s \n", (char*)thread_result);


printf("Message is now %s \n", message);
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {


printf("thread_function is running. Argument was %s\n", (char*)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time\n");
}

4. Compile program tersebut. Tambahkan opsi −lpthread seperti berikut ini :

root@someserver:/home/spc/07_thread# gcc thread2.c -o thread2 –lpthread

5. Run program dan kemudian amati oputputnya.

Sistem call pthread_create digunakan untuk mencreate sebuah thread baru. Jika pemanggilan system
call ini berhasil, maka akan ada dua thread yang running. Thread original (main) akan melanjutkan
untuk mengeksekusi kode program setelah pemanggilan pthread_create. Sedangkan thread yang baru
akan megneksekusi kode yang ada pada fungsi yang sudah didefinisikan thread_function.

Kemudian thread original akan mencek apakah thread yang baru sudah mulai dan
memanggilpthread_join. Fungsi ini akan menunggu hingga thread yang lain diterminasi sebelum
threat ini return.

root@someserver:/home/spc/07_thread# ./thread2
Waiting for thread to finish....

10-Thread 4/4/2017 10:25 PM Page 2 of 4


thread_function is running. Argument was Hello World
Thread joined, it returned Thank you for the CPU time

Message is now Bye!


root@someserver:/home/spc/07_thread#

6. Program untuk mencek apakah ekseskusi dua buat thread terjadi secara simultan. Program ini
merupakan modifikasi program thread2.c dengan menambahkan sebuah variabel sharing antar kedua
thread tersebut.

/* Filename: thread3.c */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void *thread_function(void *arg);

char message[] = "Hello World";

int run_now = 1;

int main() {
int res;
pthread_t a_thread;
void *thread_result;

res = pthread_create(&a_thread, NULL, thread_function, (void*)message);

if(res != 0) {
perror("Thread creation failed\n");
exit(EXIT_FAILURE);
}

int print_count1 = 0;

while(print_count1++ < 20) {


if (run_now == 1) {
printf("1");
run_now = 2;
}
else {
sleep(1);
}
}
printf("\nWaiting for thread to finish....\n");

res = pthread_join(a_thread, &thread_result);

if(res != 0){
perror("Thread join failed");
exit(EXIT_FAILURE);
}

printf("Thread joined, it returned %s \n", (char*)thread_result);


printf("Message is now %s \n", message);
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {

int print_count2 = 0;

while(print_count2++ < 20) {


if (run_now == 2) {
printf("2");
run_now = 1;
}
else {
sleep(1);
}
}
}

10-Thread 4/4/2017 10:25 PM Page 3 of 4


7. Compile dan run program tersebut. Amati output program tersebut. Output program tersebut akan
terlihat seperti berikut ini:

root@someserver:/home/spc/07_thread# ./thread3
121212121212121212
Waiting for thread to finish....
Thread joined, it returned (null)
Message is now Hello World

Setiap thread memberitahukan thread yang lain untuk menjalankan dengan menetapkan variable run_now
dan kemudian menunggu sampai thread yang lain telah mengubah nilai itu sebelum berjalan kembali.
Contoh program ini memperlihatkan perpindahan eksekusi antar dua thread secara otomatis dan
menggambarkan bahwa kedua thread berbagi bersama antar dua variabel.

C. Tugas

1. Buatlah sebuah program yang menjalankan 2 buah thread dimana :


a. Masing-masing thread menuliskan angka dari 1 sampai x
b. x merupakan nilai dari parameter (integer) yang di passing pada saat meng-create thread

2. Apa yang terjadi jika thread yang dibuat tidak di join oleh main program??
a. Coba analisis dengan menghilangkan baris code yang men-join thread ke main program
b. Jadi apa gunanya thread join?

3. Bagaimana cara mempassing beberapa variable ke sebuah thread?

4. Buatlah sebuah thread yang menerima argument berupa array of integer [1,2,...10] dan menampilkan
isi dari array tersebut. (array --> void --> array)

Submit hasil pekerjaan anda ke ecourse.del.ac.id :


1. Code program 1
2. Report untuk Latihan 2 dan 3
3. Code untuk program 4

5. References

a. Alan Cox, Beginning Linux Programming, 2nd Edition, Wrox Prss

10-Thread 4/4/2017 10:25 PM Page 4 of 4

Anda mungkin juga menyukai