为什么这里的子进程不打印任何东西?
假设所有变量都已在之前声明过...因为它们已经声明过。 子进程不会打印任何让我认为它没有被执行的内容。 父进程运行良好,尽管它没有获得共享内存。 对于这段代码的长度,我深表歉意......
// 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
Assume all the variables have been declared previously... because they have been. The child process does not print anything which makes me think it's not being executed. The parent process runs fine, albeit it doesn't get the shared memory.
I apologize for the length of this code...
// 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C stdout 流在内部缓冲数据。 很可能您的“这是子”消息正在被缓冲,并且缓冲区没有被 execlp 刷新,所以它就消失了。 在 printf 之后尝试
fflush(stdout);
。 顺便说一句,您也应该在fork()
之前执行此操作,这样子进程和父进程就不会都尝试写入在 fork 之前缓冲的输出。The C stdout stream internally buffers data. It's likely that your "this is child" message is being buffered, and the buffer isn't being flushed by execlp, so it just disappears. Try a
fflush(stdout);
after the printf. Incidentally, you should do this before thefork()
as well, so that child and parent don't both try to write output buffered from before the fork.打印到 stderr 它没有被缓冲。
此外,共享内存内容(segment_id、shared_memory)未在父级中初始化(或者如果是,则与子级不同)。
此外,当子进程仍在处理时,父进程可能会破坏共享内存。 父级应先等待,然后处理子级产生的数据。
Print to stderr it is not buffered.
Also the shared memory stuff (segment_id, shared_memory) is not initialized in the parent (or if it is then it is not the same as the child).
Furthermore the parent is potentially destroying the shared memory stuff while the child is still processing. The parent should wait first then process the data produced by the child.
首先清除所有共享内存的东西,然后看看子进程是否可以成功 printf。
从该代码片段来看,您正在子进程中初始化共享内存,但不是在父进程中初始化。
Get rid of all the shared memory stuff first and see if the child process can printf successfully then.
By the look of that snippet, you are initializing shared_memory in the child process, but not in the parent process.