跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] Linux 写时复制问题

发表于
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int g=100;

int main()
{

	static int x=666;

	
	pid_t pid = fork();


	if (pid==-1){
		printf("创建进程失败\r\n");
	}

	if (pid==0){
		printf("pid=%d g=%d,x=%d,g=%p,x=%p\r\n",pid,g,x,&g,&x);
		g=111;
		x=222;

		printf("我子进程 pid=%d,g=%d,x=%d,g=%p,x=%p\r\n",pid,g,x,&g,&x);
	}else{

		printf("pid=%d,g=%d,x=%d,g=%p,x=%p\r\n",pid,g,x,&g,&x);
		g=666;
		x=777;
		printf("我是父进程 pid=%d,g=%d,x=%d,g=%p,x=%p\r\n",pid,g,x,&g,&x);
		
	}


	return 0;
}

输出

pid=4502,g=100,x=666,g=0x601044,x=0x601048
我是父进程 pid=4502,g=666,x=777,g=0x601044,x=0x601048
pid=0 g=100,x=666,g=0x601044,x=0x601048
我子进程 pid=0,g=111,x=222,g=0x601044,x=0x601048

我的问题是:

子进程修改变量后,会重新开启一块新内存,再我重新修改变量值后,为什么在打印变量的地址还是相同的?

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.