|
#include <unistd.h>
#include <pthread.h>
#include <string.h>
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;
static int sequence1 = 0;
static int sequence2 = 0;
int func1()
{
pthread_mutex_lock(&mutex1);
++sequence1;
sleep(1);
pthread_mutex_lock(&mutex2);
++sequence2;
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex1);
return sequence1;
}
int func2()
{
pthread_mutex_lock(&mutex2);
++sequence2;
sleep(1);
pthread_mutex_lock(&mutex1);
++sequence1;
pthread_mutex_unlock(&mutex1);
pthread_mutex_unlock(&mutex2);
return sequence2;
}
void* thread1(void* arg)
{
while (1)
{
int iRetValue = func1();
if (iRetValue == 100000)
{
pthread_exit(NULL);
}
}
}
void* thread2(void* arg)
{
while (1)
{
int iRetValue = func2();
if (iRetValue == 100000)
{
pthread_exit(NULL);
}
}
}
void* thread3(void* arg)
{
while (1)
{
sleep(1);
char szBuf[128];
memset(szBuf, 0, sizeof(szBuf));
strcpy(szBuf, "thread3");
}
}
void* thread4(void* arg)
{
while (1)
{
sleep(1);
char szBuf[128];
memset(szBuf, 0, sizeof(szBuf));
strcpy(szBuf, "thread3");
}
}
int main()
{
pthread_t tid[4];
if (pthread_create(&tid[0], NULL, &thread1, NULL) != 0)
{
_exit(1);
}
if (pthread_create(&tid[1], NULL, &thread2, NULL) != 0)
{
_exit(1);
}
if (pthread_create(&tid[2], NULL, &thread3, NULL) != 0)
{
_exit(1);
}
if (pthread_create(&tid[3], NULL, &thread4, NULL) != 0)
{
_exit(1);
}
sleep(5);
//pthread_cancel(tid[0]);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_join(tid[2], NULL);
pthread_join(tid[3], NULL);
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_mutex_destroy(&mutex3);
pthread_mutex_destroy(&mutex4);
return 0;
}
/*
http://www.ibm.com/developerworks/cn/linux/l-cn-deadlock/
产生死锁的四个必要条件
(1) 互斥条件:一个资源每次只能被一个进程(线程)使用。
(2) 请求与保持条件:一个进程(线程)因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件 : 此进程(线程)已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件 : 多个进程(线程)之间形成一种头尾相接的循环等待资源关系。
[sts@localhost 20160729]$ gcc -g 0.c -o lock -lpthread
[sts@localhost 20160729]$ ps -ef|grep lock
root 16 2 0 Jul29 ? 00:00:08 [kblockd/0]
root 11954 1 0 Jul29 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
sts 12388 1 0 Jul29 ? 00:01:57 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3
sts 12457 1 0 Jul29 ? 00:00:07 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=38
sts 14732 14204 0 01:36 pts/2 00:00:00 grep lock
[sts@localhost 20160729]$ ./lock
^C
[sts@localhost 20160729]$ ps -ef|grep lock
root 16 2 0 Jul29 ? 00:00:08 [kblockd/0]
root 11954 1 0 Jul29 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
sts 12388 1 0 Jul29 ? 00:01:57 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3
sts 12457 1 0 Jul29 ? 00:00:07 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=38
sts 14743 14204 0 01:37 pts/2 00:00:00 grep lock
[sts@localhost 20160729]$ pstack 14743
Process 14743 not found.
[sts@localhost 20160729]$ ps -ef|grep lock
root 16 2 0 Jul29 ? 00:00:08 [kblockd/0]
root 11954 1 0 Jul29 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
sts 12388 1 0 Jul29 ? 00:01:57 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3
sts 12457 1 0 Jul29 ? 00:00:07 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=38
sts 14751 14204 0 01:38 pts/2 00:00:00 grep lock
[sts@localhost 20160729]$ pstack 14751
Process 14751 not found.
[sts@localhost 20160729]$ ps -ef|grep lock
root 16 2 0 Jul29 ? 00:00:08 [kblockd/0]
root 11954 1 0 Jul29 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
sts 12388 1 0 Jul29 ? 00:01:57 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3
sts 12457 1 0 Jul29 ? 00:00:07 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=38
sts 14755 14204 0 01:38 pts/2 00:00:00 grep lock
[sts@localhost 20160729]$ pstack 14773
Process 14773 not found.
[sts@localhost 20160729]$ ps -ef|grep lock
root 16 2 0 Jul29 ? 00:00:08 [kblockd/0]
root 11954 1 0 Jul29 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
sts 12388 1 0 Jul29 ? 00:01:57 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3
sts 12457 1 0 Jul29 ? 00:00:07 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=38
sts 14779 14758 0 01:39 pts/0 00:00:00 ./lock
sts 14785 14204 0 01:39 pts/2 00:00:00 grep lock
[sts@localhost 20160729]$ pstack 14785
Process 14785 not found.
[sts@localhost 20160729]$ pstack 14785
Process 14785 not found.
[sts@localhost 20160729]$ gdb lock
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/sts/Desktop/20160729/lock...done.
(gdb) l
81 char szBuf[128];
82 memset(szBuf, 0, sizeof(szBuf));
83 strcpy(szBuf, "thread3");
84 }
85 }
86
87 int main()
88 {
89 pthread_t tid[4];
90 if (pthread_create(&tid[0], NULL, &thread1, NULL) != 0)
(gdb) thread 5
Thread ID 5 not known.
(gdb) thread 1
Thread ID 1 not known.
(gdb) f 5
No registers.
(gdb)
*/ |
|
|