Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and start a Linux process

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to create and start the Linux process", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create and start a Linux process.

Type of process

There are two main types of processes in Linux:

Foreground processes (also known as interactive processes)-these processes are initialized and controlled by terminal sessions. In other words, a user connected to the system is required to start such processes; they are not started automatically as part of the system function / service. Background processes (also known as non-interactive / automatic processes)-these processes are not connected to the terminal; they do not require any user input. What is a daemon daemon

This is a special type of background process that starts when the system starts and runs as a service; they do not die. They are started spontaneously as system tasks (running as services). However, they can be controlled by the user through the init process (systemd replaces the init process after RHEL&CentOs7. For more information, please see Linux.)

Create a process in Linux

There are three ways to create a process in Linux:

Fork () mode

Use the fork () function to copy a process based on the parent process, whose PID number is different from the parent process PID number. In the Linux environment, fork () is implemented by write replication, and the environment of the new child process is the same as the parent process, only the memory is different from the parent process, the others are shared with the parent process, and a copy is regenerated only after the parent process or child process has been modified.

System () mode

The system () function calls / bin/sh-c command to execute a specific command and blocks the execution of the current process until the command command finishes. The new child process will have a new PID.

Exec () mode

The exec () method has several different functions. Unlike the previous fork () and system () functions, exec () will replace the original process with a new process, the system will run from the new process, and the PID value of the new process will be the same as the PID value of the original process.

How does Linux identify the process?

Because Linux is a multi-user system, which means that different users can run a variety of programs on the system, the kernel must uniquely identify each instance of the program running.

A program is recognized by its process ID (PID) and its parent process ID (PPID), so processes can be classified as:

Parent processes-these are processes that create other processes at run time. Child processes-these are processes created by other processes at run time.

Init process

The init process is the parent of all processes in the system, and it is the first program to run after starting the Linux system; it manages all other processes on the system. It is started by the kernel itself, so it theoretically has no parent process.

The process ID of the init process is always 1. It is the parent of all child processes. (similar to the root directory and subdirectory relationship, everything starts with the init process.)

Find process ID

You can use the pidof command to find the process ID of a process:

# pidof systemd# pidof top# pidof httpd

To find the process ID of the current shell and the process ID of its parent process, run:

$echo $echo $PPID

Start a process in Linux

Every time you run a command or program (such as cloudcmd-CloudCommander), it starts a process on the system. You can start a foreground (interactive) process as follows, it will be connected to the terminal, and the user can send input to it:

# cloudcmd

Linux background tasks

To start a process in the background (non-interactive), use the & symbol, where the process does not read input from the user until it is moved to the foreground.

# cloudcmd & # jobs

You can also use Ctrl + Z to suspend the execution of a program and send it to the background. It will send a SIGSTOP signal to the process, thus suspending its execution; it will become idle:

# tar-cf backup.tar / backups/* # Press Ctrl+Z# jobs

To continue running the paused command above in the background, use the bg command:

# bg

To send the background process to the foreground, use the fg command and the ID of the task, similar to:

# status of processes in jobs# fg% 1Linux

During execution, depending on its environment, a process transitions from one state to another. In Linux, a process has the following possible states:

Running-at this time it is running (it is the current process in the system) or ready to run (it is waiting to allocate CPU units). Waiting-in this state, the process is waiting for an event or system resources. In addition, the kernel also distinguishes between two different types of waiting processes; the interruptible waiting process interruptible waiting processes- can be interrupted by signals, and the non-interruptible waiting process uninterruptible waiting processes- is waiting for hardware conditions and cannot be interrupted by any event / signal. Stopped-in this state, the process has been stopped, usually due to a signal received. For example, the process being debugged. Zombie-the process is dead, it has stopped, but there is still an entry in the process table process table. How to view active processes in Linux

There are many Linux tools that can be used to view / list processes running on a system, and two traditions are known as the ps and top commands:

1. Ps command

It displays information about the active processes in the selected system, as shown in the following figure:

# ps# ps-e | head

2. Top-system monitoring tool

Top is a powerful tool that provides you with a dynamic real-time view of the running system, as shown in the screenshot below:

# top

3. Glances-system monitoring tool

Glances is a relatively new system monitoring tool with some advanced features:

# how glances controls processes in Linux

Linux also has some commands to control processes, such as kill, pkill, pgrep, and killall. Here are some basic examples of how to use them:

$pgrep-u tecmint top$ kill 2308$ pgrep-u tecmint top$ pgrep-u tecmint glances$ pkill glances$ pgrep-u tecmint glances sends a signal to the process

The basic way to control processes in Linux is to send signals to them. You can send a lot of signals to a process. Run the following command to see all the signals:

$kill-l

To signal a process, you can use the kill, pkill, or pgrep commands we mentioned earlier. But only if it is programmed to recognize these signals can the program respond to them.

Most of the signals are used within the system or when writing code for programmers. Here are some useful signals for system users:

SIGHUP 1-sends this signal to the process when the terminal that controls it is turned off. SIGINT 2-when the user interrupts the process with Ctrl+C, the terminal that controls it sends this signal to the process. SIGQUIT 3-sends the exit signal Ctrl+D to the process when the user sends it. SIGKILL 9-this signal immediately interrupts (kills) the process, and the process does not clean up. SIGTERM 15-this is a program termination signal (kill sends this signal by default). SIGTSTP 20-its control terminal sends this signal to the process asking it to stop (terminal stops); triggered by the user pressing Ctrl+Z.

Here is an example of a kill command that kills a Firefox application through its PID when it freezes:

$pidof firefox$ kill 9 2687 or $kill-KILL 2687 or $kill-SIGKILL 2687

To kill the application with its name, you can use pkill or killall as follows:

$pkill firefox$ killall firefox change Linux process priority

In Linux systems, all active processes have a priority and a nice value. Processes that have higher priorities than point priority processes generally get more CPU time.

However, system users with root privileges can use the nice and renice commands to influence (change) the priority.

In the output of the top command, NI displays the nice value of the process:

$top

Use the nice command to set the nice value for a process. Remember that an average user can set a nice value of 0 to 20 for the process he owns.

Only root users can use negative nice values.

To reset the priority of a process, use the renice command as follows:

$renice + 8 2687$ renice + 8 2103 here, I believe you have a better understanding of "how to create and start the Linux process". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report