Kernel functions in linux

Linux Kernel And Its Functions

People use Linux every day. Today almost all electronic products are built on Linux and the most popular ones are Android devices. Every day almost 850,000 Android devices are activated which is the largest compared to any other mobile devices manufacturer such as Windows Phone, iPhone, etc.

It’s not just smartphones that Linux runs but every other gadget from your TV to a refrigerator is running on Linux. So how is this all possible and what is Linux all about? Let’s talk about this. Before identifying the main functions that the famous Linux Kernel has, it is vital to define what is the Linux Kernel.

What is the Linux Kernel?

The Linux Kernel is the heart of the operating system. Without the Kernel, we simply can not perform any task, since it is mainly responsible for the software and hardware of our computer working correctly and can interact with each other.

Linux history – A little history

linux kernel archive

The Linux kernel was launched in 1991 by Linus Torvalds and was the trigger that started with the development of Linux as we know it today (you can check out the full story of its development at linuxfoundation.org). The Kernel is a relatively small part of the software that makes up a complete Linux system but it is the part that determines how well the system will work and is the truly unique component of Linux. It is also one of the projects that have more collaborators and developers than any other Open Source project. In addition, it is very well documented since, since 2005, it uses the Git source code management system. If you want to know all the Linux documentation you can check it on the Linux Foundation website.

The first version of the Linux kernel was 0.01, launched in 1991 and the first stable version was 1.0.0. From then on it has followed its evolution with the help of thousands of developers around the world and until now it continues in versions 4.x, in addition, stable updates are released every two or three months for users. If you want to know all the versions, patches, updates and changes you can visit The Linux Kernel Archives.​

Kernel Identification

After version 2.6, the Linux kernel has undergone certain changes in its identification nomenclature, since in the past it was identified with three numbers that indicated the series, the version, and the revision in that order. Now, this identification is given by 4 numbers separated by points: AA.BB.CC.DD, where:

  • AA: This number indicates the Kernel version.
  • BB: With this number, we can know the current revision of the kernel.
  • CC: It gives us information about whether the kernel has minor revisions. This number changes when new driver support is added or new features are added to the Kernel.
  • DD: This figure is an indicator of updates and correction of failures in the Kernel and changes when a correction task is performed.

how linux kernel functions

Kernel functions

The main functions of the Kernel are the following:

  • Manage RAM memory, so that all programs and running processes can work.
  • Manage the processor time, which is used by running processes.
  • Manage access and use of the different peripherals connected to the computer.
Читайте также:  Энидеск установить на линукс

kernel function

Where to find the Kernel?

All Linux distributions come with an integrated Kernel, which can be automatically updated by our distributions. All these cores are based on the basic Kernel called Vanilla, supplied directly by Linus Torvalds, from the servers of the organization Kernel.org. In this direction, we can access different repositories in which the Kernel Vanilla can be obtained quickly.

If we want to install this Kernel, we must download it and configure it manually. It is a somewhat cumbersome operation, which requires being an advanced user, so if you are a novice user, it is better to update the Kernel by automatically applying your favorite distribution.

What is the Kernel in Android?

Android uses a variation of the Linux kernel but the essence is the same as in this one: it is the core of the system and the one in charge of bridging the hardware components and the applications.

As responsible for mediating between the hardware and the system, the Android Kernel includes a series of vital components such as screen controllers, audio controllers, integrated cameras or energy management.

kernel in android

This is why those who like to try different ROMs often also experiment with different Kernel versions created or modified by the community. A third-party kernel can include performance enhancements, allow overclock (increase the clock frequency of the processor) or add support for functions that were not included in the factory kernel (such as tethering, for example).

For the user on foot, the version of the Kernel is not very relevant because it is the one that was distributed with the latest version of the system (or ROM) that has been installed on the device, and little else you can do about it. Although it includes the date in which the Kernel was compiled, really having a few months is not a problem if no error has been detected in it. In kernel versions, a larger number is not necessarily better, since Android kernels are generally based on three versions of Linux Kernel: 3.4, 3.10, and 3.18. It does not make sense, therefore, to install a Kernel just because it is more “new”. The reasons should be other, such as performance optimizations or improvements in battery life.

Conclusion

In short, the Kernel is the heart of Linux and also one of the largest and most important open source development projects, as there are already large companies that help with their development or economically as IBM, Google, Red Hat and Texas Instruments. One of the advantages of the Linux kernel is that it is possible to update it without affecting the rest of the operating system, with a couple of commands (using the root user) in the Terminal. We would achieve this in a couple of minutes or even more simple through the Software Center, although this depends on the distribution that we choose. By updating only the system’s kernel, we would have not only more stable but also safer and faster equipment, all in several minutes.

This is basically a kernel and what it does in the Linux operating system, neither more nor less. I hope you have resolved your doubts and see you in the next Linux kernel series we will be discussing the difference between the Linux kernel and mac kernel.

Читайте также:  Grub сброс пароля root linux

Источник

Linux Kernel API

We will go through the Linux application programming interface, the API. Linux kernel provides the system calls which can be used to get the task done through kernel. Let us discuss the few, widely used system calls of Linux.

Description:

Linux kernel provides the set or list of functions which can be used by the user space programs to utilize the Linux kernel services.

The block diagram looks like the following:

Some of the few widely used system calls are open, close, read, and write. These are basic system calls provided by Linux kernel. Every task is done through file in Linux. So, the basic operations on any file can be done through open, close, read, and write.

Let us take a real example where we want to print the “hello world” on serial console. To achieve this task through system calls, we need to open the device file for console in /dev. Once we locate the device file for console or uart, we can use the open system call to open the device.

Here is the syntax of the open system call:

int open ( const char * pathname , int flags ) ;

int open ( const char * pathname , int flags , mode_t mode ) ;

From this syntax, the first argument is the file path which we want to open. In our case, it is the device file which we located as a console device. In the next argument, the flags provide the user some flexibility to play with the file. Few example of the flags are O_CREAT, O_APPEND, etc. These flags have specific meanings and purpose and the discussion is out of the scope of this discussion. For more details on the flags and parameters, please refer the kernel man pages.

Once the file is open successfully, we need to use the write system call to send the “hello world” to the console device. The prototype of the write system call is as follows:

The first parameter of the write system call is the fd which is the file descripter. The “fd” is given to us by the open system call. After opening the file successfully, we should get the file descriptor. This fd is used further to write the data.

The second parameter is the buffer address for the data to be sent to the console device. In our case, the test data is “hello world”.

The last parameter is the total number of bytes that the user wants to write to the console device. In our case, the number of bytes is the size of “hello world”. We can either use the sizeof operator or the strlen function to get the number of bytes. We should be careful while using the strlen. This function ignores the string terminator character like “\0”. So, while reading the string, we have to ensure that the null character is handled properly. Otherwise, we will end up with the segmentation fault.

Now, let us implement the code part of this example. First, we need to locate the serial console or uart device. In the machine, what we use has the serial tty device as /dev/pts/0. So, as discussed, we first need to open this file. We must also include the harder file which provides the declaration of open system call.

Читайте также:  Нет wi fi ноутбуке linux

Our code for opening the device file looks like the following:

fd = open ( “ / dev / pts / 0 ” , O_WRONLY ) ;

Next, when we want to call the write system call, we use the fd provided by the open. Our call to write function looks like the following:

write ( fd , data , strlen ( data ) + 1 ) ;

The previous code snippet writes the “hello world” to the serial console that we located and want to write.

Let us put all the pieces together:

int main ( )
{
int fd ; int rc ;
char * data = “hello world” ;
fd = open ( “ / dev / pts / 0 ” , O_WRONLY ) ;
if ( fd < 0 )
{
printf ( “Error opening file :% d” , fd ) ;
exit ( — 1 ) ;
}
rc = write ( fd , data , strlen ( data ) + 1 ) ;
if ( rc < 0 )
{
printf ( “Error writing file :% d” , rc ) ;
exit ( — 1 ) ;
}
close ( fd ) ; // file operation is done, close the file.
return 0 ;
}

Compiling of the previous program is the same as compiling the other C programs like the following:

The test_write.c is the file name to our C code.

After the compilation, we get the executable with the test_write name.

Refer to the following snapshot:

After running the compiled and generated binary, we have the following output.

Our program output is highlighted in bold letters. The following is the snapshot for reference:

So far, we have seen an example where we use the Linux API to display the test string to the console. This is the simple example. There are many other offerings that are provided by the system call. Few of the system calls which are provided by Linux are as follows:

  • read: Reading from the file.
  • write: Writing to the file.
  • open: Opening the file.
  • close: Closing the file.
  • poll: Polling the device for the change in state.
  • lseek: Seeking at a particular offset in the file.
  • mmap: Mapping the virtual memory to the physical memory.
  • brk: Change the segment size.
  • ioctl: Control devices.
  • access: Access to get the permissions of file.
  • pipe: Refers to the pipe creation.

This is the huge list of functions provided by Linux kernel. We have seen and discussed very few. Refer to the kernel source for the complete list of system calls that is provided by kernel.

Conclusion

We discussed about the system calls which is a way to ask the kernel to perform the tasks for the user space programs. Without the system call, it will not be possible for the user space programs to get the task done by the kernel. We took a simple task or example of writing the test data, “hello world”, to the serial device. We employed the open, write, and close API’s of the kernel to complete the task in hand. One most important thing is to check the system call return value. Kernel provides proper return values to explain the reason in case of failure of function. The user can get the idea on the failure reason by seeing the return values from the system call. Return values have some specific meaning and are well-captured in the kernel documentation.

About the author

Sushil Rathore

Sushil Rathore is having hands-on experience in Linux Platform SW. He’s an expert of Linux on ARM/X86 Boards. He has very good understanding on Bootloaders and other platform softwares. He has good industrial experience and have worked in reputed Organizations. Currently he is associated with a reputed firm in the networking domain.

Источник

Оцените статью
Adblock
detector