I have a C program with two threads one of those threads is almost all the time blocked in a fgets()
waiting for user input. The second thread may need to print to the terminal while the first one is blocked on fgets()
.
From my tests it seems that the program waits for the fgets()
on the first thread to return and then the second thread can print.
Is this who it works or could I print while the the other thread is blocked on the fgets()
?
This implementations runs on eCos (embedded Configurable operating system).
Thread locked on fgets()
:
int my_getline (char** argv, int argvsize)
{
static char line[MAX_LINE];
char *p;
int argc;
fgets(line, MAX_LINE, stdin);
for (argc=0,p=line; (*line != '\0') && (argc < argvsize); p=NULL,argc++) {
p = strtok(p, " \t\n");
argv[argc] = p;
if (p == NULL) return argc;
}
argv[argc] = p;
return argc;
}
Thread trying to print:
while(1){
unsigned char bufr[50];
read_until(bufr);
if (bufr[1] == (unsigned char)NMFL ){
cyg_mutex_lock(&scree_mtx);
printf("Memory half full!\n");
cyg_mutex_unlock(&scree_mtx);
continue;
}
cyg_mbox_put( mbx_serial_userH, bufr );
}
Output (I'm sure the message was there before):
The C standard does not specify any association at all between the standard input stream and the standard output stream. In particular, it does not specify that one thread blocking on reading from standard input, via any standard function, should cause any output function to block.
HOWEVER, the standard also does not say the opposite, that a thread blocking on input input from
stdin
must not cause another to block on output tostdout
. Whether that happens would be a function of the C implementation, and probably of the specific devices with whichstdin
andstdout
are associated.You appear to be using a Windows C implementation with
stdin
andstdout
both connected to a CMD.EXE window. Windows has a lot of idiosynchrasies, and I'm inclined to guess that the blocking you observe is one of them. I would not expect the same on Linux or OSX, but that does not mean it is erroneous.