操作系统如何检测数字和字符串?
如果我们有一个字符串 "A"
和一个数字 65,因为它们在内存中看起来相同,那么操作系统如何知道哪个是字符串,哪个是数字?
另一个问题 - 假设程序分配一些内存(例如,一个字节)。操作系统如何记住内存分配的位置?
If we have a string "A"
and a number 65, since they look identical in memory, how does the OS know which is the string and which is the number?
Another question - assume that a program allocates some memory (say, one byte). How does the OS remember where that memory has been allocated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(5)
明媚殇2024-12-04 04:02:55
- “操作系统”应用一种算法,看起来像:“如果字符串中的每个字符都是数字,那么该字符串就是数字”,并且对于小数、+/- 等会变得更加复杂!
- http://en.wikipedia.org/wiki/Dynamic_memory_allocation!
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这些细节都不由操作系统处理。它们由用户程序处理。
对于你的第一个问题,在内存内部,字符“A”和数值 65 之间绝对没有区别(当然,假设你只是查看一个字节的数据)。当您看到程序如何解释这些位时,就会出现差异。例如,如果用户程序尝试将字符串打印到屏幕上,它可能会对操作系统进行一些系统调用,要求操作系统打印该字符。在这种情况下,操作系统中的代码由一系列汇编指令组成,用于将这些位复制到显示设备中的某个位置。然后显示器的任务是渲染一组适当的像素来绘制字符“A”。换句话说,程序从来没有“知道”该值是“A”。相反,硬件只是简单地推动控制另一段代码的位,该代码最终的任务是将这些位转换为像素。
对于你的第二个问题,这实际上取决于内存管理器。程序可以通过多种方式分配内存并了解其存储位置。我不完全确定我明白你在问什么,但我相信这个答案应该足够了:
在操作系统级别,操作系统内核甚至不知道该字节已分配。相反,操作系统只是分配巨大的内存块供用户程序在运行时使用。当程序终止时,所有内存都会被回收。
在程序级别,大多数程序都包含一个内存管理器,这是一段代码,其任务是分配大块内存并将其划分为较小的内存块,然后供程序使用。这通常将分配的内存作为“块”列表进行跟踪,其中每个内存块都被视为元素的双向链接列表。每个块通常都注释有信息,表明它正在使用以及该块有多大,这允许内存管理器在释放内存后回收内存。
在用户代码级别,当您请求内存时,通常将其存储在指针中以跟踪内存的位置。这只是内存中存储地址的一系列字节,除非有指示,否则操作系统和内存管理器永远不会查看这些字节。
希望这有帮助!
Neither of these details are handled by the operating system. They're handled by user programs.
For your first question, internally in memory there is absolutely no distinction between the character 'A' and the numeric value 65 (assuming, of course, that you're just looking at one byte of data). The difference arises when you see how those bits are interpreted by the program. For example, if the user program tries to print the string to the screen, it will probably make some system call to the OS to ask the OS to print the character. In that case, the code in the OS consists of a series of assembly instructions to replicate those bits somewhere in the display device. The display is then tasked with rendering a set of appropriate pixels to draw the character 'A.' In other words, at no point did the program ever "know" that the value was an 'A.' Instead, the hardware simply pushed around bits which controlled another piece of code that ultimately was tasked with turning those bits into pixels.
For your second question, that really depends on the memory manager. There are many ways for a program to allocate memory and know where it's stored. I'm not fully sure I understand what you're asking, but I believe that this answer should be sufficient:
At the OS level, the OS kernel doesn't even know that the byte was allocated. Instead, the OS just allocates giant blocks of memory for the user program to use as it runs. When the program terminates, all that memory is reclaimed.
At the program level, most programs contain a memory manager, a piece of code tasked with allocating and divvying up that large chunk of memory into smaller pieces that can then be used by the program. This usually keeps track of allocated memory as a list of "chunks," where each chunk of memory is treated as a doubly-linked list of elements. Each chunk is usually annotated with information indicating that it's in use and how large the chunk is, which allows the memory manager to reclaim the memory once it's freed.
At the user code level, when you ask for memory, you typically store it in a pointer to keep track of where the memory is. This is just a series of bytes in memory storing the address, which the OS and memory manager never look at unless instructed to.
Hope this helps!