C: Fifo between threads, writing and reading strings
Posted
by Yonatan
on Stack Overflow
See other posts from Stack Overflow
or by Yonatan
Published on 2010-06-08T11:21:23Z
Indexed on
2010/06/08
11:42 UTC
Read the original article
Hit count: 217
Hello once more dear internet,
I writing a small program that among other things, writes a log file of commands received.
to do that, I want to use a thread that all it should do is just attempt to read from a pipe, while the main thread will write into that pipe whenever it should.
Since i don't know the length of each string command, i thought about writing and reading the pointer to the char buf[MAX_MESSAGE_LEN]
.
Since what i've tried so far doesn't work, i'll post my best effort :P
char str[] = "hello log thread 123456789 10 11 12 13 14 15 16 17 18 19\n";
if (pipe(pipe_fd) != 0) return -1;
pthread_t log_thread;
pthread_create(&log_thread,NULL, log_thread_start, argv[2]);
success_write = 0;
do {
write(pipe_fd[1],(void*)&str,sizeof(char*));
} while (success_write < sizeof(char*));
and the thread does this:
char buffer[MAX_MSGLEN];
int success_read;
success_read = 0;
//while(1) {
do {
success_read += read(pipe_fd[0],(void*)&buffer, sizeof(char*));
} while (success_read < sizeof(char*));
//}
printf("%s",buffer);
(Sorry if this doesn't indent, i can't seem to figure out this editor...)
oh, and pipe_fd[2]
is a global parameter.
So, any help with this, either by the way i thought of, or another way i could read strings without knowing the length, would be much appreciated.
On a side note, i'm working on Eclipse IDE C/C++, version 1.2.1 and i can't seem to set up the compiler so it will link the pthread library to my project. I've resorted to writing my own Makefile to make it (pun intended :P) work. Anyone knows what to do ? i've looked online, but all i find are solutions that are probably good on an older version because the tabs and option keys are different.
Anyways, Thanks a bunch internet ! Yonatan
© Stack Overflow or respective owner