C pthread and struct questions - c

Basic function of this code is to get number of counter and thread, creates the counters then create the threads then get numbers of instructions in each thread (format of instruction [counter] [work-function] [repetition] )
/* ============================================================================
* File-global variables
* ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;
static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;
struct counter {
long long counter; /* to store counter */
};
/* counter value */
struct instruction {
struct counter *counter; /* pointer to counter */
int repetitions; /* number of repetitions */
void (*work_fn)(long long *); /* function pointer to work function */
};
/* ============================================================================
* Thread function
* ========================================================================== */
static void *
worker_thread(void *arg) {
(void)arg;
int Tcounter;
int Trepetition;
char Tfuntion;
scanf(" %d %c %d", &Tcounter, &Tfuntion, &Trepetition);
How do I actually store this three variable using struct instruction **instructions???
return NULL;
}
/* ============================================================================
* Main function
* ========================================================================== */
int
main(void) {
scanf(" %d", &ncounters);
int i;
if(counters = malloc(sizeof(struct counter)*ncounters)){
for( i=0; i < ncounters ;i++){
counters[i].counter = 0;
}
for( i=0; i < ncounters ;i++){
printf("%lld\n", counters[i].counter);
}
}
scanf(" %d", &nthreads);
pthread_t threads[nthreads];
int ninst;
for( i=0; i < nthreads ;i++){
scanf(" %d", &ninst);
ninstructions = ninst;
for( i=0; i < ninstructions ;i++){
pthread_create(&threads[i], NULL, worker_thread, NULL);
}
}
free(counters);
return 0;
}
Is the pthread_create function correct?
int
main(void) {
scanf(" %d", &ncounters);
int i;
if(counters = malloc(sizeof(struct counter)*ncounters)){
for( i=0; i < ncounters ;i++){
counters[i].counter = 0;
}
}
scanf(" %d", &nthreads);
pthread_t threads[nthreads];
if(ninstructions = (int*)malloc(nthreads*sizeof(int)){
for( i=0; i < nthreads ;i++){
scanf(" %d", &ninstructions[i]);
int j;
for(j=0; i < ninstructions[i] ;j++){
pthread_create(&threads[j], NULL, worker_thread, NULL);
}
}
}
free(ninstructions);
free(counters);
return 0;
}
I also tried to change the ninstruction into array, if its correct...

OK, next attempt. This time in pseudo code since this smells like homework...
struct instruction
{
long long counter;
int repetitions
};
main()
{
ask #threads
pthread_t threads[nthreads];
struct instruction intructions[nthreads];
while( i < nthreads )
{
pthread_create( &threads[i], NULL, threadMain, &instructions[i] );
}
i = 0
while ( i < nthreads )
{
//now wait until all threads have finished
pthread_join( threads[i], NULL );
}
}
void threadMain( void* arg )
{
struct instruction* pInstruction = (struct instruction*)arg;
char cFunctionCode;
enter protected section, otherwise all threads will ask at the same time
scanf(" %d %c %d", &pInstruction->counter, &cFunctionCode, &pInstruction->repetitions
leave protected section here
do youtr computes here
return;
)
For protected sections, look up mutex or semaphore.

OK. I assume you want to do the following:
...
//create the instructions, which is missing.
// be carefull that instruction.counter is not allocated!!!
struct instruction instructions[ncounters];
...
pthread_create( &threads[i], NULL, worker_thread, &instructions[i] );
...
And in the worker thread
worker_thread( void* arg )
{
struct instruction* pInstruction = (struct instruction*)arg;
pInstruction->repetions = 42;
...
I hope this is what you wanted...
Mario

Related

Multiple threads to find prime factors of integers, segmentation fault

I can't figure out what I am doing wrong with my pointers. It is causing a segmentation fault. I am convinced the problem is rooted in my use of the array of pointers I have and the pthread_join I am using.
The goal is to read multiple integers into a gcc compiler, then print out the integer with all its factors, like this, 12: 2 2 3
I created a struct containing an int array to store the factors of each integer as the factor function pulls it apart and a counter(numfact) to store how many factors there are stored in the array.
I commented out the section at the bottom that prints out the factors.
I think the problem is how I am trying to store the output from the pthread_join in the pointer array, ptr[]. Whenever I comment it out, it does not get the segmentation error.
Either I have my pointers screwed up in a way I don't understand or I can't use an array of pointers. Either way, after many hours, I am stuck.
Please help.
#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <stdlib.h>
struct intfact
{
long int factors[100];
int numfact;
};
struct intfact *factor(long int y)
{
struct intfact threadfact;
threadfact.numfact = 0;
// Store in struct the number of 2s that divide y
while (y % 2 == 0)
{
threadfact.factors[threadfact.numfact] = 2;
threadfact.numfact++;
y = y/2;
}
// Store in struct the odds that divide y
for (int i = 3; i <= floor(sqrt(y)); i = i+2)
{
while (y % i == 0)
{
threadfact.factors[threadfact.numfact] = i;
threadfact.numfact++;
y = y/i;
}
}
// Store in struct the primes > 2
if (y > 2)
{
threadfact.factors[threadfact.numfact] = y;
threadfact.numfact++;
}
struct intfact *rtnthred = &threadfact;
return rtnthred;
}
/* Trial Division Function */
void *divde(void *n)
{
long int *num = (long int *) n;
struct intfact *temp = factor(*num);
return temp;
}
/* Main Function */
int main(int argc, char *argv[])
{
pthread_t threads[argc-1];
void *ptr[argc-1];
/* loop to create all threads */
for(int i=0; i < argc; i++)
{
long temp = atol(argv[i+1]);
pthread_create(&threads[i], NULL, divde, (void *) temp);
}
/* loop to join all threads */
for(int i=0; i < argc; i++)
{
pthread_join(threads[i],(void *) ptr[i]); //THIS POINTER IS THE PROBLEM
}
/* loops to print results of each thread using pointer array*/
//for(int i = 0; i < argc; i++)
//{
// printf("%s: ", argv[i+1]); /* print out initial integer */
// struct intfact *temp = (struct intfact *) ptr[i]; //cast void pointer ptr as struct intfact pointer
// printf("%d", temp->numfact);
//for(int j = 0; j < temp->numfact; j++) /*(pull the numfact(count of factors) from the struct intfact pointer??)*/
//{
// printf("%d ", temp->factors[j]); /* print out each factor from thread struct */
//}
}
}
In my Linux) terminal this code is stored in p3.c
"./p3 12" should yeild "12: 2 2 3"
For starters:
Here
long temp = atol(argv[i+1]);
pthread_create(&threads[i], NULL, divde, (void *) temp);
you define a long int and pass it as argument to the thread. For example 12
Inside the thread function then
void *divde(void *n)
{
long int *num = (long int *) n;
you treat the long int passed in as pointer to long int.
And then here dereference it
... = factor(*num);
So this *num for example would become *12. That is referencing memory address 12 to read out its content and pass it to factor). Aside the fact that this mostly likely is an invalid address, there would be nothing relevant store, at least nothing your code defined.
To (more or less fix) this do
void *divde(void *n)
{
long int num = (long int) n;
... = factor(num);
The second issues is mentioned in the comment: Multiple threads to find prime factors of integers, segmentation fault
The problem you are trying to solve is a special case of parallel programming, namely that the tasks to be run in parallel are completely independent. In such cases it makes sense to give each task its own context. Here such a context would include the
thread-id,
the thread specific input
as well as its specific output.
In C grouping variables can be done using structures, as your implementation already comes up with for the output of the tasks:
struct intfact
{
long int factors[100];
int numfact;
};
So what is missing is thread-id and input. Just add those for example like this.
/* group input and output: */
struct inout
{
long int input;
struct intfact output;
};
/* group input/output with thread-id */
struct context
{
pthread_t thread_id;
struct inout io;
};
Now before kicking off the threads define as many contexts as needed:
int main(int argc, char *argv[])
{
size_t num_to_process = argv - 1;
struct context ctx[num_to_process];
then create the threads passing in what is needed, that is input along with space/memory for the output:
for (size_t i = 0; i < num_to_process ; i++)
{
ctx[i].io.input = atol(argv[i]);
pthread_create(&ctx[i].thread_id, NULL, divide, &ctx[i].io);
}
Inside the thread function convert the void-pointer received back to its real type:
void *divide(void * pv)
{
struct inout * pio = pv; /* No cast needed in C. */
Define the processing function to take a pointer to the context specific input/output variables:
void factor(struct inout * pio) /* No need to return any thing */
{
/* Initialise the output: */
pio->output.numfact = 0;
/* set local copy of input: */
long int y = pio->input; /* One could also just use pio->input directly. */
Replace all other occurrences of threadfact by pio->output.
Use
return;
}
to leave the processing function.
Then inside the thread function call the processing function:
factor(pio);
Use
return NULL;
}
to leave the thread function.
In main() join without expecting any result from the threads:
/* loop to join all threads */
for (size_t i = 0; i < num_to_process; i++)
{
pthread_join(ctx[i].thread_id, NULL);
}
Putting this all together:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <math.h>
struct intfact
{
long int factors[100];
size_t numfact;
};
/* group input and output: */
struct inout
{
long int input;
struct intfact output;
};
/* group input/output with thread-id */
struct context
{
pthread_t thread_id;
struct inout io;
};
void factor(struct inout * pio)
{
/* Initialise the output: */
pio->output.numfact = 0;
/* set local copy of input: */
long int y = pio->input; /* One could also just use pinout->input directly. */
if (0 == y)
{
return; /* Nothing to do! */
}
// Store in struct the number of 2s that divide y
while (y % 2 == 0)
{
pio->output.factors[pio->output.numfact] = 2;
pio->output.numfact++;
y = y/2;
}
// Store in struct the odds that divide y
for (int i = 3; i <= floor(sqrt(y)); i = i+2)
{
while (y % i == 0)
{
pio->output.factors[pio->output.numfact] = i;
pio->output.numfact++;
y = y/i;
}
}
// Store in struct the primes > 2
if (y > 2)
{
pio->output.factors[pio->output.numfact] = y;
pio->output.numfact++;
}
return;
}
void *divide(void * pv)
{
struct inout * pio = pv; /* No cast needed in C. */
factor(pio);
return NULL;
}
int main(int argc, char *argv[])
{
size_t num_to_process = argc - 1;
struct context ctx[num_to_process];
for (size_t i = 0; i < num_to_process; i++)
{
ctx[i].io.input = atol(argv[i+1]);
if (!ctx[i].io.input)
{
fprintf(stderr, "COnversion to integer failed or 0 for '%s'\n", argv[i]);
}
pthread_create(&ctx[i].thread_id, NULL, divide, &ctx[i].io);
}
/* loop to join all threads */
for (size_t i=0; i < num_to_process; i++)
{
pthread_join(ctx[i].thread_id, NULL);
}
/* loops to print results of each thread using pointer array*/
for(size_t i = 0; i < num_to_process; i++)
{
printf("%ld: ", ctx[i].io.input); /* print out initial integer */
printf("%zu factors --> ", ctx[i].io.output.numfact);
for(size_t j = 0; j < ctx[i].io.output.numfact; j++)
{
printf("%ld ", ctx[i].io.output.factors[j]); /* print out each factor from thread struct */
}
putc('\n', stdout);
}
}

double free or corruption (!prev) in c , using thread, malloc

I'm tired about this problem. I use valgrind also. but I don't know why. Please find what is the problem in my code.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static pthread_t *tid=NULL;
static int **data3=NULL;
typedef struct _Thdata
{
int *data;
int size;
int nthread;
} Thdata;
Thdata *tdata=NULL;
void *bubble(void *d){
Thdata *arr =(Thdata *)d;
int i,j,tmp;
int n=arr->size;
printf("thread #=%d n=%d\n",arr->nthread,n);
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if((arr->data[j])>(arr->data[j+1]))
{
tmp = (arr->data[j]);
(arr->data[j])=(arr->data[j+1]);
(arr->data[j+1])=tmp;
}
}
}
for(j=0;j<n;j++)
printf("%d ",(arr->data[j]));
printf("\n");
pthread_exit((void *)1);
}
int main(int argc, char **argv){
FILE * fd;
int i,j;
int data[100];
int tcount = atoi(argv[1]);
int n = 100/tcount;
int err;
void *b;
//dynamic data
tid = (pthread_t *)malloc(tcount* sizeof(pthread_t));
data3 = (int **)malloc(tcount *sizeof(int*));
for( i=0; i<tcount; i++)
data3[i] = (int *)malloc((100/tcount) *sizeof(int));
tdata = (Thdata *)malloc(tcount*sizeof(Thdata));
for(i=0;i<tcount; i++) {
tdata[i].data =(int *)malloc(n*sizeof(int));
}
//dynamic data end
fd = fopen("data.txt", "r");
printf("tcount = %d n=%d\n",tcount,n);
// origin data
for(i =0; i<100;i++)
{
fscanf(fd, "%d",&data[i]);
printf("%d ", data[i]);
}
printf("\n");
for(j=0;j<tcount;j++){
for(i=0;i<n;i++){
data3[j][i]=data[n*j+i];
printf("%d ",data3[j][i]);
//tdata[j].data[i]=data[j][i];
}
printf("\n");
tdata[j].data=data3[j];
tdata[j].size=n;
tdata[j].nthread=0;
}
for(j=0;j<tcount;j++){
for(i=0;i<n;i++){
printf("%d ",tdata[j].data[i]);
}
printf("tdata[%d].size = %d",j,tdata[j].size);
printf("\n");
}
for(i =0; i<tcount;i++)
{
err=pthread_create(&tid[i],NULL,bubble,(void *)&tdata[i]);
if(err != 0)
printf("creat thread error");
tdata[i].nthread=i;
}
for(i=0;i<tcount;i++)
pthread_join(tid[i],&b);
for(i=tcount-1;i>=0;i--){
free(tdata[i].data);
}
free(tdata);
for(int i=tcount-1; i>=0; i--)
free(data3[i]);
free(data3);
free(tid);
fclose(fd);
return 0;
}
You assigned data3[j] to tdata[j].data as
tdata[j].data=data3[j];
so passing both of them to free() will cause double-free error as you said.
If you want to only copy pointers and copying values in data3[j] isn't needed, remove the part
for(i=0;i<tcount; i++) {
tdata[i].data =(int *)malloc(n*sizeof(int));
}
because the variable tdata[i].data will be overwritten later and memory leak will be caused. Also remove the part
for(i=tcount-1;i>=0;i--){
free(tdata[i].data);
}
because it will cause double-free error as descrived above.

Segmentation Fault on PThread Malloc

First let me start by posting the code:
#include <pthread.h>
#include<stdlib.h>
//64 BITS -ALL 32 bit Arch
//32 BIT - UNIX 64 bit arch
//64 BIT - WINDOWS 64 bit arch
long long sum = 0;
static enum turn
{
PING,
PONG
}def;
struct threads_util
{
pthread_t *t_id;
pthread_attr_t *attr;
void (*init_attr)(pthread_attr_t *);
};
void init_attr_fn(pthread_attr_t *attr)
{
pthread_attr_init(&attr);
}
void* sum_runner(void* arg)
{
long long* limit_ptr = (long long *) arg;
long long limit = *limit_ptr;//Derefrencing
for(long long i=0; i<=limit; i++)
sum += i;
printf("Sum is %lld \n", sum);
pthread_exit(0);
}
void ping()
{
puts("Ping");
def = PONG;
}
void pong()
{
puts("Pong");
def = PING;
}
pthread_t * all_thread(pthread_t *t_id)
{
t_id = malloc(sizeof(pthread_t));
return t_id;
}
int main(int argc, char **argv)
{
if(argc<2)
{
puts("Usage ./objFile <num 1> <num 2> .. <num n>");
exit(1);
}
int args = argc-1;
long long limit = atoll(argv[1]);
def = PING;
struct threads_util *threads[args];
for (int i=0; i<args; i++)
threads[i]->t_id = all_thread(threads[i]->t_id);
puts("In");
for(int i=0; i<args; i++)
{
threads[i]->init_attr = init_attr_fn;
threads[i]->init_attr(threads[i]->attr);
pthread_create(threads[i]->t_id,threads[i]->attr,sum_runner,&limit);
}
//Begin -Main Functions
for(int i=0; i<= 10; i++)
{
if(def == PING)
ping();
else if(def == PONG)
pong();
else
puts("UNKOWN PI-PO");
}
//End - Main Functions
for(int i=0; i<args; i++)
{
pthread_join(threads[i]->t_id,NULL);
}
}
You can see i have a puts("In"), in the main function, just after the for loop when i call all_thread args times. Well calling the function argc times with the for loop according to my debugging skills, is the problem. And also before we did all the allocation strategy, I had a problem on calling the thread function, of course resulting in a Segmentation Fault. threads[i]->init_attr(threads[i]->attr);. Help would be very much appreciated.
struct threads_util *threads[args];
means that you define an array of pointers to struct threads_util. But you don't actually create any struct threads_util so this line:
threads[i]->t_id = all_thread(threads[i]->t_id);
is illegal as it writes to memory not allocated.
You need to allocate memory for struct threads_util first:
for (int i=0; i<args; i++)
threads[i] = malloc(sizeof(struct threads_util));

Writing a parallel quick sort in c

I need to write a parallel quick sort in c using pthreads. This is what I did so far.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h> // sleep()
#include <stdio.h>
#include <stdlib.h> // EXIT_SUCCESS
#include <string.h> // strerror()
#include <errno.h>
#define SIZE_OF_DATASET 6
void* quickSort( void* data);
int partition( int* a, int, int);
struct info {
int start_index;
int* data_set;
int end_index;
};
int main(int argc, char **argv)
{
int a[] = { 7, 12, 1, -2,8,2};
pthread_t thread_id;
struct info *info = malloc(sizeof(struct info));
info->data_set=malloc(sizeof(int)*SIZE_OF_DATASET);
info->data_set=a;
info->start_index=0;
info->end_index=SIZE_OF_DATASET-1;
if (pthread_create(&thread_id, NULL, quickSort, info)) {
fprintf(stderr, "No threads for you.\n");
return 1;
}
pthread_join(thread_id, NULL);
printf("\n\nSorted array is: ");
int i;
for(i = 0; i < SIZE_OF_DATASET; ++i)
printf(" %d ", info->data_set[i]);
return 0;
}
void* quickSort( void *data)
{
struct info *info = data;
int j,l,r;
l = info->start_index;
r = info->end_index;
pthread_attr_t attr;
pthread_t thread_id1;
pthread_t thread_id2;
pthread_attr_init(&attr);
if( l < r )
{
j = partition( info->data_set, l, r);
info->start_index=l;
info->end_index=j-1;
if(info->end_index<0)info->end_index=0;
if (pthread_create(&thread_id1, NULL, quickSort, info)) {
fprintf(stderr, "No threads for you.\n");
return NULL;
}
info->start_index=j+1;
info->end_index=r;
if (pthread_create(&thread_id2, NULL, quickSort, info)) {
fprintf(stderr, "No threads for you.\n");
return NULL;
}
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
}
return NULL;
}
int partition( int* a, int l, int r) {
int pivot, i, j, t;
pivot = a[l];
i = l; j = r+1;
while( 1)
{
do ++i; while( a[i] <= pivot && i <= r );
do --j; while( a[j] > pivot );
if( i >= j ) break;
t = a[i]; a[i] = a[j]; a[j] = t;
}
t = a[l]; a[l] = a[j]; a[j] = t;
return j;
}
But inside quick sort function only call first thread only. Cant understand what is been happening here.
Note : serial version of code has been tested. no issue with that
UPDATE:
This is modified version based on John Bollinger's solution. But still second half of array which is taken by newly created thread inside quicksort is not sorted.
int main(int argc, char **argv)
{
int a[] = { 7, 12, 1, -2, 0, 15, 4, 11, 9,5,3,24,5,23,3,1,56,8,4,34,23,51};
struct info *info = malloc(sizeof(struct info));
info->data_set=malloc(sizeof(int)*SIZE_OF_DATASET);
info->data_set=a;
info->start_index=0;
info->end_index=SIZE_OF_DATASET-1;
quickSort(info);
printf("\n\nSorted array is: ");
int i;
for(i = 0; i < SIZE_OF_DATASET; ++i)
printf(" %d ", info->data_set[i]);
return 0;
}
void* quickSort( void *data)
{
struct info *info = data;
struct info *info1 = data;
int j,l,r;
l = info->start_index;
r = info->end_index;
pthread_attr_t attr;
pthread_t thread_id1;
pthread_attr_init(&attr);
if( l < r )
{
j = partition( info->data_set, l, r);
info1->start_index=j+1;
info1->end_index=r;
info1->data_set = info->data_set;
if(info1->end_index<0)info1->end_index=0;
if (pthread_create(&thread_id1, NULL, quickSort, info1)) {
fprintf(stderr, "No threads for you.\n");
return NULL;
}
info->start_index=l;
info->end_index=j-1;
if(info->end_index < 0) info->end_index = 0;
quickSort(info); /* don't care about the return value */
pthread_join(thread_id1, NULL);
}
return NULL;
}
The program is incorrect because your threads all share the same struct info structure describing the sub-problem they are supposed to be working on. They run concurrently (or may do, anyway) and they modify that structure as they proceed, so the values that any particular thread sees are indeterminate.
To resolve this, each quickSort frame must create at least one new struct info, so that the two quickSort() calls it makes in different threads each has its own. As a matter of efficiency, it would also be better to spawn only one additional thread in each quickSort() call. For example:
void* quickSort( void *data)
{
struct info *info = data;
struct info other_info;
/* ... */
/* launch a new thread to handle one partition: */
other_info.start_index = j + 1;
other_info.end_index = r;
other_info.data_set = info->data_set;
if (pthread_create(&thread_id1, NULL, quickSort, &other_info)) {
fprintf(stderr, "No threads for you.\n");
return NULL;
}
/* handle the other partition in the current thread: */
info->start_index = l;
info->end_index = j - 1;
if(info->end_index < 0) info->end_index = 0;
quickSort(info); /* don't care about the return value */
/* after this thread is done, wait for the other thread to finish, too */
pthread_join(thread_id1, NULL);
/* ... */
}
Note that this does not ensure that any particular pair of threads runs concurrently, neither in a multi-core sense nor in a time-slicing sense. That's up to the OS. Certainly, however, the multi-core sense of parallelism applies only where there are in fact multiple cores available on the host machine on which the OS is willing to schedule your process.

Creating Pthreads

I'm trying to create a thread for the function *producer, but the line for creating the thread is showing in error. I starred the line, but I'm having trouble figuring out what is wrong with it...
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#define TOTALLOOPS 100 /*Num of loops run*/
#define NUMOFPAIRS 4 /*For each 1 it produces 1 consumer and 1 producer*/
typedef struct {
int q[NUMOFPAIRS];
int head;
int tail;
int full;
int empty;
pthread_mutex_t mut; /*Creates a mutex Lock*/
pthread_cond_t notFull; /*Creates conditional*/
}Queue;
int main(void)
{
Queue buf; /* Declare and initialize parts of struct */
buf.head = 0;
buf.tail = 0;
buf.full = 0;
buf.empty = 0;
pthread_mutex_init(&buf.mut, NULL);/*intitializes mutex for struct*/
//pthread_cond_init(&buf.nutFull, NULL);
pthread_t pro;
**pthread_create(&pro, NULL, producer, &buf);**
pthread_mutex_destroy(&buf.mut);
return 0;
}
void *producer(int x, Queue *buf){
int id = x;
int i;
for(i = 0; i < TOTALLOOPS; i++){
while(buf->full == 1){
//do nothing
}
mClock();
printf(" - Producer%d:\n", id);
}
}
void* consumer(int x, Queue *buf){
int id = x;
int i;
for(i = 0; i < TOTALLOOPS; i++){
while(buf->empty == 1){
//do nothing
}
mClock();
printf(" - Consumer%d:\n", id);
}
}
void addToQueue(Queue *buf, int x){
//Checks if empty flag is triggered, if so un triggers
if(buf->empty) buf->empty = 0;
buf->q[buf->tail] = x;
if(buf->tail == 3) buf->tail = 0; /*Resets to beginning if at end*/
else buf->tail += 1; /*else just moves to next*/
//Checks if full flag needs to be triggered, if so triggers
if(buf->tail == buf->head) buf->full = 1;
}
int removeFromQueue(Queue *buf){
int t; /*return value from queue*/
//Checks if full flag is triggered, if so un triggers
if(buf->full == 1)buf->full = 0;
t = buf->q[buf->head];
if(buf->head == 3) buf->head = 0; /*Resets to beginning if at end*/
else buf->head += 1; /*else just moves to next*/
//Checks if full flag needs to be triggered, if so triggers
if(buf->tail == buf->head) buf->empty = 1;
return t;
}
void mClock(){
struct timeval tv;
gettimeofday(&tv,NULL);
long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec;
printf("%u", time_in_micros);
}
You have to declare producer before the pthread_create call.
void *producer(int x, Queue *buf);
should show up first.
Likewise, mClock has to be declared first.
Also, the function should only take one argument
I don't see where you're calling the initializers for pthread_mutex_t mut and pthread_cond_t notFull in your structure anywhere.
PTHREAD_MUTEX
PTHREAD_COND
Also, you need to change the order of declaration of your functions, or add prototypes to the top of the file.

Resources