


#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;
}
*/
(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...
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 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 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...