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)文章
Linux自動(dòng)化構(gòu)建工具-make/Make?le使用解讀
Linux項(xiàng)目自動(dòng)化構(gòu)建工具M(jìn)ake/Makefile指南,涵蓋基礎(chǔ)語(yǔ)法、高級(jí)特性及實(shí)戰(zhàn)技巧,如變量、偽目標(biāo)、模式規(guī)則、條件編譯等,提升開(kāi)發(fā)效率2025-09-09
淺談linux kernel對(duì)于浮點(diǎn)運(yùn)算的支持
今天小編就為大家分享一篇淺談linux kernel對(duì)于浮點(diǎn)運(yùn)算的支持,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

