Linux進(jìn)程管理之如何創(chuàng)建和銷毀進(jìn)程
Linux是一個(gè)多任務(wù)操作系統(tǒng),進(jìn)程管理是其核心功能之一。
本文將詳細(xì)介紹如何在Linux中創(chuàng)建和銷毀進(jìn)程,包括示例代碼和詳細(xì)說(shuō)明。
創(chuàng)建進(jìn)程
在Linux中,可以使用多種方法創(chuàng)建新的進(jìn)程。
以下是幾種常見(jiàn)的方法:
1. 使用fork()系統(tǒng)調(diào)用
fork()
系統(tǒng)調(diào)用是創(chuàng)建新進(jìn)程的最常見(jiàn)方式。
它會(huì)創(chuàng)建一個(gè)與父進(jìn)程幾乎完全相同的子進(jìn)程。
#include <stdio.h> #include <unistd.h> int main() { pid_t child_pid; child_pid = fork(); if (child_pid == 0) { // 子進(jìn)程代碼 printf("This is the child process\n"); } else if (child_pid > 0) { // 父進(jìn)程代碼 printf("This is the parent process, child PID: %d\n", child_pid); } else { // 創(chuàng)建進(jìn)程失敗 perror("fork"); return 1; } return 0; }
2. 使用exec()系列函數(shù)
exec()
系列函數(shù)用于在當(dāng)前進(jìn)程中執(zhí)行一個(gè)新的程序。
它們通常與fork()
一起使用,以替換子進(jìn)程的內(nèi)存映像。
#include <stdio.h> #include <unistd.h> int main() { pid_t child_pid; child_pid = fork(); if (child_pid == 0) { // 子進(jìn)程代碼 printf("This is the child process\n"); // 在子進(jìn)程中執(zhí)行新程序 execl("/bin/ls", "ls", "-l", NULL); } else if (child_pid > 0) { // 父進(jìn)程代碼 printf("This is the parent process, child PID: %d\n", child_pid); } else { // 創(chuàng)建進(jìn)程失敗 perror("fork"); return 1; } return 0; }
3. 使用系統(tǒng)調(diào)用clone()
clone()
系統(tǒng)調(diào)用與fork()
類似,但它允許更精細(xì)的控制,例如共享文件描述符和內(nèi)存空間。
#define _GNU_SOURCE #include <stdio.h> #include <sched.h> #include <stdlib.h> #include <unistd.h> int child_function(void *arg) { printf("This is the child process\n"); return 0; } int main() { char *stack; char *stack_top; pid_t child_pid; stack = (char *)malloc(8192); if (stack == NULL) { perror("malloc"); return 1; } stack_top = stack + 8192; child_pid = clone(child_function, stack_top, CLONE_VM | CLONE_FS | CLONE_FILES, NULL); if (child_pid == -1) { perror("clone"); return 1; } printf("This is the parent process, child PID: %d\n", child_pid); return 0; }
銷毀進(jìn)程
Linux中,有幾種方法可以銷毀進(jìn)程,其中最常見(jiàn)的是使用exit()
系統(tǒng)調(diào)用。
以下是一個(gè)示例:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { pid_t child_pid; child_pid = fork(); if (child_pid == 0) { // 子進(jìn)程代碼 printf("This is the child process\n"); // 子進(jìn)程退出 exit(0); } else if (child_pid > 0) { // 父進(jìn)程代碼 printf("This is the parent process, child PID: %d\n", child_pid); // 等待子進(jìn)程退出 wait(NULL); printf("Child process has exited\n"); } else { // 創(chuàng)建進(jìn)程失敗 perror("fork"); return 1; } return 0; }
進(jìn)程組與會(huì)話
在Linux中,進(jìn)程組和會(huì)話是進(jìn)程管理的重要概念。進(jìn)程組是一組相關(guān)聯(lián)的進(jìn)程的集合,而會(huì)話則是一組進(jìn)程組的集合。
進(jìn)程組通常用于將多個(gè)相關(guān)的進(jìn)程組織在一起,以便更好地進(jìn)行控制和信號(hào)處理。
以下是創(chuàng)建進(jìn)程組和會(huì)話的示例:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { pid_t child_pid; child_pid = fork(); if (child_pid == 0) { // 子進(jìn)程代碼 printf("This is the child process (PID: %d)\n", getpid()); // 創(chuàng)建一個(gè)新會(huì)話并成為會(huì)話領(lǐng)袖 if (setsid() == -1) { perror("setsid"); return 1; } // 創(chuàng)建一個(gè)新進(jìn)程組 if (setpgid(0, 0) == -1) { perror("setpgid"); return 1; } printf("Child process is in a new session and process group\n"); sleep(10); // 保持進(jìn)程運(yùn)行10秒 } else if (child_pid > 0) { // 父進(jìn)程代碼 printf("This is the parent process, child PID: %d\n", child_pid); // 等待子進(jìn)程退出 wait(NULL); printf("Child process has exited\n"); } else { // 創(chuàng)建進(jìn)程失敗 perror("fork"); return 1; } return 0; }
在上述示例中,子進(jìn)程首先創(chuàng)建了一個(gè)新會(huì)話并成為會(huì)話領(lǐng)袖,然后創(chuàng)建了一個(gè)新進(jìn)程組。
這將導(dǎo)致子進(jìn)程脫離父進(jìn)程的控制終端和進(jìn)程組,成為一個(gè)獨(dú)立的會(huì)話。這對(duì)于守護(hù)進(jìn)程等后臺(tái)任務(wù)非常有用。
殺死進(jìn)程
在Linux中,可以使用kill
命令或kill()
系統(tǒng)調(diào)用來(lái)殺死進(jìn)程。
以下是一個(gè)示例:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> void sig_handler(int signo) { if (signo == SIGTERM) { printf("Received SIGTERM, exiting...\n"); exit(0); } } int main() { pid_t child_pid; child_pid = fork(); if (child_pid == 0) { // 子進(jìn)程代碼 printf("This is the child process (PID: %d)\n", getpid()); // 注冊(cè)信號(hào)處理函數(shù) signal(SIGTERM, sig_handler); while (1) { // 子進(jìn)程持續(xù)運(yùn)行 sleep(1); } } else if (child_pid > 0) { // 父進(jìn)程代碼 printf("This is the parent process, child PID: %d\n", child_pid); // 等待一段時(shí)間后向子進(jìn)程發(fā)送SIGTERM信號(hào) sleep(5); kill(child_pid, SIGTERM); printf("Sent SIGTERM to child process\n"); // 等待子進(jìn)程退出 wait(NULL); printf("Child process has exited\n"); } else { // 創(chuàng)建進(jìn)程失敗 perror("fork"); return 1; } return 0; }
在上述示例中,父進(jìn)程通過(guò)kill()
系統(tǒng)調(diào)用向子進(jìn)程發(fā)送SIGTERM
信號(hào),以請(qǐng)求子進(jìn)程優(yōu)雅地退出。
總結(jié)
Linux進(jìn)程管理是操作系統(tǒng)的核心功能之一,對(duì)于系統(tǒng)開(kāi)發(fā)和管理人員來(lái)說(shuō)是重要的知識(shí)點(diǎn)。
本文詳細(xì)介紹了如何創(chuàng)建和銷毀進(jìn)程,以及如何使用進(jìn)程組和會(huì)話來(lái)組織進(jìn)程。此外,還介紹了如何殺死進(jìn)程。
希望本文提供的示例代碼和詳細(xì)說(shuō)明有助于大家更好地理解和應(yīng)用Linux進(jìn)程管理的概念和技巧。也希望大家多多支持腳本之家。
相關(guān)文章
centos7云主機(jī)系統(tǒng)下掛載磁盤的方法
本篇文章主要介紹了centos7云主機(jī)系統(tǒng)下掛載磁盤的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02linux下查看內(nèi)存條數(shù)及每根內(nèi)存大小的實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)?lái)一篇linux下查看內(nèi)存條數(shù)及每根內(nèi)存大小的實(shí)現(xiàn)方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11從Linux源碼看Socket(TCP)Client端的Connect的示例詳解
這篇文章主要介紹了從Linux源碼看Socket(TCP)Client端的Connect,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07linux后臺(tái)運(yùn)行的幾種方式(小結(jié))
這篇文章主要介紹了linux后臺(tái)運(yùn)行的幾種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12ubuntu 16.04LTS 開(kāi)機(jī)啟動(dòng)自動(dòng)更換壁紙的實(shí)現(xiàn)方法
下面小編就為大家分享一篇ubuntu 16.04LTS 開(kāi)機(jī)啟動(dòng)自動(dòng)更換壁紙的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02You don’t have permission to access /index.php on.
運(yùn)行php時(shí)提示You don't have permission to access /index.php on.錯(cuò)誤的解決方法,需要的朋友可以參考下2013-02-02Ubuntu中安裝MySQL更改默認(rèn)密碼的步驟詳解
本文分步驟給大家介紹Ubuntu中安裝MySQL更改默認(rèn)密碼的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-10-10