meCreation
17.06.2020 •
Computers and Technology
In the following code, use a lock to protect any data that might need protecting. Keep your critical sections as small as possible. (10%) int flag = 0; int a = 0; void thread_a(){ a += 2; Flag = 1; } 1 void thread_b(){ int b = 0; if (flag) { b++; } b += 3; a -= b; }
Solved
Show answers
More tips
- H Health and Medicine How to Choose the Right Tanning Cream?...
- C Computers and Internet How to Download Movies from Torrents?...
- S Style and Beauty How to Sew Harem Pants?...
- C Computers and Internet Е-head: How it Simplifies Life for Users?...
- F Family and Home How to Choose the Best Diapers for Your Baby?...
- F Family and Home Parquet or laminate, which is better?...
- L Leisure and Entertainment How to Properly Wind Fishing Line onto a Reel?...
- L Leisure and Entertainment How to Make a Paper Boat in Simple Steps...
- T Travel and tourism Maldives Adventures: What is the Best Season to Visit the Luxurious Beaches?...
- H Health and Medicine Kinesiology: What is it and How Does it Work?...
Answers on questions: Computers and Technology
- H History How did southern states limit voting rights of african americans? (give two...
- C Computers and Technology (1)Suppose that you find a job in the Human Resources Department of The DreamCompany. You are responsible for building a database to store various information about...
- M Mathematics 6th grade math help it’s due tomorrow...
- M Mathematics a classroom is 6.8m long 3.5m wide and 270cm high find the cost of plastering the 4 walls and ceiling at the rate of 12per m^2...
- P Physics The two most prominent wavelengths in the light emitted by a hydrogen discharge lamp are 656 nm (red) and 486 nm (blue). Light from a hydrogen lamp illuminates a diffraction...
Ответ:
A mutex is used here.
A mutex is a locking mechanism set before using a shared resource and is released after using the shared resource. When the lock is set, only one task accesses the mutex.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<unistd.h>
pthread_t tid[2];
int flag=0;
int a = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *thread_a(void *arg)
{
pthread_mutex_lock(&lock);
a += 2;
pthread_mutex_unlock(&lock);
flag = 1;
return NULL;
}
void *thread_b(void *arg)
{
int b = 0;
if(flag){
b++;
}
b += 3;
pthread_mutex_lock(&lock);
a -= b;
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
while(1) {
pthread_create(&tid[0],NULL,thread_a,NULL);
pthread_create(&tid[1],NULL,thread_b,NULL);
sleep(1);
}
pthread_exit(NULL);
return 0;
}
Ответ:
Correct answers are as follows;
hyperlink
web page
top-level domain name
website
the government
good luck :)