Page 1 of 1 [ 8 posts ] 

MrOddBall
Velociraptor
Velociraptor

User avatar

Joined: 3 Feb 2014
Age: 124
Gender: Male
Posts: 426
Location: Here, there, and everywhere

01 Apr 2014, 1:23 am

Okay I know so far that Linux processes are created with the fork() system call and that the parent and child processes share the address space. If the parent closes a waitpid() is called before the child process can be terminated.

How am I doing so far ? Do I need to read up on how Linux processes work again ? How exactly does this work with the parent and child processes sharing the same address spaces and is that even how it works ?

I'm not sure I'm getting the correct picture here :/ Also do not turn this into some f*****g OS fanboy debate because I could careless about that s**t. PM me if you would like to help me out through messages at the risk of such a thing happening please. Thanks you :D

If you can't resist arguing pointlessly over this sort of thing, here's my middle finger to you:

Image

Okay moving on here's a little something I might know

Image

If I can remember correctly, the stack is used mainly for the variables and the heap is used for the constants right ?

The text section would contain the program code, the bss section the declared variables and the data is the executed program ? Am I even on the right track here ?



drh1138
Velociraptor
Velociraptor

User avatar

Joined: 2 Dec 2012
Gender: Male
Posts: 498

01 Apr 2014, 2:32 am

MrOddBall wrote:
If I can remember correctly, the stack is used mainly for the variables and the heap is used for the constants right ?

The text section would contain the program code, the bss section the declared variables and the data is the executed program ? Am I even on the right track here ?


You're not too far off; your questions are pretty sharp, actually.

The stack is used for so-called automatic variables, which are local to the stack frame of the presently executing function. The heap, sometimes called the free store, is for dynamically-allocated variables which persist across function calls and returns (these you access by reference or pointer).

BSS is for statically-allocated variables which are uninitialized (zeroed out when the program begins), the data segment is for what you thought the BSS was (holding global/static variables).



one-A-N
Veteran
Veteran

User avatar

Joined: 2 Mar 2010
Age: 69
Gender: Male
Posts: 883
Location: Sydney

01 Apr 2014, 5:21 am

MrOddBall wrote:
Okay I know so far that Linux processes are created with the fork() system call and that the parent and child processes share the address space.


No. They do not share the same address space: "In Unix systems equipped with virtual memory support (practically all modern variants), the fork operation creates a separate address space for the child." (my emphasis) - see http://en.wikipedia.org/wiki/Fork_%28system_call%29



MrOddBall
Velociraptor
Velociraptor

User avatar

Joined: 3 Feb 2014
Age: 124
Gender: Male
Posts: 426
Location: Here, there, and everywhere

01 Apr 2014, 8:00 am

drh1138 wrote:

The stack is used for so-called automatic variables, which are local to the stack frame of the presently executing function. The heap, sometimes called the free store, is for dynamically-allocated variables which persist across function calls and returns (these you access by reference or pointer).

BSS is for statically-allocated variables which are uninitialized (zeroed out when the program begins), the data segment is for what you thought the BSS was (holding global/static variables).


I still need to read up on a stack frame and I wonder if the heap is used by C pointers as well ? From my little understanding about pointers in C is that they merely reference data or points to something in an address ? Like if you declared something like x to have the value 5 in C, you can also have another variable point to it ?

The BSS is only temporal for variables you declare ?

I also wonder how the Linux kernel is layered like I understand you have the lowest which is the hardware level, and then the higher kernel layer which communicates with the hardware and then the user layer which is for all your programs and etc. So basically the kernel hands all the input and output functions at an assembly level, or is like running an endlessly looping c program which all your programs and etc run atop ?

Thanks for your help by-the-way :D



MrOddBall
Velociraptor
Velociraptor

User avatar

Joined: 3 Feb 2014
Age: 124
Gender: Male
Posts: 426
Location: Here, there, and everywhere

01 Apr 2014, 8:06 am

one-A-N wrote:

No. They do not share the same address space: "In Unix systems equipped with virtual memory support (practically all modern variants), the fork operation creates a separate address space for the child." (my emphasis) - see http://en.wikipedia.org/wiki/Fork_%28system_call%29


Okay, maybe I'm biting off more than I can chew here :/ I'm not saying that I can't learn this stuff but maybe I should start smaller and with simpler concepts ?



Kurgan
Veteran
Veteran

User avatar

Joined: 6 Apr 2012
Age: 35
Gender: Male
Posts: 4,132
Location: Scandinavia

01 Apr 2014, 3:38 pm

Actually, the fork creates a separate adress space for the child. This is how it is in most (if not all) modern UNIX-like systems that use processes. Fork is the only way to create a new process in Linux and BSD, I think. If you're interested, get the book Operating Systems: Principles and Practice, by Anderson and Dahlin. It features a lot of interesting stuff you can't look up on Google just like that.



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 36
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

02 Apr 2014, 4:48 pm

MrOddBall wrote:
I also wonder how the Linux kernel is layered like I understand you have the lowest which is the hardware level, and then the higher kernel layer which communicates with the hardware and then the user layer which is for all your programs and etc. So basically the kernel hands all the input and output functions at an assembly level, or is like running an endlessly looping c program which all your programs and etc run atop ?


The linux kernel isn't as strictly layered as you're thinking it is. At a very high level, the kernel code is entirely modular - each distribution (or, sometimes, user) compiles whatever modules they choose to give them the functionality they want - they can also load kernel modules in during runtime. Each specific component of the system (network, storage, etc) has their own vertical tower of modules that implement both the kernel-userland API/ABI as well as the in-kernel API for other towers. Then, within each tower there are separate layers to implement each layer of functionality (hardware drivers, thread handlers, up through the userland API/ABI). These layers are drawn slightly differently depending on which system component you're talking about. Here's a pretty good graphical representation of what I'm talking about:

[img][800:768]http://upload.wikimedia.org/wikipedia/commons/5/5b/Linux_kernel_map.png[/img]

The kernel includes code to execute one root userland process at boot - usually this is init. init is almost always the root parent of every linux system.


_________________
Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned.


LupaLuna
Veteran
Veteran

User avatar

Joined: 14 Jan 2013
Age: 53
Gender: Male
Posts: 1,551
Location: tri-cities WA

06 Apr 2014, 2:24 am

MrOddBall wrote:
If I can remember correctly, the stack is used mainly for the variables and the heap is used for the constants right ??


A stack is not a place for variables or to random access. It is use for storing Program Line Counter(PLC) locations. If you call a subroutine, the current PLC location is "pushed" into the stack and when you return from that subroutine? the stack "pops" the stored PLC location so the PLC can return to the exact spot where it left before the subroutine was called. Think of a stack like leaving a crumb trail while exploring the maze of caves so you can backtrack where you came from and you don't get lost.

Another analogy for describing the mechanics of a stack is think of it like stacking books one on top of the other. if you add a book to the stack, it's called "pushing". If you take a book off the stack, It's called "popping". It's important to know that you can only add or remove books from the top of the stack, you can't add or remove them from any where else.