2013-10-23 1 views
0

Windows 파일 매핑을 사용하여 간단한 클라이언트 - 서버 프로그램을 만들고 세마포어를 사용하려고합니다. 클라이언트는 서버에 2 개의 번호를 보내고 서버는 nr1 + nr2와 nr1 * nr2를 계산합니다. 나는 무언가를 시도했지만 1 명의 클라이언트에게는 작동하지 않으며 더 많은 클라이언트에서 작동하기를 원합니다. 여기 코드는 다음과 같습니다세마포어의 WaitForSingleObject가 대기하지 않고 즉시 반환됩니다.

서버 :

#include <windows.h> 
#include <stdio.h> 
#include <iostream> 
using namespace std; 

typedef struct { 
    int nr1; 
    int nr2; 
} Mesaj; 

int main(int argc, char** argv) { 

    Mesaj* mesaj; 

    HANDLE createSemaphore = CreateSemaphore(NULL, 1, 1, "Semafor"); 
    if (createSemaphore == NULL || createSemaphore == INVALID_HANDLE_VALUE) { 
     wcout << "Failed to create a semaphore\n"; 
    } else { 
     wcout << "Created the semaphore\n"; 
    } 

    HANDLE hMemory = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, 
      PAGE_READWRITE, 0, sizeof(Mesaj), "SharedMemory"); 

    WaitForSingleObject(createSemaphore, INFINITE); 

    mesaj = (Mesaj*) MapViewOfFile(hMemory, FILE_MAP_READ, 0, 0, sizeof(Mesaj)); 

    printf("The numbers received are: %d, %d\n", mesaj->nr1, mesaj->nr2); 

    int produs = mesaj->nr1 * mesaj->nr2; 
    int suma = mesaj->nr1 + mesaj->nr2; 

    printf("\nSuma numerelor este: %d iar produsul lor este: %d", suma, produs); 

    ReleaseSemaphore(createSemaphore, 1, NULL); 

    Sleep(INFINITE); 

    return 0; 
} 

클라이언트 :

#include <windows.h> 
#include <stdio.h> 
#include <iostream> 
using namespace std; 

typedef struct { 
    int nr1; 
    int nr2; 
} Mesaj; 

int main(int argc, char** argv) { 

    Mesaj* mesaj, *mesaj2; 

    mesaj2 = (Mesaj*) malloc(sizeof(Mesaj)); 

    HANDLE hMemory = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, 
      "SharedMemory"); 

    if (hMemory == NULL) { 
     wcout << "Error at OpenFileMapping\n"; 
    } 

    HANDLE openSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,"Semafor"); 
    if(openSemaphore != NULL || openSemaphore != INVALID_HANDLE_VALUE){ 
     wcout<<"the semaphore is opened\n"; 
    } 

    mesaj2 = (Mesaj*) MapViewOfFile(hMemory, FILE_MAP_WRITE, 0, 0, 
      sizeof(Mesaj)); 

    int nr1 = 0, nr2 = 0; 
    printf("Give a number: "); 
    scanf("%d", &nr1); 
    printf("Give another number: "); 
    scanf("%d", &nr2); 

    mesaj2->nr1 = nr1; 
    mesaj2->nr2 = nr2; 

    if (mesaj2 == NULL) { 
     wcout << "Error\n" 
    } else { 
     wcout << "I sent " << mesaj2->nr1 << " and " << mesaj2->nr2 << endl; 
    } 


    system("pause"); 
    return 0; 
} 
정확히 내가 잘못 뭐하는 거지

? 세마포어는 어떻게 사용해야합니까?

+1

"작동하지 않는다"는 것을 구체적으로 설명하십시오. 한 가지는 세마포 신호를 보내지 않는 것입니다. 다른 하나는 메시지가 하나 나면 서버가 종료됩니다. 세 번째로, 두 클라이언트가 동일한 메모리를 수정하여 데이터 손상을 초래할 수 있습니다. –

+0

서버를 열 때 클라이언트가 숫자를 전송할 때까지 기다리지 않고 아무 것도하지 않기 때문에 작동하지 않습니다. – MathMe

+0

'WaitForSingleObject'가 기다리지 않고 반환된다는 문제가 있다면, 그 문제를 말하고 그 문제와 관련이 없으므로 모든 공유 메모리를 삭제하십시오. http://sscce.org/ –

답변

6

서버를 열면 클라이언트를 기다리지 않습니다.

The documentation for CreateSemaphore 그 카운트가 0보다 큰 경우에는 세마포어 객체의 상태를 시그널링

라고하고, 그 카운트가 0 인 경우 nonsignaled. lInitialCount 매개 변수는 초기 개수를 지정합니다.

세마포를 만들 때 lInitialCount=1을 전달했습니다. 1 > 0이므로 세마포가 신호되고 WaitForSingleObject이 즉시 반환됩니다.

초기 카운트가 0 인 세마포어를 만들어서 누군가가 ReleaseSemaphore을 호출하기 전까지 신호를 보내지 않도록하려는 것입니다.