Home > Software engineering >  What are the exact programming instructions that are in user space?
What are the exact programming instructions that are in user space?

Time:10-30

I know that a process switches between user mode and kernel mode for running. I am confused that for every line of code, we should possibly need the kernel. Below is the example, could I get explanation of the kernels role in execution of the following coding lines. Does the following actually require kernel mode.

if(a < 0) a

CodePudding user response:

I am confused that for every line of code, we should possibly need the kernel.

Most code in user-space is executed without the kernel being involved. The kernel becomes involved (and the CPU switches from user-space to kernel) when:

a) The user-space code explicitly asks the kernel to do something (calls a system call).

b) There's an IRQ (from a device) that interrupts user-space code.

c) The kernel is providing some functionality that user-space code is unaware of. The most common reason is virtual memory management; but debugging and profiling are other reasons.

d) Asynchronous notifications (e.g. something causing a switch to kernel so that kernel can redirect the program to a suitable signal handler).

e) The user-space code does something illegal (crashes).

Does the following actually require kernel mode.

That code (if(a < 0) a ;) probably won't require kernel's assistance; but it possibly might. For example, if the variable a is in memory that was previously sent to swap space, then any attempt to access a is a request for the kernel to fetch that data from swap space. In a similar way, if the executable file was memory mapped but not loaded yet (a common optimization to improve program startup time), then attempting to execute any instruction (regardless of what the instruction is) could ask the kernel to fetch the code from the executable file on disk.

CodePudding user response:

Whatever the code we write is but obvious in the realm of user mode.. Kernel mode is only going to be in picture when you write any code that performs any system call..

and since the if() is not calling any system function it's not going to be in kernel mode.

CodePudding user response:

Short answer: It depends on what you are trying to do, following code depending on which enviroment and how its compiled it shouldn't need to use the kernel.

Long answer: Comparing something and increasing value of something shouldn't require use of a kernel, On x86 (64 bit) architecture following could be represented like this (in nasm syntax):

; think this like the "a" with value set to -1
mov rax, -1
; cmp instruction
cmp rax, 0
jb increase
increase:
inc rax

Clearly there aren't any syscalls so this piece of code can be ran on any x86 device but it wouldn't be meaningful What requires kernels are the system calls now sys calls aren't required to have a device that can output something in theory you can output something by finding a memory location that let's say corresponds to video memory and you can manipulate pixels to output something in the screen but for userland this isn't possible due virtual memory.

A userspace application needs a kernel to exist if a kernel did not exist then userspace wouldn't exist :) and please note not every kernel let's a userspace.

So only doing something like:

write(open(stdout, _O_RDWR), "windows sucks linux rocks", 24);

would obviously require a kernel.

Writing / reading to arbitary memory location for example: 0xB8000 to manipulate video memory doesn't need a kernel.

TL:DR; For example code you provided it needs a kernel to be in userspace but can be written in a system where userspace and kernel doesn't exist at all and work perfectly fine (eg: microcontrollers)

In simpler words: It doesn't require a kernel to be work since it doesn't use any system calls, but for meaningful operation in a modern operating system it would atleast require a exit syscall to exit with a code otherwise you will see Segmentation fault even though there isn't dynamic allocation done by you.

  • Related