Question

In: Computer Science

Answer the following questions based on the C code provided. What would happen if the spinlock...

Answer the following questions based on the C code provided.

  1. What would happen if the spinlock implementation code given here were to be run on a single processor / single core system as is? Explain your reasoning for your answer.
  2. What would happen if this code were to be run on a single processor / single core system with the line sched_yield() deleted from simple_spin_lock()? Explain your reasoning for your answer.
  3. What happens to threads waiting on a particular spinlock if the thread that has it locked is interrupted or otherwise suspended while it is in its critical section. Is this a problem? If so, is there a way to fix it?  

typedef int simple_spinlock_t;

int simple_spin_init(simple_spinlock_t* lock) { // The next line sets the compile time memory fence
__asm__ __volatile__("" ::: "memory");
// here we set the flag to zero. Apparently “unlocked” is zero  Exciting, right?
*lock = 0; return 0;
}


int simple_spin_lock(simple_spinlock_t* lock)
{
int i;

while (1)
{
for (i=0; i < 5000; i++)
if (__sync_bool_compare_and_swap(lock, 0, 1)
return 0;

sched_yield();
}
}

int simple_spin_unlock(simple_spinlock_t *lock)
{
__asm__ __volatile__("" ::: "memory");
*lock = 0; return 0;
}

int c = 0;

simple_spinlock_t lock; // declare the lock variable as a global

void* fnC() {
int i;
for (i = 0; i < 10; i++)
{
simple_spin_lock(&lock); // Lock the critical section   
c++; printf(" %d", c);
simple_spin_unlock(&lock); // Unlock the critical section
}

}
int main() {
int rt1, rt2; pthread_t t1, t2; int trial_count = 0;
simple_spin_init(&lock); // Initialize the lock flag

for (trial_count = 0; trial_count < 50; trial_count++)
{
c = 0;
/* Create two threads */
if ((rt1 = pthread_create(&t1, NULL, &fnC, NULL)))
printf("Thread creation failed: %d\n", rt1);   
if ((rt2 = pthread_create(&t2, NULL, &fnC, NULL)))
printf("Thread creation failed: %d\n", rt2);

pthread_join(t1, NULL);   
pthread_join(t2, NULL);   
printf("\n");
}   
return 0;
}

Solutions

Expert Solution

a.Firstly, spinning is just a loop where CPU keep looping itself to check on a variables value.If that value changes another CPU will modify it.

In a single CPU system another CPU will not be there and the CPU will just spin wait eternally.

So when we compile this all the spinlocks are not compiled into kernel.

b.Spinlock is mutual exclusion that has two values locked and unlocked, operations must be done in an automatic manner so that only one thread can obtain the lock even several are spinning at the given time.Here, if the lock is available the locked bit is set and code continues into critical section.

If the locks has been taken by someone else code goes into loop where it repeatedly checks the lock until its available. This part of of loop is spin.

If a non preemptive unprocessed system ever went into a spin,it would spin forever no other thread would be able to obtain the CPU to release the lock.


Related Solutions

Answer all questions given. Answer them with 2 or more sentences. 1. What would happen to...
Answer all questions given. Answer them with 2 or more sentences. 1. What would happen to equilibrium price and quantity if there were a decrease in demand: (Circle one answer) A. A decrease in both equilibrium price and quantity B. A decrease in equilibrium price C. A decrease in equilibrium quantity D. Noneoftheabove 2. Define/ Explain Demand: (Short Answer) 3. Shortage can be defined as: 4. Define/ Explain Supply (Short Answer): 5. Define Price ceilings & Price floors (Short Answer):...
Answer the following questions based on the information provided below: You are conducting a study to...
Answer the following questions based on the information provided below: You are conducting a study to determine the impact of a new reading intervention on students test scores. You randomly assign 40 students to either the intervention group or the control group (those not participating in the intervention). You also break the students into morning and afternoon classes to determine which is the best time of day to conduct the intervention. 1. What is the dependant variable? 2. What is/are...
Question 12 Please answer the following set of questions, based on the information provided below. The...
Question 12 Please answer the following set of questions, based on the information provided below. The data listed below give information for 10 middle-level managers at a particular company. The first column is years of experience [X] and the second column is annual salary (in thousands) [Y]. We are going to examine the relationship between salary in thousands [Y] and years of experience[X]. Below is the regression output. Manager#     (X)     (Y) 1 xx 66 2 xx 69 3...
Question 15 Please answer the following set of questions, based on the information provided below. A...
Question 15 Please answer the following set of questions, based on the information provided below. A service company is curious to know whether customer age is crucial in deciding to subscribe. The company has collected a random sample of 1000 people, and asked everyone whether he or she would subscribe to the service. The company knows which age group the person falls into: under 18 years of age, 18 - 29 years of age, 30-45 years of age and 46...
Question 14 Please answer the following set of questions, based on the information provided below. A...
Question 14 Please answer the following set of questions, based on the information provided below. A service company is curious to know whether customer age is crucial in deciding to subscribe. The company has collected a random sample of 1000 people, and asked everyone whether he or she would subscribe to the service. The company knows which age group the person falls into: under 18 years of age, 18 - 29 years of age, 30-45 years of age and 46...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the code is instantiated in a process and it has JUST returned from its own call to fork() and there is now a clone of that process that is itself “just about” to pick up execution. Where will the clone child pick up its execution? ii)How does the clone child know it’s a clone? How does the parent process know it is not a clone,...
Answer the following sub-questions based on the information provided. You will draw explanatory graphs associated with...
Answer the following sub-questions based on the information provided. You will draw explanatory graphs associated with each market structure as is relevant to the explanation. Consider an (inverse) demand curve P = 30 - Q. And a total cost curve of C(Q) = 12Q. Assume a monopolist is operating in this market. (i) Calculate the quantity (qM) chosen by a profit-maximizing monopolist. (ii) At the profit-maximizing quantity, what is the monopolistic market price (pM) of the product. (iii) Calculate the...
Based on the information provided for a one-year project, answer the following questions. PV =$100,000     AC...
Based on the information provided for a one-year project, answer the following questions. PV =$100,000     AC =$60,000     BAC=$125,000    ETC = $100,000 If 75% of the Planned Value was completed to date, what is the Earned Value of the work completed (round to nearest whole number)? What is the Cost Variance (round to nearest whole number)? What is the Schedule Variance (round to nearest whole number)? What is the Cost Performance Index (round to 2 decimals)? What is the Schedule Performance...
Please read the scenarios and answer the questions based on the nursing code of ethics. A)....
Please read the scenarios and answer the questions based on the nursing code of ethics. A). You are a home health nurse caring for patient who was discharged home after a fall. The patient sustained a hip fracture and is in significant pain. You review the chart of the nurse who has seen him for the past two weeks, and they have charted the patient has taken prescribed oxycodone several times per day. During ambulation the patient states they need...
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); } END of the questions :::::::::::::::::::::::::: :::::::::::::::::::::::::: Submission...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT