1 year ago

#385440

test-img

CDog

Should I exec() a client child process after accept()ing a new socket FD and fork()ing from the server?

I am creating a server that listens for a connection from any device. After a connection is made and a client socket FD is created, I want to write all data transmitted by the device to a database. I am wondering if I should do all my work in the child process produced by fork or should I create a new process image through exec(). The client process will exist for as long as the device is transmitting data.

For example, should I do this:

//Main loop
while(1){
int newfd = accept(sockfd, (struct sockaddr *)&their_addr, (socklen_t*)&addr_size);

//Create child process to handle the accepted connection
pid_t proc_id = fork();
if(proc_id == 0){

  close(server_fd);

  execvp("client", argv);

  }else{
    printf("Parent process, closing socket\n");
    close(new_fd);
  }
}

or should I continue executing from the if statement like so:

//Main loop
while(1){
int newfd = accept(sockfd, (struct sockaddr *)&their_addr, (socklen_t*)&addr_size);

//Create child process to handle the accepted connection
pid_t proc_id = fork();
if(proc_id == 0){

  close(server_fd);

  /***Process data here***/

  }else{
    printf("Parent process, closing socket\n");
    close(newfd);
  }
}

c

linux

unix

network-programming

0 Answers

Your Answer

Accepted video resources