问题描述
亲爱的,
考虑以下计划
#include< stdio.h>
#包括< unistd.h>
main()
{
int pid,* i,j;
i =& j;
* i = 10;
pid = fork();
if(pid == 0)
{
printf(" \ n地址i =%p",i);
printf(" \ n最初我在孩子的值=%d,* i);
* i = * i +10;
printf(\\ n增加后的\ n在子项中的值=%d,* i);
puts(Child terminated ....);
}
其他
{
等待((int *)0);
printf(i =%p的\ n地址,i);
printf(父项中的i的值为\\%{%d,* i);
}
i我的o / p为: -
地址i = 0xbfe6c01c
最初我估计在孩子= 10
之后增量孩子的ni值= 20
孩子被终止....
地址i = 0xbfe6c01c
我父母的价值= 10
我在父母和子女程序中的地址相同,但
子进程中所做的更改未反映在
中
父进程为什么?
Dear all,
consider the following program
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,*i,j;
i = &j;
*i= 10;
pid = fork();
if(pid == 0)
{
printf("\n address of i = %p",i);
printf("\n initially i value in child = %d",*i);
*i = *i +10;
printf("\n after incrementation i value in child = %d",*i);
puts("Child terminated....");
}
else
{
wait( (int*) 0);
printf("\n address of i = %p",i);
printf("\n value of i in parent = %d",*i);
}
i am getting o/p as:-
address of i = 0xbfe6c01c
initially i value in child = 10
after incrementation i value in child = 20
Child terminated....
address of i = 0xbfe6c01c
value of i in parent = 10
the address of i is same in both parent and child process but
the change made in child process is not reflected in the
parent process why ?
推荐答案
看起来你还没有理解过程是什么意味着或分叉()
。在许多系统上,特别是那些有fork()的系统,每个
进程都拥有它自己的内存,操作系统设置了内存
进程A中的地址0xbfe6c01c指向与进程B中相同地址不同的RAM地址。这样每个进程都可以假设
没有其他人会写入内存这个过程。
如果你想要
并发修改,你需要这些系统上的线程或共享内存。
Looks like you have yet to understand what "process" means or fork()
does. On many system, in particular those which have "fork()", every
process has it''s own memory, the OS sets things up that the memory
address 0xbfe6c01c in process A points to a different RAM address than
the same address in process B. That way every process can assume that
nobody else will write into the memory of the process.
You need threads or shared memory on these systems, if you want
concurrent modification.
看起来你还没有理解过程是什么手段或叉子()
。在许多系统中,特别是那些具有fork()的系统,每个进程都有自己的内存,操作系统设置了进程A中的内存0xbfe6c01c指向的内存一个不同的RAM地址而不是进程B中的相同地址。这样每个进程都可以假设没有其他人会写入进程的内存。
Looks like you have yet to understand what "process" means or fork()
does. On many system, in particular those which have "fork()", every
process has it''s own memory, the OS sets things up that the memory
address 0xbfe6c01c in process A points to a different RAM address than
the same address in process B. That way every process can assume that
nobody else will write into the memory of the process.
什么?没有标准的CLC答案?没有人指出这篇文章中提到的一些
推定的头文件不存在 - 不是C,
等等?没有人指出我们不知道什么是叉子。 (并且,对于
而言,关于勺子和刀子)和等待等等。 (和犹豫,
停止,以及其他......)?指出我们需要OP来
提供完整的源代码(完全符合ANSI C,当然)
这些完全未知的功能,我们甚至可以考虑
回应?
来吧,伙计们。你正在滑倒...
What? No standard CLC answers? Nobody pointing out that some of the
putative header files mentioned in this post do not exist - are not C,
etc, etc? No pointing out that we have no idea what "fork" (and, for
that matter, what about "spoon" and "knife") and "wait" (and "hesitate",
"stop", and others...) are? Pointing out that we would need the OP to
supply complete source code (in completely compliant ANSI C, of course)
for these totally unknown functions, before we can even consider
responding?
Come on, guys. You''re slipping...
当然,所有这些都完全是OT。
All completely OT, of course.
这篇关于为什么我的价值不会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!