实验四 进程通信

宏定义
https://www.cnblogs.com/fnlingnzb-learner/p/6903966.html

命名管道参考文章:

https://blog.csdn.net/MoreWindows/article/details/8260087?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://blog.csdn.net/zhangye3017/article/details/80189861?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

https://blog.csdn.net/qq_42659468/article/details/90414265?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-2

https://blog.csdn.net/zhangye3017/article/details/80189861?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

https://blog.csdn.net/qq_36829091/article/details/80138836

参数见MSDN

涉及函数

第一个进程:
//线程函数的编写
DWORD WINAPI server(LPVOID param)

对于DWORD WINAPI的类比解释
https://www.cnblogs.com/ay-a/p/8763274.html

其实线程的返回值不用一定返回DWORD WINAPI,void也行,至少不影响程序,但是一般都是写成DWORD WINAPI。算是标明系统调用?

HANDLE Pile = CreateNamedPipe(TEXT("\\.\Pipe\mypipe"), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0); //创建了一个命名管道实现进程通信

ConnectNamedPipe(Pile, NULL)
//指示一台服务器等待下去,直至客户机同一个命名管道连接。

https://blog.csdn.net/MoreWindows/article/details/8260087?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

CreateFile、WriteFile、ReadFile

https://blog.csdn.net/Avalon_Y/article/details/51520447?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

https://blog.csdn.net/zzq060143/article/details/55263169

client

#include 
#include 
#include 
using namespace std;

DWORD WINAPI client(LPVOID param)
{
    //DWORD wlen = 0;
    Sleep(1000); //µÈ´ýpipe
    if (WaitNamedPipe(TEXT("\\\\.\\Pipe\\mypipe"), NMPWAIT_WAIT_FOREVER) == FALSE) {
        cout << "ʧ°Ü" << endl;
        return 0;
    }
    HANDLE wait_Pile = CreateFile(TEXT("\\\\.\\Pipe\\mypipe"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    while (true) {
        char rbuf[256];
        DWORD len_r = 0;
        ReadFile(wait_Pile, rbuf, sizeof(rbuf), &len_r, 0);
        cout << "thread2 receive data " << rbuf << endl;
		//end
        if (strcmp(rbuf, "end") == 0) {
            cout << "end" << endl;
            break;
        }
        char wbuf[256] = "ok";
        DWORD len_w = 0;
		//end
        /*if (rand() % 2 == 0) {
            char end[256] = "end";
            cout << "thread2 send data end" << endl;

            WriteFile(wait_Pile, "end", sizeof(end), &len_w, 0);
            break;
        }*/
        WriteFile(wait_Pile, wbuf, sizeof(wbuf), &len_w, 0);
        cout << "thread2 send data " << wbuf << endl;
    }
    return 0;
}

int main()
{
    HANDLE threads[1];
    threads[0] = CreateThread(0, 0, client, 0, 0, 0);
    WaitForMultipleObjects(1, threads, TRUE, INFINITE);
    return 0;
}


你可能感兴趣的:(操作系统)