2012-09-07 29 views
6

Câu hỏi của tôi là:Tại sao các quy trình của tôi không hoạt động đồng thời?

1.) Làm thế nào tôi có thể nhận được quy trình cha mẹ luôn chết cuối cùng? Tôi nhận được rằng điều này không được thực hiện bởi vì cha mẹ là pid đầu tiên được chạy, nhưng tôi không biết làm thế nào để thay đổi nó.

2.) Làm cách nào để con tôi xử lý đồng thời thực thi cùng lúc? Tôi thậm chí đã tăng số lượng thực sự cao để xem nếu nó chỉ là một sự trùng hợp, nhưng nó dường như không được.

EDIT: GIẢI PHÁP

1.) thêm chờ đợi (NULL) hai lần trong nội Mặc định
2.) đã xảy ra. sử dụng giấc ngủ (1) để chứng minh điều đó.

Mã của tôi là như sau

#include <stdio.h> 
int main() { 
    int pid, i; 
    pid = fork(); 
    switch(pid) { 
     case -1: 
      // error 
      printf("Fork error"); 
      break; 
     case 0: 
      // child process 
      printf("First child is born, my pid is %d\n", getpid()); 
      for(i=1; i<10; i++) 
       printf("First child executes iteration %d\n", i); 
      printf("First child dies quietly.\n"); 
      break; 
     default: 
      // parent process 
      printf("Parent process is born, my pid is %d\n", getpid()); 
      pid = fork(); 
      switch(pid) { 
       case -1: 
        // error 
        printf("Fork error"); 
        break; 
       case 0: 
        // child process 
        printf("Second child is born, my pid is %d\n", getpid()); 
        for(i=1; i<10; i++) 
         printf("Second child executes iteration %d\n", i); 
        printf("Second child dies quietly.\n"); 
        break; 
       default: 
        // parent process 
        printf("Parent process dies quietly."); 
     } 
    } 
    return 0; 
} 

ra tôi luôn trông như thế này:

 
Parent process is born, my pid is 7847 
First child is born, my pid is 7848 
First child executes iteration: 1 
First child executes iteration: 2 
First child executes iteration: 3 
First child executes iteration: 4 
First child executes iteration: 5 
First child executes iteration: 6 
First child executes iteration: 7 
First child executes iteration: 8 
First child executes iteration: 9 
First child executes iteration: 10 
First child dies quietly. 
Parent process dies quietly. 
Second child is born, my pid is 7849 
Second child executes iteration 1 
Second child executes iteration 2 
Second child executes iteration 3 
Second child executes iteration 4 
Second child executes iteration 5 
Second child executes iteration 6 
Second child executes iteration 7 
Second child executes iteration 8 
Second child executes iteration 9 
Second child executes iteration 10 
Second child dies quietly. 

phân công của tôi là:

Viết một chương trình C ("procs.c") mà tạo ra ba tiến trình: một tiến trình cha tạo ra hai tiến trình con.

Đứa trẻ đầu tiên nên làm như sau:

  • hiển thị "đứa con đầu lòng chào đời, pid của tôi là ..."

  • hiển thị mười lần thông báo "con đầu tiên thực hiện lặp X" , trong đó X là số lần lặp lại

  • hiển thị "Con đầu tiên chết lặng lẽ".

Đứa trẻ thứ hai nên làm như sau:

  • hiển thị "con thứ hai chào đời, pid của tôi là ..."

  • hiển thị mười lần thông báo "thực thi con thứ hai lặp lại X ", trong đó X là số lần lặp lại

  • hiển thị" Con thứ hai chết lặng lẽ ".

Quá trình cha mẹ nên làm như sau:

  • hiển thị "quá trình Chánh được sinh ra, pid của tôi là ..."

  • tạo ra đứa con đầu lòng

  • tạo con thứ hai

  • hiển thị "Bố mẹ quá trình chết lặng lẽ."

Biên soạn chương trình sử dụng gcc và đặt tên cho tập tin thực thi 'procs' Thực hiện các chương trình nhiều lần và nhận thấy như thế nào đầu ra của hai đứa con interlace

Một đầu ra có thể có của chương trình này là:..

 
nova> ./procs 

Parent process is born, my pid is 7847 
First child is born, my pid is 7848 
First child executes iteration: 1 
First child executes iteration: 2 
First child executes iteration: 3 
First child executes iteration: 4 
First child executes iteration: 5 
Second child is born, my pid is 7849 
Second child executes iteration 1 
Second child executes iteration 2 
Second child executes iteration 3 
First child executes iteration: 6 
Second child executes iteration 4 
Second child executes iteration 5 
Second child executes iteration 6 
First child executes iteration: 7 
Second child executes iteration 7 
Second child executes iteration 8 
Second child executes iteration 9 
Second child executes iteration 10 
Second child dies quietly. 
First child executes iteration: 8 
First child executes iteration: 9 
First child executes iteration: 10 
First child dies quietly. 
Parent process dies quietly. 

Trả lời

3
  1. Quá trình cha mẹ phù hợp nên chờ (sử dụng wait() or waitpid() hoặc một biến nền tảng cụ thể) cho con của nó chết trước khi thoát.

  2. Thực thi đồng thời yêu cầu trình lập lịch để có cơ hội chạy. Bạn có thể ép buộc vấn đề với các chế độ ngủ thích hợp (vi-ngủ, nano-sleep). Một số cuộc gọi hệ thống sẽ giúp (vì vậy fflush(0) có thể giúp ích).


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

int main(void) 
{ 
    int pid, i; 
    struct timespec tw = { .tv_sec = 0, .tv_nsec = 10000000 }; 
    pid = fork(); 
    switch(pid) 
    { 
     case -1: 
      printf("Fork error"); 
      break; 
     case 0: 
      printf("First child is born, my pid is %d\n", getpid()); 
      for(i=1; i<10; i++) 
      { 
       printf("First child executes iteration %d\n", i); 
       nanosleep(&tw, 0); 
      } 
      printf("First child dies quietly.\n"); 
      break; 
     default: 
      printf("Parent process is born, my pid is %d\n", getpid()); 
      pid = fork(); 
      switch(pid) 
      { 
       case -1: 
        printf("Fork error"); 
        break; 
       case 0: 
        printf("Second child is born, my pid is %d\n", getpid()); 
        for(i=1; i<10; i++) 
        { 
         printf("Second child executes iteration %d\n", i); 
         nanosleep(&tw, 0); 
        } 
        printf("Second child dies quietly.\n"); 
        break; 
       default: 
        printf("Parent process waiting for children.\n"); 
        int corpse; 
        int status; 
        while ((corpse = waitpid(0, &status, 0)) > 0) 
         printf("Child %d died with exit status 0x%.4X\n", corpse, status); 
        printf("Parent process dies quietly.\n"); 
        break; 
      } 
    } 
    return 0; 
} 

Ví dụ đầu ra:

Parent process is born, my pid is 46624 
First child is born, my pid is 46625 
First child executes iteration 1 
Parent process waiting for children. 
Second child is born, my pid is 46626 
Second child executes iteration 1 
First child executes iteration 2 
Second child executes iteration 2 
First child executes iteration 3 
Second child executes iteration 3 
First child executes iteration 4 
Second child executes iteration 4 
Second child executes iteration 5 
First child executes iteration 5 
Second child executes iteration 6 
First child executes iteration 6 
Second child executes iteration 7 
First child executes iteration 7 
Second child executes iteration 8 
First child executes iteration 8 
Second child executes iteration 9 
First child executes iteration 9 
First child dies quietly. 
Second child dies quietly. 
Child 46625 died with exit status 0x0000 
Child 46626 died with exit status 0x0000 
Parent process dies quietly. 

Lưu ý rằng 10 phần nghìn giây chậm trễ gần như cưỡng chế thi hành xen kẽ. Nếu không có một cái gì đó tương tự, bạn đang mắc kẹt với các idiosyncrasies của hệ thống của bạn và lên lịch của nó, và nhiều người một máy hiện đại chỉ là quá nhanh!

+0

Sách của tôi có ví dụ giống như của tôi, ngoại trừ mặc định cũng có vòng lặp for. Và kết quả thường trông giống như parent1, parent2, child1, child2, child3, child4, parent3, v.v. Trong tôi, tại sao con thứ 2 không bắt đầu thực hiện ở giữa việc thực hiện của đứa trẻ thứ nhất? –

+1

Máy của bạn quá nhanh. –

+0

Cảm ơn bạn. Sự giúp đỡ của bạn đã truyền cảm hứng cho tôi để ném một giấc ngủ (1) vào quá trình con tôi để chứng minh họ đang chạy cùng một lúc. –

1

fork() tạo ra một quá trình con mới, chạy của độc lập từ công ty mẹ.

Nếu bạn muốn quá trình cha mẹ chờ các trẻ em để kết thúc, bạn có thể sử dụng hệ thống gọi wait, đi qua một PID, mà sẽ chặn cho đến khi quá trình đã hoàn thành.

Xem bài viết wait(System Call) on Wikipedia

Các vấn đề liên quan