1 / 7

作業系統作業訂正

作業系統作業訂正. 資工三甲 49852011 張任頡. Q 3.10 包含最初父行程,下列程式建立多少行程 ?. #include<stdio.h> #include<unistd.h> int main() { /* fork a child process */ fork(); /* fork another child process */ fork(); /*and fork another */ fork(); return 0; }. Answer 3.10.

rashad
Download Presentation

作業系統作業訂正

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 作業系統作業訂正 資工三甲 49852011 張任頡

  2. Q 3.10 包含最初父行程,下列程式建立多少行程? #include<stdio.h> #include<unistd.h> int main() { /* fork a child process */ fork(); /* fork another child process */ fork(); /*and fork another */ fork(); return 0; }

  3. Answer 3.10

  4. Q 3.11使用下列程式,以辨認pid的值在行A、B 、C 和D。( 假定實際父行程與子行程的pids分別為2600和2603 ) #include<stdio.h> #include<unistd.h> int main() { pid_t pid, pid1; /* fork a child process */ pid = fork(); if ( pid < 0) { /* error occurred */ fprintf ( stderr, “Fork Failed ”) ; return 1; } else if (pid == 0) { /* child process */ pid1 = getpid(); printf("child : pid = %d",pid); /* A */ printf("child : pid1 = %d",pid1); /* B */ } else { /* parent process */ pid1 = getpid(); printf("parent : pid = %d",pid); /* C */ printf("parent : pid1 = %d",pid1); /* D */ wait(NULL); } return 0; }

  5. Answer 3.11 A : pid = 0 →子行程本身的值(fork 回傳的值) B : pid1 = 2603 → 子行程真正的值 C : pid = 2603 → 子行程的值(fork 回傳後的值) D : pid1 = 2600 → 父行程真正的值

  6. Q 3.12 使用下列程式解釋Line A將輸出什麼 #include<sys/types.h> #include<stdio.h> #include<unistd.h> int value=5; int main(void) { pid_t pid; pid=fork(); if (pid==0) { /* child process */ value+=15; return 0; } else if (pid>0) { /* parent process */ wait(NULL); printf("PARENT: value = %d \n",value); /* Line A */ return 0; } }

  7. Answer 3.12 PARENT: value = 5 子行程的value不影響父行程的value 雖然子行程改變value 的值 但是父行程的value 值仍是5。

More Related