1,死锁问题
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;void* thread_func1(void* arg) {pthread_mutex_lock(&lock1);printf("Thread pid[%lx]: Locked lock1\n",pthread_self());sleep(1); // 模拟一些工作pthread_mutex_lock(&lock2);printf("Thread 1 pid[%lx]: Locked lock2\n",pthread_self());pthread_mutex_unlock(&lock2);pthread_mutex_unlock(&lock1);return NULL;
}void* thread_func2(void* arg) {pthread_mutex_lock(&lock2);printf("Thread pid[%lx]: Locked lock2\n",pthread_self());sleep(1); // 模拟一些工作pthread_mutex_lock(&lock1);printf("Thread pid[%lx]: Locked lock1\n",pthread_self());pthread_mutex_unlock(&lock1);pthread_mutex_unlock(&lock2);return NULL;
}int main() {pthread_t t1, t2;pthread_create(&t1, NULL, thread_func1, NULL);pthread_create(&t2, NULL, thread_func2, NULL);pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_mutex_destroy(&lock1);pthread_mutex_destroy(&lock2);return 0;
}
运行
lark@ubuntu:~/test$ ./pthread1
Thread pid[7fc61fba2700]: Locked lock2
Thread pid[7fc6203a3700]: Locked lock1
lark@ubuntu:~$ ps -axf | grep pthread1
2769 pts/0 Sl+ 0:00 | \_ ./pthread1
2835 pts/2 S+ 0:00 \_ grep --color=auto pthread1
lark@ubuntu:~$ sudo strace -p 2769
[sudo] password for lark:
strace: Process 2769 attached
futex(0x7fc6203a39d0, FUTEX_WAIT, 2770, NULL
可以看到是锁在等待。
2,文件操作失败:
#include<stdio.h>
#include<unistd.h>
#include <string.h>
#include<errno.h>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{#if 0int fd = open("/etc/shadow",O_RDONLY);if(fd < 0){printf("fd[%d], errno[%d]\r\n", fd,errno);return -1;}#endifFILE* fp = fopen("/etc/shadow","r");if(NULL == fp){printf("errno[%s]\r\n",strerror(errno));return -1;} return 0;
}
lark@ubuntu:~/test$ gcc strace.c
lark@ubuntu:~/test$ strace ./a.out
execve("./a.out", ["./a.out"], 0x7ffdc7708a50 /* 25 vars */) = 0
brk(NULL) = 0x559038b11000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffe2657c8f0) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=86377, ...}) = 0
mmap(NULL, 86377, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6ac1965000
close(3) = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/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\300A\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\7\2C\n\357_\243\335\2449\206V>\237\374\304"..., 68, 880) = 68
fstat(3, {st_mode=S_IFREG|0755, st_size=2029592, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6ac1963000
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\7\2C\n\357_\243\335\2449\206V>\237\374\304"..., 68, 880) = 68
mmap(NULL, 2037344, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f6ac1771000
mmap(0x7f6ac1793000, 1540096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x22000) = 0x7f6ac1793000
mmap(0x7f6ac190b000, 319488, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19a000) = 0x7f6ac190b000
mmap(0x7f6ac1959000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f6ac1959000
mmap(0x7f6ac195f000, 13920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f6ac195f000
close(3) = 0
arch_prctl(ARCH_SET_FS, 0x7f6ac1964540) = 0
mprotect(0x7f6ac1959000, 16384, PROT_READ) = 0
mprotect(0x55903745a000, 4096, PROT_READ) = 0
mprotect(0x7f6ac19a8000, 4096, PROT_READ) = 0
munmap(0x7f6ac1965000, 86377) = 0
brk(NULL) = 0x559038b11000
brk(0x559038b32000) = 0x559038b32000
openat(AT_FDCWD, "/etc/shadow", O_RDONLY) = -1 EACCES (Permission denied)
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x1), ...}) = 0
write(1, "errno[Permission denied]\r\n", 26errno[Permission denied]
) = 26
exit_group(-1) = ?
+++ exited with 255 +++
可以看到openat函数异常退出-1,原因为Permission denied。