Advertisement
Guest User

task2

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<Windows.h>
  4. #include<process.h>
  5.  
  6. HANDLE EventA, EventB, EventC;
  7.  
  8.  
  9. unsigned int _stdcall Print_a(void* pPM) {
  10.     char id = *(char*)pPM;
  11.     for (int i = 0; i < 10; i++) {
  12.         WaitForSingleObject(EventA, INFINITE);
  13.         printf("%c", id);
  14.         SetEvent(EventB);
  15.     }
  16. }
  17.  
  18. unsigned int _stdcall Print_b(void* pPM) {
  19.     char id = *(char*)pPM;
  20.     for (int i = 0; i < 10; i++) {
  21.         WaitForSingleObject(EventB, INFINITE);
  22.         printf("%c", id);
  23.         SetEvent(EventC);
  24.     }
  25. }
  26.  
  27. unsigned int _stdcall Print_c(void* pPM) {
  28.     char id = *(char*)pPM;
  29.     for (int i = 0; i < 10; i++) {
  30.         WaitForSingleObject(EventC, INFINITE);
  31.         printf("%c", id);
  32.         SetEvent(EventA);
  33.     }
  34. }
  35.  
  36. void main() {
  37.  
  38.     EventA = CreateEvent(NULL, FALSE, TRUE, NULL);
  39.     EventB = CreateEvent(NULL, FALSE, FALSE, NULL);
  40.     EventC = CreateEvent(NULL, FALSE, FALSE, NULL);
  41.  
  42.     char A = 'A', B = 'B', C = 'C';
  43.     HANDLE a, b, c;
  44.         a = _beginthreadex(NULL, 0, Print_a, &A, 0, NULL);
  45.         b = _beginthreadex(NULL, 0, Print_b, &B, 0, NULL);
  46.         c = _beginthreadex(NULL, 0, Print_c, &C, 0, NULL);
  47.    
  48.     WaitForSingleObject(a, INFINITE);
  49.     WaitForSingleObject(b, INFINITE);
  50.     WaitForSingleObject(c, INFINITE);
  51.     CloseHandle(EventA);
  52.     CloseHandle(EventB);
  53.     CloseHandle(EventC);
  54.     CloseHandle(a);
  55.     CloseHandle(b);
  56.     CloseHandle(c);
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement