tags:

views:

218

answers:

6

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

A: 

Why do you read and write data by chunks of sizeof(char*) bytes that is 4 or 8 bytes? You are not forced to read and write by the same number of bytes!

If your problem is that you do not receive data in the reading thread, you should think about syncing after the write, using sync().

Didier Trosset
No, fflush() is for flushing stdio buffers. He's using read() and write() directly, which bypass stdio.
psmears
A: 

Seems to me like if you just happened to read or write a little too much, then your code would terminate. You should have, while success_read < MAX_MSGLEN or sizeof(str).

DeadMG
A: 

Do you really need an extra thread for this? As you're finding, adding threads increases complexity a lot... in this case, writing to the pipe is not likely to be much faster than writing to the file - and in fact, it may well end up being slower in practice, since write() doesn't have the buffering that fprintf() and friends do. The best solution may be simply to write to the logfile directly...

psmears
A: 

Have you tried socketpair(AF_UNIX,SOCK_DGRAM)? It is reliable by its nature and also would delimit messages for you.

In your case, you have to make sure that MAX_MESSAGE_LEN is less than PIPE_BUF, the platform's limit for pipe()'s internal buffer.

And obviously, change sending/recving of pointer into sending of the actual string, e.g.:

-   char str[] = "hello log thread 123456789 10 11 12 13 14 15 16 17 18 19\n";
+   char str[MAX_MESSAGE_LEN] = "hello log thread 123456789 10 11 12 13 14 15 16 17 18 19\n";

- write(pipe_fd[1],(void*)&str,sizeof(char*));
+ write(pipe_fd[1],(void*)&str,MAX_MESSAGE_LEN);
Dummy00001
A: 

You are not updating success_write in the loop.

Dipstick
A: 

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.

You could create a simple protocol to communicate over the FIFO/pipe. The writer thread writes a single byte to the FIFO to indicate the message length and then writes the variable length message. The reader will read a single byte to learn the length of the message and then issue a subsequent read command for the specified message length.

You may want to check out my answer to a similar question on FIFOs for more details.

jschmier