Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 C


  Exit main thread and keep other threads running in C

In C programming, if using return in main function, the whole process will terminate. To only let main thread gone, and keep other threads live, you can use thrd_exit in main function. Check following code:#include #include #include intprint_thread(void *s){ thrd_detach(thrd_current()); for (size_t i = 0; i < 5; i++) { sleep(1); printf("i=%zu\n", i); } thrd_exit(0);}intmain(void){ thrd_t tid; if (thrd_success != thrd_create(&tid, print_thread, NULL)) { fprintf(stderr, "Create thread err...

1,334 0       MAIN THREAD MULITHREAD C LANGUAGE


  A trick of building multithreaded application on Solaris

Firstly, Let’s see a simple multithreaded application:#include <stdio.h>#include <pthread.h>#include <errno.h>void *thread1_func(void *p_arg){ errno = 0; sleep(3); errno = 1; printf("%s exit, errno is %d\n", (char*)p_arg, errno);}void *thread2_func(void *p_arg){ errno = 0; sleep(5); printf("%s exit, errno is %d\n", (char*)p_arg, errno);}int main(void){ pthread_t t1, t2; pthread_create(&t1, NULL, thread1_func, "Thread 1"); pthread_create(&t2, NULL, thread2_func, "Thread 2")...

5,260 0       C SOLARIS


  C programming tips in SPARC architecture

If you are a newbie of C programmers in SPARC architecture (For example, working on Solaris), you should pay attention to the following tips:(1) By default, SPARC is big-endian (For Endianness, you can refer http://en.wikipedia.org/wiki/Endianness). It means for an integer (short, int, long, etc), the MSB will be stored in the lower address, while the LSB will be stored in the higher address. (2) SPARC requires byte-alignment. It means for a short (2 bytes long) variable, the start address of the variable must be the multiples of 2, while a int (4 bytes long) variable, the start...

6,552 0       C


  Socket programming tips in Solaris

I sponsored a topic in stackoverflow.com, and hoped the programmers can share the socket programming tips in different UNIX flavors. But unfortunately, the responders are few. So I can only share my socket programming tips in Solaris at here (the Chinese version can be found there):1. Use the following link options: "-lresolv -lnsl -lsocket";2. Solaris doesn't provide socket options: SO_SNDTIMEO and SO_RCVTIMEO(Why does Solaris OS define SO_SNDTIMEO and SO_RCVTIMEO socket options in header file which actually not support by kernel?);3. In SCTP programming, must call bind() befor...

11,588 0       C SOCKET SOLARIS


  An experience of fixing a memory-corruption bug

During the last 4 months, I was disturbed by a memory-corruption bug, and this bug will cause program crash. Until last Monday, I found the root cause and fixed it. This debug process is a difficult but memorable experience, so I will share it in this article. My program works as a SMS Hub. When it receives a SMS, it will allocate a structure in heap memory like this:typedef struct{ ...... int *a[8]; ......} info;After processing the SMS, the program will free the memory, and send the SMS to the next Hub or Operator.  Since last November, the program will crash som...

8,791 0       C UNIX DEBUG SOLARIS MULTI-THREAD


  HeartBleed: Should C be blamed for the HeartBleed bug?

There is a discussion about the security of applications written in C on Hacker News recently after the report of HeartBleed bug in OpenSSL. In this discussion, some people are saying that the applications written in C are unsafe. It seems all or most of the faults should be laid on C. I think this is biased. The language itself should not be blamed.Safety is a relative term for programming languages. No language is absolutely safe. We claim some languages like Java and C# are safer than C/C++ because they have memory protection mechanism built in, we cannot access arbitrary memory locations i...

14,040 5       C ANALYSIS CODE REVIEW HEARTBLEED


  do {...} while (0) in macros

If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease your work if used correctly. However, if you don't define macros carefully, they may bite you and drive you crazy. In many C programs, you may see a special macro definition which may seem not so straightforward. Here is one example:#define __set_task_state(tsk, state_value) \ do { (tsk)->state = (state_value); } while (0)There are many this kind of macros which uses do{...}while(0) in Linux kernels and other popular C libraries. What's the use of this macro? Robert Love from Google(...

139,166 14       C C++ MACRO


  How big is sizeof structure?

First let's see the codes of a structure:struct node{ int a; int b;};Question : What's sizeof(node)? The answer is very simple, on a 32 bit machine, an int will take 4 bytes and two ints will take 8 bytes. So sizeof(node) is 8.The answer for the above codes is 8, then how about the following structure:struct node{ char a; int b;};Question : Then what's sizeof(node) now? int takes 4 bytes, char takes 1 bytes, is the answer 5?Now the answer may not be 5, on some machines, the answer is 8. Why?In fact, this is not the problem of the language. You will not find why in ANSI C. Even you ...

3,712 0       DATA STRUCTURE ALIGNMENT PACK