I have searched hard but still confused why POSIX is called "Portable Operating System Interface", what I learned is that it is some threading library for Unix environment, because when you need to use it under windows you have to use cygwin or "Windows Services of Unix", etc. That's why I am confused why it is called Portable OSIX. I am a professional C/C++ programmer in Windows domain but new in Unix/Linux. Thanks for your answers in advance.
Before Posix, the Unix family tree was becoming very diverse and incompatible. A program written for one Unix was not compatible with a different Unix without significant porting effort.
Posix was one of the attempts to present a common set of utilities and programming interfaces so that your software would be portable to multiple versions of Unix.
Since Posix is about the interface and not the actual OS, it is possible to have a Posix facade on a non Unix OS (such as the Microsoft Windows Services for Unix presenting a Posix facade on top of Windows).
That one or two stragglers have decided to not make it part of their core doesn't not make it any less portable to almost every other important operating system.
POSIX is simply an interface for operating systems that defines concepts like threads, processes, signals, pipes and I/O. This is not the only interface that's portable across multiple interfaces, but is simply one standard. The name was actually defined by Richard Stallman in the 1980s.
The reason for defining POSIX was that many different versions of UNIX were incompatible, because operating systems hadn't agreed on the implementation of threading or processes.
Here are some more links for further research:
The History of Posix: A Study in the Standards Process
POSIX (Wikipedia)
POSIX defines a set of portable C functions, shell, programs that make the life of a programmer easier because given the definitions in POSIX, it is much more easier to write portable code (not just C, but shell scripts too). Imagine if everyone had their own way of doing things!
Not all the operating systems are POSIX compliant, so if you have to develop for those too, you have to do system-specific things. But POSIX is probably the portable standard across different kinds of systems today. Sure, there is ISO C, which is more portable, but then it's hard to write very useful programs in just ISO C!
Related
Suppose you are developing two application (A and B).
How can you send some piece of information to B from A if you are only allowed to work at the c++ language level (that is including the standard libraries and STL) ?
Now Im thinking std::ofstream and std::ifstream could be a possible solution (albeit a crude one) ? - but what pitfalls is there and can they be avoided ? (how?).
You just cannot. Standard C++17 does not know about any kind of inter-process communication and does not know much about processes (except thru std::system whose behavior is not really specified). Some operating systems don't have any processes and some of them don't have files and some of them don't have pipes.
Read more about operating systems. I strongly recommend Operating Systems: Three Easy Pieces (which is freely available).
Of course, you can read and write a file, but the synchronization between the two processes should still happen (perhaps by running one after the other, in some operating system specific way, so running A then B, and how that exactly happens is OS specific)
Read that C++17 standard (e.g. the draft here) to check.
Some C++17 implementations might not even have any notion of process. You could have a fully compliant C++17 on some embedded system without any operating system dealing with processes.
My recommendation is to be pragmatical, and use some framework like Boost, Qt, ZeroMQ, or POCO (or old Berkeley sockets) which deals with processes and inter-process communication facilities; you'll likely to find a framework supporting the several OSes you really care about (AFAIK, all of Boost, POCO, Qt know about Linux, Windows, MacOSX and offer a common API abstracting them; but you could find some academic operating system which is incompatible with them; in practice, any framework targeting both Windows and POSIX should be practically enough).
With some curiosity, you may find an OS with a good C++17 implementation which has a very weird API (look into GNU Hurd for an example).
If your IPC facility is based on byte streams, look into text-based protocols (perhaps JSONRPC, SOAP, HTTP, ...). They are easier to code and most of them come with some C++ compatible library...
And with a few months of work and a lot of know how, you might even port a recent GCC or Clang to most other operating systems: they are careful to abstract the requirements on the OS in a clever way.
Remember, you could find OSes which don't even have any file system: look into CapROS or Contiki for some recent example, and look also inside tunes.org where interesting discussions related to your topic, in the past century, have been archived. But with some pain (my guess is a few months of work for a GCC or Clang expert), you'll be able to port a recent GCC or Clang to target it to obtain a C++17 cross-compiler targetting them.
IMHO, a C++ standard library which enables only to "open" one single "file" (supposedly named THEFILE) is conforming to the letter of the C++17 standard. AFAIK, you don't have any guarantee that std::ifstream or std::ofstream works successfully.
BTW, current processors are practically multi-core, so it makes a lot of sense to try running A and B in parallel and doing some IPC (in an OS specific way, perhaps abstracted by some framework or library).
Sometimes I read about (if I'm not interpreting wrong) that posix threads are not available or valid on sone platforms such as some RTOSs which inplements their own threading mechanism.
So, is posix thread can be considered as standard (at least on general purpose OS)? And is it platform independent?
"So, is posix thread can be considered as standard (at least on general purpose OS)?"
No, it's not standard for OS's that POSIX threads are supported.
I'd say that std::thread implementations rely on some POSIX thread commonly defined features.
Embedded Linux platforms are POSIX compatible of course, and you can rely on pthreads.
Windows platforms (counting as a general purpose OS) for instance, doesn't support POSIX threads natively, but there are wrapper APIs available with e.g. MinGW or cygwin.
"that posix threads are not available or valid on sone platforms such as some RTOSs which inplements their own threading mechanism."
Other embedded platforms like FreeRTOS don't support that threading model directly, but are eligible to write a POSIX wrapper.
The basic thread semantics could be usually wrapped well for the POSIX standard requirements, and injected to newlib or whatever you like to use as binding for realising the standards implementation.
POSIX, the standard that specifies pthread, is a standard (Actually, more a group of standards) defining operating system behavior and functions. An OS either implements the standard or it doesn't. The goal of POSIX is platform independence, all the same OS calls on all platforms, but as with all standards, folks bolts stuff onto the edges to get extra functionality that is not portable or leave stuff out when it becomes problematic.
Linux implements POSIX fairly closely (but not even all families of Linux agree on how closely) so your concern will be with compatibility edge cases that I hope will be well documented at this point.
Windows POSIX support is somewhere below weak so you cannot count on it on general purpose OSes.
I recommend a quick search here on SO, Google or giving Wikipedia a look-over for a better description of POSIX.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I was confused by the numerous standards and interfaces for C and C++ programming. There's ANSI C, ISO C, GLIBC, POSIX, Win32, MFC, et cetera. What are the differences between these standards, and how are they related to each other? Under what scenarios would you pick a particular standard? Is there a diagram showing the relationships?
ANSI / ISO C
ANSI C / ISO C are standards designed to support an incredibly broad array of different systems and allow compiling legacy code from ages past. We can call this "standard C". Because C is standardized, there are many implementations.
Half of the C standard refers to the language itself, which includes specific guarantees about what types are available, what syntax you can use, et cetera. These guarantees are sometimes too broad to work with comfortably. For example, in standard C, it is guaranteed that at least the following types exist:
short int, which represents at least -32767..+32767
int, which represents at least -32767..+32767
long int, which represents at least -2147483647..+2147483647
Each type in the list must be wider than the last, but there is a lot of leeway for systems to choose different sizes. For example, on a DSP or an old supercomputer all of the types might be exactly the same size. There is also no guarantee that a byte has 8 bits (maybe it has more than 8 bits).
The other half of the C standard specifies the standard library, such as which functions are provided by each header. For example, the <stdlib.h> header must define the malloc() function.
A program written in standard C can be run almost anywhere, as long as you are careful not to rely on non-portable constructs. However, the C standard does not provide very much functionality... so these portable programs cannot do much more than open files or read input from the user on the console.
There are several versions of standard C. The most common ones are C89/C90, C99, and C11. It is not uncommon to find systems which only support C90 (for example, MSVC).
POSIX
POSIX is a much larger and more comprehensive standard which includes standard C as a part of it. POSIX also specifies parts of the operating system. Because POSIX is standardized, there are many implementations.
On a POSIX system, there are some restrictions on the C implementation. For example, on POSIX:
A byte is always 8 bits
Integers are always two's-complement
The / character is always used as a path separator
Certain errors are signaled by setting errno
POSIX also specifies some additions the the standard library, such as
Network sockets
Creating new processes
Multithreaded programming
Memory-mapped IO
A program written to run on POSIX can be run on Linux, Unix, OS X, or other POSIX-compliant systems. These programs will usually require extra work before they can be run on Windows. The POSIX standard includes interfaces for things like networking, process creation, shells, terminals, and filesystems. It is not too difficult to write a sophisticated POSIX program like a web server or a command-line shell.
There are several versions of POSIX.
GLibc
GLibc is the GNU C library. It implements the standard C library, POSIX extensions to the C library, and some extra functionality. GLibc is not standardized and there is only one implementation.
For example, GLibc provides asprintf(), which is like sprintf() but it allocates the buffer automatically.
Programs that use GLibc extensions are not generally portable, although certain extensions are also available on BSD systems.
Win32
Win32 is an API specific to Windows. The API provides functions not available in standard C, such as functions for creating a graphical user interface. Win32 is not standardized and there are only two implementations (Windows and WINE). Win32 provides a large set of interfaces, such as:
Network sockets
Creating new processes
Multithreaded programming
Memory-mapped IO
These interfaces overlap with POSIX, but the function calls are mostly different. For example, on Windows you can create a mutex with CreateMutexEx(), and on POSIX you create a mutex with pthread_mutex_init(). The exception to this is network sockets, which are mostly the same between Windows and POSIX.
Programs written for Win32 will generally only run on Windows and possibly WINE.
MFC
MFC is a library provided by Microsoft which makes it easier to write Win32 applications. It is effectively obsolete and should not be used for new projects. MFC is not standardized and there is only one implementation.
If a program was written in c++ to run on Windows, does it have to be completely rewritten to run on Mac OS or a mobile OS?
C++ is a standard language, which means that the source code that you write can be compiled on any platform which has an implementation of the C++ standard. There are two ways you can write C++ programs that can't be compiled on different implementation. First, if you use language extensions that are found on a specific (set of)implementation(s) only. Second, using a library that depends on code that doesn't ship with the standard library(like on OS API).
For the first matter, try always to write standard code. For the second, use cross-platform libraries like Boost, Qt...
Typically, yes, because the program will need to use OS-specific features for windowing and possibly for other features as well (networking, synchronization, etc.) However, many programs try to mitigate this as much as possible by building wrapper classes so that most of the program deals with these wrappers rather than the raw platform-specific tools. To port the program from one platform to another, you just need to reimplement the wrappers using the new platform's tools.
Many programs take this a step further by using prewritten libraries like Qt or Boost to handle some of the cross-platform silliness, but this is (essentially) the above idea at a larger scale.
This depends. In general, Standard C++ is a general-purpose, portable language which can be compiled to run on any system or platform that has a standard-compliant compiler.
However, a lot of the more "interesting" features you might want to add to a typical application are not part of Standard C++. This includes GUIs, threads, sockets, and low-level OS API calls. These are generally not portable, and parts of the code which use these features will need to be implemented separately for each operating system or platform.
Fortunately, this is not as daunting as it sounds, because there are a lot of cross-platform libraries in existence that have already gone through the trouble of doing this. For example, the Boost threading library already has threading code written for different platforms or operating systems, but all of this is abstracted behind a nice uniform API that can be used portably in C++ application code.
Additionally, a lot of non-Standard C++ code still conforms to some standard, such as POSIX, which is supported across multiple platforms. For example, most UNIX-ish systems, including Linux and Mac OS X, support POSIX threads (the pthread API).
If the code itself uses libraries that are supported on all the target platforms then you will only need the appropriate compilers to generate a valid binary for each system.
Software can be written in multiple languages and then linked together. For example, I can code the back-end logic of my application in C++ (often using Boost), and then build two separate front-ends in C# for Windows and Objective-C for Mac. I can link the C++ and C# components to ship for one platform, then link the C++ and Objective-C components to ship for another. That approach will give the most "native" look-and-feel for each platform.
As alternative, I can code the entire front-end in C++ using Qt or WxWidgets. This will run on all platforms, albeit without 100% of a platform's bells and whistles.
Is there a portable way of getting thread and/or process identifier (string, int, ...) with C++?
You have a few ways, but all imply the use of an external library abstracting the thread for you.
Among the popular choices, two are:
The Boost.Thread library. This is the most portable but imply working with Boost, which is a HUGE library
The Qt library. This is less portable and imply to work with Qt, a big library.
If you already use any on these two libraries, I would recommend sticking with it. Otherwise, look at what other tools they provide and make a choice.
There is no portable way when portable means a way that works on every platform for that a C++ compiler exists. Such a way had to be part of the C++ standard, in which case it really would work everywhere (just like the other parts of the C++ standard work everywhere). Everything not in the standard is not guaranteed to work on any platform, unless the platform states to support this standard.
Every solution people have suggested here is a solution that uses an external library and thus can only work on platforms supported by that library; and no library is available for every existing platform.
Probably the one that will get you farthest is POSIX, after all every UNIX like system tries to support at least some POSIX (the more the better), very little can call themselves really being 100% POSIX-compliant platforms (e.g. A/UX, AIX, HP-UX, IRIX, Mac OS X 10.5, MINIX, QNX, Solaris, UnixWare, VxWorks, ... to name a few, there are more of course). However, there are quite a couple of platforms that offer at least some POSIX support, some more, some less and some are almost POSIX-compliant (e.g. FreeBSD, Linux, NetBSD, BeOS, OpenBSD, ... and others).
Windows is unfortunately far away from being one. NT used to be partly POSIX conform, but now it has more or less vanished (Win2000/20003, WinXP, and Vista can still be set into a POSIX emulating mode, translating some POSIX call to internal API calls by installing Microsoft Windows Services for UNIX - SFU 3.5 or higher), however there are ways to get some POSIX functionality on Windows through external libraries as well (Cygwin offers LGPL libraries you can link with your app to enable a fair amount of POSIX functions on Windows).
The advantage of POSIX is not only that it is relatively widespread, but also that it is standardized and you can easily look up the standard on the Internet. Using POSIX calls you can get a thread id and a process id.
I always thought threads were external from C++. In Java, there's a native thread built-in to the language.
You'd have to find a portable thread library.
The only way is to use portable library. Id recommend Qt (it doesn't have to be GUI app) or maybe wxWidgets. If you are developing a game check out SDL
Also check out boost libs they might have something.
You may also use part of ACE library, which implements a platform independent wrapper. Finding PID would be one of the files in the library (maybe ACE_Process/ ACE_Thread).
I'm not sure how portable they are but Posix threads may be another option you want consider. See also here. I agree with Steve's comment--portable to which platforms?
getpid() is a portable way to get the process ID.
I don't think you're going to find a portable method unless it is through a wrapper library. Each threading system (eg. Windows, or POSIX) are going to have their own mechanism.