1

I have program and I want it to create a thread, thread 'a', I want it to wait for input from a file to be read into a buffer, buffer 1, then execute its function and put the answer to that question in a second buffer, buffer 2. At the moment the thread waits but it never executes and I can't figure out why. Could it be that the parent thread is exiting before thread 'a' has a chance to execute? If this is the case how do I stop the parent thread from exiting? If it is not the case could I have some pointers on what I am doing wrong The code is below.

EDIT: This is a homework assignment and it has to be done in this manner as opposed to reading in the input first or having the thread read the input.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "LinkedList.h"
#include "macros.h"
#include "FileReading.h"

pthread_mutex_t lock;
pthread_cond_t cond;

int returnSeekTime(int curData, int nextData)
{
    int seekTime;

    if (curData > nextData)
    {
        seekTime = curData - nextData;
    }
    else
    {
        seekTime = nextData - curData;
    }
    return seekTime;
}

void* firstComeFirstServed(void* data)
{
    Buffers* buffers = (Buffers*)data;
    Buffer1 buffer1 = buffers->buffer1;
    Buffer2 buffer2 = buffers->buffer2;
    int curData, nextData;
    Node* current = current = buffer1.disks->head;

    pthread_cond_wait(&cond, &lock);
    buffer2.seekTime += returnSeekTime(buffer1.secondDisk, current->data);


    while(current != NULL && current != buffer1.disks->tail)
    {
        curData = current->data;
        nextData = current->next->data;

        buffer2.seekTime += returnSeekTime(curData, nextData);
        current = current->next;
    }

    return NULL;
}

int main (void)
{
    char fileName[11] = "input.txt\0";
    pthread_t a;
    Buffers* buffers = (Buffers*)malloc(sizeof(Buffers));
    buffers->buffer1.disks = createLinkedList();
    
    pthread_create(&a, NULL, firstComeFirstServed, (void*)buffers);
    readInputFile(fileName, &buffers->buffer1);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&lock);

    
    
    return 0;
}
evie
  • 73
  • 1
  • 8
  • 1
    Why not read the input in the thread? – Some programmer dude Apr 28 '22 at 06:13
  • 1
    On another couple of notes (unrelated to your problem): First of all in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc); Secondly there's no need for dynamic allocation at all here, just use the pointer-to operator `&` when passing a pointer to the structure; Thirdly, all literal strings in your code will already have a null-terminator, you don't need to add an extra. – Some programmer dude Apr 28 '22 at 06:15
  • I have to do it in this way because it was set as a homework assignment. It's just whats outlined in the project. Thank you for your tips. I'll add them in. – evie Apr 28 '22 at 06:16
  • 2
    I'm not sure that the lock and condition variable will be properly initialized unless you explicitly does it. You also don't lock the lock. And you don't wait for the thread to finish before you exit the process (don't forget to `join` the main thread). – Some programmer dude Apr 28 '22 at 06:20
  • 1
    Also, with `Buffer2 buffer2 = buffers->buffer2;` you create a *copy* of `buffers->buffer2`. Any change you make to your local variable `buffer2` will not be copied back to `buffers->buffer2`. You also don't seem to be initializing `buffers->buffer2`. Or all of `buffers->buffer1`. Since `buffers->buffer2` is uninitialized its contents will be *indeterminate* (and seemingly garbage) so `buffer2.seekTime += anything` will result in more garbage. – Some programmer dude Apr 28 '22 at 06:23
  • I will initiliase the buffers appropriately and remove the copy of buffer2. – evie Apr 28 '22 at 06:47

0 Answers0