More information on C Library Implementation

Programming, for all ages and all languages.
Post Reply
climjark
Posts: 1
Joined: Sun Jul 02, 2017 9:10 pm
Freenode IRC: climjark

More information on C Library Implementation

Post by climjark »

Hello all, i have been writing my own Operating System , Xenia (github : metallicsoul92/xenia ) and i was wondering, other then looking at other C Standard library implementations, if there is any other ways of implementing it. I have read the open standards library file, but it just gave me a information on what should be defined, and not how.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania
Contact:

Re: More information on C Library Implementation

Post by Love4Boobies »

If you're told both what to implement and how then what is your role, other than that of a secretary/typist? The C standard defines the semantics (how things should behave) and you have to solve the problem of how this should happen by making trade-offs that make sense given your own set of constraints. Different constraints should yield different implementations, especially since they often compete. But don't worry, this is great practice! If you are struggling to do it, you are learning something in the process and becoming a better engineer. Don't be afraid to spend a lot of time not knowing what to do.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: More information on C Library Implementation

Post by Solar »

P.J. Plaugher, "The Standard C Library". It's an actual book on how to implement an actual C standard library ('89).

I bought it, I read it, I learned a lot, and then did it rather differently as I didn't agree with all of Plaugher's ideas and designs (also, copyright). But it gave some good advice, raised some awareness for things I would have considered too late (resulting in major rewrites), and gave me an impression of how I did not want to do certain things. ;-)
Every good solution is obvious once you've found it.
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

Re: More information on C Library Implementation

Post by Geri »

c library is basically just a few function like strcpy, memset and stuff like that, maybe totally 50 or 60 function, basically one day of work.
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: More information on C Library Implementation

Post by bzt »

climjark wrote:it just gave me a information on what should be defined, and not how.
That makes sense as the standard library's main purpose is to hide kernel interface details and provide a standard way for applications to interact with the OS. Therefore it needs to define functions (which are common among OSes), but cannot say how to implement them in general (as the implementation varies from kernel to kernel).
It worth pointing out that implementing those functions in userspace would improve performance but would also lower security. For example: memcpy() can use rep movsb instruction (no priviledge level switch, no overhead, but any address can be passed, most OS implementation choose this), or could use a memcpy system call (slow, as priviledge level has to be changed and transfer passed to kernel, but can limit input to only specific memory address regions). Likewise fopen() could be implemented in userspace only (assumes single address space for the system (like CP/M and Singularity) not really good for security, but fast as hell), or with system calls (as overhead is insignificant compared to disk latency and security is more of importance most OSes (SunOS, Linux, BSDs, WindowsNT etc.) choose this). The standard library's prototype for memcpy() and fopen() would be the same regardless of the choice.
alexfru
Member
Member
Posts: 1108
Joined: Tue Mar 04, 2014 5:27 am

Re: More information on C Library Implementation

Post by alexfru »

Geri wrote:c library is basically just a few function like strcpy, memset and stuff like that, maybe totally 50 or 60 function, basically one day of work.
Your days must be very long then! I've spent several weeks implementing the library for Smaller C. With the floating point support it's probably several months. And there still are a few limitations and bugs that need addressing (nothing urgent, though).
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: More information on C Library Implementation

Post by Solar »

Geri wrote:..maybe totally 50 or 60 function, basically one day of work.
Yeah, right. And we're all idiots for not realizing how easy it all is.

<stdio.h> has 64 functions (including printf(), and that alone you won't do correctly in one day). <stdlib.h> has 48 functions, including (among other non-trivials) malloc() and free(). <string.h> has 33 functions, some of which very habitually get implemented naively, and wrongly (looking at you, strncpy()). And that's the easy part.

You need <math.h> code, including proper handling of INFs, NaNs, over-/underflow etc.; and printf() / scanf() / strtod() to support it. You would need a multibyte / Unicode implementation for <wchar.h>, <wctype.h> and <uchar.h> to be considered "complete". There's <locale.h>, and since C11 you need <threads.h>...
Every good solution is obvious once you've found it.
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

Re: More information on C Library Implementation

Post by Geri »

alexfru wrote:Your days must be very long then
it took me 1 or 2 days to implement most the functions (string and number management like atoi, fgetc, itoa, strcpy, etc, it took a few minutes each. after all an strstr is just like 2 for cycle in each other....). later i invested another day to optimize some of the functions (memcpy for example) to suit for subleq instruction better. mathematical functions (sin, sqrtf, etc) took additional few days, i didnt however optimised those. my complete standard library including stdlib, math functions, and the full client-side interaction with the os is together approx 2000 line (and approx 1000 on the kernel side, plus of couse on the kernel side i had to implement for example a malloc or fread functions as well, but i consider that to the implementation of kernel and not as implementation of the c library).
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: More information on C Library Implementation

Post by Solar »

I'd like to see the source to those claims. Ideally in a repo with the corresponding commit times. I'm especially interested in putting your printf() through its paces.

Tried the link in your signature, but all I get there is binaries.
Every good solution is obvious once you've found it.
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

Re: More information on C Library Implementation

Post by Geri »

dawn os is not opensource
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
Post Reply