I am working on a C program where I need to alter the behavior of a task (Tarefa). I am using fork to create a child process, which makes some changes that need to be communicated back to the parent process. However, I am encountering an issue where some changes seem to get lost after the waitpid call.
Here is the code snippet I am working with:
void alterarComportamento(Tarefa* t) {
int * atuador = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
int * nivel_de_atividade = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
int * stMudanca = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
pid_t child;
int stChild;
child = fork();
if(child < 0) {
perror("fork");
exit(0);
}
if(child == 0) {
*atuador = t->atuador;
*stMudanca = 0;
int chance = rand() % 100;
if (chance <= 20) {
*stMudanca = 1;
exit(0);
} else {
*nivel_de_atividade = t->nivel_de_atividade;
pthread_mutex_lock(mutexAtuador[*atuador]);
atuadorTable[*atuador] = *nivel_de_atividade;
sleep(rand() % 2 + 2);
pthread_mutex_unlock(mutexAtuador[*atuador]);
exit(0);
}
}
waitpid(child, &stChild, 0);
int chance = rand() % 100;
if (chance <= 20 || *stMudanca == 1) {
printf("Atuador: %d Falhoun", *atuador);
sleep(1);
} else {
printf("Atuador: %d, Nivel de atividade: %dn", *atuador, *nivel_de_atividade);
sleep(1);
}
pthread_mutex_lock(&mutexTarefasConcluidas);
tarefasConcluidas++;
printf("Tarefas concluidas: %dn", tarefasConcluidas);
if (tarefasConcluidas == MAX_ITEMS) {
signal_thread_exit();
}
pthread_mutex_unlock(&mutexTarefasConcluidas);
}
I have a thread pool that calls this function
void submeterTarefa(Tarefa t) {
pthread_mutex_lock(&mutexFila);
tarefaFila[tarefaCount] = t;
tarefaCount++;
pthread_mutex_unlock(&mutexFila);
pthread_cond_signal(&condFila);
}
void* unidade_controle(void* args) {
while (!should_exit) {
Tarefa t;
pthread_mutex_lock(&mutexFila);
while (tarefaCount == 0 && !should_exit) {
pthread_cond_wait(&condFila, &mutexFila);
}
if (should_exit) {
pthread_mutex_unlock(&mutexFila);
break;
}
t = tarefaFila[0];
for (int i = 0; i < tarefaCount - 1; i++) {
tarefaFila[i] = tarefaFila[i + 1];
}
tarefaCount--;
pthread_mutex_unlock(&mutexFila);
alterarComportamento(&t);
}
printf("Thread exiting...n");
pthread_exit(NULL);
}
Issues:
1 – After the waitpid call, some of the changes made by the child process (e.g., *stMudanca and *nivel_de_atividade) seem to be lost when the parent process tries to read them.
2 – I need clarification on whether the use of mmap for shared memory between parent and child is correctly implemented in this context.
Environment:
OS: Linux
Compiler: GCC
Pthreads for mutex
Any help or insights would be greatly appreciated!
1 – Why do some changes seem to be lost after the waitpid call? How can I ensure that the parent process accurately receives the changes made by the child process? Like I need to print x items, but some get lost.
2 – Is mmap correctly used to share memory between parent and child processes in this code? If not, what is the correct approach?
Lucas Broering is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.