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 int x = 80; // SHARED VARIABLE AMONG THREADS void add1 ( void *a ) } void add2 ( void *b ) } void add3 ( void *c ) } int main ( ) pthread_t T1, T2, T3; // TASK 5 // TASK 6 // TASK 7 // TASK 8
#include
#include
pthread_mutex_t one_lock = PTHREAD_MUTEX_INITIALIZER;
{
// TASK 1
// use mutex to lock before increment
// add code to increment the global variable x by *a
// use mutex to unlock before increment
{
// TASK 2
// use mutex to lock before increment
// add code to increment the global variable x by *b
// use mutex to unlock before increment
{
// 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 a = 10, b = 20, c = 30;
// TASK 4
// create the thread T1 and pass the function add1
// create the thread T2 and pass the function add2
// create the thread T3 and pass the function add3
// wait for the threads
//
// print the value of x, it should be 140
//
}
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.