1,验证代码转载
#include <semaphore.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
sem_t sem_a; void *task1();
int main(void){ int ret=0; pthread_t thrd1; sem_init(&sem_a,0,1); //create children process ret=pthread_create(&thrd1,NULL,task1,NULL); //waiting for children process end pthread_join(thrd1,NULL); }
void *task1() { int sval = 0;
//holding the semaphore sem_wait(&sem_a); sleep(5); //do_nothing sem_getvalue(&sem_a,&sval); printf(“sem value = %d\n”,sval); //release the semaphore sem_post(&sem_a);
}
执行反馈结果:
sem value = 0
2,gcc -o t_mutex t_mutex -lpthread
备注:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数
3,跟踪执行过程
strace -o t1_mutex.str ./t1_mutex
sem value = 0 [root@SZB-L0004396 appcode]# less t1_mutex.str execve(“./t1_mutex”, [“./t1_mutex”], [/* 19 vars */]) = 0 brk(0) = 0x237e000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fac07ffe000 access(“/etc/ld.so.preload”, R_OK) = -1 ENOENT (No such file or directory) open(“/etc/ld.so.cache”, O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=72041, …}) = 0 mmap(NULL, 72041, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fac07fec000 close(3) = 0 open(“/lib64/libpthread.so.0”, O_RDONLY|O_CLOEXEC) = 3 read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240l\0\0\0\0\0\0″…, 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=141616, …}) = 0 mmap(NULL, 2208864, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fac07bc3000 mprotect(0x7fac07bd9000, 2097152, PROT_NONE) = 0 mmap(0x7fac07dd9000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7fac07dd9000 mmap(0x7fac07ddb000, 13408, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fac07ddb000 close(3) = 0 open(“/lib64/libc.so.6”, O_RDONLY|O_CLOEXEC) = 3 read(3, “\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\34\2\0\0\0\0\0″…, 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=2107768, …}) = 0 mmap(NULL, 3932736, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fac07802000 mprotect(0x7fac079b8000, 2097152, PROT_NONE) = 0 mmap(0x7fac07bb8000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b6000) = 0x7fac07bb8000 mmap(0x7fac07bbe000, 16960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fac07bbe000 close(3) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fac07feb000 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fac07fe9000 arch_prctl(ARCH_SET_FS, 0x7fac07fe9740) = 0 mprotect(0x7fac07bb8000, 16384, PROT_READ) = 0 mprotect(0x7fac07dd9000, 4096, PROT_READ) = 0 mprotect(0x600000, 4096, PROT_READ) = 0 mprotect(0x7fac07fff000, 4096, PROT_READ) = 0 munmap(0x7fac07fec000, 72041) = 0 set_tid_address(0x7fac07fe9a10) = 9159 set_robust_list(0x7fac07fe9a20, 24) = 0 rt_sigaction(SIGRTMIN, {0x7fac07bc9780, [], SA_RESTORER|SA_SIGINFO, 0x7fac07bd2130}, NULL, 8) = 0 rt_sigaction(SIGRT_1, {0x7fac07bc9810, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x7fac07bd2130}, NULL, 8) = 0 rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0 getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7fac07001000 brk(0) = 0x237e000 brk(0x239f000) = 0x239f000 brk(0) = 0x239f000 mprotect(0x7fac07001000, 4096, PROT_NONE) = 0 clone(child_stack=0x7fac07800fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fac078019d0, tls=0x7fac07801700, child_tidptr=0x7fac078019d0) = 9160 futex(0x7fac078019d0, FUTEX_WAIT, 9160, NULL) = 0 exit_group(0) = ? +++ exited with 0 +++
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/190926.html原文链接:https://javaforall.cn