So I am writing a program in C that creates 4 threads that produce/consume from buffers. I initialized all the threads in my main function but I am getting the following error. Does anyone know why? I ran it on my local zsh shell on macOS and it works fine. But when I try running it on my school's server, I think it's linux with bash, it gives me the errors.
flip1 ~/CS344/assignment4 1022$ gcc -std=gnu99 -o line-processor line_processor2.c
/tmp/ccYF7Kqe.o: In function `main':
line_processor2.c:(.text+0x7b5): undefined reference to `pthread_create'
line_processor2.c:(.text+0x7d9): undefined reference to `pthread_create'
line_processor2.c:(.text+0x7fd): undefined reference to `pthread_create'
line_processor2.c:(.text+0x821): undefined reference to `pthread_create'
line_processor2.c:(.text+0x832): undefined reference to `pthread_join'
line_processor2.c:(.text+0x843): undefined reference to `pthread_join'
line_processor2.c:(.text+0x854): undefined reference to `pthread_join'
line_processor2.c:(.text+0x865): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Below is my main function
int main()
{
pthread_t inputThread, lineSeparatorThread, plusSignThread, outputThread;
pthread_attr_t attr;
// Set up Sentinal Values at the begining of each buffer to indicate whether or not
// the buffer line has been read or not
for (int i = 0; i < BUFSIZE; i++)
{
buffer1[i][0] = -1;
buffer2[i][0] = -1;
buffer3[i][0] = -1;
}
// Initialize a pthread attribute structure to set up joinable threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Initialize mutex and condition variables
pthread_mutex_init(&mutex1, NULL);
pthread_mutex_init(&mutex2, NULL);
pthread_mutex_init(&mutex3, NULL);
pthread_cond_init(&readyBuffer1, NULL);
pthread_cond_init(&readyBuffer2, NULL);
pthread_cond_init(&readyBuffer3, NULL);
// Set up the thread in reverse order so that the readers/consumers will pend
// waiting for the writers/consumers to start up
pthread_create(&outputThread, &attr, output_thread, NULL);
usleep(100); // Force the program to allow output thread to actually come up and pend on readyBuffer 3 first
pthread_create(&plusSignThread, &attr, plus_sign_thread, NULL);
usleep(100); // Force the program to allow plus_sign_thread thread to actually come up first
pthread_create(&lineSeparatorThread, &attr, line_separator_thread, NULL);
usleep(100); // Force the program to allow line_separator_thread thread to actually come up first
pthread_create(&inputThread, &attr, input_thread, NULL);
pthread_join(inputThread, NULL);
pthread_join(lineSeparatorThread, NULL);
pthread_join(plusSignThread, NULL);
pthread_join(outputThread, NULL);
// Freeing up memory.
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_mutex_destroy(&mutex3);
pthread_cond_destroy(&readyBuffer1);
pthread_cond_destroy(&readyBuffer2);
pthread_cond_destroy(&readyBuffer3);
return 0;
}
And lastly, my #include statements and buffer variables.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <ctype.h>
#define BUFSIZE 50
// Buffers can hold up to 50 lines that can be 1000 characters.
char buffer1[BUFSIZE][1000]; //inputThread + lineSeparatorThread
char buffer2[BUFSIZE][1000]; //lineSeparatorThread + plus_sign_thread
char buffer3[BUFSIZE][1000]; //plus_sign_thread + output_thread