3、Linux-进程管理类
发布人:shili8
发布时间:2024-11-19 01:19
阅读次数:0
**Linux 进程管理**
在 Linux 系统中,进程是系统资源的基本单位。每个进程都有自己的虚拟地址空间、堆栈、程序计数器等资源。进程管理是 Linux 内核的一个重要组成部分,它负责创建、调度和终止进程。
**3.1 进程状态**
Linux 中,进程可以处于以下几种状态:
* **运行态(Running)**:进程正在执行。
* **睡眠态(Sleeping)**:进程暂停执行,等待某个事件发生。
* **停止态(Stopped)**:进程暂时停止执行,但仍然占有系统资源。
* **僵死态(Zombie)**:进程已经终止,但父进程尚未回收其资源。
###3.2 进程创建Linux 中,进程可以通过以下几种方式创建:
####1) fork()函数`fork()`函数用于创建一个新进程。新进程与原进程共享同样的虚拟地址空间、堆栈和程序计数器等资源。
c#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid ==0) { // 子进程 printf("子进程
");
} else { // 父进程 printf("父进程
");
}
return0;
}
####2) exec()函数`exec()`函数用于创建一个新进程,并执行指定的程序。
c#include <stdio.h>
#include <unistd.h>
int main() {
execl("/bin/ls", "ls", NULL);
return0;
}
###3.3 进程终止Linux 中,进程可以通过以下几种方式终止:
####1) exit()函数`exit()`函数用于直接终止当前进程。
c#include <stdio.h>
#include <stdlib.h>
int main() {
exit(0);
return0;
}
####2) kill()函数`kill()`函数用于向指定的进程发送信号,导致其终止。
c#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = getpid();
kill(pid, SIGKILL);
return0;
}
###3.4 进程调度Linux 中,进程调度是指系统根据一定的策略选择哪些进程执行。
####1) Round-Robin(轮询)调度算法Round-Robin 是一种简单的调度算法,它将所有进程按顺序分配时间片。
c#include <stdio.h>
#include <unistd.h>
int main() {
for (int i =0; i < 10; i++) {
printf("进程%d
", i);
sleep(1); // 等待1秒 }
return0;
}
####2) Priority-Based(优先级)调度算法Priority-Based 是一种根据进程优先级来分配时间片的调度算法。
c#include <stdio.h>
#include <unistd.h>
int main() {
for (int i =0; i < 10; i++) {
printf("进程%d
", i);
if (i ==5) { // 当前进程优先级最高 sleep(2); // 等待2秒 } else {
sleep(1); // 等待1秒 }
}
return0;
}
###3.5 进程同步Linux 中,进程同步是指多个进程之间的协调工作。
####1) semaphores(信号量)机制semaphores 是一种用于实现进程同步的机制,它通过共享一个资源来控制进程的执行顺序。
c#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int semaphore =0;
void* thread_func(void* arg) {
while (1) {
sem_wait(&semaphore);
printf("线程正在执行
");
sleep(2); // 等待2秒 sem_post(&semaphore);
}
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
while (1) {
printf("主线程正在执行
");
sleep(1); // 等待1秒 }
return0;
}
####2) mutex(互斥锁)机制mutex 是一种用于实现进程同步的机制,它通过共享一个资源来控制进程的执行顺序。
c#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* thread_func(void* arg) {
while (1) {
pthread_mutex_lock(&mutex);
printf("线程正在执行
");
sleep(2); // 等待2秒 pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
while (1) {
printf("主线程正在执行
");
sleep(1); // 等待1秒 }
return0;
}
###3.6 进程通信Linux 中,进程通信是指多个进程之间的数据交换。
####1) pipes(管道)机制pipes 是一种用于实现进程通信的机制,它通过共享一个资源来控制进程的执行顺序。
c#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipefd[2];
pipe(pipefd);
pid_t pid = fork();
if (pid ==0) { // 子进程 close(pipefd[0]); // 关闭读端 char buffer[10] = "Hello";
write(pipefd[1], buffer, strlen(buffer));
close(pipefd[1]);
} else { // 父进程 close(pipefd[1]); // 关闭写端 char buffer[10];
read(pipefd[0], buffer,10);
printf("%s
", buffer);
close(pipefd[0]);
}
return0;
}
####2) sockets(套接字)机制sockets 是一种用于实现进程通信的机制,它通过共享一个资源来控制进程的执行顺序。
c#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addrlen = sizeof(client_addr);
char buffer[10] = "Hello";
server_fd = socket(AF_INET, SOCK_STREAM,0);
if (server_fd < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
listen(server_fd,3);
client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &addrlen);
if (client_fd < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
write(client_fd, buffer, strlen(buffer));
close(client_fd);
close(server_fd);
return0;
}
###3.7 总结Linux 中,进程管理是系统资源的基本单位。每个进程都有自己的虚拟地址空间、堆栈、程序计数器等资源。进程管理包括创建、调度和终止进程。进程状态包括运行态、睡眠态、停止态和僵死态。进程可以通过fork()函数创建,exec()函数执行指定的程序,exit()函数直接终止当前进程,kill()函数向指定的进程发送信号,导致其终止。进程调度是指系统根据一定的策略选择哪些进程执行。Round-Robin和Priority-Based是两种常见的调度算法。进程同步是指多个进程之间的协调工作。semaphores和mutex是两种用于实现进程同步的机制。进程通信是指多个进程之间的数据交换。pipes和sockets是两种用于实现进程通信的机制。
###3.8 参考* Linux

