corrupted size vs. prev_size in C - c

I get "corrupted size vs prev_size" error whenever I allocate a dynamic memory in a thread. Whenever I allocate the memory in main() it works fine. But allocating dynamic memory in thread produces the error.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *fib(void *p);
struct mystruct
{
int *array;
int size;
};
int main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
struct mystruct obj;
obj.size = atoi(argv[1]);;
pthread_create(&tid, &attr, fib, (void *)&obj);
pthread_join(tid, NULL);
}
void *fib (void *p)
{
struct mystruct *x = (struct mystruct *)p;
x->array = (int *) malloc (x->size);
for (int i=0; i<x->size; i++){
x->array[i] = i;
printf("The array is = %d\n", x->array[i]);
}
}
I've added snapshot for details.
Thanks!

Try with the following line:
x->array = malloc (x->size*sizeof(int));
You need to allocate space for x->size integers. malloc accepts as parameter the number of bytes you need. And for n int you need n times the size in bytes of a single int.
Do not forget to return from the main.

Related

Passing struct data arguments to threads

I'm confused on how to pass struct arguments to threads in the for loop.
When I try with this approach, I get garbage port values. When I try to change the struct to just astruct argstruct; without the pointer, the second port overwrites the first one so I get 200 200 printed.
Also, would I have to free the struct in main, Func, or both?
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* Func(void* pstruct);
typedef struct thread_args{
int port;
} astruct;
int main()
{
int peers[2] = {100,200};
pthread_t threads[2];
astruct *argstruct[2];
for (int i = 0; i < 2; i++) {
argstruct[i] = malloc(sizeof(astruct));
argstruct[i]->port = peers[i];
pthread_create(&threads[i],NULL,Func,&argstruct[i]);
}
for(int i = 0; i < 2; i++){
pthread_join(threads[i], NULL);
}
return 0;
}
void* Func(void* pstruct){
int port;
astruct *argstruct = (astruct *)pstruct;
port = argstruct->port;
printf("port: %d\n", port);
return 0;
}
I'm confused on how to pass struct arguments to threads in the for loop.
argstruct[i] elements are pointers, you don't need address operator:
pthread_create(&threads[i], NULL, Func, argstruct[i]);
Note that you wouldn't need memory allocation for this simple case, you can use a local array:
//...
astruct argstruct[2];
for (int i = 0; i < 2; i++) {
argstruct[i].port = peers[i];
pthread_create(&threads[i], NULL, Func, &argstruct[i]);
}
//...
Also, would I have to free the struct in main, Func, or both?
You should free it one time only. The procedure is always the same, you free it when you don't need it anymore, that can be in the the thread routine or in main.
In your example, you could very well free the memory after port = argstruct->port; as you no longer use it after this assignment:
void* Func(void* pstruct){
int port;
astruct *argstruct = (astruct *)pstruct;
port = argstruct->port;
free(argstruct); //<-- here
printf("port: %d\n", port);
return 0;
}

Parallel programming using pthread

I have just started learning parallel programming using pthreads. So, for the learning purpose I tried sum of two integer arrays parallely. I have declared struct construct with three array variables a, b and c. I want to do addition of a, b and store the result in c.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define MAX 6
struct data {
int a[MAX];
int b[MAX];
int c[MAX];
};
void *addition(void *index) {
struct data *d1 = (struct data *)index;
printf("value of d1 structure=%d\n", d1->a[0]);
}
int main() {
int i, j, t;
struct data *item = NULL;
pthread_t threads[MAX];
item = (struct data *)malloc(sizeof *item);
printf("enter the value for arrray a\n");
for (i = 0; i < MAX; i++) {
scanf("%d", &item->a[i]);
}
printf("enter the value of array b\n");
for (j = 0; j < MAX; j++) {
scanf("%d", &item->b[j]);
}
for (t = 0; t < MAX; t++) {
pthread_create(&threads[t], NULL, addition, (void *)&item);
}
}
Here, as of now I have not added the addition of arrays in the function addition(), because in pthread_create() when I am passing the three arguments with the help of structure, in function addition the variables a and b are not getting copied. Printing a is giving me garbage value. Can anybody help me how to copy the structure variable to the function argument called by pthread_create().
pthread_create Function :
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Kindly note (*arg) is pointer to the arg.
You have created struct data *item as a pointer then you are passing its address i.e. pointer to pointer.
pthread_create(&threads[t], NULL, addition, (void *)&item);
Change the call to
pthread_create(&threads[t], NULL, addition, (void *)item).

Extract sub struct from main struct for thread creation

I am having issues trying to send multiple arguments to the pthread_create, the issue is basically because one of the arguments is another struct.
This is the node.
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#define NUM_THREADS 4
struct arr {
char line[10];
};
struct args {
struct arr record; int count; int init; int end;
};
void* processarr(void *arguments)
{
int count; int init; int end;
struct args *argstmp=arguments;
init=argstmp->init;
count=argstmp->count;
end=argstmp->end;
struct arr record[count];
record=(struct arr)argstmp->record;
printf("Some of the vals are init %d count %d end %d\n",init, count, end);
printf("vals like record 0\n", record[0].line);
pthread_exit(NULL);
}/*end of processarr*/
int main (int argc, char *argv[])
{
int line_count;
FILE *ptr_file;
char buf[10];
ptr_file =fopen(argv[ 1 ],"r");
if (!ptr_file)
return 1;
while (fgets(buf,10, ptr_file)!=NULL)
{
line_count++ ;
}
rewind(ptr_file);
struct arr record[line_count];
line_count=0;
while (fgets(buf,10, ptr_file)!=NULL)
{
line_count++ ;
buf[strcspn(buf, "\r\n")] = 0; /* Removing end null chars*/
strcpy(record[line_count].line,buf);
}
float grptmp,group, lgroup;
grptmp=line_count/NUM_THREADS;
int counter1,counter2,init,end;
counter2=1;
struct args myargs;
//processarr(record, line_count, init, end);
pthread_t threads[NUM_THREADS];
for (counter1=0;counter1<=line_count;counter1++)
{
if(counter2==NUM_THREADS)
{
end=line_count;
}else{
end=counter1+grptmp;
}
init=counter1;
myargs.record=*record;
myargs.count=line_count;
myargs.init=init;
myargs.end=end;
printf ("Run job #%d with paramts Init=%d and End=%d\n",counter2, init, end);
//call here
//struct arr *record; int count; int init; int end;
int rc;
long t;
for(t=0;t<NUM_THREADS;t++){
rc = pthread_create(&threads[t], NULL,processarr,&myargs);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
counter1=counter1+grptmp;
counter2++;
}
return 0;
}
So, when I send my arguments,the one stored inside myargs.record=*record, for some reason I am not able to "unpack" it once in the function.
The function is was defined as void to be able to catch the whole big argument, and I am trying to re-map everything there, the counts are working fine, but the one called record, that is actually another struct is not working, looks like a cast issue.
void* processarr(void *arguments)
{
int count; int init; int end;
struct args *argstmp=arguments;
init=argstmp->init;
count=argstmp->count;
end=argstmp->end;
struct arr record[count];
record=(struct arr)argstmp->record;
printf("Some of the vals are init %d count %d end %d\n",init, count, end);
printf("vals like record 0\n", record[0].line);
pthread_exit(NULL);
}
I am getting the following error when compiling.
test4.c: In function processarr:
test4.c:31:7: error: assignment to expression with array type
record=(struct arr)argstmp->record;
Any idea why this is not working? This last one was my last change using the casting (struct arr) in front of the argstmp (which is supposed to have everything inside).
Elaborating a little more on my response, this is what I would do using another struct to pass the arguments.
typedef struct {char line[10];}mystruct;
typedef struct {mystruct struct1;char line[10];}wrapper;
struct wrapper2 {mystruct struct1;char line[10];};
void unwrap(struct wrapper2 args){
printf("val is %s\n",args.line);
mystruct tmp=args.struct1;
printf("val inside structure is %s\n\n", tmp.line);
}
int main ()
{
mystruct names;
strcpy(names.line,"TEST");
struct wrapper2 wrapper1;
wrapper1.struct1=names;
strcpy(wrapper1.line,"Value1");
unwrap (wrapper1);
}
I hope this example helps you fix the issue, you just need to pass the same thing using the pthread_create.
Update:
The final code could look like this:
#include <pthread.h>
#include <stdlib.h>
struct mystruct {
int val;
};
void * func (void *args){
struct mystruct *st1=args;
printf("Thread created..%d\n", st1->val);
pthread_exit(NULL);
}
int main ()
{
/* Thread creation logic as void * (*)(void *)
int pthread_create (pthread_t *, const pthread_attr_t *,
void *(*)(void *), void *);
* */
struct mystruct mystruct1;
mystruct1.val=230;
pthread_t threads;
pthread_create(&threads,NULL,&func,&mystruct1);
pthread_exit(NULL);
return 0;
}
I recommend you read the actual manual of pthread_create.
http://man7.org/linux/man-pages/man3/pthread_create.3.html

Thread struct as function parameter C

I'm having a trouble passing a struct pointer into a function because I'm a bit confused with those pointers and references. I want to modify the thread.thread_num value from the thread_startfunction.
#include <stdio.h>
#include <stdlib.h> //malloc, free
#include <pthread.h>
#define N 5
// void *malloc(size_t);
struct thread {
pthread_t thread_id;
int thread_num;
// int thread_sum;
};
void *thread_start(void *thread)
{
struct thread *my_data;
my_data = (struct thread *)thread;
printf("num T: %i\n", my_data->thread_num);
my_data->thread_num=4;
printf("num T: %i\n", my_data->thread_num);
return NULL;
}
int main(int argc, char *argv[])
{
int i;
struct thread pthread_data;
struct thread *thread = &pthread_data;
thread->thread_num=2;
pthread_create(&thread->thread_id, NULL, thread_start, (void *)&thread);
printf("num: %i\n",thread->thread_num);
pthread_exit(NULL);
return 0;
}
But the value that print the main doesn't change (2).
And then I want to create an array of thread struct, but I don't know how exactly do that:
I guess it should be something like this:
int main(int argc, char *argv[])
{
int i;
struct thread pthread_data;
struct thread *thread[N-1] = &pthread_data; // I don't know how to manage this.
for(i=0; i<N; i++)
{
thread->thread_num=i;
pthread_create(&thread[i]->thread_id, NULL, thread_start, (void *)&thread[i]);
printf("num %i: %i\n",i,thread[i]->thread_num);
}
pthread_exit(NULL);
return 0;
}
Any thoughts?
I recommend you to read http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf
And here what you wanted:
#define N 5
typedef struct thread {
pthread_t thread_id;
int thread_num;
// int thread_sum;
} ThreadData;
void *thread_start(void *thread)
{
ThreadData *my_data = (ThreadData*)thread;
//there is no guarantee that prints will be in order
// we will use its initial thread->num ,cause it differs for each thread
//plus you will see how threads will behave
int order=my_data->thread_num;
printf("%i) before num T: %i\n",order, my_data->thread_num);
my_data->thread_num=4;
printf("%i) after assignment num T: %i\n",order ,my_data->thread_num);
return NULL;
}
int main(int argc, char *argv[])
{
int i;
ThreadData thread[N];
for(i=0; i<N; i++)
{
thread[i].thread_num=i;
pthread_create(&(thread[i].thread_id), NULL, thread_start, (void *)(thread+i));
}
//wait for all threads
for (i = 0; i < N; i++)
pthread_join(thread[i].thread_id, NULL);
//print results of each thread
for (i = 0; i < N; i++)
printf(" %i)thread: number %i\n",i,thread[i].thread_num);
return 0;
}
I can see multiple mistakes in your code.
First you have your pointers wrong. In the first example, it is enough to pass &pthread_data to pthread_create, &thread is the address of the thread pointer, so you are passing struct thread ** to your function instead of struct thread *. In the second example you should use (void *) thread[i]. &thread[i] is again struct thread **.
When you want each thread to write to its own thread data, then you should make an array of thread data, so that each thread has its own piece, else you will run into race conditions.
As others have already pointed out, you should call pthread_join before calling printf to ensure that the main thread will wait for all worker threads.
Also note that if you are calling pthread_join from other function that the one that spawned those threads, you have to ensure that the array of tread data will not go out of scope (in this case it would be probably better to use malloc or a global array).
You can't. As soon as main exits, which it does when you call pthread_exit, pthread_data ceases to exist, since it's local to the main function. So it can't be modified.

pthread_create and passing an integer as the last argument

I have the following functions :
void *foo(void *i) {
int a = (int) i;
}
int main() {
pthread_t thread;
int i;
pthread_create(&thread, 0, foo, (void *) i);
}
At compilation, there are some errors about casting ((void *) i and int a = (int) i). How can I pass an integer as the last argument of pthread_create properly?
Building on szx's answer (so give him the credit), here's how it would work in your for loop:
void *foo(void *i) {
int a = *((int *) i);
free(i);
}
int main() {
pthread_t thread;
for ( int i = 0; i < 10; ++1 ) {
int *arg = malloc(sizeof(*arg));
if ( arg == NULL ) {
fprintf(stderr, "Couldn't allocate memory for thread arg.\n");
exit(EXIT_FAILURE);
}
*arg = i;
pthread_create(&thread, 0, foo, arg);
}
/* Wait for threads, etc */
return 0;
}
On each iteration of the loop, you're allocating new memory, each with a different address, so the thing that gets passed to pthread_create() on each iteration is different, so none of your threads ends up trying to access the same memory and you don't get any thread safety issues in the way that you would if you just passed the address of i. In this case, you could also set up an array and pass the addresses of the elements.
You can allocate an int on the heap and pass it to pthread_create(). You can then deallocate it in your thread function:
void *foo(void *i) {
int a = *((int *) i);
free(i);
}
int main() {
pthread_t thread;
int *i = malloc(sizeof(*i));
pthread_create(&thread, 0, foo, (void *) i);
}
You should cast the address of i (rather than the value of i as you do now) in the last argument of pthread_create().
pthread_create(&thread, 0, foo, (void *) &i);
^ is missing
And the casting is wrong in your function too. It should be:
int a = *((int*) i);
If you intend to read the value, you should also initialize i to some value in main() as it's uninitialized now.
2 Use proper definition for main():
int main(void)
or int main(int argc, char *argv[]) or its equivalent.
Old question, but I faced the same problem today, and I decided not to follow this path.
My application was really about performance, so I chose to have this array of ints declared statically.
Since I don't know a lot of applications where your pthread_join / pthread_cancel is in another scope than your pthread_create, I chose this way :
#define NB_THREADS 4
void *job(void *_i) {
unsigned int i = *((unsigned int *) _i);
}
int main () {
unsigned int ints[NB_THREADS];
pthread_t threads[NB_THREADS];
for (unsigned int i = 0; i < NB_THREADS; ++i) {
ints[i] = i;
pthread_create(&threads[i], NULL, job, &ints[i]);
}
}
I find it more elegant, more efficient, and you don't have to worry about freeing since it only lives in this scope.
While this is an old question there is one option missing when all you need is to pass a positive integer like a descriptor: you can pass it directly as the address, while it it a hack it works well and avoid allocating anything :)
NOTE: the size of the integer must match the size of a pointer on your OS but nowadays most systems are native 64bits.
#include <pthread.h>
#include <inttypes.h>
#include <stdio.h>
void *_thread_loop(void *p)
{
uint64_t n = (uint64_t)p;
printf("received %llu\n", n);
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_t read_thread_id;
uint64_t n = 42;
pthread_create(&read_thread_id, NULL, _thread_loop, (void *)n);
pthread_join(read_thread_id, NULL);
return 0;
}

Resources