2

So I did a quick test and

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main (int argc, char *argv[]) {
        printf("Hello World\n");
        printf("%d\n",getpid());
}

compiled with gcc on my macbook pro running OSX 10.9.5 prints

Hello World
640

As I would expect it to on most linux distributions. I know the darwin kernel is based on UNIX, but will all the linux system calls behave exactly the same on OSX as they do on let's say Ubuntu? (I am aware the pid is different different times time I run it will be different, but that's not what I'm really talking about here). I also have Ubuntu installed on a small partition of my SSD, so if the answer is no, that's okay.

8

I would say that it is misleading to call getpid() a "linux system call". That gives the impression that it is a Linux-specific system call, which it isn't. Actually, getpid() and many other system calls are specified by POSIX, and you will find it implemented on both Linux and MacOS and on many other systems, with identical behaviour.

The majority of system calls or even C library functions you will use in typical software are specified by standards like POSIX and ANSI C, and you will them implemented with the same behaviour on many different operating systems. Portable software is software that keeps to this set of common system calls and functions that are widely available.

Linux also has Linux-specific system calls. MacOS also has MacOS-specific system calls. Neither of those will work on the opposite operating system, obviously. The manpages for such system calls will usually call out the fact that they are not portable. Furthermore, they exist quite often as low-level implementation details and most software need not use them, which makes it easier to keep most software portable.

  • Thanks for the response. I'm in a class where the professor claims we have to have access to a linux environment so that we can use all the system calls he wants us to. Can you tell me roughly how different OSX system calls are from Linux ones - that is how many are OSX specific or Linux specific because the postix parts will be the same. Or can you direct me towards the proper reference materials might be for OSX (I found them for linux). – asky Sep 3 '15 at 4:33
  • 1
    To be more precise getpid() and similar are not system calls per se, they are libc wrappers around the system call (the code that actually transfers control from userspace to kernel). Thus it actually might be possible to have a Linux-specific system call when running on a different kernel - all you'd need would be a (user-space) wrapper, that would emulate the desired missing system call (if possible). – peterph Sep 3 '15 at 7:06
  • 1
    If the underlying question is "how can I do my assignments on my Mac", a popular solution is to install Parallels or VMware or Virtualbox on the Mac and run an entire Linux system in a virtual machine. – Mark Plotnick Sep 3 '15 at 8:16
  • @asky Your follow-up question is too open-ended to answer in this format. You can read the manpage for each system call or function in which you are interested; it will document whether it's standard and if so which standard specified is, usually near the bottom of the page. But in any case, by and large, your instructor will probably keep to POSIX and not require any esoteric Linux-specific stuff. Probably. – Celada Sep 3 '15 at 8:45
2

OS X is a certified UNIX operating system, guaranteeing it implements the POSIX standard. Linux, while not a certified UNIX, also implements the POSIX standard. If you limit your API calls to things that are part of POSIX you should have consistent behavior between OS X and Linux. Aside from POSIX, the C standard library is also standardized and you should have the same behavior between compiler and platforms (as long as you conform to the standard).

The getpid(2) man page on Linux says it conforms to POSIX.1-2001, 4.3BSD and SVr4. The same man page on OS X 10.10 says it conforms to POSIX.1-1988.

  • Certified POSIX does not mean POSIX compliant :-( Note that both Linux and OS/X implement waitid() non-POSIX compliant, but still different. A correct waitid() returns all 32 bits from the exit() parameter but currently only Solaris and (a very recent) FreeBSD are correct – schily Sep 3 '15 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.