Question

In: Computer Science

Language: C and comment code please PThreads The goal of this homework is to show an...

Language: C and comment code please

PThreads

The goal of this homework is to show an understanding of pthreads and C.

Assignment:

Write a C program to use the bitwise operations with each bitwise operation contained within its own thread. Each thread should take 1 parameter and will only modify global values.

First, create global integers x and y, then, in the main function you will read in values for x and y from the user and then create the threads.

Each thread will use the parameter passed to them to determine which operation to run:

               Thread 0: |

               Thread 1: &

               Thread 2: ~

               Thread 3: ^

               Thread 4: <<

               Thread 5: >>

(You should probably use a switch statement for this)

Once a thread has computed its value (x OP y, where OP is one of the above operations, or, in the case of ~ you will just do ~x and ~y), it will then set x = y and y = (x OP y).

You will need to use a mutex to keep x and y safe when you are reading and writing this data.

Solutions

Expert Solution

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

//Pthread mutex created
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
//Two global operand initialized to zero
int OP1 = 0;
int OP2 = 0;

//Function to perform bitwise operation
void *BitWiseOperation(int ch)
{
//Mutex lock operation
pthread_mutex_lock(&mutex1);
//Performs Bitwise operation as per the user choice
switch(ch)
{
case 0:
printf("\n Thread %d: OP1(%d) | OP2(%d) = %d", ch, OP1, OP2, (OP1 | OP2));
break;
case 1:
printf("\n Thread %d: OP1(%d) & OP2(%d) = %d", ch, OP1, OP2, OP1 & OP2);
break;
case 2:
printf("\n Thread %d: OP1(%d) = ~OP1(%d) and OP2(%d) = ~OP2(%d)", ch, OP1, ~OP1, OP2, ~OP2);
break;
case 3:
printf("\n Thread %d: OP1(%d) ^ OP2(%d) = %d", ch, OP1, OP2, OP1 ^ OP2);
break;
case 4:
printf("\n Thread %d: OP1(%d) << OP2(%d) = %d", ch, OP1, OP2, OP1 << OP2);
break;
case 5:
printf("\n Thread %d: OP1(%d) >> OP2(%d) = %d", ch, OP1, OP2, OP1 >> OP2);
default:
printf("\n Invalid choice ");
}//End of switch
//Mutex unlock
pthread_mutex_unlock( &mutex1 );
}//End of function

//Menu function
void menu()
{
printf("\n 0 - | Operation");
printf("\n 1 - & Operation");
printf("\n 2 - ~ Operation");
printf("\n 3 - ^ Operation");
printf("\n 4 - << Operation");
printf("\n 5 - >> Operation");
printf("\n 6 - Exit");
}

//Main function
int main()
{
int rc1, ch, x;
//Five threads created
pthread_t thread1[5];
//Loops till 6 is the choice
do
{
//Displays menu
menu();
//Accepts the choice
printf("\n Enter your choice: ");
scanf("%d", &ch);
//If choice is 6 exit
if(ch == 6)
exit(0);
//Accepts the operand values
printf("\n Enter the value of Operand 1: ");
scanf("%d", &OP1);
printf("\n Enter the value of Operand 2: ");
scanf("%d", &OP2);

/* Create independent threads each of which will execute functionC */
if( (rc1 = pthread_create( &thread1[ch], NULL, BitWiseOperation(ch), NULL)))
{
printf("\n Thread creation failed: %d\n", rc1);
}

//Wait till threads are complete before main continues.
/* Unless we wait we run the risk of executing an exit which will terminate
/* the process and all threads before the threads have completed. */
pthread_join( thread1[ch], NULL);
}while(1);
}//End of main

Output:

0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 0

Enter the value of Operand 1: 60

Enter the value of Operand 2: 13

Thread 0: OP1(60) | OP2(13) = 61
0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 1

Enter the value of Operand 1: 12

Enter the value of Operand 2: 10

Thread 1: OP1(12) & OP2(10) = 8
0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 2

Enter the value of Operand 1: 3

Enter the value of Operand 2: 5

Thread 2: OP1(3) = ~OP1(-4) and OP2(5) = ~OP2(-6)
0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 3

Enter the value of Operand 1: 60

Enter the value of Operand 2: 13

Thread 3: OP1(60) ^ OP2(13) = 49
0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 4

Enter the value of Operand 1: 62

Enter the value of Operand 2: 2

Thread 4: OP1(62) << OP2(2) = 248
0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 5

Enter the value of Operand 1: 12

Enter the value of Operand 2: 3

Thread 5: OP1(12) >> OP2(3) = 1
Invalid choice
0 - | Operation
1 - & Operation
2 - ~ Operation
3 - ^ Operation
4 - << Operation
5 - >> Operation
6 - Exit
Enter your choice: 6


Related Solutions

Code the following in bash and run. Please show full code and don’t forget to comment...
Code the following in bash and run. Please show full code and don’t forget to comment your code. Make a code that takes any list of numbers and calculates and displays the mean, median and mode.
QUESTION 6 A single line comment in C++ language source code can begin with _____   ...
QUESTION 6 A single line comment in C++ language source code can begin with _____        a) // b) ; c) : d) /* QUESTION 7 What is the output of the following program? #include<iostream> using namespace std; class abc { public: int i; abc(int i) { i = i; } }; main() { abc m(5); cout<<m.i; } a)Garbage b)5        c)Compile Error d)None of the answers QUESTION 8 The following operator can be used to calculate the...
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Goal: Please answer the questions below. The main goal of this homework is to see if...
Goal: Please answer the questions below. The main goal of this homework is to see if you can calculate the profit maximization point for this small wedding cake business. I hope that you will be able to merge your knowledge of basic accounting and microeconomic theory in order to calculate the profit maximization point, make comments about efficiency, and make logical recommendations to the firm's management to ensure their future success. Current Situation:The local wedding cake business was very competitive...
Goal: Please answer the questions below. The main goal of this homework is to see if...
Goal: Please answer the questions below. The main goal of this homework is to see if you can calculate the profit maximization point for this small wedding cake business. I hope that you will be able to merge your knowledge of basic accounting and microeconomic theory in order to calculate the profit maximization point, make comments about efficiency, and make logical recommendations to the firm's management to ensure their future success. Current Situation: The local wedding cake business was very...
Goal: Please answer the questions below. The main goal of this homework is to see if...
Goal: Please answer the questions below. The main goal of this homework is to see if you can calculate the profit maximization point for this small wedding cake business. I hope that you will be able to merge your knowledge of basic accounting and microeconomic theory in order to calculate the profit maximization point, make comments about efficiency, and make logical recommendations to the firm's management to ensure their future success. Current Situation:The local wedding cake business was very competitive...
Write the simple shell in C language. Please show some outputs.
Write the simple shell in C language. Please show some outputs.
Use R programming language to answer and please so show the code as well. A paper...
Use R programming language to answer and please so show the code as well. A paper manufacturer studied the effect of three vat pressures on the strength of one of its products. Three batches of cellulose were selected at random from the inventory. The company made two production runs for each pressure setting from each batch. As a result, each batch produced a total of six production runs. The data follow. Perform the appropriate analysis. Table is below Batch Pressure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT