i have some trouble with fork and its copy on write system. I will create params.writersCount procesess and in each i need to get its internal id( from 1 to params.writersCount). So in child i am waiting for parent process, that initialize childs internal id (writers[i] = processId). Then i can call writerSimulation and pass context address like argument, because in context.id is now right internal id for that child, because of copy on write system (context.id = j+1 will force unix to copy page, so each child has then own context copy with its internal id). But if i try to use context.id in writerSimulation function, i am getting 0. What i am doing wrong?
for(int i = 0; i < params.writersCount; i++)
{
pid_t processId = fork();
if(!processId)
{
srand((unsigned int)(seconds+getpid()));
while(!context.id)
{
for(int j = 0; j < params.writersCount; j++)
{
if(writers[j] == getpid())
{
context.id = j+1;
}
}
struct timespec wait = {.tv_sec = 0, .tv_nsec = 500000};
nanosleep(&wait, NULL);
}
int simError = writerSimulation(&context);
return simError;
}
writers[i] = processId;
}