Commit 9e08a6cd authored by cdf009's avatar cdf009

Lab 3.2 completed

parent f483310b
Casey Friend
Lab 3
answers.txt
Problem 1:
1) When you run the summation.c using the typical method of compilation you recieve and undefined refernece to pow, pthread_create and pthread_join! You also get an unused variable status.
2) The linker flag that you need to use is -lm.
3) You no longer have an undefined reference to pow. You only have an undefined reference to pthread_create and pthread_join.
3) You no longer have an undefined reference to pow. You only have an undefined reference to pthread_create and pthread_join.
Problem 2:
1) Threads only take in one argumen therefore the mechanism used to pass arguments to the other threads it creates is by structs. The struct allows for all 5 apparameters to be passed to the function and used in the created thread.
2) The calloc() call allocates memory for an array of elements. The size of the array will be defined by the argument that is called in the function. The calloc will allocate the memory for the bytes and then create a pointer to this memory allocation. The main difference of malloc is that calloc initializes the memory to 0 where the malloc does not.
3) A detached thread is one that when it exits, it will not save the value of the exit status. Therefore, the tid can be reused. Detached threads are not joined. The detached thread can exit without having to wait for other threads to exit or enter. THis program requires the return of the exit value so therefore it should not be creating detached threads. We see in the code that we join the treahds to find the summation. THe summation needs the exit value to be returned in order for this function to run properly.
4) The ret_val variable pulls together the exit values of each of the threads. The main thread will recieve the threads by the pthread_join and the attributres of it get the summation from the terminated thread and it continues to do so for every thread until the total summation is found.
5) When you call the exit(3) halfway through the computation everything will STOP. You will not get returned any summation nd all execution of the further threads will not happen. For example, when I put in the exit(3) in my code, you will only see an output for the thread 2 starting and not all of the summation code for other threads and the runtime.
6) The execl(3) when used to run "/bin/ls" will print the file name since you are in the working directory. The ls command on the linux command line also printsthe contents so this execl(3) prints contents.
Problem 4:
1) No, you cannot garuntee that the date printed by each thread is always the termination time of tht very thread.
2) We see in the man page that ctime returns a pointer to static data. This is the reason that the ctime function is not thread safe. The threads are all trying to use the same place to store the varible time data and therefore there will be descripencies in what time is returned due to ctime returning static data. The ctime_r() takes in two arguments and one of thos arguments is the buffer that allocates memory to store data. This allows each thread to be able to access a unique memory space for its time variables.
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