Hi thanks for the responce, well the program will do the following:
it will take an unsorted input and writes it to a file in the proper order. The threads have their own local store of buffers that they have read, but otherwise share access to the source pipe, destination file, the index of the current entry to write, and the index of the last entry.
The main function in the receiver will call two functions that create and manage threads for this project: thread_pool_init, which initializes the tpool_t data structure, and run_pool_threads, which creates the threads that run the function rcv_file. Initially, create one such thread, but ultimately six threads must be run.
When a thread reads a line, the line is placed in a store. These stores are of limited size, so the you must ensure that a store does not overflow. If a store becomes full, then you must cause thread to wait until it has the next_entry line before it can proceed. I will make sure that the input file does not cause all stores to fill.
Here is the psudo code:
Code:
rcv_file( input_ipc, outfile )
{
initialize( entry_store );
initialize( next_entry = 1 );
while ( more_to_receive && more_to_write ) {
if ( more_to_receive ) {
read_entry( input_ipc, entry_store );
determine if more_to_receive;
}
get lowest_read from entry_store;
while (TRUE) {
if ( lowest_read == next_entry ) {
write_entry( outfile, entry_store, lowest_read );
incr next_entry
get lowest_read
determine if more_to_write
if ( !more_to_write ) break
}
else break;
}
}
read_entry( input_ipc, entry_store )
{
initialize( buf, max_size );
bytes = mypipe_read( input_ipc, buf, max_size );
if ( buf has bytes ) {
get_index( buf, index );
check_for_eof( buf );
store_entry( buf, index, bytes, entry_store );
return index
}
else {
dealloc( buf )
return unusable index
}
}
write_entry( outfile, entry_store, lowest_read )
{
buf = get_entry_from_store( entry_store, lowest_read );
write( outfile, buf );
dealloc( buf );
}
I'm not sure if this helps at all, the professor supplied the library code but didn't explain how they correlate and I was hoping any of you could take a guess at what it might be doing there.
He mentioned this:
#
With a single thread, you can put the mutual exclusion code in to protect the shared data structures. You must identify these shared data structures and add the appropriate pthreads code for mutual exclusion. The variables for mutual exclusion are provided in the data structure tpool_t.
#
Then, try two threads at a time. In this case, you will need to use a condition variable for a thread to wait and be awoken. The condition variable is also defined in tpool_t.
So to answer your question, can that pointer be used to perhaps switch between threads in the pool thread? LIke that thread pointer will point to the active thread?