Chapter 4 Answers
Chapter 4 Answers
If we are timing some part of multi thread program we'd like for all the thread to start the timed
Code from some instance and then report the time taken by last thread to finish (slowest)
Double elapsed_time
/*private */
Double my_start,my_finish,my_elapsed ;
Synchronization threads ;
My_elapsed=my_start-my_finish
APP2
using barriers in debugging
Difficult to determine where an error is occurring in a parallel programe
Each thread print aMSG indicating which point it's reached in the program
Paint on programe we want to reach ;
Barriers;
If(my_rank==0 ){
PrintF("All threads reached this paint /n")
FFlush(stdout);
}
4. Use insert function to add 2, 8, 7, 5 to a linked list. Then, use member function to find
5 and 9. Finally, delete 5 from the list.
Insert Function
Int insert(int value, struct list node*head_p){
Struct list_node_s* curr-p=*head_p;
Struct list_node_s* pred-p=null;
Struct list_node_s* temp-p;
While(curr-p != null || curr-p->data <value){
Pred-p=curr-p;
Curr-p=curr-p ->next;
}
If(curr-p == null || curr-p ->data >value){
Temp-p -> data =value
Temp-p->next = curr-p;
If (pred-p ==null) *head-p=temp-p
else pred-p->next=temp-p
return 1;}
else return 0;
}}
Code
Int member(Int value , Struct list_node_s* head-p){
Struct list_node_s* curr-p =head-p;
While(curr-p != null && curr-p-> data <value)
curr-p = curr-p->next;
if(curr-p == null || curr-p -> data >value) return 0;
else return 1;
}
Delete 5 from the list
5. Executing concurrent multiple read and write operations on a linked list can cause
problems. What are these problems and how they can be solved?
Insert / Delete write to memory location so there is problem if we try to execute these operation
At some time with another operation
Problem1
If
Thread 0 -> execute member(5)
//at the same time
Thread 1 -> delete (5)
Problem -> when thread 0 is executing member(5) it is going to report in the list when it may be
deleted before thread return
Problem2
If thread 0 is exciting member(8) thread 1 may free the memory use for the node storing 5 before
thread 0 advance to member 8
Problem -> if the memory is reallocated before thread 0 advance