Live data from Hacker News

If everything in Unix development is POSIX, what the hell does C library do?

thejaywalker.net

1–10 of 24 posts

Re: If everything in Unix development is POSIX, what the hell does C library do?

#2
Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer.

I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

Re: If everything in Unix development is POSIX, what the hell does C library do?

#3

Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer. I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

thanks for correcting me on fork(); not sure what I was thinking when I wrote that statement :)

Re: If everything in Unix development is POSIX, what the hell does C library do?

#4

Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer. I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

I caught the same thing, but learned something interesting while double checking I wasn't crazy (from man 2 fork):

Since version 2.3.3, rather than invoking the kernel's fork() system call, the glibc fork() wrapper that is provided as part of the NPTL threading implementation invokes clone(2) with flags that provide the same effect as the traditional system call. The glibc wrapper invokes any fork handlers that have been established using pthread_atfork(3).

At least on my ubuntu box.

  andrew-think ⚑ ~ strace -f sh -c 'sh &' 2>&1 | egrep -A1 'clone|fork'
  clone(Process 2646 attached
  child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7ff002bac9d0) = 2646

Re: If everything in Unix development is POSIX, what the hell does C library do?

#5

Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer. I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

Ah, fork(). One of my favorite C calls. Not.

That looks to be some questionable phrasing from the article.

Yes, fork() is a system call on many platforms, but not on all.

And fork() is not ubiquitous. If anything, it's one of the common and can be one of the more intractable sources of porting problems within C code.

fork() does rather more than many C programmers might realize. About 5 or 10% of the calls I've encountered in the many C applications I've ported will use fork() for most or all of what it can do. The remaining calls throw that context away, and can often be equally or better served with vfork()/exec() or some other C call.

Those applications that use fork() for what it can do can be and usually are more difficult to port to the various platforms that lack copy-on-read virtual memory.

Re: If everything in Unix development is POSIX, what the hell does C library do?

#6
post #5

Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer. I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

Ah, fork(). One of my favorite C calls. Not. That looks to be some questionable phrasing from the article. Yes, fork() is a system call on many platforms, but not on all. And fork() is not ubiquitous. If anything, it's one of the common and can be one of the more intractable sources of porting problems within C code. fork() does rather more than many C programmers might realize. About 5 or 10% of the calls I've encou…

vfork is probably worse because you have to be very careful if exec fails. Calling a function to log the error is likely to mess up the parent's memory.

Re: If everything in Unix development is POSIX, what the hell does C library do?

#8
post #4

Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer. I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

I caught the same thing, but learned something interesting while double checking I wasn't crazy (from man 2 fork): Since version 2.3.3, rather than invoking the kernel's fork() system call, the glibc fork() wrapper that is provided as part of the NPTL threading implementation invokes clone(2) with flags that provide the same effect as the traditional system call. The glibc wrapper invokes any fork handlers that have…

Ah, but just because glibc doesn't call the fork() system call doesn't mean the kernel doesn't have one. :) I bet you could still get to by calling something like __glibc_old_fork(), or at the very least a manual syscall(SYS_FORK) should work.

POSIX itself doesn't even use the words system call, only function, to describe them.

Re: If everything in Unix development is POSIX, what the hell does C library do?

#9

Not sure where you got the idea that fork() isn't a system call. It certainly is on my computer. I think the overall sentiment is right on, but I'm not sure I get the last question. posix is a superset, yes, but it doesn't provide replacements for the C library. Perhaps better phrased as "Why limit yourself to only standard C?"?

thanks for correcting me on fork(); not sure what I was thinking when I wrote that statement :)

Well, I guess I should have waited for this discussion to reach a conclusion :) I read in Advanced Linux Programming (the famous ALP book) that fork() as well as creating threads via pthread library use clone() in the background. Any ways, the discussion here is quite informative!

Re: If everything in Unix development is POSIX, what the hell does C library do?

#10
post #4

Earlier quoted context omitted.

I caught the same thing, but learned something interesting while double checking I wasn't crazy (from man 2 fork): Since version 2.3.3, rather than invoking the kernel's fork() system call, the glibc fork() wrapper that is provided as part of the NPTL threading implementation invokes clone(2) with flags that provide the same effect as the traditional system call. The glibc wrapper invokes any fork handlers that have…

Ah, but just because glibc doesn't call the fork() system call doesn't mean the kernel doesn't have one. :) I bet you could still get to by calling something like __glibc_old_fork(), or at the very least a manual syscall(SYS_FORK) should work. POSIX itself doesn't even use the words system call, only function, to describe them.

Yep:

  $ grep fork /usr/include/asm/unistd_64.h     
  #define __NR_fork				57
  __SYSCALL(__NR_fork, stub_fork)
  #define __NR_vfork				58
  __SYSCALL(__NR_vfork, stub_vfork)
  
  $ strace -f ruby -e 'syscall 57' 2>&1 | egrep 'fork|clone'
  fork(Process 4138 attached (waiting for parent)

  $ strace -f ruby -e 'fork' 2>&1 | egrep 'fork|clone'
  execve("/home/andrew/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby", ["ruby", "-e", "fork"], [/* 47 vars */]) = 0
  clone(Process 4145 attached
Post reply on HN