Linux network Programming (3)-multi-process, multi-threaded

Source: Internet
Author: User


In my inside has introduced the Linux under C's process, the thread interface, here does not do too much elaboration.


Multi-process

Many processes here use the traditional multi-process model, whenever a client sends a connection to create a process to process the connection, and a child process corresponds to a connection.

With the foundation of the last single process, there are only simple modifications that can be implemented here.

    while (1) {        clientfd = Accept (servfd, (struct sockaddr*) &cliaddr, &clientlen);        Host = gethostbyaddr ((const char*) &cliaddr.sin_addr.s_addr, sizeof (CLIADDR.SIN_ADDR.S_ADDR), af_inet);        printf ("Server Connect to host:%s%s\n", Host->h_name, Inet_ntoa (CLIADDR.SIN_ADDR));        if ((Child_pid = Fork ()) = = 0) {            Close (servfd);            Echo (CLIENTFD);            Close (CLIENTFD);        }        Close (CLIENTFD);    }
You only need to add the process creation in the while and then close the parent process's listener socket in the child process.

Of course, don't forget to add the wrapping function for the fork error handling (as described in the Fork section).

void Error_msg (char *msg) {    perror (msg);    Exit (0);} int Fork () {    pid_t pid;    if (PID = fork ()) < 0)        error_msg ("fork Failed");    return PID;}

Operation Result:

Client:


Server:

Multithreading

Threads and processes are interlinked in many ways, and it is not difficult to implement the traditional model of multithreading in the traditional model of multi-process.

It is still a simple modification in the while.

        CLIENTFD = (int*) malloc (sizeof (int));        *CLIENTFD = Accept (servfd, (struct sockaddr*) &cliaddr, &clientlen);        Host = gethostbyaddr ((const char*) &cliaddr.sin_addr.s_addr, sizeof (CLIADDR.SIN_ADDR.S_ADDR), af_inet);        printf ("Server Connect to host:%s%s\n", Host->h_name, Inet_ntoa (CLIADDR.SIN_ADDR));        Pthread_create (&tid, NULL, &thread, clientfd);        Close (*CLIENTFD);

malloc is used to avoid all manual allocations because of the unpredictable consequences of having multiple threads accessing the same clientfd.

The thread function is

void *thread (void* arg) {    int clientfd = * ((int*) arg);    Free (ARG);    Pthread_detach (Pthread_self ());    Echo (CLIENTFD);    Close (CLIENTFD);    return NULL;}

Operation Result:

The problem with this code is that after clientfd the incoming thread, the ARG pointer does not receive a value, meaning it is in an inaccessible place (GDB displays a value of 0x00) and the solution is baffled.

(The principle is very simple, the problems encountered first recorded, if someone knows where the wrong hope can correct it out ...) Environment Ubuntu 64 bit, compiler GCC)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux network Programming (3)-multi-process, multi-threaded

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.