home / linux / lprog (navigation links)

Marina, te llevaste todas las respuestas contigo


 
Some programming tips

 
command line paramaters Display
 int main ( int argc, char ** argv ) {
   for ( j = 0 ; j < argc ; j ++ )
     printf ( "\targv[%d] : (%s).\n", j, argv [ j ] ) ;
Help
 if ( argc > 1 ) {
     if ( strcmp( argv[1], "?" ) == 0 ) {
          printf ( "Please contact {%s}.\n", Autor ) ;
          exit( 0 ) ;
     } ;
 } ;
daemon if ( fork() <> 0 ) then exit() ;
display a HEX value, 8 digit printf ( "(0x%8.8X).\n", HexVal ) ;
display a String printf ( "unknown field id (%.8s).\n", szTAG ) ;
errno use perrno() or strerror(errno)
identify the code static char * kszAutor = "Robot Bellido, v 1.1" ;
mutex use pthread_mutex_lock()
(sample : tp760:suse72:/home/sebas/progs/cstq/cstq.c)
pid use getpid() (and #include </sys/types.h>)
random numbers use srandom() and random ()
(sample : tp760:suse72:/home/sebas/progs/random/ran.c)
semaphores use sem_init(), sem_post(), sem_wait(), sem_destroy() - POSIX.
(sample : tp760:suse72:/home/sebas/progs/cstq/cstq.c)
See Unix history.
sockets On the client, use connect() :
(sample : tp760:suse72:/home/sebas/progs/tcpclients)
(sample : tp770:e:\sebas\linux\progs\panel\sebas\panelcli.c)
(sample : t30:f:\linux\progs\panel\sebas\panelcli.c)
  • socket(),
  • gethostbyname(),
  • connect(),
  • send(),
  • recv(),
  • shutdown()
On the server, use bind :
(sample : tp770:d:/eines/rexx/sockets/tutor/tmp/sebas/mserver.cmd)
(sample : tp770:e:\sebas\linux\progs\panel\sebas\panelsrv.c)
(sample : t30:f:\linux\progs\panel\sebas\panelsrv.c)
  • socket(),
  • gethostid(),
  • bind(),
  • listen(),
  • accept(),
  • recv(),
  • send(),
  • shutdown()
threads use pthread_create( & TCB )
Under RH 6.2 I got errno = EINTR : Interrupted System Call in thread blocked at "recv()", so I moved to SuSE 7.2 and got it fixed.
(sample : \\t400\linux\progs\threads/th.c)

To use pthread library in linux environment, you must include pthread.h and compile the source code with -lpthread option. -lpthread means link to libpthread.so

gcc th.c -o th -lpthread
timeout use sigaction() and alarm()
in handle_sigalrm, use alarm() again !
(sample : tp760:suse72:/home/sebas/progs/timeout/to.c)
(sample : t30:f:/linux/progs/timeout/subto.c)
timeout (sub-second) start a thread that does nanosleep()
(sample : tp760:suse72:/home/sebas/progs/timeout/tsrv.c)
time of the day Define
 #include <time.h>
 void LeeHora ( char * pszHora )
 {
  time_t MiTiempo ;
     MiTiempo = time ( NULL ) ;
     strftime ( pszHora, 10, "%T", localtime ( & MiTiempo ) ) ;
 } ;
and use
 char szMiTiempo [ 11 ] ;
     LeeHora ( szMiTiempo ) ;

(sample : tp760:suse72:/home/sebas/progs/timeout/to.c)
write system/user log configure /etc/syslog.conf : LOCAL5.debug         /var/log/sebas.log
use openlog() and syslog()
(sample : tp760:suse72:/home/sebas/progs/writelog/writelog.c)


Here's few samples of mine.
Nice Beej's guide to network programming using sockets.
Fork() details (AIX).
Advanced Memory Allocation
Amunt! Top Amunt!
gcc c compiler
-bash-3.00$ gcc --help Usage: gcc [options] file... Options: -pass-exit-codes Exit with highest error code from a phase --help Display this information --target-help Display target specific command line options (Use '-v --help' to display command line options of sub-processes) -dumpspecs Display all of the built in spec strings -dumpversion Display the version of the compiler -dumpmachine Display the compiler's target processor -print-search-dirs Display the directories in the compiler's search path -print-libgcc-file-name Display the name of the compiler's companion library -print-file-name=<lib> Display the full path to library -print-prog-name=<prog> Display the full path to compiler component -print-multi-directory Display the root directory for versions of libgcc -print-multi-lib Display the mapping between command line options and multiple library search directories -print-multi-os-directory Display the relative path to OS libraries -Wa,<options> Pass comma-separated on to the assembler -Wp,<options> Pass comma-separated on to the preprocessor -Wl,<options> Pass comma-separated on to the linker -Xassembler <arg> Pass on to the assembler -Xpreprocessor <arg> Pass on to the preprocessor -Xlinker <arg> Pass on to the linker -save-temps Do not delete intermediate files -pipe Use pipes rather than intermediate files -time Time the execution of each subprocess -specs=<file> Override built-in specs with the contents of -std=<standard> Assume that the input sources are for -B <directory> Add to the compiler's search paths -b <machine> Run gcc for target , if installed -V <version> Run gcc version number , if installed -v Display the programs invoked by the compiler -### Like -v but options quoted and commands not executed -E Preprocess only; do not compile, assemble or link -S Compile only; do not assemble or link -c Compile and assemble, but do not link -o <file> Place the output into -x <language> Specify the language of the following input files Permissible languages include: c c++ assembler none 'none' means revert to the default behavior of guessing the language based on the file's extension Options starting with -g, -f, -m, -O, -W, or --param are automatically passed on to the various sub-processes invoked by gcc. In order to pass other options on to these processes the -W options must be used.

-lxxx means to link a library named libxxx, so with gcc -lpthread -lrt -lc project1.c -o out you're telling it to link libpthread, librt and libc.


Amunt! Top Amunt!
How to init misc vars
#define bzero( thing, sz ) memset( thing, 0, sz ) bzero( pkt.padding, 18 ) ; memset ( & Ncb, 0, sizeof( Ncb ) ) ; bzero( & Ncb, sizeof( Ncb ) ) ; char my_buffer [ 48 ] ; my_buffer [ 0 ] = '\0' ; char mine_buffer[ BUF_LEN ] ; memset( mine_buffer, '\0', BUF_LEN ) ; char QMgrName [ MQ_Q_MGR_NAME_LENGTH + 1 ] = "" ; QMgrName [ 0 ] = 0 ;

Amunt! Top Amunt!
While loop
unsigned int cnt = 5 ; unsigned long factorial = 1 ; do { factorial *= cnt-- ; /* Multiply, then decrement */ } while ( cnt > 0 ) ; printf ( "{%lu}.\n", factorial ) ;

Amunt! Top Amunt!
send an ARP packet (linux style)

See send_arp.c :

... pending to compile & run on Linux ...

Signals

Command kill -l under SuSE 7.2 displays :
  1) SIGHUP         2) SIGINT (a)     3) SIGQUIT        4) SIGILL   
  5) SIGTRAP        6) SIGABRT        7) SIGBUS         8) SIGFPE   
  9) SIGKILL (-)   10) SIGUSR1       11) SIGSEGV       12) SIGUSR2  
 13) SIGPIPE       14) SIGALRM       15) SIGTERM       17) SIGCHLD  
 18) SIGCONT       19) SIGSTOP (-)   20) SIGTSTP       21) SIGTTIN  
 22) SIGTTOU       23) SIGURG        24) SIGXCPU       25) SIGXFSZ  
 26) SIGVTALRM     27) SIGPROF       28) SIGWINCH      29) SIGIO    
 30) SIGPWR        31) SIGSYS
(-) are not catchable by sigaction.
(a) keyboard interrupt Control-C. See man 7 signal

Linux supports nearly every signal provided by SVR4, BDS and POSIX, with few exceptions ("The Linux Programmer's Guide", Sven Goldt, v 0.4, March 1995) :

Signal's description (see /usr/include/bits/signum.h) :

Lliçons que hem après


6.2 errors I've found


Pending items


Amunt! Top Amunt!
Links

Ep ! Site under construction. Escriu-me !
Actualitzat el 20130114 (a).
Uf !