Notes
main
main
  • Introduction
  • linuxKernel
    • tips
    • make_help
    • old linux
      • compile_linux0.11
      • TestEnvironment
      • load_setup
      • get_hard_data
    • list
    • plist
    • fifo
    • idr
    • xarray
    • rbtree
    • maple_tree
    • syscall
    • bitmap
    • page
    • page_flags
    • page_size
    • page mapcount
    • page refcount
    • folio
    • slub
      • proc_slabinfo
      • slub_theory
      • kmalloc_kfree
      • kmem_cache
      • slab_alloc
      • slab_free
      • proc_meminfo_SReclaimable_SReclaimable
    • vmalloc
    • brk
    • mmap
    • mremap
    • mprotect
    • madvise
    • read
    • write
    • shmem
    • huge_page
    • page_fault
    • rmap
    • lru
    • multi-gen-LRU
    • page_reclaim
    • page_cache
    • page_table
    • rcu
    • kvm
    • aarch64_boot
    • tracing_system
    • cache_coherence_and_memory_consistency
    • cpu_speculates
    • mmap_lock
    • per-vma_lock
    • cgroup
    • symbol
    • campact
    • page_ext
    • mempool
    • kernelstack
    • filesystem
    • io_stack
    • workingset
    • ioremap
    • sched_period
  • linuxDebug
    • openocd_openjtag
    • i2c_tools
    • objdump
    • addr2line
    • gdb_useage
    • debug_linux_kernel_via_gdb
    • debug_linux_module_via_gdb
    • early_boot
    • sequentially_execute
    • dynamic_debug
    • research_linuxKernel_by_patch
    • tracefs
    • ebpf
    • bpftrace
    • perf
    • flame_graph
    • crash
    • ASAN_HWASAN_MTE_check_mem_bug
    • page_owner
    • vmtouch
    • fio
    • benchmark
  • linuxSystem
    • common
      • system_version
      • procfs
      • proc_sys_vm
      • cmd_ps
      • makefile
      • file_descriptor
      • psi
      • ulimit
      • top
      • delay_accounting
    • ubuntu
      • custom_kernel
      • get_cmd_src
      • record_ssh_info
      • log
      • run_custom_script
      • repo
      • cockpit
      • nfs
      • tftp
      • misc
    • fedora
      • system_upgrade
      • custom_kernel
      • lvextend
      • yt-dlp
      • jellyfin
  • linuxDriver
    • i2c_peripherals_driver
    • spi_peripherals_driver
    • gpio_subsystem
    • IRQ_driver
    • blockIO_unblockIO_async
    • linux_own_driver
    • misc_device
    • input_device
    • timer
    • atomic_spinlock_semaphore_mutex
    • lcd
    • touch_screen
    • debugfs
    • v4l2
    • mmap
  • hardware
    • paging_mmu_pt
    • iommu
  • process_thread_scheduler
    • scheduler01
    • scheduler02
    • scheduler03
    • scheduler04
    • scheduler05
    • scheduler06
  • memory_management
    • mm1
    • mm2
    • mm3
    • mm4
    • mm5
  • input_output_filesystem
    • io_fs_01
    • io_fs_02
    • io_fs_03
    • io_fs_04
  • lock_and_lockup_detector
    • general_lock
    • hung_task
    • softLockup_hardLockup
    • crash_experiment
  • MIT_6.S081
    • 6.S081_Operating_System_Engineering
    • Schedule.md
    • Class
      • Overview
      • Administrivia
    • Labs
      • Tools
      • Guidance
      • startup
      • syscall
      • page_table
      • Calling_Convention
      • traps
    • xv6
      • xv6
    • References.md
  • qemu
    • qemu_buildroot
    • qemu_busybox.md
    • Serial.md
    • demo_mini2440
      • 0_compilation_error_summary
      • 1_compilation_steps
      • 2_operation_mode
      • 3_transplant_tools_libraries
      • 4_tools_use
      • reference_website
  • tools
    • getKernelSourceCodeList
    • nat
    • shell
    • translating
    • YouCompleteMe
    • cscope
    • global
    • vscode
    • vim
    • binary
    • markdown
    • draw
    • git
    • tig
    • tmux
    • mail_client
    • download_patchset_from_LKML
    • minicom
    • clash
  • other
    • interview
    • interview_c_base
    • know_dontknow
    • Stop-Ask-Questions-The-Stupid-Ways
    • How-To-Ask-Questions-The-Smart-Way
    • docker
    • buildroot
    • rv32_to_rv64
Powered by GitBook
On this page
  • 1. linux进程周期
  • 2. task_struct
  • 2. 查看目前系统支持最多pid数量
  • 3. 内存泄漏
  • 4. 停止/恢复进程与作业控制 cpulimit
  • 5. fork

Was this helpful?

  1. process_thread_scheduler

scheduler01

1. linux进程周期

就绪、运行、睡眠、停止、僵死

2. task_struct

struct task_struct {
    ...
    pid_t pid;
    struct mm_struct *mm;
    struct fs_struct *fs;
    struct files_struct *files;
    ...
};

struct mm_struct {
    ...
    struct vm_area_struct * mmap;
    pgd_t * pgd;
    ...
};

struct fs_struct {
    ...
    struct path root, pwd;
    ...
};

struct files_struct {
    ...
    struct fdtable fdtab;
    struct file * fd_array[NR_OPEN_DEFAULT];
    ...
};

task_struct形成 链表、树、哈希 来管理

2. 查看目前系统支持最多pid数量

$ cat /proc/sys/kernel/pid_max
32768

3. 内存泄漏

僵死进程,资源已经释放,无内存泄漏,只存下 task_struct 供父进程查死亡原因

进程运行中,运行越久,内存消耗越多,即 发生内存泄漏

4. 停止/恢复进程与作业控制 cpulimit

ctrl + z : 暂停执行进程

fg/bg : 恢复进程执行

$ cpulimit -l 20 -p 10111

限制pid为10111进程的cpu使用率不超过20%

5. fork

$ vim fork1.c

main()
{
    fork();
    printf("hello\n");
    fork();    
    printf("hello\n");
    while(1);
}

$ vim fork2.c

#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pid,wait_pid;
    int status;

    pid = fork();

    if (pid==-1) {
        perror("Cannot create new process");
        exit(1);
    } else if (pid==0) {
        printf("a\n");
    } else {
        printf("b\n");
    }

    printf("c\n");
    while(1);
}

$ vim life_period.c

#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pid,wait_pid;
    int status;

    pid = fork();

    if (pid==-1)    {
        perror("Cannot create new process");
        exit(1);
    } else if (pid==0) {
        printf("child process id: %ld\n", (long) getpid());
        pause();
        _exit(0);
    } else {

#if 0 /* define 1 to make child process always a zomie */
        printf("ppid:%d\n", getpid());
        while(1);
#endif
        do {
            wait_pid=waitpid(pid, &status, WUNTRACED | WCONTINUED);

            if (wait_pid == -1) {
                perror("cannot using waitpid function");
                exit(1);
            }

            if (WIFEXITED(status))
                printf("child process exites, status=%d\n", WEXITSTATUS(status));

            if(WIFSIGNALED(status))
                printf("child process is killed by signal %d\n", WTERMSIG(status));

            if (WIFSTOPPED(status))
                printf("child process is stopped by signal %d\n", WSTOPSIG(status));

            if (WIFCONTINUED(status))
                printf("child process resume running....\n");

        } while (!WIFEXITED(status) && !WIFSIGNALED(status));

        exit(0);
}
Previousprocess_thread_schedulerNextscheduler02

Last updated 4 years ago

Was this helpful?