进程调度技术咨询:同时调度双进程与进程完成后顺序调度
Got it, let's break down how to handle both concurrent (simultaneous) and sequential process scheduling—this is a common scenario across different programming languages and shell environments, so I'll cover practical examples and key notes for each case.
一、同时调度(并发执行)
Simultaneous scheduling here means letting multiple processes run in parallel (on multi-core CPUs) or via time-slice rotation (on single-core CPUs, giving the illusion of simultaneity). Below are examples in common tools/languages:
1. Python 使用 multiprocessing 模块
Python's multiprocessing lets you spawn independent processes easily:
import multiprocessing import time def task_process1(): for _ in range(5): print("* Process 1 is running *") time.sleep(1) def task_process2(): for _ in range(5): print("** Process 2 is running **") time.sleep(1) if __name__ == "__main__": # 初始化两个进程 p1 = multiprocessing.Process(target=task_process1) p2 = multiprocessing.Process(target=task_process2) # 同时启动 p1.start() p2.start() # 可选:等待两个进程都结束后再执行主进程后续逻辑 p1.join() p2.join() print("✅ Both concurrent processes finished")
2. Shell 脚本直接后台启动
In a Unix-like shell, use & to run processes in the background, then wait to wait for all to complete:
#!/bin/bash # 同时启动两个脚本/程序 ./your_process1.sh & ./your_process2.sh & # 等待所有后台进程终止 wait echo "✅ All concurrent processes done"
Key Notes:
- On single-core CPUs, the OS uses time-slice scheduling to switch between processes rapidly—you'll see interleaved output, even though only one runs at a time.
- If processes need to share data or sync actions, use inter-process communication (IPC) tools like
multiprocessing.Queue(Python), pipes (Linux), or shared memory.
二、顺序调度(先完成第一个再启动第二个)
Sequential scheduling ensures the second process starts only after the first has fully terminated (successfully or with an error). Here's how to implement it:
1. Python 等待进程结束再启动下一个
Use join() to block the main thread until the first process finishes:
import multiprocessing import time def task_process1(): print("Starting Process 1...") time.sleep(3) print("✅ Process 1 finished") def task_process2(): print("Starting Process 2...") time.sleep(3) print("✅ Process 2 finished") if __name__ == "__main__": p1 = multiprocessing.Process(target=task_process1) p1.start() # 等待p1完全终止 p1.join() # 现在启动p2 p2 = multiprocessing.Process(target=task_process2) p2.start() p2.join() print("✅ Sequential execution complete")
2. Shell 脚本顺序执行
In shell, simply run commands one after another—each command waits for the previous to exit before running:
#!/bin/bash # 先执行process1,等它结束再执行process2 ./your_process1.sh echo "Process 1 exited with code: $?" # 可选:检查退出码 ./your_process2.sh echo "✅ Sequential execution done"
3. C 语言使用 waitpid 等待子进程
For low-level C programming, use fork() to create processes and waitpid() to wait for their termination:
#include <stdio.h> #include <unistd.h> #include <sys/wait.h> int main() { // 启动第一个进程 pid_t pid1 = fork(); if (pid1 == 0) { printf("Process 1 running...\n"); sleep(3); printf("Process 1 finished\n"); return 0; } else { // 等待第一个进程结束 int status; waitpid(pid1, &status, 0); printf("Process 1 exited with status: %d\n", WEXITSTATUS(status)); // 启动第二个进程 pid_t pid2 = fork(); if (pid2 == 0) { printf("Process 2 running...\n"); sleep(3); printf("Process 2 finished\n"); return 0; } waitpid(pid2, &status, 0); printf("✅ All sequential processes done\n"); } return 0; }
Key Notes:
- Always check the exit code of the first process if you need to handle failures (e.g., skip the second process if the first fails).
- For longer-running processes, you can add timeout logic (e.g.,
waitpidwithWNOHANGin C, ormultiprocessing.Process.join(timeout)in Python) to avoid infinite waiting.
内容的提问来源于stack exchange,提问作者Chin Na

