Question
I need help with this C programming assignment. This is what i have so far and its not working 100% could someone help fix it
I need help with this C programming assignment. This is what i have so far and its not working 100% could someone help fix it for me.
Codes:
#include
#include
#include
#include
pthread_barrier_t barrier;
int *arr,*sum;
pthread_mutex_t lock;
struct index
{
int i;
int j;
};
void *sum_func(void *arg)
{
struct index *st=(struct index*)arg;
printf("adding %d %d ",st->i,st->j);
pthread_mutex_lock(&lock);
sum[st->j]=sum[st->j]+sum[st->j-st->i];
pthread_mutex_unlock(&lock);
pthread_barrier_wait(&barrier);
pthread_exit(NULL);
}
int main()
{
pthread_attr_t tattr;
int i,j,k,l,count=0,number;
pthread_attr_init(&tattr);
pthread_attr_setscope(&tattr,PTHREAD_SCOPE_SYSTEM);
FILE *ftp=fopen("data","r+");
while(fscanf(ftp, "%d",&number) != EOF)
{
count++;
}
rewind(ftp);
printf("count = %d",count);
arr=(int*)malloc(sizeof(int)*count);
for(i=0;i { fscanf(ftp, "%d",&arr[i]); } for(j=0;j { printf("%d ",arr[j]); sum[j]=arr[j]; } printf(" "); for(i=0;i { if(pthread_barrier_init(&barrier,NULL,(int)(count-(pow(2,i))))!=0) { printf(" barrier init has failed "); return 1; } pthread_t tid[(int)(count-(pow(2,i)))]; printf(" i=%d ",i); for(j=count-1;j>=pow(2,i);j--) { struct index st; pthread_mutex_init(&lock, NULL) ; st.i=pow(2,i); st.j=j; int r=pthread_create(&tid[j],&tattr,sum_func,(void*)&st); printf(" %d %d : ",st.i,st.j); if(r!=0) { printf(" thread creation has failed "); return 1; } else printf("thread %d created ",j); } //pthread_attr_destroy(&tattr); for(k=0;k pthread_join(tid[k],NULL); for(l=0;l { printf("%d ",sum[l]); } printf(" "); pthread_mutex_destroy(&lock); pthread_barrier_destroy(&barrier); //pthread_exit(NULL); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started