KAER
KAER

Reputation: 5

Two processes handling their own lines of code in one main function (c++)

So my task goes like this:

The program needs 2 processes in one main function

Note that I am aware that the program itself is a process, but it needs to be done like that. There are some "tips" to use a file as mutex (CreateFile parameters) that would be the dsShareMode with FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE parameter.

Now my question is: how do you create 2 processes to handle its own line of code? I've seen many examples of CreateProcess function but I don't really understand the first two parameters of this function

What am I supposed to pass to it in order to run 2 processes, one to handle the user input and the other to be the "monitor"?

The first process is meant to handle those lines of code:

        std::string buffer;
        std::cout << "Enter your text:" << std::endl;
        getline(std::cin, buffer);
        HANDLE hFile = CreateFile("log.txt", FILE_APPEND_DATA, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        DWORD written = 0;
        WriteFile(hFile, buffer.c_str(), buffer.size(), &written, NULL);

While the second process should only care about this:

hFile = CreateFile("log.txt", FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        if (hFile == INVALID_HANDLE_VALUE)
        {
            std::cout << "CreateFile error " << GetLastError() << std::endl;
        }
        else
        {
            DWORD size = GetFileSize(hFile, NULL);
            std::cout << "\nCurrent file size: " << size << std::endl;
            CloseHandle(hFile);
        }

        int stringLength = 0;
        for(int i=0; buffer[i]; i++)
            stringLength++;

            std::cout << "\nCharacters given since last startup: " << stringLength << std::endl;

Upvotes: 0

Views: 550

Answers (3)

KAER
KAER

Reputation: 5

@Botje I've managed to do something like that. Could you take a look at tell me if such solution is acceptable?

int main(int argc, char *argv[])
{
std::string cmdline1 = "main Proc1";
std::string cmdline2 = "main Proc2";
std::string buffer;
HANDLE hFile;

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));

    si.cb = sizeof(si);
    CreateProcess(argv[0], const_cast<char *>(cmdline1.c_str()), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    CreateProcess(argv[0], const_cast<char *>(cmdline2.c_str()), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

if(strcmp(argv[1], "Proc1"))
{
    while(1)
    {
        std::cout << "Enter your text:" << std::endl;
        getline(std::cin, buffer);
        hFile = CreateFile("log.txt", FILE_APPEND_DATA, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        DWORD written = 0;
        WriteFile(hFile, buffer.c_str(), buffer.size(), &written, NULL);
    }
}

if(strcmp(argv[1], "Proc2"))
{
    DWORD charactersGiven = 0;
    while(1)
    {
        hFile = CreateFile("log.txt", GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
            if (hFile == INVALID_HANDLE_VALUE)
            {
                std::cout << "CreateFile error " << GetLastError() << std::endl;
            }
            else
            {
                DWORD size = GetFileSize(hFile, NULL);
                std::cout << "\nCurrent file size: " << size << std::endl;
                if(charactersGiven == 0)
                charactersGiven = size;

                std::cout << "Characters given since last startup: " << size - charactersGiven << std::endl;
            }
            Sleep(4000);
        }
}
return 0;
}

Upvotes: 0

Den-Jason
Den-Jason

Reputation: 2573

The question appears to demand having the same program being run as two separate processes. If that is the case, the program will need to handle command line arguments and tailor its functionality accordingly.

Upvotes: 0

Botje
Botje

Reputation: 30830

Assuming you have a separate helper.exe, you can do:

CreateProcess(nullptr, "helper logger-mode", ...)

and

CreateProcess(nullptr, "helper monitor-mode", ...)

This will create two processes that see either logger-mode or monitor-mode in their second argument (argv[1]).

Upvotes: 1

Related Questions