Multithreading and Deadlock in C, Assignments of Computer Networks

Two programming exercises related to multithreading and deadlock in the c programming language. The first exercise involves creating a program that sends the string "hello thread" to a thread, which then saves the string to a file named "hello.txt". The second exercise illustrates a deadlock scenario using three threads that acquire and release different resources. The code provided demonstrates the implementation of pthread functions, mutex locks, and resource management to create these multithreading scenarios. This document could be useful for students studying operating systems, concurrent programming, or system programming concepts, particularly those interested in understanding the practical applications of multithreading and deadlock in c. Important topics such as thread creation, resource locking, and synchronization, which are fundamental to understanding concurrent programming and system design.

Typology: Assignments

2021/2022

Uploaded on 11/14/2023

fitriansyah-eka-putra
fitriansyah-eka-putra 🇮🇩

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 8
Fitriansyah Eka Putra
22/504395/PA/21696
1. Buatlah sebuah program yang mengirimkan sebuah string ”Hello Thread” ke sebuah
thread yang akan menyimpan string tersebut ke sebuah file bernama ”hello.txt”.
Jawab:
Program dibuat dengan membuat fungsi bernama print yang dengan parameter berupa
string yang kemudian string tersebut disimpan ke file hello.txt
Fungsi tersebut dipanggil dengan menggunakan thread dan mengirim string ”Hello
Thread” ke fungsi print
1#include <pthread .h>
2#include <s t d i o . h>
3#include <s t d l i b . h>
4
5void p r i n t ( void a rg ) {
6cha r string = (c h a r ) a rg ;
7FILE f i l e = fopen ( h e l l o . t x t ,”w” ) ;
8f p r i n t f ( f i l e , %s \n” , string ) ;
9f c l o s e ( f i l e ) ;
10
11 r e t u r n NULL;
12 }
13
14 i n t ma in ( ) {
15 pthread t t;
16
17 p t h r e a d c r e a t e (& t , NULL, p r i n t , H e l l o T hr ea d ) ;
18
19 r e t u r n 0 ;
1
pf3
pf4

Partial preview of the text

Download Multithreading and Deadlock in C and more Assignments Computer Networks in PDF only on Docsity!

Homework 8

Fitriansyah Eka Putra

22/504395/PA/

1. Buatlah sebuah program yang mengirimkan sebuah string ”Hello Thread” ke sebuah

thread yang akan menyimpan string tersebut ke sebuah file bernama ”hello.txt”.

Jawab:

Program dibuat dengan membuat fungsi bernama print yang dengan parameter berupa

string yang kemudian string tersebut disimpan ke file hello.txt

Fungsi tersebut dipanggil dengan menggunakan thread dan mengirim string ”Hello

Thread” ke fungsi print

1 #i n c l u d e <pthread. h> 2 #i n c l u d e 3 #i n c l u d e < s t d l i b. h> 4 5 v o i d ∗ p r i n t ( v o i d ∗ arg ) { 6 char ∗ s t r i n g = ( char ∗ ) a rg ; 7 FILE ∗ f i l e = f o p e n ( ” h e l l o. t x t ” , ”w” ) ; 8 f p r i n t f ( f i l e , ”%s \n” , s t r i n g ) ; 9 f c l o s e ( f i l e ) ; 10 11 r e t u r n NULL; 12 } 13 14 i n t main ( ) { 15 p t h r e a d t t ; 16 17 p t h r e a d c r e a t e (&t , NULL, p r i n t , ” H e l l o Thread ” ) ; 18 19 r e t u r n 0 ;

21

2. Buatlah sebuah program yang mengilustrasikan sebuah deadlock dengan menggunakan

tiga threads.

Jawab:

Program dibuat dengan kode yang diambil dari modul dengan memberikan thread,

fungsi dan resource tambahan di mana masing masing fungsi mengunci resource yang

berbeda agar menyebabkan deadlock.

1 2 #i n c l u d e 3 #i n c l u d e <pthread. h> 4 #i n c l u d e 5 6 v o i d ∗ f u n c t i o n 1 ( ) ; 7 v o i d ∗ f u n c t i o n 2 ( ) ; 8 v o i d ∗ f u n c t i o n 3 ( ) ; 9 10 p t h r e a d m u t e x t r e s a ; 11 p t h r e a d m u t e x t r e s b ; 12 p t h r e a d m u t e x t r e s c ; 13 14 i n t main ( ) { 15 p t h r e a d m u t e x i n i t (& r e s a , NULL) ; 16 p t h r e a d m u t e x i n i t (& r e s b , NULL) ; 17 p t h r e a d m u t e x i n i t (& r e s c , NULL) ; 18 p t h r e a d t one , two , t h r e e ; 19 20 p t h r e a d c r e a t e (&one , NULL, f u n c t i o n 1 , NULL) ; 21 p t h r e a d c r e a t e (&two , NULL, f u n c t i o n 2 , NULL) ; 22 p t h r e a d c r e a t e (& t h r e e , NULL, f u n c t i o n 3 , NULL) ; 23 p t h r e a d j o i n ( one , NULL) ; 24 p t h r e a d j o i n ( two , NULL) ; 25 p t h r e a d j o i n ( t h r e e , NULL) ; 26 27 p r i n t f ( ” Threads j o i n e d \n” ) ; 28 } 29 30 v o i d ∗ f u n c t i o n 1 ( ) { 31 p t h r e a d m u t e x l o c k (& r e s a ) ;

86 87 p t h r e a d m u t e x u n l o c k (& r e s b ) ; 88 p r i n t f ( ” Thread two r e l e a s e d r e s b \n” ) ; 89 90 p t h r e a d m u t e x u n l o c k (& r e s a ) ; 91 p r i n t f ( ” Thread two r e l e a s e d r e s a \n” ) ; 92 93 p t h r e a d m u t e x u n l o c k (& r e s c ) ; 94 p r i n t f ( ” Thread two r e l e a s e d r e s c \n” ) ; 95 96 r e t u r n NULL; 97 } 98