Linux interprocess communication-sockets

Source: Internet
Author: User

Six. Socket (socket)

Socket is also an inter-process communication mechanism, but its main difference from other communication methods is that it can achieve process communication between different hosts. a set of interfaces can be seen as the endpoints of interprocess communication ( Endpoint ), the name of each socket interface is unique, and other processes can access, connect, and communicate data.

  Socket Interface (Socket) programming is the implementationLinuxone of the primary ways of communicating between processes in the system and most other operating systems. We are familiar withWWWServices,FTPServices,TELNETServiceand so on is based on the set of interface programming to achieve. The socket interface is also suitable for inter-process communication within the same computer, in addition to the offsite computer processes. The classic textbook on the set of interfaces is alsoRichard Stevenscompiled by the "UnixNetwork programming: NetworkedAPIand Sockets ", Tsinghua University Press published a photocopy of the book. It is alsoLinuxone of the necessary books for programmers.

In this part of the content, you can refer to the author of another article, "Design your Own network ant", there are several sets of common interface functions of the introduction and sample programs. This section may be linux process internet internet , then, it can be said that his set

3 Linux the process and Win32 the process / Thread Comparison

  familiar withWIN32programming people must know thatWIN32the process management method andLinuxthere is a big difference inUNIXonly the concept of process, but inWIN32but there's another one."Threads"the concept, thenLinuxand theWIN32What difference does it have here?

  WIN32the process in/threads are inherited fromOS/2the. In theWIN32inside,"Process"refers to a program, and"Threads"is a"Process"one of the executions in"Clues". From the core,WIN32the multi-process andLinuxand there's no big difference inWIN32in the line friend equivalent toLinuxprocess, is a code that is actually executing. However,WIN32in the same one inthread Each thread is a shared data segment. This is theLinuxprocess is the biggest difference.

  The following procedure shows the WIN32 how the next process starts a thread.

int g;

DWORD WINAPI childprocess (lpvoid lpparameter) {

int i;

For (i = 1; I <1000; i + +) {

g + +;

printf ("This is a child Thread:%d\n", g);

}

exitthread (0);

};

void Main ()

{

int ThreadID;

int i;

g = 0;

CreateThread (null, 0, childprocess, NULL, 0, &threadid);

For (i = 1; I <1000; i + +) {

g + +;

printf ("This is the Parent Thread:%d\n", g);

}

  in theWIN32under, useCreateThreadfunction to create threads, andLinuxthe next creation process is different,WIN32A thread is not run from the point of creation, but rather by theCreateThreadspecifies a function at which the thread starts running. This program is the same as the previousUNIXlike a program, printed by two threads +message. ThreadIDis the thread number of the child thread, in addition, the global variablegis the child thread that is shared with the parent thread, which is theLinuxthe biggest difference. As you can see,WIN32the process/Threadsto be more thanLinuxComplex, inLinuxto implement a similarWIN32the thread is not difficult, as long asForklater, let the child process callThreadProcfunction, and share for global variablesThe data area is OK, but theWIN32will not be able to achieve similarForkfunction of the. So nowWIN32under theCAlthough the library functions provided by the language compiler are already compatible with mostLinux/unixlibrary function, but it still cannot be implementedFork. for multi-tasking systems, shared data areas are necessary, but also a problem that can cause confusion,WIN32Next, a programmer can easily forget that the data between threads is shared in this case, a line Chengafter a variable has been changed, another thread modifies it, causing the program to go wrong. But inLinux, because the variables are not shared, and the programmer explicitly specifies the data to be shared, the program changesto be clearer and safer.

asWIN32of the"Process"concept, the meaning of which is"Application", which is equivalent toUNIXunder theexecup. Linuxalso has its own multithreaded functionspthread, it is not only different fromLinuxprocess, which is also different fromWIN32under the process, aboutpthreadthe introduction and how theLinuxenvironment to write multithreaded programs we will be in another article, "Linuxin multithreaded programming under the ".  

Linux interprocess communication-sockets

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.