Commit 3b992659 authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

lab 3.2 complete

parent 23f4eaa6
......@@ -14,3 +14,31 @@ collect2: error: ld returned 1 exit status
make: *** [summation] Error 1
2. The flag is: -lm and -lpthread
Problem 2:
1)Explain the mechanism by which the main thread passes arguments to the other threads it creates.
THe main thread uses a custom C struct, thread_args, to package all four arguements into one unit "thread_args".
Then, it makes an array of these thread_args, so for each thread, main passes
one thread_args with its corresponding threadid.
2) Lines 89, 90, and 91 make calls to function calloc(3). Explain what this function does and why it is preferable to malloc(3) in this type of circumstance. (Hint: Consider the data type that is being dynamically allocated.)
The reason calloc is used is because it can allocate memory spaces for an array of elements, unlike malloc which can only create the memory space for one element.
3)Explain what is means for a thread to be in detached state (line 97 in source code). Discuss whether or not this program should create detached threads.
When a detached thread terminates, its resources are auto‐
matically released back to the system without the need for another
thread to join with the terminated thread. And since that, we will not
able to know its return value. However, here we have already stored the
return value in pre-defined struct so we can use detached thread.
4)Explain the mechanisms by which the main thread receives the results from each computation thread and by which it synchronizes with them.
It uses pthread_join.The pthread_join() function waits for the thread specified by thread to
terminate. If that thread has already terminated, then pthread_join()
returns immediately.
5)What would happen to the execution of the program if one of the child threads should decide to call exit(3) halfway through its computation? (Modify your source code to include this change and experiment with it. When you are done, leave this line commented out.)
The other threads still runs to get their own results, but since I let thread 3 exists, the whole program stopped as it was trying to join thread 3. So no summation generated.
6)What would happen if one of the child threads should decide to call execl(3) to run “/bin/ls” in the executable’s current working directory? (Again, modify your source code to include this change and experiment with it. When you are done, leave this line commented out.)
When execl is called, all chid threads executes except the main thread does not perform the thread join and also there is no summation at the end.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment