Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ask about the purpose and meaning of some code #18

Open
ucsk opened this issue Mar 5, 2020 · 1 comment
Open

Ask about the purpose and meaning of some code #18

ucsk opened this issue Mar 5, 2020 · 1 comment

Comments

@ucsk
Copy link

ucsk commented Mar 5, 2020

This was the first project I studied, probably because of my limited ability, and I found that some places were not well understood. The following three points are some of my questions. Thank you for your guidance.

(1) Why isn't pid < 3 here? Is there any special meaning for pid being 2?

WebBench/webbench.c

Lines 411 to 416 in b1acf3c

pid=fscanf(f,"%d %d %d",&i,&j,&k);
if(pid<2)
{
fprintf(stderr,"Some of our childrens died.\n");
break;
}

(2) Why does the bench() function return i? Isn't this the speed value that the parent process successfully read the last time? Could I change the program to return 0?

return i;

(3) Add close(sock); is it more rigorous?

WebBench/socket.c

Lines 54 to 55 in b1acf3c

if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
return -1;

@hewei-nju
Copy link

hewei-nju commented Sep 22, 2021

This was the first project I studied, probably because of my limited ability, and I found that some places were not well understood. The following three points are some of my questions. Thank you for your guidance.

(1) Why isn't pid < 3 here? Is there any special meaning for pid being 2?

WebBench/webbench.c

Lines 411 to 416 in b1acf3c

pid=fscanf(f,"%d %d %d",&i,&j,&k);
if(pid<2)
{
fprintf(stderr,"Some of our childrens died.\n");
break;
}

(2) Why does the bench() function return i? Isn't this the speed value that the parent process successfully read the last time? Could I change the program to return 0?

return i;

(3) Add close(sock); is it more rigorous?

WebBench/socket.c

Lines 54 to 55 in b1acf3c

if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
return -1;

My personal opinion:
(1): Because in common Linux operating systems, the PID of 0 and 1 have been occupied by the process operating system and the software which cooperates with the operating system.

(2): Currently, I'm facing the same problem with you, and I think you are true.

(3): Use man 3 connect, and I have found this sentenceAPPLICATION USAGE: If connect() fails, the state of the socket is unspecified. Conforming applications should close the file descriptor and create a new socket before attempting to reconnect. So I agree with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants