So I'm doing homework for my C class and I came across a problem. I want to sync the acess to my global array with pthread_mutex_lock() but it seems that when 2 or more threads try to lock at the same time it just bugs. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#define NUM_THREADS 10
#define VAL 10
int numbers[49];
pthread_mutex_t mutex;
typedef struct {
int start;
int* vec;
} dados;
void* threadFunction(void *arg){
dados* d = (dados*) arg;
int i = 0;
int j = 0;
int count[49];
memset(count, 0, 49);
for(i = 0; i < 49; i++){
for(j = d->start; j < (d->start + VAL); j++) {
if(d->vec[j] == i+1)
count[i] = d->vec[j];
}
}
pthread_mutex_lock(&mutex);
for(i = 0; i < 49; i++) {
numbers[i]+= count[i];
}
pthread_mutex_unlock(&mutex);
free(d);
pthread_exit(NULL);
}
int main(int argc, char** argv){
srand(time(NULL));
/*=============== Threads ================*/
pthread_mutex_init(&mutex, NULL);
pthread_t threads[NUM_THREADS];
/*========================================*/
int vec[NUM_THREADS * VAL];
int i;
for(i = 0; i < NUM_THREADS * VAL; i++)
vec[i] = (int)(rand() % 49 + 1);
for(i = 0; i < NUM_THREADS; i++) {
dados* d = malloc(sizeof(dados));
d->start = i*VAL;
d->vec = vec;
pthread_create(&threads[i], NULL, threadFunction, d);
}
for(i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
pthread_mutex_destroy(&mutex);
for(i = 0 ; i < 49; i++)
printf("NUMERO %d = %d\n", i+1, numbers[i]);
return 0;
}
Two bugs
memset(count, 0, 49);
should be:
memset(count, 0, sizeof(count));
And..
count[i] = d->vec[j];
should be:
count[i]++;
Related
I have just started working with multi-threading in C and I was given a task to create simple sleepsort algorithm. However when I try to run this, nothing is printing. I have no idea what am I doing wrong. Here is my code:
void* sleepSort(void* arr) {
int x = *(int *) arr;
sleep(x);
printf("%d ", x);
return NULL;
}
#define ARR_SIZE 10
#define MAX_ELEMENT 30
int main(void) {
srand(time(NULL));
int arr[ARR_SIZE];
for (int i = 0; i < ARR_SIZE; i++)
arr[i] = rand() % MAX_ELEMENT;
pthread_t threads[10];
for (int i = 0; i < ARR_SIZE; i++)
pthread_create(&threads[i], NULL, sleepSort, &arr[i]);
getchar();
return 0;
}
So, this is my program that calculates matrix determinant using system calls, not good at all, but, the trouble is that when i put in a number bigger than 8 for dimension of matrix, it crashes somehow and i can't figure why it keeps happening. Please, give me some ideas.
The task was to calculate determinant using multithreading. Maybe, the problem is that I exceed max threads? valgrind says that
Use --max-threads=INT to specify a larger number of threads
and rerun valgrind
valgrind: the 'impossible' happened:
Max number of threads is too low
compile it with gcc -g -pthread
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include <malloc.h>
pthread_mutex_t mutex;
typedef struct {
int **matrix;
int size;
} T_MS;
void* determinant(void *npt) {
T_MS* tmp = (T_MS*) npt;
int i,j;
double det = 0;
pthread_t *array = malloc(sizeof(pthread_t) * tmp->size);
T_MS *mtarr = malloc(sizeof(T_MS) * tmp->size);
if (tmp->size == 1) {
det = tmp->matrix[0][0];
} else if (tmp->size == 2) {
det = tmp->matrix[0][0] * tmp->matrix[1][1] - tmp->matrix[0][1] * tmp->matrix[1][0];
} else {
for (i = 0; i < tmp->size; ++i) {
mtarr[i].matrix = (int **)malloc(sizeof(int *) * tmp->size);
mtarr[i].size = tmp->size - 1;
for (j = 0; j < tmp->size - 1; ++j) {
if (j < i)
mtarr[i].matrix[j] = tmp->matrix[j];
else
mtarr[i].matrix[j] = tmp->matrix[j + 1];
}
pthread_create(&array[i], NULL, determinant, mtarr + i);
}
for (i = 0; i < tmp->size; ++i) {
void *res;
for (j = 0; j < tmp->size - 1; ++j) {
}
pthread_join(array[i], &res);
double x = *(double *)&res;
det += (-1 + 2 * !(i % 2)) * x * tmp->matrix[i][tmp->size - 1];
double answer = *(double*)&det;
free(mtarr[i].matrix);
}
}
free(mtarr);
free(array);
void* ans = *(void **)&det;
return ans;
}
int main(int argc, char const *argv[]) {
srand(time(NULL));
int **matrix;
int n = 0;
int a;
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
printf("Insert the demention of matrix:\n");
scanf("%d", &n);
matrix = (int**)malloc(n * sizeof(int*));
for (int i=0; i<n; ++i)
matrix[i] = (int*)malloc(n * sizeof(int));
printf("Insert matrix:\n");
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
matrix[i][j]=rand()%15;
//matrix[i][j] = i;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
T_MS* npt = (T_MS*)malloc(sizeof(T_MS));
npt->matrix = matrix;
npt->size = n;
void *det;
pthread_mutex_init(&mutex, NULL);
pthread_create(&tid, NULL, determinant, npt);
pthread_join(tid, &det);
double answer = *(double*)&det;
printf("Det is: %f\n", answer);
for (int i = 0; i < n; ++i)
free(matrix[i]);
free(matrix);
free(npt);
return 0;
} ```
I am learning pthreads.
Right now I am trying to make the program that writes to one 2d array using multiple pthreads. Each pthread is responsible for only one line of the array. So there is no race or overlap there.
The goal is to make it as fast as possible without using global variables.
The first solution that I implemented was the one that uses a global variable. And it works as intended. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int **array;
const int NTHREADS = 5;
const int ELEMENTS = 3;
void *worker(void *arg);
void print_array(int **array);
int main()
{
int i, j;
pthread_t* threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
array = (int**)malloc(sizeof(int*));
for(i = -1; i < NTHREADS; i++)
{
array[i] = (int*)malloc(sizeof(int));
for (j = -1; j < ELEMENTS; j++)
{
array[i][j] = (int)malloc(sizeof(int));
}
}
for (i = 0; i < NTHREADS; i++)
pthread_create(&threads[i], NULL, worker, (void*)i);
for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);
print_array(array);
return 0;
}
void *worker(void *arg)
{
int tid = (int)arg;
for (int j = 0; j < ELEMENTS; j++)
array[tid][j] = j;
return (NULL);
}
void print_array(int **array)
{
for (int i = 0; i < NTHREADS; i++)
{
for (int j = 0; j < ELEMENTS; j++)
printf("%d,", array[i][j]);
printf("\n");
}
}
Then I wrote the same program using struct instead of global variable. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
const int NTHREADS = 5;
const int ELEMENTS = 3;
typedef struct s_asd
{
int **array;
int tid;
} t_asd;
void *worker(void *arg);
void print_array(int **array);
int main()
{
pthread_t* threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
t_asd tmp;
int i, j;
tmp.array = (int**)malloc(sizeof(int*));
for (i = 0; i <= NTHREADS; i++)
{
tmp.array[i] = (int*)malloc(sizeof(int));
for (j = 0; j <= ELEMENTS; j++)
tmp.array[i][j] = (int)malloc(sizeof(int));
}
for (tmp.tid = 0; tmp.tid < NTHREADS; tmp.tid++)
pthread_create(&threads[tmp.tid], NULL, worker, &tmp);
for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);
print_array(tmp.array);
return 0;
}
void *worker(void *arg)
{
t_asd *tmp = (t_asd*)arg;
for (int j = 0; j < ELEMENTS; j++)
tmp->array[tmp->tid][j] = j;
return (NULL);
}
void print_array(int **array)
{
for (int i = 0; i < NTHREADS; i++)
{
for (int j = 0; j < ELEMENTS; j++)
printf("%d,", array[i][j]);
printf("\n");
}
}
This one, prints random numbers. I know that I am using the same pointer in all threads, but threads themselves, are not using the same memory area. So why does it prints random numbers?
What is the best solution, without using a global variable?
Update 1.
Output of the second program:
-1413467520,32668,-1413467440,
-1413467584,-1413467568,-1413467552,
-1413467504,-1413467488,-1413467472,
0,1,2,
0,1,2,
Try something like that :
int main()
{
pthread_t* threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));
t_asd tmp;
int i, j;
tmp.array = (int**)malloc(NTHREADS * sizeof(int*));
for (i = 0; i <= NTHREADS; i++)
{
tmp.array[i] = (int*)malloc(ELEMENTS * sizeof(int));
//can be deleted if you want
for (j = 0; j <= ELEMENTS; j++)
tmp.array[i][j] = 0;
}
for (tmp.tid = 0; tmp.tid < NTHREADS; tmp.tid++) {
t_asd *arg = (t_asd *) malloc(sizeof(t_asd));
memcpy(arg, &tmp, sizeof(t_asd)); //will copy the current tid and the pointer to the array in a new memory area
pthread_create(&threads[tmp.tid], NULL, worker, arg);
}
for (i = 0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);
print_array(tmp.array);
return 0;
}
Of course this is an example and you have to free all the allocations
You are passing local variable tmp as an argument to the thread and changing it in a loop at the same time. This is a data race and your threads most probably will operate over the same data.
Convert tmp to an array, fill and pass a corresponding element to a corresponding thread.
I work on this but i did not find solution of my problem.
This is my code. This code give an error on pthread_create and pthread_join lines. I tried everything to fix this problem but i cannot do this.
#include <stdio.h>
#include <pthread.h>
#define array_size 1000
#define no_threads 10
float a[array_size];
int global_index = 0;
int sum = 0;
pthread_t mutex_t ,mutex1;
void *slave(void *ignored)
{
int local_index, partial_sum = 0;
do {
pthread_t mutex_t ,lock(mutex1);
local_index = global_index;
global_index++;
pthread_t mutex ,unlock(mutex1);
if (local_index < array_size)
partial_sum += *(a + local_index);
}
while
(local_index < array_size);
pthread_t mutex , lock(mutex1);
sum += partial_sum;
pthread_t mutex_t , unlock(mutex1);
return 0;
}
main()
{
int i;
pthread_t thread_x[10];
pthread_mutex_init(&mutex1, NULL);
for (i = 0; i < array_size; i++)
a[i] = i+1;
for (i = 0; i < no_threads ; i++)
pthread_create(&thread_x[i] , NULL, slave,NULL);
for (i = 0; i < no_threads; i++)
pthread_join(&thread_x[i] , NULL);
printf("The sum of 1 to %d is %d\n", array_size, sum);
}
I have updated your code(pt1.c) I think you are looking for something like this.
#include <stdio.h>
#include <pthread.h>
#define array_size 1000
#define no_threads 10
float a[array_size];
int global_index = 0;
int sum = 0;
pthread_mutex_t mutex1;
void *slave(void *ignored)
{
int local_index, partial_sum = 0;
do {
pthread_mutex_lock(&mutex1);
local_index = global_index;
global_index++;
pthread_mutex_unlock(&mutex1);
if (local_index < array_size)
partial_sum += *(a + local_index);
}
while(local_index < array_size);
pthread_mutex_lock(&mutex1);
sum += partial_sum;
pthread_mutex_unlock(&mutex1);
return 0;
}
main()
{
int i;
pthread_t thread_x[10];
pthread_mutex_init(&mutex1, NULL);
for (i = 0; i < array_size; i++)
a[i] = i+1;
for (i = 0; i < no_threads ; i++)
pthread_create(&thread_x[i] , NULL, slave,NULL);
for (i = 0; i < no_threads; i++)
pthread_join(thread_x[i] , NULL);
printf("The sum of 1 to %d is %d\n", array_size, sum);
}
You should compile this code as
gcc -pthread pt1.c
output :
The sum of 1 to 1000 is 500500
I have to write two threads. Each one prints 5 even/odd numbers from 1 to 100 like this (odd is impair in French, even is pair).
even 2,4,6,8,10
odd 1,3,5,7,9
even 12,14,16,18,20
odd 13,15,17,19,21
etc...
I wrote this code:
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#define maxi 100
pthread_mutex_t mutex;
sem_t p;
sem_t imp;
int tour = 0;
void *pair(void *arg);
void *impair(void *arg);
int main() {
pthread_t tidp, tidimp;
pthread_mutex_init(&mutex, NULL);
sem_init(&p, 0, 1);
sem_init(&imp, 0, 1);
pthread_create(&tidp, NULL, pair, (void *)2);
pthread_create(&tidimp, NULL, impair, (void *)1);
pthread_join(tidp, NULL);
pthread_join(tidp, NULL);
sem_destroy(&imp);
sem_destroy(&p);
pthread_mutex_destroy(&mutex);
return 0;
}
void *pair(void *arg) {
int i = (int)arg;
int j, l;
// sleep(5);
pthread_mutex_lock(&mutex);
if (!tour) {
tour = 1;
pthread_mutex_unlock(&mutex);
sem_wait(&imp);
} else {
pthread_mutex_unlock(&mutex);
}
for (l = 0; l < maxi; l += 10) {
sem_wait(&p);
printf(" Pair ");
pthread_mutex_lock(&mutex);
for (j = 0; j < 10; j += 2) {
printf(" %4d \t", j + i);
}
pthread_mutex_unlock(&mutex);
printf("\n");
sem_post(&imp);
i += 10;
}
pthread_exit(NULL);
}
void *impair(void *arg) {
int i = (int)arg;
int j, l;
pthread_mutex_lock(&mutex);
if (!tour) {
tour = 1;
pthread_mutex_unlock(&mutex);
sem_wait(&p);
} else {
pthread_mutex_unlock(&mutex);
}
for (l = 0; l < maxi; l += 10) {
sem_wait(&imp);
printf("Impair ");
pthread_mutex_lock(&mutex);
for (j = 0; j < 10; j += 2) {
printf(" %4d \t", j + i);
}
pthread_mutex_unlock(&mutex);
printf("\n");
sem_post(&p);
i += 10;
}
pthread_exit(NULL);
}
What I don't understand is that when I run the code, sometimes it starts with odd, sometimes with even. More particularly, when it starts with odd everything goes normal and I get the all numbers from 1 to 100, but when it starts with even sometimes I only get to 91, sometimes 93, sometimes 97.
Can anyone tell me what is wrong? The screenshots below might help.
You're not waiting for both threads to exit:
pthread_join(tidp, NULL);
pthread_join(tidp, NULL);
One of those should be tidimp.