Question

(b) Consider the following two programs. They both access a shared variable count and increment it. One is lock-based using s

lock-based #include <8tdío.h> 2 #include <stdatomic.h> 3 | # include <pthread.h> 5 int count 0; 6 int lock 0; 8 voidadding (v

lock-free I 2 3 #include <stdio.h> #include < st datom ic.h> # include <pthread.h> 5 int count 0; 7 void+ adding (void *input

(b) Consider the following two programs. They both access a shared variable count and increment it. One is lock-based using spin-lock, and the other is lock-free. They both use C11 CAS(compare and swap) function and eventually get the sane result, ie., cunt 10.000.000. Please fill in the blanks to complete the lock-free version. Each blank contains at most one statement. (5 marks)
lock-based #include 2 #include 3 | # include 5 int count 0; 6 int lock 0; 8 voidadding (void input) int expected0 for (int i-0; i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdatomic.h>
#include<pthread.h>

int count=0;

void * adding(void * input)
{
   int val;
   for(int i=0;i<3;i++)
   {
       do {
           val=count; //store count value in val variable...
       }while(!atomic_compare_exchange_weak(&val,&count,++val));   
   }
}

int main()
{
   pthread_t tid[10];
  
   for(int i=0;i<10;i++)
   {
       pthread_create (&tid[i],NULL,adding,NULL);
   }
   for(int i=0;i<1;i++)
   {
       pthread_join (tid[i],NULL);
   }
   printf("The value of count is %d\n",count);
}


//code explanation:
/*
if(*(&val)==*(&count))
{
   *(&count)=++val;
   return true;
}
else
{
   *(&val)=*(&count);
   return false;
}
*/

Add a comment
Know the answer?
Add Answer to:
(b) Consider the following two programs. They both access a shared variable count and increment it. One is lock-based using spin-lock, and the other is lock-free. They both use C11 CAS(compare and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

  • Help with a Parallel and Distributed Programming assignment. In this assignment, you will be expl...

    Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT