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

home / study / questions and answers / engineering / computer science / a progra

ID: 663999 • Letter: H

Question

home / study / questions and answers / engineering / computer science / a program consists of n threads. each thread works ...

Question

A program consists of n threads. Each thread works in phases. Each thread begins in phase 1. It
is required that all threads complete phase 1 before they can begin phase 2. Likewise, each thread must
complete phase 2 before any thread can begin phase 3, and so on. So, the code for each thread has the
following structure.
for (i = 1; ;i++)
{
Execute phase i
WaitForBarrier(i, threadid);
}
The function WaitForBarrier ensures that if a thread finishes phase i too quickly then it is blocked. When
all threads finish phase i, they are resumed so that they can continue executing phase i+1.
Write the function WaitForBarrier(int, int) using semaphores. Explain the correctness of your solution.

Please code in C++

Explanation / Answer

A thread is a semi-process, that has its own stack, and executes a given piece of code. Unlike a real process, the thread normally shares its memory with other threads (where as for processes we usually have a different memory area for each one of them). A Thread Group is a set of threads all executing inside the same process. They all share the same memory, and thus can access the same global variables, same heap memory, same set of file descriptors, etc. All these threads execute in parallel (i.e. using time slices, or if the system has several processors, then really in parallel).

The advantage of using a thread group instead of a normal serial program is that several operations may be carried out in parallel, and thus events can be handled immediately as they arrive (for example, if we have one thread handling a user interface, and another thread handling database queries, we can execute a heavy query requested by the user, and still respond to user input while the query is executed).

The advantage of using a thread group over using a process group is that context switching between threads is much faster then context switching between processes (context switching means that the system switches from running one thread or process, to running another thread or process). Also, communications between two threads is usually faster and easier to implement then communications between two processes.

On the other hand, because threads in a group all use the same memory space, if one of them corrupts the contents of its memory, other threads might suffer as well. With processes, the operating system normally protects processes from one another, and thus if one corrupts its own memory space, other processes won't suffer. Another advantage of using processes is that they can run on different machines, while all the threads have to run on the same machine (at least normally).