Using a mutex to read from a file I am new to C and I am trying to implement a mutex.

The idea of the program is:


main() will create three threads.
Each thread will read one character each from individual files in serial, i.e. thread 1 will read one character store in global and then wait for thread 2 &amp; 3 to complete the same tasks, until the end of the file.
main() will then write all the data from single character global to output file.




#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;

pthread_t tid[3];
pthread_mutex_t lock;

char characters[1024] = "";

readFromFile(int fileNumber)
{
    pthread_mutex_lock(&amp;lock);

    char* firstPart = "file";
    char *secondPart = ".in";

    char fileN[1];

    sprintf(fileN, "%d", fileNumber);



    char * fileName = malloc(1 + strlen(firstPart) + strlen(fileN) + strlen(secondPart));
    strcpy(fileName, firstPart);
    strcat(fileName, fileN);
    strcat(fileName, secondPart);

    FILE *fp;
    int c;

    if (!(fp = fopen(fileName, "rt"))) 
    {
         printf("\nError Opening File\n");
         exit(1);
    }
    while ((c = fgetc(fp)) != EOF) 
    {
         strcat(characters, c);
    }
   fclose(fp);

   pthread_mutex_unlock(&amp;lock);
}

int main(int argc, char** argv) {
    int i = 0;
    int err;

    if (pthread_mutex_init(&amp;lock, NULL) != 0) {
        printf("\n Mutex Init Failed\n");
        return 1;
    }

    while (i &lt; 3) {
        err = pthread_create(&amp;(tid[i]), NULL, readFromFile(i), NULL);
        if (err != 0) {
            printf("\nCan't Create thread :[%s]", strerror(err));
        }
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_join(tid[2], NULL);
    pthread_mutex_destroy(&amp;lock);

    return 0;
}
Code broken

Your code doesn't work for several reasons:


You are not passing a function to pthread_create():

err = pthread_create(&amp;(tid[i]), NULL, readFromFile(i), NULL);


Here you are calling the function readFromFile() instead of passing a function pointer to pthread_create().  So you aren't using a thread at all.  Worse than that, when readFromFile() returns, a random value will be passed to pthread_create() because readFromFile() doesn't actually return any particular value.  This will cause the newly created thread to crash.
Even had you done that part correctly, your readFromFile() function doesn't do what you wanted.  It looks like you wanted each thread to read one character and then let another thread read the next character.  But what your function actually does is read the whole file instead of one character.
You are calling strcat() with a character as the second argument instead of a string.  This will cause a crash if the program ever reaches that line.


Pay attention to warnings

If you had built with full warnings, you would have been alerted to mistakes #1 and #3 I listed above.

Implementing a mutex?

The title of the question is misleading.  You aren't implementing a mutex.  You are trying to use one.