4 Replies - 2042 Views - Last Post: 01 April 2011 - 02:48 PM Rate Topic: ***** 1 Votes

#1 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 110
  • Joined: 04-December 09

C write() is not working correctly in Producer-Consumer problem

Posted 30 March 2011 - 07:44 PM

hi, i am working on a version of a producer-consumer problem where the producer reads some bytes from an input file and puts them in one of 3 buffers and the consumer empties the buffer and writes the bytes into the output file

the result of the program is an output file identical to the input file..

there are 5 producer threads and 2 consumer threads..

the problem i have is that the resulting output file is not identical to the input at all and lots of stuff seems cut out or rearranged somehow..

Pseudocode:
Producer()
{
    while(condition) {
        wait(empty);
        wait(mutex);
        readFileAndFillBuffer();
        signal(mutex);
        signal(full);
   }
}
Consumer()
{
   while(condition) {
       wait(full);
       wait(mutex);
       writeToFile(fileDes);
       signal(mutex);
       signal(empty);
   }
}
writeToFile(int fileDes)
{
   char data[bytes];
   // some code here
   strcpy(data, full_buffer);
   write(fileDes, data, strlen(data));
}



i opened both input and output files in main() using open() and passed in the file descriptors when creating the threads(pthreads)
the read() works perfectly but i don't know why write() doesn't seem to work..

e.g.
Input file:
This is a test of the
producer consumer problem.

Output file:
t of thea tes
 problem.umer


i actually put a printf() before the write that printed the "data" and it was a perfect match of part of the input file, but after the write() it is all garbled up..

i even tried using pwrite() and fputs() but they all result in the same garbled up output..
my teacher even told everyone to use write() because it is thread-safe, but it doesn't seem like it to me...

This post has been edited by musiliu: 31 March 2011 - 01:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C write() is not working correctly in Producer-Consumer problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C write() is not working correctly in Producer-Consumer problem

Posted 31 March 2011 - 05:06 AM

Would seem to suggest you need a mutex on the write, no?
Was This Post Helpful? 1
  • +
  • -

#3 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: C write() is not working correctly in Producer-Consumer problem

Posted 31 March 2011 - 01:11 PM

ok, I tried putting a mutex lock before the write and after the write, but it still produces the same garbled output..

i still don't understand why the write() isn't working, can someone help?

also, something weird is also happening, after i run the program in a linux server using bash shell,
the output file sometimes has some weird file permissions on it like S and T, and the output filename is sometimes highlighted gold or brown or green randomly....
Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: C write() is not working correctly in Producer-Consumer problem

Posted 31 March 2011 - 08:24 PM

If a file is created with a call to open, you will need to specify the permission flags as well.
http://pubs.opengrou...tions/open.html
You'll also need to check the sys/stat.h man page (linked to from the open page)
Was This Post Helpful? 1
  • +
  • -

#5 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: C write() is not working correctly in Producer-Consumer problem

Posted 01 April 2011 - 02:48 PM

whooaaaaaaaaaaaaaaaa, man i feel like a noob..

i forgot to include 1 extra byte space for the null terminating character when reading from the file, that was why i was getting garbled output..

now it works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1