Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:13:23+00:00 2026-06-17T23:13:23+00:00

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 2 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; char

  • 0
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS     2

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

char **AddToStack(char *buffer)
{
  static char **stack = NULL;
  int stacksize = 0;

  if (buffer == NULL)
    return stack;

  if (stack == NULL){
    stack = calloc(1, sizeof(char*));
  }
  stack[stacksize] = strdup(buffer);
  stacksize++;
  stack = realloc(stack, (stacksize+1) * sizeof(char*));
  stack[stacksize] = NULL;
  return stack;
}

void *FRead(void *threadid)
{
pthread_mutex_lock(&mutex);
  char fname[256], buffer[256];
  FILE *ifile;
  long tid;
  int counter;
  tid = (long)threadid;

  sprintf(fname, "data%ld", tid);
  if ((ifile = fopen(fname, "r")) == NULL){
    printf("Error: Thread #%ld was unable to open file %s!\n", tid, fname);
  }
  else{
    printf("Thread #%ld starting to read from file %s!\n", tid, fname);
    fscanf(ifile, "%s", buffer);
    counter = 0;
 while (!feof(ifile)){
//pthread_mutex_lock(&mutex);  
    counter++;
      AddToStack(buffer);
      fscanf(ifile, "%s", buffer);
//pthread_mutex_unlock(&mutex);
    }
    fclose(ifile);
    printf("Thread #%ld added %d entries to the stack!\n", tid, counter);
pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
  }
  return NULL;
}

int main(int argc, char *argv[])
{
  pthread_t threads[NUM_THREADS];
  int rc, i = 0;
  long t;
  char **stack;
  FILE *ofile;

  for(t=0;t<NUM_THREADS;t++){
    printf("In main: creating thread %ld\n", t);
    rc = pthread_create(&threads[t], NULL, FRead, (void *)t);
    if (rc){
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }


for(t=0;t<NUM_THREADS;t++)
{
//pthread_join(threads[t],NULL);
}


  //Lets write the content of the stack into a file
  stack = AddToStack(NULL); //Get the base of the stack pointer
  if (stack != NULL){
    ofile = fopen("result.dat", "w");
    for (i = 0; stack[i] != NULL; i++)
      fprintf(ofile, "%s\n", stack[i]);
    fclose(ofile);
  }
  fprintf(stderr, "%d data written to file result.dat\n", i);
  pthread_exit(NULL);
  return 0;
}

This is my data1 and data2 files

data1:
abandons
abase
abased
abases
abash
abashed
abashes
abashing
abasing
abate
abated
abatement
abates
abating
abattoir
abbey
abbort
abbreviate
abbreviated
abbreviates
abbreviating
abbreviation
abbreviations
abdicate
abdicated
abdicates
abdicating
abdomen
abduct
abducted

data2 :

God
I
I'll
I'm
I've
Miss
Worry
a
ability
able
aboard
about
above
abroad
absence
absent
absolute
accident
accidentally
according
accordingly
account
acquaintance
across
act
action
active
activity
actual
admiration
admission
adress
advance
advantage
adventure

Above is my mutex code, but i wonder why i only can write 1 data to the file result.dat

This is my output

1 data written to file result.dat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:13:24+00:00Added an answer on June 17, 2026 at 11:13 pm

    To start with, pthread_mutex_unlock(&mutex);pthread_exit(NULL); location is incorrect. Place this before return NULL statement.

    There were couple of errors in the code. Please find below the updated code.

        #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NUM_THREADS     2
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    char **AddToStack(char *buffer)
    {
      static char **stack = NULL;
      static int stacksize = 0;
    
      if (buffer == NULL)
        return stack;
    
      if (stack == NULL){
        stack = (char**)calloc(1, sizeof(char*));
      }
      stack[stacksize] = strdup(buffer);
      stacksize++;
      stack = (char**)realloc(stack, (stacksize+1) * sizeof(char*));
      stack[stacksize] = NULL;
      return stack;
    }
    
    void *FRead(void *threadid)
    {
      pthread_mutex_lock(&mutex);
      char fname[256], buffer[256];
      FILE *ifile;
      long tid;
      int counter;
      tid = (long)threadid;
    
      sprintf(fname, "data%ld", tid);
      if ((ifile = fopen(fname, "r")) == NULL){
        printf("Error: Thread #%ld was unable to open file %s!\n", tid, fname);
      }
      else{
        printf("Thread #%ld starting to read from file %s!\n", tid, fname);
        fscanf(ifile, "%s", buffer);
        counter = 0;
        while (!feof(ifile)){
          //pthread_mutex_lock(&mutex);  
          counter++;
          AddToStack(buffer);
          fscanf(ifile, "%s", buffer);
          //pthread_mutex_unlock(&mutex);
        }
        fclose(ifile);
        printf("Thread #%ld added %d entries to the stack!\n", tid, counter);
        //pthread_mutex_unlock(&mutex);
        //pthread_exit(NULL);
      }
      pthread_mutex_unlock(&mutex);
      pthread_exit(NULL);
      return NULL;
    }
    
    int main(int argc, char *argv[])
    {
      pthread_t threads[NUM_THREADS];
      int rc, i = 0;
      long t;
      char **stack;
      FILE *ofile;
    
      for(t=0;t<NUM_THREADS;t++){
        printf("In main: creating thread %ld\n", t);
        rc = pthread_create(&threads[t], NULL, FRead, (void *)t);
        if (rc){
          printf("ERROR; return code from pthread_create() is %d\n", rc);
          exit(-1);
        }
      }
    
    
      for(t=0;t<NUM_THREADS;t++)
      {
        //pthread_join(threads[t],NULL);
      }
    
    
      //Lets write the content of the stack into a file
      stack = AddToStack(NULL); //Get the base of the stack pointer
      if (stack != NULL){
        ofile = fopen("result.dat", "w");
        for (i = 0; stack[i] != NULL; i++)
          fprintf(ofile, "%s\n", stack[i]);
        fclose(ofile);
      }
      fprintf(stderr, "%d data written to file result.dat\n", i);
      pthread_exit(NULL);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <dirent.h> pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER, mut2 =
Here is the code #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> typedef struct std_thread { char name[20];
I have the following code: #include <stdio.h> #include <pthread.h> #define THREAD_CNT 10 #define ITER
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
I have this code : #include <iostream> #include <stdio.h> #include <pthread.h> #include <stdlib.h> using
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <vector> #include <string> #include <iostream> FILE* fp;
I written this code: #include <stdio.h> #include <pthread.h> void* cuoco(void* arg) { fprintf(stderr,Inizio codice
fifo.3 source code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include
Given this code : #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.