Technical skill

Linux programming concepts

How a program asks Linux to do work

When a photo app opens a picture, it asks Linux to find the file, check access, and supply the data. A running program doing this work is called a process. The kernel is the core part of Linux that manages hardware and enforces system rules. Programs request its help through system calls, the defined doorway between an application and the operating system.

For example, one command can print two lines and another can count them. A second command can show some of the requests made while a program reads a file:

printf 'alpha\nbeta\n' | wc -l
strace -e trace=openat,read,write,close cat /etc/hostname

In the first line, printf sends text to standard output and the pipe carries it to wc, which reads standard input. In the second line, strace reports calls such as openat, read, and close while cat reads /etc/hostname. Other library or runtime code may make calls too, and permissions or tracing restrictions can limit what the trace shows.

From a library function to an open file

Application code usually calls a library function such as open() or printf() rather than writing a processor's system-call instruction itself. A library function may do work in the program, ask the kernel to do something, or call another library. System calls cover operations such as reading a file, creating a process, using a socket, or changing memory mappings.

A successful open() returns a file descriptor, a small number the process uses in later requests. The descriptor refers to an open file description, not directly to the file's contents. That description can represent a regular file, terminal, pipe, socket, or device. A read() asks the kernel for bytes from that object. It returns the amount read or an error; a successful read can return fewer bytes than requested.

Processes commonly start with conventional descriptors 0, 1, and 2 for standard input, standard output, and standard error. A shell can redirect these streams to files or connect one program's output to another's input. That lets a pipeline join commands without either program knowing how the other is implemented.

When Linux starts another program

fork() creates a child process. execve() replaces the calling process's program image without creating a new process, so a shell commonly uses them together to start an external command. The child inherits open descriptors, although close-on-exec settings can keep selected descriptors from surviving execve().

People sometimes say “everything is a file” because many interfaces, including pipes and sockets, can be used through file descriptors. The phrase does not explain every kernel interface: each object still has its own behavior. Reads can be short or reach end-of-file; reads and writes can be interrupted or fail. Check each operation's return value. If a program cannot access a file, the call that failed and its error code can distinguish path lookup or permission trouble from descriptor state or data flow.

Interactive exercises

Skillsets
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful