'How do I block the child threads while the parent thread waits for a user input?

I have a parent thread which creates child threads A,B,C,D,E,F. Each of these child threads runs a different disk scheduling algorithm and prints the result simultaneously. The task is to block these child threads while the parent thread waits for the user to input a file name, from which the disk scheduling data is parsed and read in order to be used in the disk scheduling algorithms for the child threads.

Currently my program first takes the user input before the creation of the child threads and therefor there is no need to block however my task is to block them.

#define N 71

typedef struct 
{
    int totalCylinders;
    int cylindersInReqQueue;
    int currentPos;
    int prevDiskRequest;
    int *requestQueueArr;
    char id;
    char *filename;
} disk_data;

sem_t semaphore;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex;

void *runner(void *args);
void runscheduler(char name, disk_data data);

void *runner(void *args)
{
        disk_data data = *(disk_data*)args;
        char threadName = data.id;
        
        pthread_mutex_lock(&mutex);
        
        while(strlen(data.filename) == 0)
        {
           printf("IT REACHED THIS> GOING OUT");
           pthread_cond_wait(&cond1, &mutex);
        }
        
        printf("Hello from thread %u I am %c\n", (int)pthread_self(),threadName);
        runscheduler(threadName,data);
        pthread_cond_signal(&cond1);
        
         pthread_mutex_unlock(&mutex);
        pthread_exit(NULL);
}

void runscheduler(char name, disk_data data)
{
   switch(name) 
   {
      case 'A' :
         firstComeFirstServe(data.cylindersInReqQueue, data.currentPos, data.requestQueueArr);
         break;
      case 'B' :
         shortestSeekTimeFirst(data.totalCylinders, data.cylindersInReqQueue, data.currentPos, data.requestQueueArr);
         break;
      case 'C' :
         scan(data.totalCylinders, data.currentPos, data.prevDiskRequest, data.cylindersInReqQueue, data.requestQueueArr);
         break;
      case 'D' :
         cScan(data.totalCylinders, data.currentPos, data.prevDiskRequest, data.cylindersInReqQueue, data.requestQueueArr);
         break;
      case 'E' :
         look(data.totalCylinders,data.currentPos,data.prevDiskRequest,data.cylindersInReqQueue,data.requestQueueArr);
         break;
      case 'F' :   
         cLook(data.totalCylinders,data.currentPos,data.prevDiskRequest,data.cylindersInReqQueue,data.requestQueueArr);
         break;
      default :
         printf("Invalid grade\n" );
   }
}

int main(int argc, char*argv[])
{
   disk_data dd;
   pthread_t tid[N];
   int* res;
   char *input;
   int i,ret;
   dd.cylindersInReqQueue =0;
   dd.requestQueueArr = (int *) malloc(dd.cylindersInReqQueue * sizeof(int));   
   pthread_mutex_init(&mutex, NULL);
   printf("***Disk Scheduler Simulation***");
   input = readAndParse(&dd.totalCylinders, &dd.cylindersInReqQueue, &dd.currentPos, &dd.prevDiskRequest, &dd.requestQueueArr);
   dd.filename = input;
   for(i = 65; i < N; i++) 
   {
      dd.id = (char)i;
      ret =  pthread_create(&tid[i], NULL, &runner, &dd);
      if(ret != 0) 
      {
         perror("Error: pthread_create() failed\n");
      }
      sleep(1);
  
      printf ("Thread %c is created\n", i);
   }
   
   
   for(i = 65; i < N; i++) 
   {
      if (pthread_join(tid[i], (void **) &res) != 0) 
      {
         perror("Failed to join thread");
      }
      printf ("Thread %c has terminated\n", i);
   }
   pthread_mutex_destroy(&mutex);
   printf ("All threads are now finished\n");
   
   pthread_exit(NULL);
   return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source