answer.
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
vaieri [72.5K]
1 year ago
15

Suppose two threads execute the following C code concurrently, accessing shared variables a, b, and c: Initialization int a = 4;

int b = 0; int c = 0; Thread 1 Thread 2 if (a < 0) { b = 10; c = b-a; a = -3; } else { c = b + a; } What is the total number of possible values for c after both threads complete? You can assume that reads and writes of the variables are atomic and that the order of statements within each thread is preserved in the code generated by the C compiler?
Computers and Technology
1 answer:
Oksanka [162]1 year ago
6 0

Answer:

Therefore, there are 4 possible values for c after both threads complete

c = 4, 14, 3, 13

Explanation:

Initialization:

int a = 4;

int b = 0;

int c = 0;

Thread 1:

if (a < 0)

{

c = b - a;

}

else

{ c = b + a; }

Thread 2:

b = 10;

a = -3;

We have to consider all the possible cases.

Case 1: a = 4, b = 0, c = 0

if (a < 0) false

{

c = b - a;

}

else

{ c = b + a; }  c = 0 + 4 = 4

c = 4

Case 2: a = 4, b = 10, c = 0

if (a < 0) false

{

c = b - a;

}

else

{ c = b + a; }  c = 10 + 4 = 14

c = 14

Case 3: a = -3, b = 0, c = 0

if (a < 0) true

{

c = b - a; c = 0 - (-3) = 3

}

else

{ c = b + a; }  

c = 3

Case 4: a = -3, b = 10, c = 0

if (a < 0) true

{

c = b - a; c = 10 - (-3) = 13

}

else

{ c = b + a; }  

c = 13

Therefore, there are 4 possible values for c after both threads complete

c = 4, 14, 3, 13

You might be interested in
For drivers under 21, the penalties for driving with an illegal BAL include _____.
JulijaS [17]
Up to one year In jail
5 0
1 year ago
Read 2 more answers
python A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun
e-lub [12.9K]

Answer:

// program in Python.

#read year

i_year=int(input("Please Enter a year:"))

#check leap year

if((i_year % 4 == 0 and i_year % 100 != 0) or (i_year % 400 == 0)):

   print("{} is a leap year.".format(i_year))

else:

   print("{} is not a leap year.".format(i_year))

Explanation:

Read year from user and assign it to variable "year".If year is completely divisible by 4 and not divisible by 100 or year is completely divisible by 400 then year is leap year otherwise year is not a leap year.

Output:

Please Enter a year:2003                                                                                                          

2003 is not a leap year.

3 0
2 years ago
Read 2 more answers
Recall the problem of finding the number of inversions. As in the text, we are given a sequence of n numbers a1, . . . , an, whi
Kay [80]

Answer:

The algorithm is very similar to the algorithm of counting inversions. The only change is that here we separate the counting of significant inversions from the merge-sort process.

Algorithm:

Let A = (a1, a2, . . . , an).

Function CountSigInv(A[1...n])

if n = 1 return 0; //base case

Let L := A[1...floor(n/2)]; // Get the first half of A

Let R := A[floor(n/2)+1...n]; // Get the second half of A

//Recurse on L. Return B, the sorted L,

//and x, the number of significant inversions in $L$

Let B, x := CountSigInv(L);

Let C, y := CountSigInv(R); //Do the counting of significant split inversions

Let i := 1;

Let j := 1;

Let z := 0;

// to count the number of significant split inversions while(i <= length(B) and j <= length(C)) if(B[i] > 2*C[j]) z += length(B)-i+1; j += 1; else i += 1;

//the normal merge-sort process i := 1; j := 1;

//the sorted A to be output Let D[1...n] be an array of length n, and every entry is initialized with 0; for k = 1 to n if B[i] < C[j] D[k] = B[i]; i += 1; else D[k] = C[j]; j += 1; return D, (x + y + z);

Runtime Analysis: At each level, both the counting of significant split inversions and the normal merge-sort process take O(n) time, because we take a linear scan in both cases. Also, at each level, we break the problem into two subproblems and the size of each subproblem is n/2. Hence, the recurrence relation is T(n) = 2T(n/2) + O(n). So in total, the time complexity is O(n log n).

Explanation:

5 0
1 year ago
A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: the name of the clie
timama [110]

Answer:

see explaination

Explanation:

Java code

//Header file section

import java.util.Scanner;

import java.io.*;

//main class

public class SalesTestDemo

{

//main method

public static void main(String[] args) throws IOException

{

String inFile;

String line;

double total = 0;

Scanner scn = new Scanner(System.in);

//Read input file name

System.out.print("Enter input file Name: ");

inFile = scn.nextLine();

FileReader fr = new FileReader(new File(inFile));

BufferedReader br = new BufferedReader(fr);

System.out.println("Name \t\tService_Sold \tAmount \tEvent Date");

System.out.println("=====================================================");

line = br.readLine();

//Each line contains the following, separated by semicolons:

//The name of the client, the service sold

//(such as Dinner, Conference, Lodging, and so on)

while(line != null)

{

String temp[] = line.split(";");

for(int i = 0; i < temp.length; i++)

{

System.out.print(temp[i]+"\t");

if(i == 1)

System.out.print("\t");

}

//Calculate total amount for each service category

total += Double.parseDouble(temp[2]);

System.out.println();

line = br.readLine();

}

//Display total amount

System.out.println("\nTotal amount for each service category: "+total);

}

}

inputSale.txt:

Peter;Dinner;1500;30/03/2016

Bill;Conference;100.00;29/03/2016

Scott;Lodging;1200;29/03/2016

Output:

Enter input file Name: inputSale.txt

Name Service_Sold Amount Event Date

=====================================================

Peter Dinner 1500 30/03/2016

Bill Conference 100.00 29/03/2016

Scott Lodging 1200 29/03/2016

Total amount for each service category: 2800.0

3 0
1 year ago
2 Name the package that contains scanner class?​
Marat540 [252]
the answer is Java.util.scanner
5 0
1 year ago
Other questions:
  • What name are input devices, output devices, and auxiliary storage devices collectively known?
    9·1 answer
  • The memory allocated for a float value is ____ bytes.
    9·1 answer
  • Topological sort requires only a tiny addition to the DFS algorithms. Exactly one of the following data structures is required t
    9·1 answer
  • You have configured your firewall to authenticate a group of 100 users who are in your company. You set up the database of users
    14·1 answer
  • Consider the relation Courses(C, T, H, R, S, G), whose attributes can be thought of informally as course (C), teacher (T), hour
    10·1 answer
  • Server farms such as Google and Yahoo! provide enough compute capacity for the highest request rate of the day. Imagine that mos
    12·1 answer
  • A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative
    12·1 answer
  • If a class has member variables that are pointers, you must ensure that you implement ____.
    6·1 answer
  • [20 points] 3.3 Code Practice: Question 2
    7·1 answer
  • In a particular field, there are trees in a l single row from left to right. Each tree has a value V You cut trees from left to
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!