Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
programming:howtos-tips [2008/11/15 08:35] 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 ('' |
+ | |||
+ | ==== Catching signals ==== | ||
=> C and C++. | => C and C++. | ||
Line 33: | Line 36: | ||
You can also change the signals actions with the [[http:// | You can also change the signals actions with the [[http:// | ||
+ | ==== Timer ==== | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | unsigned int alarm(unsigned int seconds); | ||
+ | </ | ||
+ | will trigger signal SIGALRM x seconds later that you can catch (see [[# | ||
+ | |||
+ | If you need automatic repetition and/or sub-second precision, you can use: | ||
+ | <code c> | ||
+ | #include < | ||
+ | int setitimer(int whichtimer, const struct itimerval *value, struct itimerval *ovalue); | ||
+ | </ | ||
+ | |||
+ | ==== Waiting event ==== | ||
+ | |||
+ | Monitor multiple file descriptors. | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | int select(int nfds, fd_set *readfds, fd_set *writefds, | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ==== Posix Threads ==== | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | |||
+ | void *threadFunction(void *threadid) | ||
+ | { | ||
+ | printf(" | ||
+ | pthread_exit(NULL); | ||
+ | } | ||
+ | |||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | pthread_t threadId; | ||
+ | int threadData = 42; | ||
+ | if (pthread_create(& | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | FIXME join sem etc | ||
+ | |||
+ | ==== Change program owner ==== | ||
+ | |||
+ | <code c> | ||
+ | pwd = getpwnam(" | ||
+ | if (pwd) | ||
+ | { | ||
+ | setgid(pwd-> | ||
+ | setuid(pwd-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | But it doesn' | ||
+ | <code c> | ||
+ | system(" | ||
+ | system(" | ||
+ | </ | ||
===== Formatting IOs (iomanip) ===== | ===== Formatting IOs (iomanip) ===== | ||
Line 48: | Line 117: | ||
<code cpp> | <code cpp> | ||
- | std::cout << " | + | std::cout << " |
- | << " | + | << " |
<< " | << " | ||
</ | </ | ||
Line 113: | Line 182: | ||
<code c> | <code c> | ||
#define ERROR(msg, ...) fprintf(" | #define ERROR(msg, ...) fprintf(" | ||
+ | </ | ||
+ | You can also use the '' | ||
+ | < | ||
+ | #define ERROR(msg, ...) fprintf(" | ||
</ | </ | ||
Line 137: | Line 210: | ||
#include < | #include < | ||
char s[128]; | char s[128]; | ||
- | sprintf(" | + | sprintf(s, " |
</ | </ | ||
Line 146: | Line 219: | ||
#include < | #include < | ||
int i = atoi(" | int i = atoi(" | ||
- | float f = atof("4.5"); | + | long l = atol("4500294"); |
- | double | + | double |
</ | </ | ||
- | ===== Define | + | ===== # |
You can define names for the preprocessor from the command line (or the makefile) instead of the source file (''# | You can define names for the preprocessor from the command line (or the makefile) instead of the source file (''# | ||
Line 165: | Line 238: | ||
===== Function pointers ===== | ===== Function pointers ===== | ||
+ | |||
The full test program is available [[http:// | The full test program is available [[http:// | ||
Line 251: | Line 325: | ||
| | ||
... | ... | ||
- | double() { return data / (double)(1<< | + | |
}; | }; | ||