Skip to content

Instantly share code, notes, and snippets.

@bakkiraju
Last active April 9, 2017 03:45
Show Gist options
  • Select an option

  • Save bakkiraju/5b20ae3c8f9bf364138053c0944e713d to your computer and use it in GitHub Desktop.

Select an option

Save bakkiraju/5b20ae3c8f9bf364138053c0944e713d to your computer and use it in GitHub Desktop.
Linux kernel Concepts
Reference:Linux programmers toolbox by John Fusco
Userspace:
Linux is a multiuser operating system, so one process should not be allowed to view another process’s memory,
which could contain passwords or sensi- tive information. User mode ensures that a process sees only memory that belongs to it. Moreover, if the process corrupts its internal structures, it can crash only itself; it will not take any other processes with it and certainly not the whole system.
The memory that the process sees when in user mode is called user space.
Kernel Space:
Because the kernel is executed by every process in the system, every process needs access to a common memory region.
To preserve security, however, the kernel code and data structures must be strictly iso- lated from user code and data.
That is why there is a kernel mode. Only kernel code runs in kernel mode, where it can see the common kernel data and
execute privileged instructions. We call the memory that the process sees in kernel mode kernel space.
There is only one kernel space, which is seen by every process when it runs in kernel mode, unlike user space,
which is unique to every process.
Syscall: Jump from user space code to kernel space code
# Use the C preprocessor for this example
#include "sys/syscall.h"
.data
# Contents of struct timespec {1,0}
sleeptime:
.long 1, 0
.text
# Linker uses _start as the entry point.
# Equivalent to main() in C.
.global _start
.type _start, @function
_start:
# Execute the nanosleep(2) syscall
# Parameters are stored in registers.
# Interrupt 0x80 takes us into kernel space.
movl $SYS_nanosleep, %eax
movl $sleeptime, %ebx
int $0x80
# 1st arg, system call number
# 2nd arg, pointer to struct timespec
# execute the system call
# Can’t just return. We have to call the exit(2) system call
# with our exit status.
movl $SYS_exit,%eax
movl $0, %ebx
int $0x80
# 1st arg, 1 = exit()
# 2nd arg, exit code
# execute the system call
Scheduling : Linux co-operative scheduling
When the device is slow, most of the process’s run time will be spent waiting. Such a process does not con- sume much CPU time as a proportion of overall run time. If every process were like this, the operating system could leave it up to the processes to call the scheduler, and everything would work out. Such a scheme does exist, and it’s called cooperative multitasking.
@bakkiraju
Copy link
Author

Reference:Linux programmers toolbox by John Fusco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment