University of California Los Angeles C Programming Code Project Consider this variable x as critical section. That is : many threads will compete to update this variable.
int x = 80;
pthread_mutex_t one_lock = PTHREAD_MUTEX_INITIALIZER;
declare three functions
void * add1 ( void *a )
{
add code with mutex to increment x by *a and unlock the mutex
}
void * add2 ( void *b )
{
add code with mutex to increment x by b unlock the mutex
}
void * add3 ( void *c )
{
add code with mutex to increment x by c unlock the mutex
}
in the main function:
Create three threads using create function :
pthread_t t1, t2, t3 ;
int x1 = 10, x2 = 20, x3 = 30;
while creating thread t1, pass add1 function and variable x1
while creating thread2, pass add2 function and variable x2
while creating thread3, pass add3 function and variable x3
wait for the threads to terminate
print the value of x now
}
_________________________use this _______________________________________________
#include
#include
#include
int x = 80; // SHARED VARIABLE AMONG THREADS
pthread_mutex_t one_lock = PTHREAD_MUTEX_INITIALIZER;
void add1 ( void *a )
{
// TASK 1
// use mutex to lock before increment
// add code to increment the global variable x by *a
// use mutex to unlock before increment
}
void add2 ( void *b )
{
// TASK 2
// use mutex to lock before increment
// add code to increment the global variable x by *b
// use mutex to unlock before increment
}
void add3 ( void *c )
{
// TASK 3
// use mutex to lock before increment
// add code to increment the global variable x by *c
// use mutex to unlock before increment
}
int main ( )
{
pthread_t T1, T2, T3;
int a = 10, b = 20, c = 30;
// TASK 4
// create the thread T1 and pass the function add1
// TASK 5
// create the thread T2 and pass the function add2
// TASK 6
// create the thread T3 and pass the function add3
// TASK 7
// wait for the threads
//
// TASK 8
// print the value of x, it should be 140
//
}
Science is the pursuit and application of knowledge and understanding of the natural and social…
Clearly stating the definition, the values, the meaning of such values and the type of…
All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…
All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…
https://www.npr.org/sections/ed/2018/04/25/605092520/high-paying-trade-jobs-sit-empty-while-high-school-grads-line-up-for-university Click on the link above. Read the entire link and answer the questions below…
All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…