当前位置: 技术问答>linux和unix
Unix高级环境编程的一条例题
来源: 互联网 发布时间:2016-02-01
本文导语: 既然是父子进程交替地对内存增加1,最后结果应该2000啊,为什么实际输出结果是1000? 如果说是fork后进程空间复制了一份,父子进程只是对各自的变量增自,那么update函数的返回值该如何解释呢?代码如下: #include...
既然是父子进程交替地对内存增加1,最后结果应该2000啊,为什么实际输出结果是1000?
如果说是fork后进程空间复制了一份,父子进程只是对各自的变量增自,那么update函数的返回值该如何解释呢?代码如下:
#include "apue.h" //包括TELL_WAIT()等进行同步的函数
#include
#include
#define NLOOPS 1000
#define SIZE sizeof(long) /* size of shared memory area */
static int
update(long *ptr)
{
return((*ptr)++); /* return value before increment */
}
int
main(void)
{
int fd, i, counter;
pid_t pid;
void *area;
if ((fd = open("/dev/zero", O_RDWR))
如果说是fork后进程空间复制了一份,父子进程只是对各自的变量增自,那么update函数的返回值该如何解释呢?代码如下:
#include "apue.h" //包括TELL_WAIT()等进行同步的函数
#include
#include
#define NLOOPS 1000
#define SIZE sizeof(long) /* size of shared memory area */
static int
update(long *ptr)
{
return((*ptr)++); /* return value before increment */
}
int
main(void)
{
int fd, i, counter;
pid_t pid;
void *area;
if ((fd = open("/dev/zero", O_RDWR))