Kudzu World

"البرمجة: فنّ و تمرّد"

Kudzu World  »  Articles  »  AdvancedIPC
English - Română - Русский - عربي

هل تعلم ؟

تنمو نبتة الـ kudzu أكثر من سبعة أقدام ( أي أكثر من مترين ) في الأسبوع الواحد. استكشف المزيد عن هذه النبته الرائعه.

Advanced Interprocess Communication (IPC)

مقدمة

هذه الجلسة تم تصميمها حتي تكون مألوفة مع طرق الـ ( IPC ) المعتادة عن طريق المقارنة و التباين بين التقنيات حتي يمكنك أن تقرر بكفاءة أيهما تستخدم. سوف أوضح أيضاً كلا من نقاط القوة و الضعف لكل طريقة و سأقدم نقاط البداية لتطبيق كل طريقة.

بسبب محدودبة الوقت، سوف تحتوي تلك الجلسة علي عدة أمثلة فقط:

أنواع الـ IPC

محلي

إن الـ IPC المحلية مفيدة لمشاركة البيانات، الإشارة، و كذلك التزامن.

يمكن تسمية الإشارات و الأحداث، أو عدم تسميتها ( يتم تعريفها عن طريق المتعامل معها) ، و كذلك يمكن حمايتها باستخدام ترخيصات الـ NT.

الذرات

الذرات بصفة عامة هي متغيرات نصية أو رقمية صحيحة يتم تعريفها عن طريق المتعامل معها . كان استخدام الذرات اساسا للـ DDE.إن الذرة العامة محدودة أيضاً إلي 37 نص كلياً. أنا لا أوصي باستخدام الذرات كما هي مهجورة، فإن تقنيات ال_IPC الأخري يجب أن يتم استخدامها.

الذاكرة المشتركة:

إن الذاكرة المشتركة هي جزء من الذاكرة تستطيع المعالجات المتعددة أن تصل إليها. ما قبل Win16 كانت كل الذاكرة متاحة للدخول لأي عملية و لذلك كان يمكن للمؤشرات أن تتنقل بين العمليات. يمتلك Win32 تقنيات لحماية الذاكرة، و يتم تخطيط ذاكرة العمليات فعليا لهذه العملية. و علي الرغم من ذلك فإن Win32 يمكنه تخطيط جزء من الزاكرة إلي مساحات الذاكرة المخصصة للعمليات المتعددة. و بالرغم من أن جزء الذاكرة المشترك قد يظهر أنه في مكان مختلف لكل عملية ، فإن كل منها سوف يؤشر إلي نفس المكان الفعلي بالذاكرة . بالإضافة إلي ذلك، فإن هذه التقنية تمكنك من التحكم في أي العمليات التي يمكنها الدخول إلي أجزاء الذاكرة المشتركة. يستطيع Win32 أيضاً أن يقوم بتخطيط ملفات القرص بالذاكرة التخيلية.بسبب هذا، عندما يتم استخدام الذاكرة المشتركة، يتم تسميتها بالذاكرة المخططة للملفات.

يستطيع Win32 أيضاً أن يفعل ذلك لأجزاء الكود للملفات التنفيذية. عندما يتم تحميل عدة مرات من الملف التنفيذي، يتم تحميل نسخة واحدة فقط بالذاكرة المادية، حيث يتم تخطيطه إلي مساحات تخيلية متعددة.

الاستثناءات المشتركة (Mutual Exclusion)

 

الاستثناءات المشتركة هي كائن يمكن انتلاكه، و لكن يمكن امتلاكه عن طريق عملية واحده لكل مرة. كل عملية أخري تحاول أن تمتلكها ،سيتم حجبها و صفها في الصف الخاص بترتيب الطلبات. الاستثناءات المشتركة يتم استخدامها للتحكم في الاستثناء الذي يدخل لأي كائن عام ، أو أي كود لم يتم إعادة إدخاله. يمكن استخدام الاستثناءات المشتركة للتحكم في الدخول إلي الذاكرة المشتركة ، المكونات المادية ، أو الأشياء الأخري المحددة بدخول واحد.

الإشارات

الإشارات شبيهة بالاستثناءات المشتركة، باستثناء أن عدد محدد من العمليات يمكنه أن يمتلك إشارة محددة في نفس الوقت. عندما يتم عمل أشارة جديدة، فإن هذا الرقم يتم تخصيصه.عادة ما تستخدم المؤشرات لدخول محدود للموارد. كمثال لهذا فانه قد يمنع الاستغلال الشديد للموارد عن طريق وضع عدد أقصيللعمليات التي يمكنها أن تدخل الكائن في نفس الوقت.

Critical Sections

Critical sections are similar to mutexes, however mutexes differ from critical sections in that they can be accessed across processes and threads, while a critical section, can only be access across threads from within the same process. Critical sections are also lighter weight, cannot be named, or have security attributes assigned to them.

Events

An event is an object which threads can wait for to be triggered. Any free thread may then trigger the event, which will then release the other waiting threads. An event can be used to hold threads from accessing something that has not been initialized or ready yet. They can also be used to signal that input is available, or a device is ready to accept more data.

Messages

Messages can be used to send a one way queued instructions/status to a single thread. You can send messages to an existing windows message queue, and filter them as they come in, or create your own queue within a secondary thread. Messages are good for non time critical communications, or to serialize handling. Messages can contain only a limited amount of data however. Each message can contain only one 16 bit integer, and one 32 bit integer. When communicating between threads, pointers may be passed in the 32 bit parameter. However when communicating between processes pointers may not be passed, even when the pointer identifies shared memory as each process may see the shared memory at a different location in its virtual space. Object handles (Mutexes, semaphores, events) cannot be passed in the message data, however such handles can be duplicated, and the duplicated handles may be passed via the message data. (See DuplicateHandle)

DDE

DDE (Dynamic Data Exchange) was the common way to externally control applications in the 16 bit world. DDE is still used occasionally, however it has been strategically replaced by COM. You should use Com instead of DDE, unless the application you need to communicate with only understands DDE. DDE also tends to be slow, and the built in DDE in Delphi/C++ Builder is "less than perfect".

Clipboard

The clipboard can also be used for IPC, however in most situations it is not advisable as it is a global resource.

OLE/Com

OLE (Object Linking & Embedding) has evolved into COM (Component Object Model). COM allows you to do many things. With COM you can create language independent objects, expose interfaces from applications to allow external control, and much more. COM allows you to expose interfaces. An interface is a defined set of methods and properties which allow you to proxy calls to any code or object that you wish. An interface is like a contract. Any time that the interface is implemented, all of its members must be implemented.

Network

Network IPC is for communication between processes on different machines.

Network Protocols

Network protocols allow you the most flexibility, however they bind you to a single protocol, and require a lot of extra programming. Most times you do not need this flexibility and it is better to use a higher level protocol. Network protocols are also lighter weight.

Some common protocols used for network communication are IPX/SPX (Novell), NetBEUI (Microsoft), and TCP/IP (Internet).

NetBios is also a protocol, but it is a slightly higher protocol than the others. It abstracts some of the features of the other network protocols while still providing you with most of their advantages. NetBioss main feature is that is does not bin you to a particular protocol which your network configuration and/or hardware may dictate. NetBios commonly runs on top of NetBEUI or TCP/IP.

If you do decide you need to work at the network protocol level, TCP/IP is probably your best bet. It is very flexible, in widespread use (The internet runs on it), works with any operating system, has tons of existing services, and has become the de facto standard.

Mailslots

Mailslots are like messages except that they can be sent across a network. Mailslots also differ from messages in that they are not arbitrarily limited in the amount of data they can contain, and mailslots can be broadcast to a group of receivers. Mailslots however are sent as datagrams, and therefore are not guaranteed to arrive at their destination. Mailslots are similar to TCP/IPs UDP mechanism, but at a slightly higher level. If delivery must be guaranteed, another method should be used.

Pipes

A pipe is a communication channel with two endpoints. Pipes are similar to TCP connections. Pipes however can be optionally named, and contain security attributes. Pipes can also be one or two way. Pipes are used both for network communication, and locally. For example, the console is a pipe. If you want to open a DOS session / command window, you can swap a pipe for its standard in and standard out, thus giving your application full control of the window.

RPC

RPC (Remote Procedure Calls) allow you to make function/procedure calls in which the code actually executes on another system. RPCs are typically used with older systems, mainframes, or for implementing distributed object systems.

Distributed Objects

Distributed objects are used for communication among objects. These objects can be local or remote. This easily allows you to distribute your programs with complete abstraction of location. The two most common distributed object standards are DCom and Corba.

Without distributed object systems, developers need to implement their own protocols and also decide upon and program for a network transport. In many cases they also tie their protocol to a specific language, and even a platform. Distributed object systems abstract the user from all this and provide a high level interface which is platform independent, language independent, and network protocol independent.

DCom

DCom is just a distributed version of COM (Component Object Model). DCom is controlled by Microsoft and therefore is tied heavily to Win32 and other Microsoft technologies. The specification is available for other platforms, but is rarely used. For all intents and purposes, DCom is for WinTel only. DCom is also heavily tied to NT security. If you have lots of users that do not have NT accounts, this may pose a major headache for you. NT security can be bypassed by using the Midas socket server (Ive been told you do not need a Midas license to merely use this single component).

Corba

Corba (Common Object Request Broker Architecture) is a platform independent distributed object protocol. Corba is also implemented by many vendors including Inprise/Visigenic. This allows you to choose your implementation. The Corba standard is controlled by the OMG (Object Management Group). You can find tons of Corba information at their web site http://www.omg.org