-6

I came across system call "write" then, I tried to compare "putchar" with "write". Now, I am confused with above lines of code.

  • 3
    Did you look at the manual pages for `write` and `putchar`? What is it you are finding confusing? – lurker May 21 '16 at 10:13
  • I have seen the manuals, I know that write is system call and putchar is library function. I am confusing in terms of the betterness of one over another i.e. in terms of efficiency and memory usage. i.e. what is internal architecture of "putchar"? – 0x47-sci-tech May 21 '16 at 12:58
  • I asked what you find confusing, but you didn't say. – lurker May 21 '16 at 13:00
  • I am sorry, To be specific putchar must be using write system call then, How that system call is different than above implementation? – 0x47-sci-tech May 21 '16 at 13:04

1 Answers1

2

The putchar is a library function. It calls the write system call to write the character in stdout.

If we access the system call so many time, the system performance will get slow.

So only, the library functions are implemented.

The library functions for writing, it allocates a buffer, once if the buffer is fulled or flushed then only it calls the write system call to write the contents in stdout.

So, if you want good system performance, you have to use the library functions (putchar).

Is there any need you have the write the output immediately, you can use the write system call.

  • A few improvements are possible: `write` is also a [library function](http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html), usually implemented by wrapping the `write` system call - but maybe not. `putchar` may or may not make the `write` system call directly - it might call the `write` function instead, or something else entirely. *if you want good system performance, you have to use the library functions* Maybe. That's too broad a generalization. *any need you have the write the output immediately, you can use the write system call* Or use `STDIO` to `stderr` - `fputc`. – Andrew Henle May 21 '16 at 15:17