home *** CD-ROM | disk | FTP | other *** search
- /*
- SEMTEST.C -- Using WaitEvent and PostEvent semaphores
-
- From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC SEMTEST HANDLES (for Borland C++ v3.00)
- WINIOMS SEMTEST HANDLES (for Microsoft C/SDK)
-
- Changed from version in book: the task doing WaitEvent()
- shouldn't yield. Unfortunately, the puts() function in WINIO
- has to yield. So, don't print anything.
- */
-
- #include <windows.h>
- #include <stdlib.h>
- #include <time.h>
- #include "winio.h"
- #include "handles.h"
-
- /* from WINEXP.H -- "scheduler things that the world knows not" */
- BOOL far PASCAL WaitEvent(HANDLE);
- BOOL far PASCAL PostEvent(HANDLE);
-
- void waste_time() // waste a random amount of time
- {
- static time_t t = 0;
- int i;
-
- if (t == 0) // one-time initialization
- srand(time(&t));
-
- for (i=rand(); i--; )
- GetVersion();
- }
-
- main(int argc, char *argv[])
- {
- extern HANDLE __hPrevInst; // in ARGCARGV.C
- HANDLE hSecondInst, hSecondTask;
- int i;
-
- winio_about("SEMTEST"
- "\nUsing WaitEvent and PostEvent semaphores"
- "\n\nFrom Chapter 5 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- if (! __hPrevInst)
- {
- winio_settitle(winio_current(), "SEMTEST - First Task");
- if ((hSecondInst = WinExec(argv[0], SW_NORMAL)) <= 32) // fork
- fail("Could not EXEC second task");
- hSecondTask = hTask_from_hInstance(hSecondInst);
- for (i=10; i--; )
- {
- waste_time();
- puts("I'm about to wake second task");
- PostEvent(hSecondTask);
- puts("I woke second task");
- }
- }
- else // I must be the second task
- {
- winio_settitle(winio_current(), "SEMTEST - Second Task");
- for (i=10; i--; )
- {
- // yielding in here causes problems
-
- waste_time();
- // puts("I'm about to wait");
- WaitEvent(0);
- // puts("Thanks, I needed that");
- }
- }
- puts("done");
- return 0;
- }
-
-