Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Queing into a array

Hi, I want put data (string) to an array for query sql,

like

string1 ="insert into table ..."
string2 ="insert into table1 ..."
string3 ="insert into table2 ..."
string4 ="insert into table3 ..."

I want put them into an array for "execute" them later with a pthread process, I have the code for start a thread, but I can't figure how I can fill an array with that.... my thread will check if the array is empty, is yes it will loop, else it will send the query to my database. In some time, my main process can add stuff to my array... you see the point?

how can I do that? I'm still learning c.
0
Yard072999
Asked:
Yard072999
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
Easiest method (not the best necessarily) is to declare as a fixed array, maybe global:

#define MAX_QUERIES 20
#define QUERY_LEN 200
char MYQUERIES[MAX_QUERIES][QUERY_LEN];

since array has a fixed number of element, you will need a size control variable, let's say:

int LAST_QUERY = 0;

now, every time you want to add a query to list do something like that:

strcpy(MYQUERIES[LAST_QUERY++], "insert into table....");

finally to loop all your queries somewhere:

int i;
for (i=0; i<LAST_QUERY; i++) {
    your_query_function(MYQUERIES[i]);
}

Hope to be useful,
Jaime.
0
 
sunnycoderCommented:
Hi Yard,

>, I have the code for start a thread, but I can't figure how I can fill an array with that....
Where do you get this information from? Is it a file or user inputs them interactively or is there some other source?

Reading from a source and filling it into an array is not quite difficult.


fgets (buffer, BUF_LEN, fp );

will read a single *line* from fp file pointer up to maximum BUF_LEN-1 bytes. Replace fp with stdin to get the query from the user. However, this gets tricky when length of query exceeds BUF_LEN as you will need more memory to store the entire query.

An array of char * should serve the purpose. Can you post some more details along with the code snippets that you have already written.

cheers
sunnycoder
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Yard,

One HUGE item that you're going to have to manage is data locking.  It's not hard to do this, but it is critical.

For instance, imagine trying to insert the string "INSERT into table MYTABLE (VAR1, VAR2, VAR2) values (\"X1\", \"X2\", \"X3\");" into your C table from one thread.  However, the string copy process (likely strcpy() or sprintf()) is interrupted so that the O/S can assign the CPU to the other thread.  The thread might only recieve "INSERT into table MYT" and try to execute it.  The result will be an SQL error.

You can manage this several ways.  The main thread can use the Suspend() and Resume() methods to control when the second thread is executing.  But you'll also want to know when the second thread is finished so that you know when it's safe to modify your table.  Use a variable to control which thread is locking the table.

int TableLock = 0;

/*  Main thread  */

if (TableLock == 0)  /*  Safe to change the table  */
{
  /*  Make table Changes  */
  TableLock = 1;      /*  Tell the second thread to run  */
}


/*  Second thread  */

if (TableLock)         /*  The maint thread is not locking the table  */
{
  /*  Run the statements  */
  TableLock = 0;     /*  Tell the main thread that this thread is done  */
}


These simple controls will manage the two threads in a way that keeps them from stepping on each other.

Kent
0
 
CmdrRickHunterCommented:
that will keep two threads executing in a perfect lockstep.
Two problems with it:
1) your code will busy wait.  This means that while the second thread is waiting for input from the first, it sits, using up all of its time checking if the 1st is done.  You instantly just lost 50% of your CPU power.
2) If all you have to do is prepare a table, then run the second thread... is there such a gain of having two threads?  If you want to be able to write to the "queue" while you're executing a previous query, you'll have to wait for the queue to empty.

pthreads offer a few things you might find useful: mutexes and events.

mutexes are a construct that a thread "owns".  You aquire a mutex, and no other thread can aquire it until you release it.  Think of it like a locking mechanism.  When you want to read from/write to the table, you aquire the mutex.  This means that, as long as you hold the mutex, no one else can be working with the table (unless they're being bad... if they completly ignore the mutex, it wont do you any good).  once you have the mutex, you can write to the array without fear of someone else reading it at a bad time.  You then release the mutex, and go on your merry way.  The reader locks it, reads, then unlocks it.

events help the "busy wait" syndrome - where you keep checking over and over if there's new data available. The thread that does the queries waits for the "add item" event to occur.  Only when that event occurs does it try to read  a string (using the mutexes to lock the queue).  The thread that adds queries first adds the query (using the mutexes to lock the queue), then signals the event.  Unlike polling to see if there's new items, "waiting" on an event (or a mutex, or any of the syncronization objects) is almost free... takes almost zero CPU time).



An alternative to a shared memory queue would be a pipe.  Unix programs use these quite often to pass information between processes.  They're slower than shared memory, but they are really easy to work with... and the "slowness" of pipes will be minimal compared to the time it takes to make a SQL query (probably minimal compared with the time it takes to even PARSE the SQL query string, much less execute it):

int thePipe[2]; // thePipe[0] is for input, thePipe[1] is for output
if (pipe(thePipe) != 0) {
 // check errno, etc
}
spawn threads.

fprintf(thePipe[1], "MyString");


fscanf(thePipe[0], "%s", buffer)


thePipe is a pair of file-descriptors, so anything you can do on a file descriptor (wait, read, write, select, etc) you can do on thePipe.

My advice is to search for pipe IPC (Inter Process Communication).  There are people who are much better with examples/tutorials than I.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now