Start with a simple program:
#include <stdio.h>#include <fcntl.h>int main() { FILE* fp = open("./1", O_WRONLY); char a[100]; while (1){ write(fp,"aaaa",4); read(fp, a, 100); } close(fp);}
Other sys calls are not read, mainly write and read. in Linux, the two are from the glibc library. Compile this program to check the function call status.
Pai_^ [/home/crazybaby/] # GCC main. c ^_^ [/home/crazybaby/] # valgrind -- tool = callgrind. out-> C-c ^_^ [/home/crazybaby/] # callgrind_annotate callgrind. out.9150... omitted... IR file: function ------------------------------------------------------------------------------------ 4,534,175 ??? : Main [/home/crazybaby/welcomec/a. Out] 4,534,167 ??? :__ Write_nocancel [/lib64/libc-2.5.so] 4,534,160 ??? :__ Read_nocancel [/lib64/libc-2.5.so] 824,404 ??? : Read [/lib64/libc-2.5.so] 824,394 ??? : Write [/lib64/libc-2.5.so]
It can be seen that the read and write operations are from libc during normal compilation (without the thread library pthread), and they are normal.
Let's see how to add-Pthread.
Pai_^ [/home/crazybaby/] # GCC main. c-lpthread pai_^ [/home/crazybaby/] # valgrind -- tool = callgrind. out-> C-c ^_^ [/home/crazybaby/] # callgrind_annotate callgrind. out.9150... omitted... IR file: function ------------------------------------------------------------------------------------ 2,769,176 ??? : Main [/home/crazybaby/welcomec/a. Out] 2,769,162 ??? :__ Read_nocancel [/lib64/libpthread-2.5.so] 2,769,162 ??? :__ Write_nocancel [/lib64/libpthread-2.5.so] 503,499 ??? : Read [/lib64/ld-2.5.so] 503,484 ??? : Write [/lib64/libpthread-2.5.so] 33,527 ??? : Do_lookup_x [/lib64/ld-2.5.so]
We can see that both write and read come from the libpthread library.
Solution:
Added the glibc dynamic library-LC to the display during compilation.
Pai_^ [/home/crazybaby/] # GCC main. c-LC-lpthread ^_^ [/home/crazybaby/] # valgrind -- tool = callgrind. out-> C-c ^_^ [/home/crazybaby/] # callgrind_annotate callgrind. out.9276... omitted... sort IR file: function ------------------------------------------------------------------------------------ 2,183,618 ??? : Main [/home/crazybaby/welcomec/a. Out] 2,183,610 ??? :__ Write_nocancel [/lib64/libc-2.5.so] 2,183,599 ??? :__ Read_nocancel [/lib64/libc-2.5.so] 397,033 ??? : Read [/lib64/libc-2.5.so] 397,020 ??? : Write [/lib64/libc-2.5.so] 28,297 ??? : Do_lookup_x [/lib64/ld-2.5.so] 22,899 ??? : _ Dl_lookup_symbol_x [/lib64/ld-2.5.so]
This morning I analyzed and found that pthread is part of glibc, so only the glibc library is required.