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 computer terminology, what is a network?
RSB [31]
It is like where two or more computers are connected and they exchange informations, (sharing) and also they can communicate. and also share their some softwares etc. etc... This is called network in computer terminology

4 0
3 years ago
An analyst receives an alert from the SIEM showing an IP address that does not belong to the assigned network can be seen sendin
lianna [129]

Answer: (A)  Firewall; implement an ACL on the interface

Explanation:

 According to the question, for re-mediate the issue firewall should be implemented the ACL (Access control list) on the given interface. The access control list is one of the type of logic which selectively give permission or deny the number of packet in the system which to through the interface.

Firewall is the type of device that basically examine the traffic in the network system and also make several decisions in the system. ACL is the set of rule which mainly define the route of the packet in the router interface state.

4 0
3 years ago
_____ should be used to create a project schedule.
dem82 [27]

Answer:

meeting is the correct answer

3 0
3 years ago
Isabel has an interesting way of summing up the values in a sequence A ofn integers, where n is a power of two. She creates a ne
olganol [36]

Answer:

Running time of algorithm is O(n).

Explanation:

n is power of 2

n =2,4,8,16,32,...................................

A is an array having n elements

B is an array of size 0 to (n/2)-1

if n=4 B then (4/2)-1 =1 So B has size 2

for(i=0;i<=(n/2)-1;++)

{

B[i]=A[2i]+A[2i+1];

}

This for loop will run n/2 times so complexity in terms of Big Oh is O(n/2) =O(n)

Running time of algorithm is O(n).

6 0
3 years ago
Help me with this please. I'll mark Brainiest
S_A_V [24]
What do you need help with?
3 0
3 years ago
Other questions:
  • What is the portion of the PowerPoint window that contains the current slide, total slides, zoom options, and various shortcuts
    8·1 answer
  • If you wanna buy a Monitor, please explain why you wanna buy it! PLEASE HELP ME!!!!!!!!!!!!!!!!!!!!!!!!!
    9·1 answer
  • What does a contain after the following code runs?int[] a = {1, 3, 7, 0, 0, 0};int size = 3, capacity = 6, value = 5;int pos = 0
    10·1 answer
  • Kaira's company recently switched to a new calendaring system provided by a vendor. Kaira and other users connect to the system,
    11·1 answer
  • Some 3d printers work by controlling the exact locations where a liquid
    5·1 answer
  • In PumaMart system, why an initial offer is not necessary at the beginning of negotiation?
    6·1 answer
  • A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To ac
    5·2 answers
  • CSc 2720 - Data Structures: Assignment 1 [100 points] How to Submit: Turn the .java file in Folder Assignment 1 in iCollege no l
    11·1 answer
  • Assume variable age = 22, pet = "dog", and pet_name = "Gerald".
    6·1 answer
  • Daily IT Question
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!