Monday, 12 August 2013

If Condition Stopped After First Statement Execution in C

If Condition Stopped After First Statement Execution in C

I am really having a hard time trying to make a function to loop within a
pthread. My function is called by this pthread and it is supposed to loop
up to infinite but, it is just not happening. That is why I am asking for
help. I created my thread making use of the following code:
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void *thread_s(void *params_s);
int main(int argc, char *argv[]) {
int s1_hand = 0;
pthread_t routines[1];
printf("Creating Thread -> Main Thread Extremely Busy!\n");
s1_hand = pthread_create(&(routines[0]), NULL, thread_s, (void
*)&(routines[0]));
if (s1_hand != 0) {
printf("Not possible to create thread:[%s]\n", strerror(s1_hand));
exit(EXIT_FAILURE);
}
void* result;
if ((pthread_join(routines[0], &result)) == -1) {
perror("Cannot join S thread");
exit(EXIT_FAILURE);
}
pthread_exit(NULL);
return 0;
}
And the function I would like to have continuously running is:
void *thread_s(void *params_s) {
printf("Thread starting...\n");
int fd, chars, pars, c_1, f_parse, pripa = 1;
fd = open("/dev/ttyUSB0", O_RDONLY | O_NOCTTY | O_SYNC);
if (fd < 0){
perror("Unable to open the fildes!");
exit (EXIT_FAILURE);
}
FILE *stream_a, *stream_b;
stream_a = fdopen(fd, "r");
stream_b = fopen(FILE_II, "w+");
if (stream_a == NULL && stream_b == NULL){
perror("IMPOSSIBLE TO CREATE STREAMS");
exit(EXIT_FAILURE);
}
c_1 = fgetc(stream_a);
f_parse = lookatit(pripa, c_1, array);
printf("First Parse Done -> (%i)\n", f_parse);
while ((chars = fgetc(stream_a)) != EOF){
pars = lookatit(0, (uint8_t)chars, array);
if (pars == 1){
printf("MESSAGE FOUND AND SAVED -> (%i)\n", parsing);
fprintf(stream_b,"%.6f %.6f %.6f\n", array[0], array[1],
array[2]);
} else{
printf("Looking for the Message: %i\n", parsing);
continue;
}
chars++;
}
fflush(stream_b);
fclose(stream_b);
fclose(stream_a);
close(fd);
pthread_exit(NULL);
return 0;
}
The function "lookatit" performs a parsing routine, returning 1 when a
message is found and 0 when no message is found. The function is looping
perfectly when no messages are found but, when it finds a message, the if
statement (line 22 second code) prints "MESSAGE FOUND AND SAVED" and it
immediately terminates the program. Why is my program presenting this
behaviour?

No comments:

Post a Comment