PThread Segmentation Fault, Incrementor/Decrementor

PThread Segmentation Fault, Incrementor/Decrementor



I keep getting a Seg Fault, im assuming its trying to access past DECREMENTORS 4 that doesn't exit.



I added in a couple of print statements to help narrow the problem.



What im trying to create is a simple increment, decrement program of a threads and variables.



I've also thought about using a struct to store the variables however im unsure if that would be a good idea or not.


/* readerswriters.c */
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

#define READERS 10
#define INCREMENTORS 5
#define DECREMENTORS 4

sem_t s;
int sum = 0;
int counter = 0;
int total = 0;

void *incrementorfunc(void *arg)

int *me;

me = (int *)arg;
sem_wait(&s);
sum++;
printf("Incrementor %d set sum = %dn", *me, sum);
sem_post(&s);

pthread_exit(NULL);



void *decrementorfunc(void *arg)


int *me;
me = (int *) arg;

//Problem is in here i believe
sem_wait(&s);
sum--;
printf("Decrementor %d set sum = %dn", *me, sum);
sem_post(&s);

//printf("hellon");
pthread_exit(NULL);



int main(void)

int i;
pthread_t iID[INCREMENTORS];
int increID[INCREMENTORS];

pthread_t dID[DECREMENTORS];
int decreID[DECREMENTORS];

counter = 0;

for(i = 0; i < INCREMENTORS; i++)
increID[i] = i;

for(i = 0; i < DECREMENTORS; i++)
decreID[i] = i;

sem_init(&s, 0, 1);

for(i = 0; i < INCREMENTORS; i++)
pthread_create(&iID[i], NULL, incrementorfunc, (void *)&increID[i]);

for(i = 0; i < DECREMENTORS; i++)
pthread_create(&dID[i], NULL, decrementorfunc, (void *)&decreID[i]);


for(i = 0; i< INCREMENTORS; i++)
pthread_join(increID[i], NULL);


for(i = 0; i < DECREMENTORS; i++)
pthread_join(decreID[i], NULL);


return 0;





You are passing the thread's data instead of the thread ID to pthread_join(). For example, pthread_join(increID[i], NULL) should be pthread_join(iID[i], NULL);
– Michael Burr
Aug 21 at 4:27



pthread_join()


pthread_join(increID[i], NULL)


pthread_join(iID[i], NULL);





Oh Wow thankyou :D
– Dreeww
Aug 21 at 4:29









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj