/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2004-2006 Intel Corporation // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither name of Intel Corporation nor the names of its contributors // may be used to endorse or promote products derived from this software // without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// // Demonstrates the use of Pthreads semaphore. #include #include #include "pthread.h" #include "semaphore.h" #define BLOCK_SIZE 100 #define BUF_SIZE 1000000 size_t bytesRead; sem_t sReadOccurred; // the semaphore we'll use pthread_t hThread; // the waiting thread void* PrintCountRead ( void* ); // the thread function int main ( int argc, char *argv[] ) { FILE *infile; char *inbuf; if ( argc != 2 ) { printf ( "Usage GetSetEvents filename\n" ); return ( -1 ); } infile = fopen ( argv[1], "r+b" ); if ( infile == NULL ) { printf ( "Error opening %s\n", argv[1] ); return ( -1 ); } inbuf = (char*) malloc ( BUF_SIZE ); if ( inbuf == NULL ) { printf ( "Could not allocate read buffer\n" ); return ( -1 ); } // first initialize the semaphore sem_init ( &sReadOccurred, // the address of the semaphore 0, // 0 = share only with threads in this program 0 ); // initial value. 0 = make threads wait // now kick off the thread pthread_create ( &hThread, NULL, PrintCountRead, (void *) NULL ); bytesRead = fread ( inbuf, 1, BLOCK_SIZE, infile ); if ( bytesRead < BLOCK_SIZE ) { printf ( "Need a file longer than %d bytes\n", BLOCK_SIZE ); return ( -1 ); } else sem_post ( &sReadOccurred ); // turn on the waiting threads while ( !feof ( infile ) && bytesRead < BUF_SIZE - BLOCK_SIZE ) bytesRead += fread ( inbuf, 1, BLOCK_SIZE, infile ); printf ( "Read a total of %d bytes\n", (int) bytesRead ); return ( 0 ); } // the thread function, which waits for the event before proceeding void *PrintCountRead ( void* pv ) { int i; sem_wait ( &sReadOccurred ); // wait on the semaphore for ( i = 0; i < 10; i++ ) printf ( "Have now read %06d bytes\n", (int) bytesRead ); return ( pv ); }