Linux 进程与线程区别

线程:

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);

内核线程和用户态线程区别

另一方面,内核线程是由内核本身创建的。这两种类型之间的主要区别在于内核线程没有有限的地址空间(每个程序都可以在其中运行一块内存

It is often useful for the kernel to perform some operations in the background.The ker-
nel accomplishes this via kernel threads—standard processes that exist solely in kernel-
space.The significant difference between kernel threads and normal processes is that
kernel threads do not have an address space. (Their mm pointer, which points at their
address space, is NULL.) They operate only in kernel-space and do not context switch into
user-space. Kernel threads, however, are schedulable and preemptable, the same as normal
processes -----LKD3 pdf

进程(子)

clone(SIGCHLD, 0);
这里的SIGCHLD意味着子进程在终止时应该将此标志发送给其父进程1

TIP:

volatile

Volatile keyword is an ANSI C type modifier, which insures that the compiler does not use over-
optimization of putting certain variable into a register
and, thus, creating a possibility for the
program code to use an old value, after the variable was updated. In other words, the compiler
generates code that reloads the variable from memory each time it is referenced by any of the
running threads, instead of just taking the value from a register (which would be a lot faster; this is
why it is called optimization)

const

near to member

const int *a; a pointer to a read-only integer; pionter can be a left set value
int * const a; a read-only pointer to a integer; interger can be a left set value

declare a constant pointer to a volatile hardware

register:
uint8_t volatile * const p_irq_reg = (uint8_t *) 0x80000

Branch prediction hints

llikely()

preemption

The act of involuntarily suspending a running process is called
Preemptions are caused by timer interrupts. A timer interrupt is basically a clock
tick inside the kernel, the clock ticks 1000 times a second;

interactive ===> non-interactive

Graphic/IO ===> mathematical calculations

sample code for rr FIFO perio

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>

#define INTERVAL 100000 // 定时器时间间隔,单位为微秒
#define MAX_PROCESSES 5 // 最大进程数
#define QUANTUM 2       // 时间片长度

typedef struct {
    int pid;       // 进程ID
    int priority;  // 进程优先级
    int remaining; // 进程剩余执行时间
    int quantum;   // 进程已执行的时间片
} process;

process processes[MAX_PROCESSES]; // 进程队列
int num_processes = 0;             // 当前进程数
int current_process = -1;          // 当前正在执行的进程
int scheduler_type = 1;            // 调度算法类型,0表示优先级算法,1表示Round Robin算法,2表示FIFO算法

// 定时器处理函数
void timer_handler(int signum) {
    if (current_process >= 0) {
        processes[current_process].remaining--; // 当前进程执行时间减1
        processes[current_process].quantum++;  // 当前进程已执行时间片加1
        if (scheduler_type == 1 && processes[current_process].quantum >= QUANTUM) {
            printf("Time slice expired for process %d\n", processes[current_process].pid);
            current_process = -1; // 时间片用完,重置为无进程执行状态
        }
        else if (processes[current_process].remaining <= 0) {
            printf("Process %d finished\n", processes[current_process].pid);
            current_process = -1; // 当前进程执行完毕,重置为无进程执行状态
        }
    }
}

// 优先级调度算法
int priority_scheduler() {
    int i, highest_priority = -1, next_process = -1;
    for (i = 0; i < num_processes; i++) {
        if (processes[i].remaining > 0 && processes[i].priority > highest_priority) {
            highest_priority = processes[i].priority;
            next_process = i;
        }
    }
    return next_process;
}

// Round Robin调度算法
int rr_scheduler() {
    int i, next_process = -1;
    if (current_process < 0) {
        for (i = 0; i < num_processes; i++) {
            if (processes[i].remaining > 0) {
                next_process = i;
                break;
            }
        }
    }
    else {
        next_process = (current_process + 1) % num_processes;
        while (next_process != current_process && processes[next_process].remaining <= 0) {
            next_process = (next_process + 1) % num_processes;
        }
        if (next_process == current_process && processes[next_process].remaining <= 0) {
            next_process = -1;
        }
    }
    return next_process;
}

// FIFO调度算法
int fifo_scheduler() {
    int i, next_process = -1;
    for (i = 0; i < num_processes; i++) {
        if (processes[i].remaining > 0) {
            next_process = i;
            break;
        }
    }
    return next_process;
}

int main() {
    int i, pid;
    struct sigaction sa;
    struct itimerval timer;

    // 安装定时器处理函数
    sa.sa_handler = timer_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGALRM, &sa, NULL);

    // 设置定时器
    timer.it_value.tv_sec = 0;
    timer.it_value.tv_usec = INTERVAL;
    timer.it_interval.tv_sec = 0;
    timer.it_interval.tv_usec = INTERVAL;
    setitimer(ITIMER_REAL, &timer, NULL);

    // 添加进程到进程队列中
    for (i = 0; i < MAX_PROCESSES; i++) {
        pid = fork();
        if (pid < 0) {
            perror("fork failed");
            exit(1);
        }
        else if (pid == 0) {
            // 子进程执行的代码
            srand(getpid());
            int runtime = rand() % 10 + 1;
            printf("Process %d created with remaining time %d and priority %d\n", getpid(), runtime, i);
            while (runtime > 0) {
                // 进程执行的代码
                runtime--;
                usleep(INTERVAL); // 休眠100毫秒,模拟进程执行
            }
            exit(0); // 进程执行完毕,退出
        }
        else {
            // 父进程记录子进程信息
            processes[i].pid = pid;
            processes[i].priority = i;
            processes[i].remaining = rand() % 10 + 1;
            processes[i].quantum = 0;
            num_processes++;
        }
    }

    while (1) {
        // 根据调度算法选择下一个进程
        switch (scheduler_type) {
            case 0:
                current_process = priority_scheduler();
                break;
            case 1:
                current_process = rr_scheduler();
                break;
            case 2:
                current_process = fifo_scheduler();
                break;
            default:
                printf("Invalid scheduler type\n");
                exit(1);
        }

        // 执行下一个进程
        if (current_process >= 0) {
            printf("Executing process %d with remaining time %d\n", processes[current_process].pid, processes[current_process].remaining);
            kill(processes[current_process].pid, SIGCONT);
            pause();
        }
        else {
            printf("No process is currently executing\n");
            // usleep(100000); // 休眠100毫秒,减少CPU占用率
            break;
        }
    }

    return 0;
}
``
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值