操作系统如何检测数字和字符串?

发布于 11-27 04:02 字数 134 浏览 1 评论 0原文

如果我们有一个字符串 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

坦然微笑2024-12-04 04:02:55

这些细节都不由操作系统处理。它们由用户程序处理。

对于你的第一个问题,在内存内部,字符“A”和数值 65 之间绝对没有区别(当然,假设你只是查看一个字节的数据)。当您看到程序如何解释这些位时,就会出现差异。例如,如果用户程序尝试将字符串打印到屏幕上,它可能会对操作系统进行一些系统调用,要求操作系统打印该字符。在这种情况下,操作系统中的代码由一系列汇编指令组成,用于将这些位复制到显示设备中的某个位置。然后显示器的任务是渲染一组适当的像素来绘制字符“A”。换句话说,程序从来没有“知道”该值是“A”。相反,硬件只是简单地推动控制另一段代码的位,该代码最终的任务是将这些位转换为像素。

对于你的第二个问题,这实际上取决于内存管理器。程序可以通过多种方式分配内存并了解其存储位置。我不完全确定我明白你在问什么,但我相信这个答案应该足够了:

  1. 在操作系统级别,操作系统内核甚至不知道该字节已分配。相反,操作系统只是分配巨大的内存块供用户程序在运行时使用。当程序终止时,所有内存都会被回收。

  2. 在程序级别,大多数程序都包含一个内存管理器,这是一段代码,其任务是分配大块内存并将其划分为较小的内存块,然后供程序使用。这通常将分配的内存作为“块”列表进行跟踪,其中每个内存块都被视为元素的双向链接列表。每个块通常都注释有信息,表明它正在使用以及该块有多大,这允许内存管理器在释放内存后回收内存。

  3. 在用户代码级别,当您请求内存时,通常将其存储在指针中以跟踪内存的位置。这只是内存中存储地址的一系列字节,除非有指示,否则操作系统和内存管理器永远不会查看这些字节。

希望这有帮助!

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:

  1. 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.

  2. 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.

  3. 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!

满天都是小星星2024-12-04 04:02:55

第二 - 系统保留(某个进程的)所有分配的记录,因此可以在进程终止时删除它们。我建议您阅读一本有关操作系统原理的书(例如 Tanenbaum 的“现代操作系统”)。

No. 2 - the system keeps a record of all allocations (of a certain process) and can thus remove them e.g. when the process terminates. I propose you read a book on operating system priciples (e.g. Tanenbaum's "Modern Operating Systems").

小女人ら2024-12-04 04:02:55
  1. 字符“A”和整数 65 以相同的方式(至少在 32 位系统上)存储在内存中。然而,字符串“A”的存储方式不同,这可能取决于系统或编程语言。以 C 为例,它本质上将字符串存储为字符数组,后跟空字符。

  2. 操作系统使用内存管理器来跟踪哪个进程正在使用内存的哪些部分。操作系统

  1. The character 'A' and the integer number 65 are stored the same way (atleast on 32bit systems) in memory. The string "A" however is stored differently, and that can depend on the system or the programming language. Take for example C which will stored strings as essentially an array of the characters followed by the null character.

  2. Operating Systems use memory managers to keep track of which process are using which parts of memory.

蓬勃野心2024-12-04 04:02:55
  1. 对于计算机来说,字符串就是数字。最简单的例子是 ASCII 表,其中每个字母都附有一个数字。因此,如果您熟悉 C,则可以编写 printf("%c", 0x65) 并实际获得 A 而不是数字。希望这是有道理的。

  2. 操作系统不记得程序分配的内存位置。这就是指针的用途!

  1. For a computer, a string is a number. A simpliest example would be an ASCII table where for every letter there is a number attached. So if you're familiar with C, you could write printf("%c", 0x65) and actually get a A instead of number. Hope that made sense.

  2. OS don't remember the location of the memory the program has allocated. That's what pointers are for!

明媚殇2024-12-04 04:02:55
  1. “操作系统”应用一种算法,看起来像:“如果字符串中的每个字符都是数字,那么该字符串就是数字”,并且对于小数、+/- 等会变得更加复杂!
  2. http://en.wikipedia.org/wiki/Dynamic_memory_allocation
  1. The 'OS' applies an algorithm, which will look something like: "if every character in the string is a number, then the string is a number", and gets more complicated for decimals, +/-, etc!
  2. http://en.wikipedia.org/wiki/Dynamic_memory_allocation!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文