I am continuing with the Yesterday's help, and I have added another code in the child thread. Basically, when user enters a stdin, the child thread should read it and return it back to the parent thread. However, after printing the output, the code should be redirected back to the child thread and wait for the user to press enter, once the user press enter, the code should be exited. This is working, but I have used sleep() and I want to use only mutex(), when I comment the sleep(), then the following codes print first ("press enter") then the parent code prints actual input.
/*Required header files are added*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
/*this structure will hold the string of user input and the lock variable has created*/
struct thread_main
{
char *buffer;
char *bufferparent;
pthread_mutex_t lock;
pthread_mutex_t lock1;
} td;
/*it is a child thread, and store the user value in a buffer variable which has been declared in the thread_main structure*/
static void *thread(void *buff)
{
/*the pointer has assigned to the structure, so we can get the values of a buffer array*/
struct thread_arguments *arg = buff;
//the previous code
pthread_mutex_unlock(&td.lock);
pthread_mutex_destroy(&td.lock);
sleep(1); // I want to ignore this.
pthread_mutex_init(&td.lock, 0);
pthread_mutex_lock(&td.lock);
printf("press enter");
/*this code will read buffer and check the enter*/
pthread_mutex_unlock(&td.lock);
pthread_mutex_destroy(&td.lock);
return NULL;
}