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
In which languages the dynamic web pages are written?<br>​
Sergeu [11.5K]

web pages that use server-side scripting are often created with the help of server-side languages such as:PHP,Perl,ASP.NET,JSP,ColdFusion.

5 0
3 years ago
What made it possible to develop personal computers?
Kay [80]
The main thing that allowed us to create personal computers were microchips. Microchips allowed us to store possessors and CPU components that would take up an entire room before the creation of them.<span />
5 0
3 years ago
Read 2 more answers
I have this questions i need to make it in a report format pages of atleast 3 pages and maximum of 5 pages​
Nuetrik [128]

First write what is software take one page for that

and each page for all other topics

6 0
3 years ago
​In sql server, the cursor property ____________________ means that the cursor is used for retrieval purposes only.
enyata [817]
I have recently found this answer https://assignment.essayshark.com/blog/sql-simple-examples-database/. Then provided me with a ready assignment in a very short period of time.

5 0
3 years ago
What are three recommendations for reducing risk and improving the privacy of your personal information while using social media
viktelen [127]

Answer:

1. Treat the “about me” fields as optional.

2. Become a master of privacy settings.

3. Know the people you friend.

Explanation:

1. Treat the “about me” fields as optional.

Because these fields are offered, however, doesn’t mean you must fill them out. Consider offering a general version of the information requested or simply leaving the field blank. For instance, listing only your state of residence, instead of both city and state, can make it harder for others to figure out exactly where you live.

2. Become a master of privacy settings.

All social media sites give you the option to limit post viewing to specific audiences. Take the time to explore these settings, try different options, and become a master of their use.

3. Know the people you friend.

Once upon a time, social media users competed with one another to have the largest number of connections. Today, however, smart social media users know that the more people you’re connected to, the harder it is to control what happens to the information you post. Make sure you know the people you add on social media, in real life if possible. Don’t hesitate to use the “block” feature when the situation seems to call for it.

6 0
3 years ago
Other questions:
  • If people have humility, they don’t tend to see themselves as overly or more important than others. True False
    13·1 answer
  • After earning a learners license what test must be successfully passed to earn an operators license in Florida
    14·1 answer
  • Network id is 192.168.10.32/28. what would be the ip address, if you assign the last available ip address in the range
    14·2 answers
  • How many words fit on a double-spaced page?
    11·1 answer
  • 1. Show the 16-bit representation of the decimal number 2437. Show your steps. 2. Convert the 16-bit representation of part (a)
    12·1 answer
  • Analyze the following recursive method and indicate which of the following will be true.
    7·1 answer
  • Malcolm Movers charges a base rate of $200 per move plus $150 per hour and $2 per mile. Write a program named MoveEstimator that
    5·1 answer
  • How do you print "Hello World" in the console with Python? Wrong Answers Only.
    7·2 answers
  • The largest group of Linux users is likely to be
    7·1 answer
  • Which statement best describes how the programming layer of abstraction in
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!