Question

In: Computer Science

1. What does the following program do? 2. What output does the program produce? 3. Examine...

1. What does the following program do?

2. What output does the program produce?

3. Examine the program code carefully. Is the program functioning correctly?

4. If you do not think the program is working correctly, describe why?

5. Include one screenshot of the program's output.

C++ PROGRAM:

#include <iostream>
#include <pthread.h>
#include <stdlib.h>

int count;

void* myFunction(void* arg)
{
int actual_arg = *((int*) arg);
  
for(unsigned int i = 0; i < 10; ++i) {
count++;
std::cout << "Thread #" << actual_arg << " count = " << count << std::endl;

// Random wait - This code is just to ensure that the threads
// show data sharing problems
int max = rand() % 100000;
  
for (int x = 0; x < max; x++);
  
// End of random wait code

}
  
pthread_exit(NULL);
}


int main()
{
int rc[2];
pthread_t ids[2];
int args[2];

count = 0;
for(unsigned int i = 0; i < 2; ++i) {
args[i] = i;
rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);
}

for(unsigned int i = 0; i < 2; ++i) {
pthread_join(ids[i], NULL);
}

std::cout << "Final count = " << count << std::endl;
pthread_exit(NULL);
}

Solutions

Expert Solution

Firstly, following are the steps to compile-and-run :

  • Copy the entire code in a file, say two_threads.cpp
  • Compile the code, via
    • g++ two_threads.cpp -lpthread -o two_threads
  • Run the executable (repeatedly) via
    • ./two_threads

1.

The program attempts to increment the global-variable count in multiple threads :

  • Number of threads spawned = 2
  • Each thread attempts to increment the variable 10 times.
  • Thus, the expected value of count, upon program termination, is 20.

2.

The program produces the state of global variable count, as and when it is incremented by a thread.

3.

Spuriously, the final value of count does not come out to be 20.

So, the program is not 100% resilient.

4.

The reason is that there is no mutual-exclusion between threads, when the shared global-variable count is modified/incremented.

Thus, count may not be incremented as expected, due to random context-switches while the variable-modification is in progress.

5.

Following is a screenshot, when the program updated count to 19 (as against expected value of 20) :


Related Solutions

What is the function of this program (what does it do)? What is the output if the input is 12d (12 decimal)?
What is the function of this program (what does it do)? What is the output if the input is 12d (12 decimal)? What would be the output if the input is 47d? Sub bx, bx Mov ecx, 16 GetInt ax Nxt: shl ax,1 jc Addone jnc LB1 Addone: inc bx LB: loop Nxt PutInt bx
What is the function of this program (what does it do)? What is the output iſ the input is 120 (12 decimal)?
What is the function of this program (what does it do)? What is the output iſ the input is 120 (12 decimal)? What would be the output if the input is 47d? 
Edit the given program to produce the following output in c++ mode: - Take in the...
Edit the given program to produce the following output in c++ mode: - Take in the name of a superhero & tell him how many villains he/she has to defeat today - The number of villains is randomly generated and should be a number between 11 and 42. - Use a seed of 7. Hint: Compile the program first before making edits What is your name? Hello Captain America There are 42 villains you need to defeat today Oops! one...
1. How does protectionism defined? 2. What is the history of the protectionism? 3. What do...
1. How does protectionism defined? 2. What is the history of the protectionism? 3. What do the pieces imply about protectionism today? 4. What do the pieces imply about the future of protectionism? 5. What has been and what would be the impact of protectionism in the World?
Identify the directives and statements in the following program. What is the output of the program?...
Identify the directives and statements in the following program. What is the output of the program? [2 points] #include <stdio.h> int main (void) { printf(“Parkinson’s Law: \n Work expands so as to \t”); If i and j are positive integers, does (-i)/j always have the same value as –(i/j)? Justify your answer. [2 points] printf(“fill the time \n”); Supply parenthesis to show how a C compiler would interpret the following expressions: [2 points] a * b – c *d +...
1. What does vasoconstriction or vasodilation do to venous return and cardiac output? How does this...
1. What does vasoconstriction or vasodilation do to venous return and cardiac output? How does this occur? 2. How would viscosity change as the number of red blood cells increase or decrease? 3. Give an example of when the size of a blood vessel's lumen would change. 4. Would vasoconstriction or vasodilation make the lumen smaller? What happens to the resistance when the lumen gets smaller? What happens to blood pressure?
Do oligopolies produce an efficient level of output?
Do oligopolies produce an efficient level of output?
1. What does a payroll accountant do? 2. How much does a payroll accountant make? 3....
1. What does a payroll accountant do? 2. How much does a payroll accountant make? 3. How to earn certifications?
In the following Java program replace conditions in while loops to produce Christmas tree output. import...
In the following Java program replace conditions in while loops to produce Christmas tree output. import java.util.Scanner; public class Tree {     public static void main(String[] args)     {         int size;         Scanner scan = new Scanner(System.in);         System.out.print("Enter the size: ");         size = scan.nextInt();         int count = 0;         while (__________) //??? condition         {             int len = 0;             // print blanks             while (____________)//??? condition             {                 System.out.print(' ');                 len++;            ...
1. Explain what cells produce HCl in the body and how they do it 2. What...
1. Explain what cells produce HCl in the body and how they do it 2. What would likely happen to someone who has a large, fatty meal soon after they have had the gall bladder removed and why 3. What are the 3 parts of the small intestine called? Which is most likely to have the least total number of bacteria and why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT