简体   繁体   中英

Manipulating a string in shared memory with fork() c++

I have a string ( fileContents ) in shared memory that consists of a 9 lines:

sprintf(shared_memory, fileContents.c_str());

I want to call on fork() to create the same number of processes as lines. These processes will manipulate each line. However, I have no idea where to start when calling fork() . Every example I have looked at just consists of returning the process ID of parents and child processes and not showing how or when the processes executes something.

Any guidance would be appreciated, thanks!

Every example I have looked at just consists of returning the process ID of parents and child processes

That's not correct.

The parent process will get the process id of the child process, but the child process will know it's the child process because fork() returns 0.

This code will fork 9 times, with each child doing specific work.

for( int line = 1; line <= 9; ++line ) // *cough*
{
    if ( fork() == 0 )
    {
        // Child process.  Handle line, and exit()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM