โค้ดที่โจทย์ให้มาค่ะ
runsim.c
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main (int arc, char *argv[]){
pid_t childpid = 0;
int i, n;
if(argc != 2){ //check for valid number of command-line arguments
fprintf(stderr, "Usage: %s processes\n", argh[0]);
return 1;
}
n = atom(argv[1]);
for(i=1; i<n; i++)
if((childpid =fork()) <= 0)
break;
fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n", i, getpid(), getppid(), childpid();
return 0;
}
รับประกันได้ว่าโค้ดที่พิมพ์พิมพ์ตามโจทย์ถูกแล้วค่ะ
------------------------------------
โจทย์หลักนะคะ
Write a C grogram called
runsim.c The program takes exactly one command-line argument that specifies the maximum number of simultaneous execution. The
runsim program runs up to
pr_limit processes at a time. The executable must be called
runsim. Follow the outline below for implementing
runsim.
ในโจทย์ส่วนนี้อยากทราบว่า pr_limit คือค่าตัวแปรที่โจทย์กำหนดให้มีค่าเท่ากับ argv[1] ใช่รึเปล่าคะ
------------------------------------
โจทย์ย่อยข้อที่ 1 ค่ะ
Check the appropriate command-line argument and output a usage message if the command line is incorrect.
จากโค้ดที่โจทย์ให้มาคือเค้าให้แก้ส่วนนี้ใช่มั้ยคะ
if(argc != 2){ //check for valid number of command-line arguments
fprintf(stderr, "Usage: %s processes\n", argh[0]);
return 1;
}
ส่วนตัวเราคิดว่า fprintf มันเป็นตัวเขียนข้อความลงไฟล์ เลยเข้าใจว่าในโค้ดเขียนมาให้ผิดๆและให้แก้ใช่รึเปล่าคะ
โดยแก้เป็น
if(argc != 2){ //check for valid number of command-line arguments
printf("Usage: %s processes\n", argh[0]);
return 1;
}
ใช่รึเปล่า??
------------------------------------
โจทย์ย่อยข้อที่ 2
initialize pr_limit from the command line. The pr_limit variable specifies the maximum number of children allowed to execute at a time.
อันนี้เปลี่ยนจาก n = atom(argv[1]); เป็น pr_limit = atom(argv[1]); ใช่รึเปล่าคะ
------------------------------------
โจทย์ย่อยข้อที่ 3
The pr_count variable holds the number of active children. Initialize it to 0.
เข้าใจว่า pr_count คือค่าตัวแปรค่ะ แต่จำนวนของ active children คืออะไรทำไมกำหนดค่าจนถึง 0
ใช่เป็นการเข้าลูปตัวนี้รึเปล่าคะ
for(i=1; i<n; i++)
if((childpid =fork()) <= 0)
break;
หรือเป็นการนับเวลาถอยหลัง
------------------------------------
โจทย์ย่อยข้อที่ 4 มีอยู่ 4 ย่อย
if pr_count is pr_limit, wait for a child to finish and decrement pr_count
ตรงส่วนนี้งงค่ะ เข้าใจว่าน่าจะนับถอยหลัง ทำไมตั้งค่า if(pr_count = pr_limit) หรือเป็นการให้ทำตั้งแต่ตอนแรก
ส่วน wait for a child คือ childpid = wait(&status); ใช่รึเปล่าคะ childpid คือโค้ดตั้งแต่ต้นที่กำหนดว่า pid_t childpid = 0;
ส่วน childpid = wait(&status); น่าจะอยู่ในเงื่อนไขที่หลังประกาศ int pid = fork(); และเป็นเงื่อนไขที่ pid !=0 ใช่รึเปล่าคะ
read a line form standard input(fgets) of up to MAX_BUF characters and execute a program corresponding to that command line by forking a child and execute the file
คิดว่าน่าจะใช้ fget อ่านค่าไฟล์ค่ะ ซึ่งเป็นไฟล์ที่ทางโจทย์กำหนดมาให้ค่ะ เป็น .data ในส่วนนี้ก็จะต้องเป็น
FILE *fd;
fd = fopen("testing.data", "r");
if(fd==NULL){
printf("cannot read file");
exit(1);
}
while(!feof(fd))
{
fget(s, pr_limit, fd); //อันนี้ใช่รึเปล่าคะ ปกติใช้แต่ fscanf ตลอดไม่เคยลองใช้ fget น่ะค่ะ ไม่เข้าใจว่า MAX_BUF คือไหน
แล้วตามโจทย์ต้อง fork() อีกรอบใช่มั้ยคะ คือต้องมีจำนวน child ตามจำนวนค่าสูงสุดที่คีย์เข้าไปใน argv[1] (ตรงนี้เริ่มสับสนแล้วค่ะ)
increment pr_count to track the number of active children
รอบนี้ให้เพิ่มค่า pr_count มาถึงตรงนี้ก็งงอีกดอกค่ะ
check to see if any of the children have finished(**). Decrement pr_count for each child that has completed pr_count for each child that has completed.
คือให้ pr_count ลดลงในแต่ละ child ใช่รึเปล่าคะ ไปไม่ถูกแล้วค่ะ
------------------------------------
โจทย์ย่อยข้อที่ 5 กับ 6
After encountering end-of-file on standard input, wait for all of the remaining children to finish and then exit
For each terminated child, prints out its exit code valve
สองข้อนี้ไม่ไหวแล้วค่ะ เบลอกับข้อข้างบนๆจนไม่รู้เรื่องแล้วค่ะ รู้ว่าต้องใช้ exit(); ใช่มั้ย หรือว่าต้องใช้ exec() คะ??
ขอความช่วยเหลือทีค่ะ ไม่เข้าใจสุดๆเลยค่ะ อย่าว่ากันแรงๆเลยนะคะ พยายามเข้าใจจนถึง File handling จนถึงจอง memory พวก malloc(), free() พอเข้าเรื่อง system progrmming ก็เบลอเลยค่ะ
ขอบคุณล่วงหน้าค่ะ
ขอคำแนะนำโจทย์ภาษาซีเกี่ยวกับ system programming ค่ะ เช่น พวก fork() และ exec() พวก child process ค่ะ
runsim.c
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
รับประกันได้ว่าโค้ดที่พิมพ์พิมพ์ตามโจทย์ถูกแล้วค่ะ
------------------------------------
โจทย์หลักนะคะ
Write a C grogram called runsim.c The program takes exactly one command-line argument that specifies the maximum number of simultaneous execution. The runsim program runs up to pr_limit processes at a time. The executable must be called runsim. Follow the outline below for implementing runsim.
ในโจทย์ส่วนนี้อยากทราบว่า pr_limit คือค่าตัวแปรที่โจทย์กำหนดให้มีค่าเท่ากับ argv[1] ใช่รึเปล่าคะ
------------------------------------
โจทย์ย่อยข้อที่ 1 ค่ะ
Check the appropriate command-line argument and output a usage message if the command line is incorrect.
จากโค้ดที่โจทย์ให้มาคือเค้าให้แก้ส่วนนี้ใช่มั้ยคะ
if(argc != 2){ //check for valid number of command-line arguments
fprintf(stderr, "Usage: %s processes\n", argh[0]);
return 1;
}
ส่วนตัวเราคิดว่า fprintf มันเป็นตัวเขียนข้อความลงไฟล์ เลยเข้าใจว่าในโค้ดเขียนมาให้ผิดๆและให้แก้ใช่รึเปล่าคะ
โดยแก้เป็น
if(argc != 2){ //check for valid number of command-line arguments
printf("Usage: %s processes\n", argh[0]);
return 1;
}
ใช่รึเปล่า??
------------------------------------
โจทย์ย่อยข้อที่ 2
initialize pr_limit from the command line. The pr_limit variable specifies the maximum number of children allowed to execute at a time.
อันนี้เปลี่ยนจาก n = atom(argv[1]); เป็น pr_limit = atom(argv[1]); ใช่รึเปล่าคะ
------------------------------------
โจทย์ย่อยข้อที่ 3
The pr_count variable holds the number of active children. Initialize it to 0.
เข้าใจว่า pr_count คือค่าตัวแปรค่ะ แต่จำนวนของ active children คืออะไรทำไมกำหนดค่าจนถึง 0
ใช่เป็นการเข้าลูปตัวนี้รึเปล่าคะ
for(i=1; i<n; i++)
if((childpid =fork()) <= 0)
break;
หรือเป็นการนับเวลาถอยหลัง
------------------------------------
โจทย์ย่อยข้อที่ 4 มีอยู่ 4 ย่อย
if pr_count is pr_limit, wait for a child to finish and decrement pr_count
ตรงส่วนนี้งงค่ะ เข้าใจว่าน่าจะนับถอยหลัง ทำไมตั้งค่า if(pr_count = pr_limit) หรือเป็นการให้ทำตั้งแต่ตอนแรก
ส่วน wait for a child คือ childpid = wait(&status); ใช่รึเปล่าคะ childpid คือโค้ดตั้งแต่ต้นที่กำหนดว่า pid_t childpid = 0;
ส่วน childpid = wait(&status); น่าจะอยู่ในเงื่อนไขที่หลังประกาศ int pid = fork(); และเป็นเงื่อนไขที่ pid !=0 ใช่รึเปล่าคะ
read a line form standard input(fgets) of up to MAX_BUF characters and execute a program corresponding to that command line by forking a child and execute the file
คิดว่าน่าจะใช้ fget อ่านค่าไฟล์ค่ะ ซึ่งเป็นไฟล์ที่ทางโจทย์กำหนดมาให้ค่ะ เป็น .data ในส่วนนี้ก็จะต้องเป็น
FILE *fd;
fd = fopen("testing.data", "r");
if(fd==NULL){
printf("cannot read file");
exit(1);
}
while(!feof(fd))
{
fget(s, pr_limit, fd); //อันนี้ใช่รึเปล่าคะ ปกติใช้แต่ fscanf ตลอดไม่เคยลองใช้ fget น่ะค่ะ ไม่เข้าใจว่า MAX_BUF คือไหน
แล้วตามโจทย์ต้อง fork() อีกรอบใช่มั้ยคะ คือต้องมีจำนวน child ตามจำนวนค่าสูงสุดที่คีย์เข้าไปใน argv[1] (ตรงนี้เริ่มสับสนแล้วค่ะ)
increment pr_count to track the number of active children
รอบนี้ให้เพิ่มค่า pr_count มาถึงตรงนี้ก็งงอีกดอกค่ะ
check to see if any of the children have finished(**). Decrement pr_count for each child that has completed pr_count for each child that has completed.
คือให้ pr_count ลดลงในแต่ละ child ใช่รึเปล่าคะ ไปไม่ถูกแล้วค่ะ
------------------------------------
โจทย์ย่อยข้อที่ 5 กับ 6
After encountering end-of-file on standard input, wait for all of the remaining children to finish and then exit
For each terminated child, prints out its exit code valve
สองข้อนี้ไม่ไหวแล้วค่ะ เบลอกับข้อข้างบนๆจนไม่รู้เรื่องแล้วค่ะ รู้ว่าต้องใช้ exit(); ใช่มั้ย หรือว่าต้องใช้ exec() คะ??
ขอความช่วยเหลือทีค่ะ ไม่เข้าใจสุดๆเลยค่ะ อย่าว่ากันแรงๆเลยนะคะ พยายามเข้าใจจนถึง File handling จนถึงจอง memory พวก malloc(), free() พอเข้าเรื่อง system progrmming ก็เบลอเลยค่ะ
ขอบคุณล่วงหน้าค่ะ