Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Porting Enterprise Apps from UNIX to Linux

By Martyn Honeyford
2005-04-17


Select the Threading Model

It is quite possible that some old application being ported to Linux are based on draft 4 of pthreads. The latest versions of Linux support pthreads draft 10, so care needs to be taken to map the calls appropriately. If the application is using some exception-handling mechanisms based on a third-party implementation (for example, TRY-CATCH macros provided by DCE), then the programmer needs to make sure that the exception-handling code is also compatible with draft 10 of pthreads.

Some examples of calls that have changed from draft 4 to 10 are in the following table.

Table 1. Calls changed from draft 4 to 10 of pthreads
pthreads draft 4 pthreads draft 10
pthread_setcancel(CANCEL_ON) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)
pthread_setcancel(CANCEL_OFF) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)
pthread_setasynccancel(CANCEL_ON) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)
pthread_setasynccancel(CANCEL_OFF) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL)
pthread_getspecific(key, value) *value = pthread_getspecific(&key)


The choice of threading model lies somewhere between native Linux threads and the Native POSIX Thread Library (NPTL) implementation, which provides a POSIX-compliant implementation for the threads in Linux. The Linux kernel (from 2.5 version onwards) has been modified to provide POSIX-compliant support. NPTL is available on SLES9. Red Hat has backported NPTL support for RHEL3 (which is based on the Linux 2.4 kernel). RHEL 3 has support for both NPTL and native Linux threads. You can switch to native Linux threads by setting an environment variable LD_ASSUME_KERNEL=2.4.1, but not many vendors have ported their software on RHEL3 using NPTL support.

Major drawbacks to using native Linux threads are the following:

• Child thread SIGCHILD comes to the thread, not the process (a non-POSIX behavior).
• getpid() is broken -- it is very hard to get the pid for a group of threads constituting a process.
• Changing the userid in a thread doesn't change it for all threads in a process.

In short, each thread looks like (and in some ways behaves like) a separate process.

There are also problems on the kernel side:

• Processes with hundreds or thousands of threads make the /proc filesystem barely usable. Each thread shows up as a separate process.
• The problems with the signal implementation are mainly due to missing kernel support. Special signals like SIGSTOP would have to be handled by the kernel and for all threads.
• The misuse of signals to implement synchronization primitives adds even more to the problems. Delivering signals is a heavy-handed approach to ensuring synchronization.

On the other hand, with NPTL:

• Signaling issues seem to have been resolved in the latest distribution (Linux 2.6 onwards). Now, signals can be delivered to the process as a whole.
• Futexes (fast userspace mutex, a basic tool to realize locking and building higher-level locking abstractions such as semaphores and POSIX mutexes on Linux) have been implemented, which helps callers wait in the kernel and be woken up explicitly. Thus, PTHREAD_PROCESS_SHARED and interprocess POSIX synchronization primitives can be implemented and are now available.
• It uses a 1:1 model (each user-level thread has an underlying kernel thread) and is pre-emptive (kernel threads can be pre-empted).
• It is Suitable for I/O- and CPU-intensive applications.
• The aim is to move towards 100 percent POSIX compliance.

Based on the improvements provided by various distributions, it would generally be advisable to use the version of Linux that supports NPTL.

Tutorial Pages:
» A Practical Checklist, Tips, and Insight Drawn from Experience
» Get the Build System Working
» Decide on a Viable Operating Environment
» Architecture-Specific Changes
» Choose an IPC Mechanism
» Select the Threading Model
» File System, Usage Parameters, Stacks
» Memory Maps and Using Shared Memory Segments
» Signaling
» Configure Kernel Karameters
» Parser Tools like lex/yacc
» Globalization Issues
» Security Concerns
» Locating Installed Packages and Variable Data
» Testing
» There's a Port in Every Storm
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation