In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to copy while writing on C++". In daily operation, I believe many people have doubts about how to copy when writing on C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to copy when writing on C++". Next, please follow the editor to study!
I. what is copy at write time
Replication on write is an optimization strategy in the field of computer programming. The core idea is that if multiple callers request the same resource (such as data storage on memory or disk) at the same time, they will jointly obtain the same pointer to the same resource, and the system will not really copy a dedicated copy (private copy) to the caller until one caller tries to modify the content of the resource, while the original resources seen by other callers remain unchanged.
This process is transparently to other callers.
The main advantage of this approach is that if the caller does not modify the resource, no copy will be made, so multiple callers can share the same resource just by reading.
Copy-when-writing technology is a very important optimization means, the core is to deal with entity resource requests lazily, only to share resources among multiple entity resources, but not to really copy resources at first. Private resources are really allocated to entities only when they need to modify resources. But the technology of copying when writing also has its advantages and disadvantages:
1. Copy-on-write technology can reduce the instantaneous delay caused by allocating and replicating a large number of resources, but in fact, this delay is attached to subsequent operations.
2. Copying technology at write time can reduce unnecessary resource allocation. For example, in the case of the fork process, not all pages need to be copied, and the code segments and read-only data segments of the parent process are not allowed to be modified, so there is no need to copy.
Second, the application of copying technology when writing.
1. Write-time replication in virtual memory management
Such shared-accessed pages are generally marked as read-only. When a task attempts to write data to memory, the memory management unit (MMU) throws an exception. When the kernel handles the exception, the kernel allocates a copy of physical memory to the task and copies the data to this memory, re-issuing a write operation to the MMU to perform the task.
For example, fork () of Linux uses copy-on-write pages to create new processes, which is a technology that can postpone or even avoid data copying. At the beginning, the kernel does not copy the entire address space, but lets the parent-child process share the address space, and only copies the address space when writing, so that the parent-child process has an independent address space, that is, resource replication occurs only when it needs to be written. Previously, resources were shared with the parent process by reading, so that in the scenario where the page will not be written at all, fork () executes exec () immediately without copying the address space. The actual cost of fork () is to copy a page table of the parent process and create a process descriptor for the child process, that is, only when the memory content of each segment in the process space changes. The parent process makes a copy of its contents and passes it to the child process, which greatly improves the efficiency.
2. Write-time replication in data storage
File management systems such as Linux use a write-time replication strategy.
For example, for example, we have a program to write files that constantly write according to the data from the network. If every fwrite or fprintf has to perform a disk Icano operation, it will simply be a huge loss of performance.
So the usual practice is that each write to a file is written in a specific size of memory (disk cache), which is written to disk only when we close the file (which is why what is written is lost if the file is not closed).
3. Write-time replication in software applications
The string class in the STL standard template library that we often use is also a class with write-only copy technology. To improve performance, many classes in STL use write-time copy technology. However, this strategy has been removed to improve parallelism in the Category 11 standard.
Class String {public: / / constructor (split memory) String (char* tmp) {_ Len = strlen (tmp); _ Ptr = new char [_ Len + 1 + 1]; strcpy (_ Ptr, tmp); / / set reference count at the end of the array _ Ptr [_ Len + 1] = 0;} / / destructor ~ String () {/ / reference count minus one _ Ptr [_ Len + 1]-- / / when the reference count is 0, free memory if (_ Ptr [_ Len + 1] = = 0) {delete [] _ Ptr }} / / copy construction (shared memory) String (string& str) {if (this- > _ Ptr! = str) {/ / shared memory, .data () returns the pointer const char* p = str.c_str () that converts the type of string to char; char* pp; strcmp (pp, p); this- > _ Ptr = pp This- > _ Len = str.size (); this- > _ Ptr [_ Len + 1] + +; / / reference count plus a}} / / pair of [] characters are overloaded. When operating on a string, copy char& operator [] (unsigned int idx) {if (idx > _ Len | | _ Ptr = = 0) {static char nullchar = 0; return nullchar when you start writing. } / / reference count minus one _ Ptr [_ Len + 1] -; char* tmp = new char [_ Len + 1 + 1]; strncpy (tmp, _ Ptr, _ Len + 1); _ Ptr = tmp; / / set reference count for new shared memory _ Ptr [_ Len + 1] = 0; return _ Ptrx [IDX];} private: int _ Len; char* _ Ptr;} At this point, the study of "how to copy when writing C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.