1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
nika2105 [10]
3 years ago
8

In the following code, use a lock to protect any data that might need protecting. Keep your critical sections as small as possib

le. (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; }
Computers and Technology
1 answer:
swat323 years ago
8 0

Answer:

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;

}

You might be interested in
Corinne is taking a chemistry class in college. A lab assignment asks students to track the melting point of ten different subst
Dmitrij [34]

Answer:

a spreadsheet

Explanation:

The data is of statistical type and is experimentally based. And hence, the spreadsheet is the best kind of software that can be used to register the record. And we can save 10 such experient results with the same format of tables on 10 different worksheets, and if we want we can adjust the readings of the 10 similar experiments at one time. And hence, we can quite effectively record as well as analyze this data on the spreadsheet. Hence, the spreadsheet is the correct option.

4 0
3 years ago
A computer with a domain name is called a what
Irina18 [472]
A domain refers to a group of computers that are part of a network and share network resources, like your home<span> computers all linked together sharing a printer. The user logs in to the domain and gains access to the resources. Several systems may be involved. The computer itself is referred to as a server.</span>
6 0
3 years ago
Read 2 more answers
Which are examples of types of audio media that can support a presentation? Check all that apply.
Schach [20]
Music,recordings,sound bites
3 0
3 years ago
Read 2 more answers
Ports that are generally used to establish outbound connections are known as ___ ports.
aivan3 [116]

Answer:

Ephemeral

Explanation:

3 0
4 years ago
Assume you have 100 values that are all different, and use equal width discretization with 10 bins.
zepelin [54]

Answer:

a) 10

b) 1

C) 10

D) 1

E) 20

F)  10

Explanation:

a) The largest number of records that could appear in one bin

 = 10

B) The smallest number of records that could appear in one bin

= 1

C) The largest number of records that cab appear in one bin

= 10

d) smallest number

= 1

e) With frequency = 20. the largest number of records that could appear in one bin with equal width discretization (10 bins)

= 20

f ) with equal height discretization

= 10

6 0
3 years ago
Other questions:
  • Run the C-LOOK algorithm (requests can only be serviced when the head is moving toward higher numbered tracks); the head is curr
    12·1 answer
  • Cartoon Disney question: Snow White asks the dwarfs a question. 2 of them are lying and 3 can only say the truth. The continuati
    10·1 answer
  • 20 PTS URGENT!!! Derek’s organization uses an emerging technology that uses specialized software to place an image on an object,
    6·2 answers
  • A cell reference =SUM(sheet1.A1,sheet 2.A2) will place the results in which of the following, the current worksheet, worksheet 1
    7·1 answer
  • What is the purpose of the Split command?
    10·2 answers
  • Pat practices on the keyboard to improve his typing speed. When he typed the sentence It was a rainy day, he missed typing the l
    6·1 answer
  • Stress is an illness not unlike cancer.<br> True<br> False
    6·2 answers
  • How to reload ingenuity when a pulsating blue thing is there
    11·1 answer
  • What is a query? State it's uses.​
    8·1 answer
  • What happens after the initial rollout of a new network generation?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!