Skip to content

Commit 7fa9e00

Browse files
authored
Update multiclientserver.c
1 parent 9cc2c11 commit 7fa9e00

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

multiclientserver.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
3+
fork() in C
4+
5+
Fork system call use for creates a new process, which is called child process, which runs concurrently with process (which process called system call fork) and this process is called parent process. After a new child process created, both processes will execute the next instruction following the fork() system call. A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process.
6+
7+
It takes no parameters and returns an integer value. Below are different values returned by fork().
8+
9+
Negative Value: creation of a child process was unsuccessful.
10+
Zero: Returned to the newly created child process.
11+
Positive value: Returned to parent or caller. The value contains process ID of newly created child process.
12+
13+
*/
14+
115
#include <stdio.h>
216
#include <stdlib.h>
317
#include <string.h>
@@ -55,7 +69,8 @@ int main(){
5569
}
5670
printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
5771

58-
if((childpid = fork()) == 0){
72+
if((childpid = fork()) == 0){//This is the child process so it shouldn't use the main listening socket
73+
//Also only the child should talk to newly accepted client via newSocket and main parent process shouldn't
5974
close(sockfd);
6075

6176
while(1){
@@ -78,4 +93,4 @@ int main(){
7893

7994

8095
return 0;
81-
}
96+
}

0 commit comments

Comments
 (0)