Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

สร้าง Multiple Processes ด้วย fork() ใน C

fork() สร้าง Child Process คืนค่า:

  • > 0: Parent Process (คืน PID ของ Child)
  • = 0: Child Process
  • < 0: Error

ตัวอย่าง

#include <stdio.h>
#include <unistd.h>

int main() {
    int n = fork();
    if (n > 0)
        printf("Parent process\n");
    else
        printf("Child process\n");
    return 0;
}

ผลลัพธ์

Parent process
Child process

(ลำดับอาจสลับได้ตาม Scheduler)