c - program spinning on pthread lock -
after banging head against wall few hours during exercise, stuck @ wall. first off, program designed find , print prime numbers between 1 , ceiling
, ceiling user input. design implement posix threads.
in program, runs until on 1 of later iterations in thread's method. when gets later iteration, steps line pthread_mutex_lock(lock);
, spins, forcing me kill ctrl+z. 2 input's i've been using 1
number of threads , 10
ceiling. flaw reproducible happens every time i've tried it. note: although code should able implement multiple threads, i'd working correctly 1 child thread before adding more.
#include <stdio.h> #include <stdlib.h> #include <pthread.h> int* numbermarker = null; int* buffer = null; int* checked = null; int pullposition = 0; int placeposition = 0; pthread_mutex_t* lock; int ceiling; /*this method places 1 of primes in buffer. offers safe way manage next value placed*/ void placevalue(int value){ buffer[placeposition] = value; placeposition++; } void* threadmethod(){ int i; int k; int l; while(1){ printf("pull %d number %d \n",pullposition, buffer[pullposition]); pthread_mutex_lock(lock); printf("flag\n"); l = buffer[pullposition]; pullposition++; printf("pulltwo %d number %d \n",pullposition, buffer[pullposition-1]); pthread_mutex_unlock(lock); for(k=l+1;k<=ceiling;k++){ if(k%l){ if(k%2){ checked[k]=1; placevalue(k); } } else{ numbermarker[k-1] = 1; } } int sum=0; for(i=0; i<ceiling; i++){ if(numbermarker[i]){ checked[i] = numbermarker[i]; } printf("checked|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|\n", checked[0], checked[1], checked[2], checked[3], checked[4], checked[5], checked[6], checked[7], checked[8], checked[9]); sum += checked[i]; printf("sum %d ceiling %d\n",sum,ceiling); } printf("number |%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|\n", numbermarker[0], numbermarker[1], numbermarker[2], numbermarker[3], numbermarker[4], numbermarker[5], numbermarker[6], numbermarker[7], numbermarker[8], numbermarker[9]); if(sum == ceiling){ return null; } } } int main() { int numthreads; int i; printf("enter number of threads: \n"); scanf("%d", &numthreads); printf("enter highest value check \n"); scanf("%d", &ceiling); /* hold 1's , 0's. 1 = number has been checked or confirmed not prime 0 = number possible prime idea behind these values next prime can identified 0 lowest index */ numbermarker = (int*)malloc(sizeof(int)*(ceiling)); checked = (int*)malloc(sizeof(int)*(ceiling)); /*this hold primes found*/ buffer = (int*)malloc(sizeof(int)*(ceiling)); /*allocate space lock*/ lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(lock,null); for(i=0; i<ceiling; i++){ if(i<1){ numbermarker[i] = 1; } else{ numbermarker[i] = 0; } checked[i]=0; buffer[i]=0; printf("%d \n",numbermarker[i]); } checked[0]=1; placevalue(2); printf("checked|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|\n", checked[0], checked[1], checked[2], checked[3], checked[4], checked[5], checked[6], checked[7], checked[8], checked[9]); pthread_t **tid = (pthread_t **) malloc(sizeof(pthread_t *) * numthreads); for(i=0;i<numthreads;i++){ tid[i] = (pthread_t *) malloc(sizeof(pthread_t)); } for(i=0;i<numthreads;i++){ if(pthread_create(tid[i], null, threadmethod, null)){ printf("could not create thread \n"); exit(-1); } } for(i=0;i<numthreads;i++){ if(pthread_join(*tid[i], null)){ printf("error joining thread \n"); exit(-1); } free(tid[i]); } free(tid); for(i=0;i<ceiling;i++){ if(numbermarker[i] == 0){ printf("%d sdfsddd \n", numbermarker[i]); printf("%d \n", i+1); } } free(buffer); free(numbermarker); buffer=null; numbermarker=null; return(0); }
i've tried code , in
void placevalue(int value) { buffer[placeposition] = value; placeposition++; }
placeposition
goes beyond size of buffer
. results in undefined behaviour, plausible outcome of trashing of mutex (which malloc()
ed right after buffer
).
on top of that, there's race condition placevalue()
. however, if you're using single worker thread, not (yet) running it.
Comments
Post a Comment