Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:howtos-tips [2008/11/15 08:38]
cyril
programming:howtos-tips [2014/04/16 18:07] (current)
cyril [Conversions with string]
Line 1: Line 1:
 ====== Howtos and Tips ====== ====== Howtos and Tips ======
  
 +===== System =====
  
-===== Catching signals =====+For details on functions search in the man page (''man <function-name>'' or ''man -a <function-name>'' if there is also a unix command with same name), or search the web. 
 + 
 +==== Catching signals ====
  
 => C and C++. => C and C++.
Line 33: Line 36:
 You can also change the signals actions with the [[http://www.opengroup.org/onlinepubs/007908799/xsh/sigaction.html|sigaction function]]. You can also change the signals actions with the [[http://www.opengroup.org/onlinepubs/007908799/xsh/sigaction.html|sigaction function]].
  
 +==== Timer ====
 +
 +<code c>
 +#include <unistd.h>
 +unsigned int alarm(unsigned int seconds);
 +</code>
 +will trigger signal SIGALRM x seconds later that you can catch (see [[#catching_signals]]).
 +
 +If you need automatic repetition and/or sub-second precision, you can use:
 +<code c>
 +#include <sys/time.h>
 +int setitimer(int whichtimer, const struct itimerval *value, struct itimerval *ovalue);
 +</code>
 +
 +==== Waiting event ====
 +
 +Monitor multiple file descriptors.
 +
 +<code c>
 +#include <sys/select.h>
 +int select(int nfds, fd_set *readfds, fd_set *writefds,
 +           fd_set *exceptfds, struct timeval *timeout);
 +</code>
 +
 +==== Posix Threads ====
 +
 +<code c>
 +#include <pthread.h>
 +
 +void *threadFunction(void *threadid)
 +{
 + printf("%d: Hello World!\n", threadid);
 + pthread_exit(NULL);
 +}
 +
 +int main (int argc, char *argv[])
 +{
 + pthread_t threadId;
 + int threadData = 42;
 + if (pthread_create(&threadId, NULL, threadFunction, (void *)threadData) != 0)
 + {
 + printf("thread creation failed");
 + exit(1);
 + }
 + return 0;
 +}
 +</code>
 +
 +FIXME join sem etc
 +
 +==== Change program owner ====
 +
 +<code c>
 +pwd = getpwnam("cyril");
 +if (pwd) 
 +{
 + setgid(pwd->pw_gid);
 + setuid(pwd->pw_uid);
 +}
 +</code>
 +
 +But it doesn't really work to start shell commands, you should use ''sudo'' too:
 +<code c>
 +system("sudo -u cyril <command>);
 +system("sudo -u cyril /bin/sh -c '<command1>; <command2>');
 +</code>
  
 ===== Formatting IOs (iomanip) ===== ===== Formatting IOs (iomanip) =====
Line 48: Line 117:
  
 <code cpp> <code cpp>
-std::cout << "normal text, "  << "\033[1;28m"  +std::cout << "normal text, "  << "\033[1;33m"  
-          << "colored text, " << "\033[1;33m"+          << "colored text, " << "\033[1;00m"
           << "normal text" << std::endl;           << "normal text" << std::endl;
 </code> </code>
Line 113: Line 182:
 <code c> <code c>
   #define ERROR(msg, ...) fprintf("ERROR:%s:%d# ", __FILE__, __LINE__, ## __VA_ARGS__);   #define ERROR(msg, ...) fprintf("ERROR:%s:%d# ", __FILE__, __LINE__, ## __VA_ARGS__);
 +</code>
 +You can also use the ''PRETTY_FUNCTION'' but it is not standard:
 +<code>
 +  #define ERROR(msg, ...) fprintf("ERROR:%s:%d %s# ", __FILE__, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__);
 </code> </code>
  
Line 137: Line 210:
 #include <stdio.h> #include <stdio.h>
 char s[128]; char s[128];
-sprintf("%d", 565);+sprintf(s, "%d", 565);
 </code> </code>
  
Line 146: Line 219:
 #include <stdlib.h> #include <stdlib.h>
 int i = atoi("562"); int i = atoi("562");
-float f atof("4.5"); +long l atol("4500294"); 
-double atol("4.546643");+double atof("4.546643");
 </code> </code>
  
Line 165: Line 238:
  
 ===== Function pointers ===== ===== Function pointers =====
 +
  
 The full test program is available [[http://www.rez-metz.supelec.fr/wiki/lib/exe/fetch.php/users:cyril:programming:function_pointers.cpp.txt|here]] The full test program is available [[http://www.rez-metz.supelec.fr/wiki/lib/exe/fetch.php/users:cyril:programming:function_pointers.cpp.txt|here]]
Line 251: Line 325:
  public:  public:
   ...   ...
-  double() { return data / (double)(1<<8); }+  operator double() { return data / (double)(1<<8); }
 }; };
  
programming/howtos-tips.1226738299.txt.gz · Last modified: 2013/09/19 16:43 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0