.


:




:

































 

 

 

 


! 5




- WAIT_TIMEOUT , .

, bWaitAll FALSE. , WaitForMultipleObjects, :

- , WAIT_OBJECT_0

(WAIT_OBJECT_0 + nCount - 1), , , , . WAIT_OBJECT_0;

- , WAIT_ABANDONED_0 (WAIT_ABANDONED_0 + nCount - 1) , , , . WAIT_OBJECT_0;

- WAIT_TIMEOUT , , .

WaitForMultipleObjects WAIT_FAILED.

, WaitForSingleObject .

 

2. WaitForMultipleObjects

#include <windows.h>

#include <iostream>

using namespace std;

void thread_0()

{

int i;

for (i = 0; i < 5; i++)

{

cout << i << ' '; cout << flush << '\a';

Sleep(500);

}

cout << endl;

}

void thread_1()

{

int i;

for (i = 5; i < 10; i++)

{

cout << i << ' ';

cout << flush << '\a';

Sleep(500);

}

cout << endl;

}

int main()

{

HANDLE hThread[2];

DWORD dwThread[2];

//

hThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_0,

NULL, 0, &dwThread[0]);

if (hThread[0] == NULL)

return GetLastError();

//

hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_1,

NULL, 0, &dwThread[1]);

if (hThread[1] == NULL)

return GetLastError();

// thread_1 thread_2

if (WaitForMultipleObjects(2, hThread, TRUE, INFINITE) == WAIT_F AILED)

{

cout << "Wait for multiple objects failed." << endl; cout << "Press any key to exit." << endl;

}

// thread_0 thread_1 CloseHandle(hThread[0]);

CloseHandle(hThread[ 1 ]);

return 0;

}

, , . , , . , :

1. 2.

void thread_1() void thread_2()

{ {

if (n%2 == 0) ++n;

n = a;.

else.

n = b;.

. }

}

, (n%2 == 0) , . n . n . , n. , n , , , , , . thread_1 thread_2 , . thread_1 thread_2 , n.

. , , .

 

Windows

, , Windows . mutex, mutual exclusion, . , . . .

CreateMutex, :

HANDLE CreateMutex(

LPSECURITY_ATTRIBUTES lpMutexAttributes, //

BOOL bInitialOwner, //

LPCTSTR lpName //

);

LPSECURITY_ATTRIBUTES NULL. , , . .

bInitialOwner TRUE, , . . , , .

lpName , . , . MAX_PATH. lpName NULL. . , .

CreateMutex . NULL. , CreateMutex , GetLastError, CreateMutex ERROR_ALREADY_EXISTS.

, ReleaseMutex, :

BOOL ReleaseMutex(HANDLE hMutex //

);

ReleaseMutex TRUE, - FALSE. , , ReleaseMutex FALSE.

CreateMutex OpenMutex. CreateMutex , , . bInitialOwner FALSE, . CreateMutex, . , OpenMutex, :

HANDLE OpenMutex(

DWORD dwDesiredAccess, //

BOOL bInheritHandle //

LPCTSTR lpName //

dwDesiredAccess :

MUTEX_ALL_ACCES S SYNCHRONIZE

. , , ReleaseMutex, . bInheritHandle . TRUE, . - .

OpenMutex , NULL.

. .

 

3. ,

#include <windows.h>

#include <iostream>

using namespace std;

int main()

{

int ij;

for (j = 10; j < 20; j++)

{

for (i = 0; i < 10; i++)

{

cout << j << ' '; cout.flush();

Sleep(5);

}

cout << endl;

}

return 0;

}

4. ,

#include <windows.h>

#include <iostream>

using namespace std;

int main()

{

char lpszAppName[] = "D:\\os.exe";

STARTUPINFO si;

PROCE S S_INF ORM ATION pi;

ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);

//

if (!CreateProcess(lpszAppName, NULL, NULL, NULL, FALSE,

NULL, NULL, NULL, &si, &pi))

{

cout << "The new process is not created." << endl;

cout << "Press any key to exit." << endl; cin.get();

return GetLastError();

}

// for (int j = 0; j < 10; j++)

{

for (int i = 0; i < 10; i++)

{

cout << j << ' '; cout.flush();

Sleep(10);

}

cout << endl;

}

// WaitForSingleObject(pi.hProcess, INFINITE);

// CloseHandle(pi.hThread);

CloseHandle(pi.hProcess);

return 0;

}

. , . - , . , . .

 

5. , // ,

#include <windows.h>

#include <iostream>

using namespace std;

int main()

{

HANDLE hMutex;

int ij;

//

hMutex = OpenMutex(SYNCHRONIZE, FALSE, "DemoMutex"); if (hMutex == NULL)

{

cout << "Open mutex failed." << endl; cout << "Press any key to exit." << endl; cin.get();

return GetLastError();

}

for (j = 10; j < 20; j++)

{

// WaitForSingleObject(hMutex, INFINITE); for (i = 0; i < 10; i++)

{

cout << j << ' ';

cout.flush();

Sleep(5);

}

cout << endl;

// ReleaseMutex(hMutex);

}

// CloseHandle(hMutex);

return 0;

}

6. , ,

#include <windows.h>

#include <iostream>

using namespace std;

int main()

{

HANDLE hMutex;

char lpszAppName[] = "D:\\os.exe";

STARTUPINFO si;

PROCESS_INFORMATION pi;

//

hMutex = CreateMutex(NULL, FALSE, "DemoMutex"); if (hMutex == NULL)

{

cout << "Create mutex failed." << endl; cout << "Press any key to exit." << endl; cin.get();

return GetLastError();

}

ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);

//

if (!CreateProcess(lpszAppName, NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))

{

cout << "The new process is not created." << endl; cout << "Press any key to exit." << endl; cin.get();

return GetLastError();

}

// for (int j = 0; j < 10; j++)

{

// W aitForSingleObj ect(hMutex, INFINITE); for (int i = 0; i < 10; i++)

{

cout << j << ' '; cout.flush();

Sleep(10);

}

cout << endl;

// ReleaseMutex(hMutex);

}

// CloseHandle(hMutex);

// W aitForSingleObj ect(pi.hProcess, INFINITE);

// CloseHandle(pi.hThread);

CloseHandle(pi.hProcess);

return 0;

}

3. Windows

. , . , .

Windows Events. :

- ;

- .

, ResetEvent, ResetEvent, . , , WaitForSingleObject, . CreateEvent, :

HANDLE CreateEvent(

LPSECURITY_ATTRIBUTES lpSecurity Attributes, //

BOOL bManualReset, //

BOOL bInitialState, //

LPCTSTR lpName //

);

, lpSecurity Attributes NULL. . bManualReset TRUE, , - . bInitialState TRUE, , - . lpName , , . NULL, .

CreateEvent , - NULL. , CreateEvent , GetLastError, CreateEvent ERROR_ALREADY_EXISTS.

, , .

 

7.

#include <windows.h>

#include <iostream>

using namespace std; volatile int n;

HANDLE hOutEvent, hAddEvent;

DWORD WINAPI thread(LPVOID)

{

int i;

for (i = 0; i < 10; i++)

{

++n;

if (i == 4)

{

SetEvent(hOutEvent);

WaitForSingleObject(hAddEvent, INFINITE);

}

}

return 0;

}

int main()

{

HANDLE hThread;

DWORD IDThread;

cout << "An initial value of n = " << n << endl;

// hOutEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (hOutEvent == NULL)

return GetLastError();

hAddEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (hAddEvent == NULL)

return GetLastError();

// thread

hThread = CreateThread(NULL, 0, thread, NULL, 0, &IDThread); if (hThread == NULL)

return GetLastError();

// thread

WaitForSingleObject(hOutEvent, INFINITE);

//

cout << "An intermediate value of n = " << n << endl;

// thread SetEvent(hAddEvent);

WaitForSingleObject(hThread, INFINITE);

cout << "A final value of n = " << n << endl;

CloseHandle(hThread);

CloseHandle(hOutEvent);

CloseHandle(hAddEvent);

return 0;

}

SetEvent, :

BOOL SetEvent(

HANDLE hEvent //

);

TRUE, - FALSE.

ResetEvent, :

BOOL ResetEvent(

HANDLE hEvent //

);

TRUE, - FALSE.

, , PulseEvent, :

BOOL PulseEvent(

HANDLE hEvent //

);

, hEvent, , . PulseEvent , . , PulseEvent, . , Windows NT/2000 , EVENT_MODIFY_STATE.

, , .

 

8.

#include <windows.h>

#include <iostream>

using namespace std;

volatile int n,m;

HANDLE hOutEvent[2], hAddEvent;

DWORD WINAPI thread_1 (LPVOID)

{

int i;

for (i = 0; i < 10; i++)

{

++n;

if (i == 4)

{

SetEvent(hOutEvent[0]);

WaitForSingleObject(hAddEvent, INFINITE);

}

}

return 0;

}

DWORD CALLBACK thread_2(LPVOID)

{

int i;

for (i = 0; i < 10; i++)

{

++m;

if (i == 4)

{

S etEvent(hOutEvent [ 1 ]);

WaitForSingleObject(hAddEvent, INFINITE);

}

}

return 0;

}

int main()

{

HANDLE hThread_1, hThread_2;

DWORD IDThread_1, IDThread_2;

cout << "An initial values of n = " << n << ", m = " << m << endl;

// hOutEvent[0] = CreateEvent(NULL, FALSE, FALSE, NULL); if (hOutEvent[0] == NULL)

return GetLastError();

hOutEvent[1] = CreateEvent(NULL, FALSE, FALSE, NULL); if (hOutEvent[1] == NULL)

return GetLastError();

//

hAddEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (hAddEvent == NULL)

return GetLastError();

//

hThread_1 = CreateThread(NULL, 0, thread_1, NULL, 0, &IDThread_1); if (hThread_1 == NULL)

return GetLastError();

hThread_2 = CreateThread(NULL, 0, thread_2, NULL, 0, &IDThread_2); if (hThread_2 == NULL)

return GetLastError();

// WaitForMultipleObjects(2, hOutEvent, TRUE, INFINITE); cout << "An intermediate values of n = " << n

<< ", m = " << m << endl;

// SetEvent(hAddEvent);

// W aitForSingleObj ect(hThread_ 1, INFINITE);

W aitForSingleObj ect(hThread_2, INFINITE);

cout << "A final values of n = " << n << ", m = " << m << endl;

CloseHandle(hThread_ 1);

CloseHandle(hThread_2);

CloseHandle(hOutEvent[0]);

CloseHandle(hOutEvent[ 1 ]);

CloseHandle(hAddEvent);

return 0;

}

CreateEvent OpenEvent. CreateEvent, bManualReset bInitialState , , , , , lpName.

OpenEvent, , , . :

HANDLE OpenEvent(

DWORD dwDesiredAccess, //

BOOL bInheritHandle, //

LPCTSTR lpName //

);

dwDesiredAccess , :

E VENT_ALL_ACCES S

EVENT_MODIFY_STATE

SYNCHRONIZE

EVENT_ALL_ACCESS , . EVENT_MODIFY_STATE , SetEvent ResetEvent . SYNCHRONIZE , .

, , . OpenEvent .

 

9.

#include <windows.h>

#include <iostream>

using namespace std;

HANDLE hInEvent;

CHAR lpEventName[]="InEventName";

int main()

{

char c;

hInEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, lpEventName); if (hInEvent == NULL)

{

cout << "Open event failed." << endl; cout << "Input any char to exit." << endl; cin >> c;

return GetLastError();

}

cout << "Input any char: "; cin >> c;

// SetEvent(hInEvent);

// CloseHandle(hInEvent);

cout << "Now input any char to exit from the process: "; cin >> c;

return 0;

}

10. //

#include <windows.h>

#include <iostream>

using namespace std;

HANDLE hInEvent;

CHAR lpEventName[] = "InEventName";

int main()

{

DWORD dwWaitResult;

char szAppName[] = "C:\\ConsoleProcess.exe";

STARTUPINFO si;

PROCESS_INFORMATION pi;

// ,

hInEvent = CreateEvent(NULL, FALSE, FALSE, lpEventName);

if (hInEvent == NULL)

return GetLastError();

// , ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);

if (!CreateProcess(szAppName, NULL, NULL, NULL, FALSE,

CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))

return 0;

// CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

// dwWaitResult = WaitForSingleObject(hInEvent, INFINITE); if (dwWaitResult!= WAIT_OBJECT_0) return dwWaitResult;

cout << "A symbol has got." << endl;

CloseHandle(hInEvent);

cout << "Press any key to exit: "; cin.get();

return 0;

}

. , , - . . .

 

1. , , .

2. , .

3. .

4. .

1. Boss () Scout (). . .

, , .

Boss:

− Scout, ;

− Scout;

− Scout . , ;

− .

Scout:

− : -, . ( , ) Boss;

− , , .

2. Boss () Scout ().

, 1, 2 Scout

Boss:

Scout, ;

Scout;

Scout . , ;

− .

Scout:

− , 1, 2, ( ) Boss;





:


: 2016-11-24; !; : 573 |


:

:

: , .
==> ...

2002 - | 1627 -


© 2015-2024 lektsii.org - -

: 0.245 .