In: Computer Science
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.
#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