LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   writing and reading at same time (https://www.linuxquestions.org/questions/programming-9/writing-and-reading-at-same-time-500467/)

Wim Sturkenboom 11-12-2006 11:35 PM

Very basic (pseudo code)
Write program:
Code:

store_sql()
{
  // try to lock file
  while (!acquire_lock());
  // write buffer with sql to file
  while(sql_in_buffer())
  {
      write_sql_to_file();
  }
  // release lock
  free_lock();
}

Read program:
Code:

read_sql()
{
  // try to lock file
  while (!acquire_lock());
  // read whole file (and empty it)
  read_sql_from_file();
  // release lock
  free_lock();
  // process sql statements
  while(sql_to_do)
  {
    // something usefull here
  }
}

The write-program tries to lock the file in an endless loop till it succeeds.
Next it writes one sql line to the file and releases the lock (so the read program can read when it when necessary).

The read program attempts to get a lock on the same file. As long as the write-program is writing, it can't get the lock. Once it gets the lock, it reads the whole file and empties it. Next it releases the lock so the write-program can write again.

There are some points:
If you forget to free the lock, you may end in a situation where one of the two programs does no longer work.
The write-program must be a bit more intelligent. In above function, it 'hangs' till it can get the lock; you probably want it to gather more sql statement to write to file.

Hope this helps.

keefaz 11-13-2006 06:49 PM

You could also use another mysql table to store the queries :)


All times are GMT -5. The time now is 12:56 AM.