[comp.windows.ms] How are multiple Windows applications scheduled?

cwinemil@digi.lonestar.org (Chris Winemiller) (08/30/90)

How does Windows 3.0 achieve the "simultaneous" execution of multiple
Windows applications? (Note: I am excluding "simultaneous" execution of
multiple DOS sessions in the 386 enhanced mode.) Must the Windows
applications be written in such a way as to voluntarily "relinquish"
control of the CPU occasionally? Or are the Windows applications
written as if each is the only one running on the CPU? In other words,
does Windows do non-preemptive "scheduling" or preemptive "scheduling"?
Or is it some other scheme? I'd be particularly interested in comments
from those who have actually developed Windows applications.  I don't
have the SDK and have not written any Windows applications (obviously),
otherwise I wouldn't need to ask! :-)
---------------------------------------------------------------------
Chris Winemiller                INTERNET: cwinemil@digi.lonestar.org 
DSC Communications Corporation  UUCP: ...uunet!digi!cwinemil
1000 Coit Rd. M/S 140                 ...texsun!digi!cwinemil
Plano, TX 75075 U.S.A.          Voice: (214) 519-3451

dzoey@terminus.umd.edu (Joe I. Herman) (08/30/90)

In article <920@digi.lonestar.org> cwinemil@digi.lonestar.org (Chris Winemiller) writes:
>How does Windows 3.0 achieve the "simultaneous" execution of multiple
>Windows applications?

It doesn't.  Windows runs Apps using cooperative tasking (I presume
for backwards compatibility) When you make certain windows calls (such
as wait for a message etc.) you yield control to the next app.

The multiple DOS sessions all run in their own VM and Windows uses preemptive
tasking for each VM.


			Joe Herman
			U. of Md.



dzoey@terminus.umd.edu
--
"Everything is wonderful until you know something about it."

strobl@gmdzi.UUCP (Wolfgang Strobl) (08/31/90)

cwinemil@digi.lonestar.org (Chris Winemiller) writes:

>How does Windows 3.0 achieve the "simultaneous" execution of multiple
>Windows applications? (Note: I am excluding "simultaneous" execution of
>multiple DOS sessions in the 386 enhanced mode.) Must the Windows
>applications be written in such a way as to voluntarily "relinquish"
>control of the CPU occasionally? Or are the Windows applications
>written as if each is the only one running on the CPU? In other words,
>does Windows do non-preemptive "scheduling" or preemptive "scheduling"?

Windows 3.0 does it the same way as Windows 1.0 did.

Windows does non-preemptive multitasking. Task switches occur only if
applications return control back to Windows. In fact, there is a API call
named Yield(), but ... it's almost never used.

This is because a Windows program is normally interactive and user
driven, not data driven. There are a few "event" generators: the
mouse, the keyboard, and the timer. All these events get time
stamped and added to a "message queue". A Windows program is 
a "message loop" dispatching these messages to a set of "window functions"
processing these messages and returning control back to Windows.
As long as this processing is fast enough, there is no need to 
give control back to Windows in between.

>Or is it some other scheme? I'd be particularly interested in comments
>from those who have actually developed Windows applications.  I don't
>have the SDK and have not written any Windows applications (obviously),
>otherwise I wouldn't need to ask! :-)

Get "Charles Petzold: Programming Windows, Microsoft Press, 
     ISBN 0-914845-91-8", it explains all this, and more.

Wolfgang Strobl
#include <std.disclaimer.hpp>

kensy@microsoft.UUCP (Ken SYKES) (09/03/90)

In article <920@digi.lonestar.org> cwinemil@digi.lonestar.org (Chris Winemiller) writes:
>How does Windows 3.0 achieve the "simultaneous" execution of multiple
>Windows applications? (Note: I am excluding "simultaneous" execution of
>multiple DOS sessions in the 386 enhanced mode.) Must the Windows
>applications be written in such a way as to voluntarily "relinquish"
>control of the CPU occasionally? Or are the Windows applications
>written as if each is the only one running on the CPU? In other words,
>does Windows do non-preemptive "scheduling" or preemptive "scheduling"?
>Chris Winemiller                INTERNET: cwinemil@digi.lonestar.org 
>DSC Communications Corporation  UUCP: ...uunet!digi!cwinemil

Windows programs are scheduled non-preemptively.  It is based on a
message passing system: each program has a message loop similar to the    
following:

WinMain()  
{

   Create windows, initialize, etc.

   while (GetMessage( stuff )) {
       TranslateMessage( stuff );
       DispatchMessage( stuff );
   }

   return exit code;
}

The primary point that Windows switches between applications is at the
GetMessage call.  Before returning the next event to the application it
can go off and execute some other program for awhile.  Eventually the
application that called GetMessage will be started up again and given
the next event in its queue.  This is the essence of multitasking in
windows.  You can also explicitely relinquish control by calling Yield or
you can modify the message loop to do things during idle time:

for (;;)  /* loop forever */
{
    /* process any pending events */
    while (PeekMessage( stuff )) {
       if (message == "quit")
           break;

       TranslateMessage( stuff );
       DispatchMessage( stuff );
    }

    /* system is idle.  do something useful here */

}

Windows apps can also be hooked into external events such as key presses
or timer events.

This is intended to give you a taste of how Windows multitasks.  If you
have any specific questions please post them to the net.

Ken Sykes
Disclaimer: The above opinions are solely my own.