📄 libc.sgml
字号:
<PARA>Here are some details about the <!-- <index></index> -->implementationwhich might be interesting, although they do not affect the ISO-definedsemantics of the library. </PARA><ITEMIZEDLIST><LISTITEM><PARA>It is possible to configure <EMPHASIS>eCos</EMPHASIS> to have the standard C library without the kernel. You might wantto do this to use less memory. But if you disable the kernel, youwill be unable to use memory allocation, thread-safety and certainstdio functions such as input. Other C library functionality isunaffected.</PARA></LISTITEM><LISTITEM><PARA>The opaque type returned by <FUNCTION>clock()</FUNCTION> is called clock_t, and is implemented as a 64 bit integer.The value returned by <FUNCTION>clock()</FUNCTION> is only correct if the kernel is configured with real-time clocksupport, as determined by the CYGVAR_KERNEL_COUNTERS_CLOCKconfiguration option in <FILENAME>kernel.h</FILENAME>.</PARA></LISTITEM><LISTITEM><PARA>The FILE type is not implemented as a structure, but ratheras a CYG_ADDRESS. </PARA></LISTITEM><LISTITEM><PARA>The GNU C compiler will place its own <EMPHASIS>built-in</EMPHASIS> implementationsinstead of some C library functions. This can be turned off withthe <EMPHASIS>-fno-builtin</EMPHASIS> option. The functions affectedby this are <FUNCTION>abs()</FUNCTION>, <FUNCTION>cos()</FUNCTION>, <FUNCTION>fabs()</FUNCTION>, <FUNCTION>labs()</FUNCTION>, <FUNCTION>memcmp()</FUNCTION>, <FUNCTION>memcpy()</FUNCTION>, <FUNCTION>sin()</FUNCTION>, <FUNCTION>sqrt()</FUNCTION>, <FUNCTION>strcmp()</FUNCTION>, <FUNCTION>strcpy()</FUNCTION>, and <FUNCTION>strlen()</FUNCTION>.</PARA></LISTITEM><LISTITEM><PARA>For faster execution speed you should avoid this optionand let the compiler use its built-ins. This can be turned off byinvoking <EMPHASIS>GCC</EMPHASIS> with the <EMPHASIS>-fno-builtin</EMPHASIS> option. </PARA></LISTITEM><LISTITEM><PARA><FUNCTION>memcpy()</FUNCTION> and <FUNCTION>memset()</FUNCTION> are located in the infrastructure package, not in the C librarypackage. This is because the compiler calls these functions, andthe kernel needs to resolve them even if the C library is not configured. </PARA></LISTITEM><LISTITEM><PARA>Error codes such as EDOM and ERANGE, as well as <FUNCTION>strerror()</FUNCTION>, are implemented in the <EMPHASIS>error</EMPHASIS> package. Theerror package is separate from the rest of the C and math librariesso that the rest of <EMPHASIS>eCos</EMPHASIS> can use these error handling facilities even if the C library isnot configured. </PARA></LISTITEM><LISTITEM><PARA>When <FUNCTION>free()</FUNCTION> is invoked, heap memory will normally be coalesced. If the CYGSEM_KERNEL_MEMORY_COALESCEconfiguration parameter is not set, memory will not be coalesced,which might cause programs to fail. </PARA></LISTITEM><LISTITEM><PARA>Signals, as implemented by <filename><signal.h></filename>, are guaranteed to workcorrectly if raised using the<FUNCTION>raise()</FUNCTION> function from a normal working program context. Using signals fromwithin an ISR or DSR context is not expected to work. Also, it isnot guaranteed that if CYGSEM_LIBC_SIGNALS_HWEXCEPTIONSis set, that handling a signal using <FUNCTION>signal()</FUNCTION> will necessarily catch that form of exception. For example, itmay be expected that a divide-by-zero error would be caught by handling <varname>SIGFPE</varname>. However it depends on the underlying HAL implementation to implementthe required hardware exception. And indeed the hardware itselfmay not be capable of detecting these exceptions so it may not bepossible for the HAL implementer to do this in any case. Despitethis lack of guarantees in this respect, the signals implementationis still ISO C compliant since ISO C does not offer any such guaranteeseither. </PARA></LISTITEM><LISTITEM><PARA>The <FUNCTION>getenv()</FUNCTION> function is implemented (unless the CYGPKG_LIBC_ENVIRONMENT configurationoption is turned off), but there is no shell or <FUNCTION>putenv()</FUNCTION> function to set the environment dynamically. The environment isset in a global variable environ, declared as:</PARA><PROGRAMLISTING>extern char **environ; // Standard environment definition</PROGRAMLISTING><PARA>The environment can be statically initialized at startup timeusing the CYGDAT_LIBC_DEFAULT_ENVIRONMENToption. If so, remember that the final entry of the array initializermust be NULL. </PARA></LISTITEM></ITEMIZEDLIST><PARA>Here is a minimal <EMPHASIS>eCos</EMPHASIS> program whichdemonstrates the use of environments (see also the test case in <filename>language/c/libc/current/tests/stdlib/getenv.c</filename>): </PARA><PROGRAMLISTING>#include <stdio.h>#include <stdlib.h> // Main header for stdlib functionsextern char **environ; // Standard environment definitionintmain( int argc, char *argv[] ){ char *str; char *env[] = { "PATH=/usr/local/bin:/usr/bin", "HOME=/home/fred", "TEST=1234=5678", "home=hatstand", NULL }; printf("Display the current PATH environment variable\n"); environ = (char **)&env; str = getenv("PATH"); if (str==NULL) { printf("The current PATH is unset\n"); } else { printf("The current PATH is \"%s\"\n", str); } return 0;} </PROGRAMLISTING></SECT1><SECT1 id="libc-thread-safety"><TITLE><!-- <index></index> -->Thread safety</TITLE><PARA>The ISO C library has configuration options that control threadsafety, i.e. working behavior if multiple threads call the samefunction at the same time.</PARA><PARA>The following functionality has to be configured correctly,or used carefully in a multi-threaded environment:</PARA><ITEMIZEDLIST><LISTITEM><PARA><function>mblen()</function></para></listitem><listitem><para><function>mbtowc()</function></para></listitem><listitem><para><function>wctomb()</function></para></listitem><listitem><para><FUNCTION>printf()</FUNCTION> (and all standard I/O functions except for <FUNCTION>sprintf()</FUNCTION> and <FUNCTION>sscanf()</FUNCTION></PARA></LISTITEM><LISTITEM><PARA><FUNCTION>strtok()</FUNCTION></PARA></LISTITEM><LISTITEM><PARA><FUNCTION>rand()</FUNCTION> and <FUNCTION>srand()</FUNCTION></PARA></LISTITEM><LISTITEM><PARA><FUNCTION>signal()</FUNCTION> and <FUNCTION>raise()</FUNCTION></PARA></LISTITEM><LISTITEM><PARA><FUNCTION>asctime()</FUNCTION>, <FUNCTION>ctime()</FUNCTION>, <FUNCTION>gmtime()</FUNCTION>, and <FUNCTION>localtime()</FUNCTION></PARA></LISTITEM><LISTITEM><PARA>the <FUNCTION>errno</FUNCTION> variable</PARA></LISTITEM><LISTITEM><PARA>the <FUNCTION>environ</FUNCTION> variable</PARA></LISTITEM><LISTITEM><PARA>date and time settings</PARA></LISTITEM></ITEMIZEDLIST><PARA>In some cases, to make <EMPHASIS>eCos</EMPHASIS> developmenteasier, functions are provided (as specified by POSIX 1003.1) that definere-entrant alternatives, i.e. <FUNCTION>rand_r()</FUNCTION>, <FUNCTION>strtok_r()</FUNCTION>, <FUNCTION>asctime_r()</FUNCTION>, <FUNCTION>ctime_r()</FUNCTION>, <FUNCTION>gmtime_r()</FUNCTION>,and <FUNCTION>localtime_r()</FUNCTION>. In other cases,configuration options are provided that control either locking of functionsor their shared data, such as with standard I/O streams,or by using per-thread data, such as with the <FUNCTION>errno</FUNCTION> variable.</PARA><PARA>In some other cases, like the setting of date and time, nore-entrant or thread-safe alternative or configuration is providedas it is simply not a worthwhile addition (date and time shouldrarely need to be set.)</PARA></SECT1><SECT1 id="c-library-startup"><TITLE><!-- <index></index> --><!-- <xref> -->C library startup</TITLE><PARA>The C library includes a function declared as:</PARA><PROGRAMLISTING>void <FUNCTION>cyg_iso_c_start</FUNCTION>( void )</PROGRAMLISTING><PARA>This function is used to start an environment in which anISO C style program can run in the most compatible way.</PARA><PARA>What this function does is to create a thread which will invoke <FUNCTION>main()</FUNCTION> — normallyconsidered a program's entry point. In particular, it cansupply arguments to <FUNCTION>main()</FUNCTION> using the CYGDAT_LIBC_ARGUMENTSconfiguration option, and when returning from <FUNCTION>main()</FUNCTION>,or calling <FUNCTION>exit()</FUNCTION>, pending stdio file outputis flushed and any functions registered with <FUNCTION>atexit()</FUNCTION> areinvoked. This is all compliant with the ISO C standard in this respect. </PARA><PARA>This thread starts execution when the <EMPHASIS>eCos</EMPHASIS> scheduleris started. If the <EMPHASIS>eCos</EMPHASIS> kernel package is notavailable (and hence there is no scheduler), then <FUNCTION>cyg_iso_c_start()</FUNCTION> willinvoke the <FUNCTION>main()</FUNCTION> function directly, i.e.it will not return until the <FUNCTION>main()</FUNCTION> functionreturns. </PARA><PARA>The <FUNCTION>main()</FUNCTION> function should be definedas the following, and if defined in a C++ file,should have “C” linkage: </PARA><PROGRAMLISTING>extern int <FUNCTION>main</FUNCTION>( int <EMPHASIS>argc,</EMPHASIS> char *<EMPHASIS>argv[] </EMPHASIS>)</PROGRAMLISTING><PARA>The thread that is started by <FUNCTION>cyg_iso_c_start()</FUNCTION> canbe manipulated directly, if you wish. For example you can suspendit. The kernel C API needs a handle to do this, which is availableby including the following in your source code.</PARA><PROGRAMLISTING>extern cyg_handle_t cyg_libc_main_thread;</PROGRAMLISTING><PARA>Then for example, you can suspend the thread with the line:</PARA><PROGRAMLISTING>cyg_thread_suspend( cyg_libc_main_thread );</PROGRAMLISTING><PARA>If you call <FUNCTION>cyg_iso_c_start()</FUNCTION> anddo not provide your own <FUNCTION>main()</FUNCTION> function,the system will provide a <FUNCTION>main()</FUNCTION> for youwhich will simply return immediately.</PARA><PARA>In the default configuration, <FUNCTION>cyg_iso_c_start()</FUNCTION> isinvoked automatically by the <FUNCTION>cyg_package_start()</FUNCTION> functionin the infrastructure configuration. This means that in the simplestcase, your program can indeed consist of simply:</PARA><PROGRAMLISTING>int main( int argc, char *argv[] ){ printf("Hello eCos\n");}</PROGRAMLISTING><PARA>If you override <FUNCTION>cyg_package_start()</FUNCTION> or <FUNCTION>cyg_start()</FUNCTION>,or disable the infrastructure configuration option CYGSEM_START_ISO_C_COMPATIBILITYthen you must ensure that you call <FUNCTION>cyg_iso_c_start()</FUNCTION> yourselfif you want to be able to have your program start at the entry pointof <FUNCTION>main()</FUNCTION> automatically.</PARA></SECT1></CHAPTER></PART>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -