Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the meaning of deep copy and shallow copy in Linux and copy when writing?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "deep copy and shallow copy in Linux and what is the meaning of copy when writing". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "deep copy and shallow copy in Linux and what is the meaning of copying when writing".

Suppose B replicates A, and when you modify A, see if B will change. If B changes with it, it means it's a shallow copy; if B doesn't change, it's a deep copy.

1. Shallow copy: assign the reference of the original object directly to the new object, which is just a reference to the original object.

2. Deep copy: create a new object and array and copy the "values" of the attributes of the original object (all the elements of the array), which is a "value" rather than a "reference".

The shallow copy is just a copy of the pointer. After the copy, the two pointers point to the same memory space. The deep copy copies not only the pointer, but also what the pointer points to. The pointer after the deep copy is the pointer to two different addresses.

3. Write-time replication technology: originally produced in the Unix system, it is used to achieve a silly process creation: when issuing a fork () system call, the kernel copies the entire address space of the parent process as is and allocates that copy to the child process. This behavior is very time-consuming because it requires:

Assign pages to the page table of a child process

Assign pages to the pages of a child process

Initialize the page table of a child process

Copy the page of the parent process to the corresponding page of the child process

This approach to creating an address space involves a lot of memory access, consumes many CPU cycles, and completely destroys the contents of the cache. In most cases, this is often pointless because many child processes begin their execution by loading a new program, thus completely discarding the inherited address space.

The current Unix kernel (including Linux) uses a more efficient approach called write-time replication (or COW). The idea is fairly simple: the parent and child processes share the page instead of copying it. However, as long as pages are shared, they cannot be modified. Whenever the parent and child processes try to write a shared page, an error is generated, and the kernel copies the page to a new page and marks it as writable. The original page is still write-protected: when other processes try to write, the kernel checks whether the write process is the sole owner of the page; if so, it marks the page as writable to the process.

Fork () of Linux uses write-time replication

The traditional fork () system call directly copies all resources to the newly created process. This implementation is too simple and inefficient because the data it copies may be shared. To make matters worse, if the new process intends to execute a new image immediately, all copies will be wasted. Fork () of Linux is implemented using a copy-on-write (copy-on-write) page.

Copying while writing is a technique that can delay or even avoid copying data. The kernel does not copy the address space of the entire process at this time, but allows the parent and child processes to share the same address space. The address space is copied only when it needs to be written, so that each process has its own address space. That is, resources are replicated only when they need to be written, and before that, they can only be shared as read-only. This technique delays the copy of the page in the address space until the write actually occurs. In cases where the page will not be written at all-for example, if exec () is executed immediately after fork (), the address space does not need to be copied. The actual cost of fork () is to copy the page table of the parent process and create a process descriptor for the child process. In general, the process is created to run an executable file immediately, and this optimization avoids copying large amounts of data that will not be used at all (the address space often contains tens of megabytes of data). This optimization is important because Unix emphasizes the ability of processes to execute quickly.

Thank you for your reading, the above is "deep copy and shallow copy in Linux and what is the meaning of copy when writing". After the study of this article, I believe you have a deeper understanding of what it means to copy deep and shallow in Linux, as well as what it means to write, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report