V
V
Venot2020-12-05 18:00:36
Programming
Venot, 2020-12-05 18:00:36

What is a program code segment in a process?

Everywhere it is written that the process consists of a program code segment, data, stack and heap. What is a code segment? Type of the instruction whence the flow takes the information on performance? And it turns out that the OS takes these instructions and sends them further?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Karpion, 2020-12-05
@Venot

First you need to understand the concept of "address space" - how it differs from "memory". Learn how "computer address space" differs from "process address space" (in particular, each process has its own address space).
Further, it would be nice to know the segment addressing system of *86 processors - a significant part of the terminology comes from there, although in reality, after the advent of 32-bit addressing on the i386 processor, segments are practically not used.
Well, you need to have an understanding of assembly language programming.
As a first approximation, the start of the process looks like this:

  • The kernel creates data structures for the process. Incl. - allocation of address space.
  • The kernel looks into the program file and allocates space for the program code of the program (here I'm talking in the context of "a process is an executing program"). This place (and/or the code placed there) is part of a "code segment". If one program is launched several times, its code segment gets into several processes.
  • The kernel looks at the list of shared libraries in the program file. If these libraries are not loaded by previously running processes, address space is also allocated for these libraries; and this address space is also included in the code segment. But you need to understand that the address space of each library falls into all processes that use this library. Ideally, the library in all processes should be at the same address in the address space; but sometimes it doesn't.
  • Along the way, in the process of work, the program can load new libraries. Their address space will also fall into the code segment.
  • As a rule, the code of the program and the code of each library are contiguous. But there are gaps between them.
  • To work, you need to allocate address space for the stack. Ideally, one d.b. continuous. But sometimes - you have to pervert and make it discontinuous. This = stack segment. It is actively used when calling subroutines / procedures / functions - arguments are passed through the stack, local data of subroutines / etc. are placed on the stack. The stack is especially actively used during recursion; whereas in ancient languages ​​the prohibition of recursion was often dispensed with by static allocation of arguments and local variables.
  • For data placed in areas reserved by the malloc() function, the heap is used. It is allowed to be non-continuous, i.e. made up of broken pieces.

The phrase "the thread takes information for execution" says that you read bad books (sorry for being direct).
Program code instructions - takes the processor (or core - if the processor is multi-core), i.e. piece of iron. The operating system kernel only prepares program code instructions so that the processor can take them. The kernel does this once at process start; well also makes amendments at a swapping/paging. And the processor executes each instruction of the code; Well, so that you understand better: every addition or other arithmetic operation, every transfer of data is an instruction.
By the way, everything that the OS kernel does is written with similar code instructions. But the kernel is additionally allowed special instructions.
It seems to me that it is too early for you to understand the concept of "flow". First you need to understand how the system works, in which threads are not implemented - i.e. Every process is single threaded. And then already - to deal with flows.

V
Vladimir Korotenko, 2020-12-05
@firedragon

By the way, this is not on all architectures. Just the most settled

V
Vasily Bannikov, 2020-12-05
@vabka

In Google you can easily find an article about memory segments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question