모든 변수가 이전에 선언되었다고 가정합니다. 자식 프로세스는 그것이 실행되지 않는다고 생각하게하는 어떤 것도 출력하지 않습니다. 부모 프로세스는 공유 메모리를 얻지는 못했지만 제대로 실행됩니다. 제가 ... 코드의 길이 죄송여기에서 하위 프로세스가 아무 것도 인쇄하지 않는 이유는 무엇입니까?
// create 5 child process
for(int k=0;k<5;k++){
// fork a child process
pid = fork();
// error occured on fork
if (pid < 0) {
fprintf(stderr, "Fork Failed");
return 1;
}
// this is what the child process will run
else if (pid == 0) {
//create a shared mem segment
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
//attach the shared memory segment
shared_memory = (char *) shmat(segment_id, NULL, 0);
printf("this is child");
double x = 0;
double sum = 0;
// Run process that sums the function
for(int i=0; i<n; i++){
// get random number in range of x1-x2
x = rand()%(x2 - x1 + 1) + x1;
sum = sum + f(x);
}
//write output to the shared memory segment
sprintf(shared_memory, "%f", sum);
execlp("/bin/ls", "ls", NULL);
}
// this is what the parent process will run
else {
//print output from shared memory
printf("\n*%s", shared_memory);
//detach shared memory
shmdt(shared_memory);
//Here we add the shared memory to the array
// To add together at the end
// but since I cant get the memory to share
// the array can't be implemented
//remove the shared memory segment
shmctl(segment_id, IPC_RMID, NULL);
wait(NULL);
}
} // End of for statement
shm * 작업에 대한 오류 조건을 확인하지 않습니다. –