Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. Describe three applications where multi-threading can lead to improved perfor

ID: 3824622 • Letter: A

Question

a. Describe three applications where multi-threading can lead to improved performance over a single-threaded application. b. Could you implement a shell using 'pthread_create()' and 'pthread_join()' (i.e., a multi-threaded shell) as opposed to 'fork()', 'exec()', and 'wait()'? If yes, explain how. If not, explain why. c. Why is it much less costly to switch between two threads that are part of the same process than between two threads where each of the threads is part of a different process? d. Why does each thread require its own stack? What would happen if there was only one stack and more than one thread in a process? e. What is a race condition'? Give an example that contains a potential race condition. Describe conditions under which the race condition could occur.

Explanation / Answer

a) Multithreading has various advantages over single threaded applications.Example-

b) Yes , we can implement a shell using pthread_create() and pthread_join().Pthread_create() creates a nw thread whereas fork() creates a identical thread.If after using pthread_create() we will call fork() it will give error.

c) Switching between two threads in same process is callled thread switching and wicthing between two threads which are part of different processes is called context switching.Context switching involves switching of all resources like memory addresses , mappings , page tables etc. while in thread switching only the processor state is changed.Therefore thread switching is cheaper than context switching.

d) Each thread requires its own stack so that it is memory address for every thread.Stack is basically a memory address for a thread.

If there is only one stack and more than one thread in the process then stack will be shared.This is more complex process as all the addresses will be stored at one place.Therefore every thread should have one stack to avoid this situation.

e) Race condition occurs in a multi threaded enviornment when two or more than two threads access a shared variable.

Example of potential race condition - There are many persons around a circular dining table.Everyone has a fork and spoon kept on both the sides of the plate.If two person tries to use the same fork here the race conditin occurs.

Conditions under race condition occurs :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote