OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 25, 2018 4:23 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: More information on C Library Implementation
PostPosted: Sun Jul 02, 2017 9:14 pm 
Offline

Joined: Sun Jul 02, 2017 9:10 pm
Posts: 1
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.


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Sun Jul 02, 2017 11:47 pm 
Offline
Member
Member
User avatar

Joined: Fri Mar 07, 2008 5:36 pm
Posts: 2072
Location: Bucharest, Romania
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 ]


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Mon Jul 03, 2017 1:17 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7220
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Tue Jul 04, 2017 10:48 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 14, 2013 6:01 pm
Posts: 442
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://gerigeri.uw.hu/DawnOS/index.html


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Tue Jul 04, 2017 11:24 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 118
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.


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Tue Jul 04, 2017 3:34 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 847
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).


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Wed Jul 05, 2017 1:57 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7220
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Wed Jul 05, 2017 8:43 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 14, 2013 6:01 pm
Posts: 442
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://gerigeri.uw.hu/DawnOS/index.html


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Wed Jul 05, 2017 9:13 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7220
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: More information on C Library Implementation
PostPosted: Wed Jul 05, 2017 9:19 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 14, 2013 6:01 pm
Posts: 442
dawn os is not opensource

_________________
Operating system for SUBLEQ cpu architecture:
http://gerigeri.uw.hu/DawnOS/index.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group