这只是更大模拟的开始。我们的想法是拥有 3 个线程和 2 个文件,并且在通常情况下会出现死锁,我专门对此进行了硬编码。
碰巧线程运行,然后进程在控制台上用 Abort 中止(核心转储)。我有下面的输出......这是死锁吗?我很抱歉没有以首选格式获得输出,但内存的回溯并不容易格式化。
Time Interval 4000250
*** glibc detected *** ./a.out: double free or corruption (out): 0x00007fa2dc000b00 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7fa2e351d626]
/lib/x86_64-linux-gnu/libc.so.6(fclose+0x155)[0x7fa2e350d2a5]
./a.out[0x400a05]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x7e9a)[0x7fa2e3863e9a]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fa2e35914bd]
======= Memory map: ========
00400000-00401000 r-xp 00000000 07:00 163351 /home/kartheek /Desktop/a.out
00601000-00602000 r--p 00001000 07:00 163351 /home/kartheek /Desktop/a.out
00602000-00603000 RW-P 00002000 07:00 163351/home/kartheek/desktop/a.out 00923000-00944000 RW-P 00000000 000000 00 000000 000 000000 0 [HEAP] 7FA2D4000000-7FA2D40000000000000000000000000000000000FA2DAD太平洋-p 00000000 00:00 0 7FA2DC0000000000000000000000000000000000000000..000000000000000000000000来/libgcc_s.so.1 7fa2e229c000-7fa2e249b000 ---p 00015000 07:00 4805 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fa2e249b000-7fa2e249c000 r--p 00014000 07:00 07:00-linux lib/x86_6- -gnu/libgcc_s.so.1 7fa2e249c000-7fa2e249d000 rw-p 00015000 07:00 4805 /lib/x86_64-linux-gnu/libgcc_s.so。1 7fa2e249d000-7fa2e249e000 ---p 00000000 00:00 0 7fa2e249e000-7fa2e2c9e000 rw-p 00000000 00:00 0 7fa2e2c9e000-7fa2e2c9f000 ---p 00000000 00:00 0 7fa2e2c9f000-7fa2e349f000 rw-p 00000000 00:00 0 7fa2e349f000-7fa2e3652000 r-xp 00000000 07:00 4784 /lib/x86_64-linux-gnu/libc-2.15.so 7fa2e3652000-7fa2e3851000 ---p 001b3000 07:00 4784 /lib/x86_64-linux-gnu/libc-2.1500-7fa2e38 7fa2e3855000 r--p 001b2000 07:00 4784 /lib/x86_64-linux-gnu/libc-2.15.so 7fa2e3855000-7fa2e3857000 rw-p 001b6000 07:00 4784 /lib/x86_604-linux-gnu/c86_604-linux-gnu/fae/lib. -7fa2e385c000 rw-p 00000000 00:00 0 7fa2e385c000-7fa2e3874000 r-xp 00000000 07:00 4864 /lib/x86_64-linux-gnu/libpthread-2.15.so --- 7fa2e38730000-7fa2e3840000-7fa2e3a00 4864 /lib/x86_64-linux-gnu/libpthread-2.15.so 7fa2e3a73000-7fa2e3a74000 r--p 00017000 07:00 4864 /lib/x86_64-linux-gnu/libpthread-2.15.so 7fa2e3a740000-7fa2e0r307a :00 4864 /lib/x86_64-linux-gnu/libpthread-2.15.so 7fa2e3a75000-7fa2e3a79000 rw-p 00000000 00:00 0 7fa2e3a79000-7fa2e3a9b000 r-xp 00000000/07:064-linux/ld-lib/4-linux 2.15.so 7fa2e3c80000-7fa2e3c83000 rw-p 00000000 00:00 0 7fa2e3c97000-7fa2e3c9b000 rw-p 00000000 00:00 0 7fa2e3c9b000-7fa2e3c9c000 r--p 00022000 07:00 4764 /lib/x86_64-linux-gnu/ld-2.15.所以 7fa2e3c9c000-7fa2e3c9e000 rw-p 00023000 07:00 4764 /lib/x86_64-linux-gnu/ld-2.15.so 7fffa77ef000-7fffa7810000 rw-p 00000000 00:00 0 [堆栈] 7fffa784f000-7fffa7850000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] 中止(核心转储)
我不知道这在输出中意味着什么,但它说 Aborted 的事实让我认为我可能很接近。
该代码的工作方式如下:
创建线程 Thread1 访问 file1 并以写入模式等待 Thread2 也访问 file1 并尝试对其进行更改,然后这就是发生的事情
现在,下一步是创建一个以更高优先级运行的监视器线程。无论如何允许死锁发生然后释放它?我的意思是,我想到了一种解决方案,在互斥条件的帮助下,我们可以检查冲突情况,但这将避免死锁,这是我不想要的。有没有办法让死锁发生然后阻止程序中止,以便监控线程可以相应地释放资源???
这是代码。再次为格式化道歉。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<sched.h>
FILE *fp1;
FILE *fp2;
struct sched_param schedparam1,schedparam2,schedparam3;
int policy=SCHED_RR;
int mutex1[3]= {0,0,0},mutex2[3]= {0,0,0};
void *routine1(void *arg)
{
int a;
printf("before");
fp1= fopen("file1.txt", "w");
usleep(250000);
printf("woke up");
fp2= fopen("file2.txt", "w");
a=fclose(fp1);
printf("%d\n",a);
fclose(fp2);
}
void *routine2(void *arg)
{
printf("asdasd");
fp2=fopen("file2.txt", "w");
fp1=fopen("file1.txt", "w");
fclose(fp1);
fclose(fp2);
}
//void *monitor_function(void *arg) //{
void main()
{
pthread_t thread1, thread2, thread3,thread4;
pthread_attr_t at1,at2,at3;
int iret1, iret2,iret3,iret4;
struct timespec tp;
schedparam1.sched_priority = 10;
sched_setscheduler(getpid(),policy,&schedparam1);
sched_rr_get_interval(getpid(),&tp);
printf("\nTime Interval %ld\n",tp.tv_nsec);
\
printf("test");
schedparam1.sched_priority = 10; //Set Nice value of Thread
schedparam2.sched_priority = 10;
schedparam3.sched_priority = 10;
/* Set attributes*/
pthread_attr_init(&at1);
pthread_attr_setinheritsched(&at1, PTHREAD_INHERIT_SCHED); // To inherit parent thread properties use PTHREAD_INHERIT_SCHED
pthread_attr_setschedpolicy(&at1, policy);
pthread_attr_setschedparam(&at1, &schedparam1);
pthread_attr_init(&at2);
pthread_attr_setinheritsched(&at2, PTHREAD_INHERIT_SCHED);
pthread_attr_setschedpolicy(&at2,policy);
pthread_attr_setschedparam(&at2, &schedparam2);
pthread_attr_init(&at3);
pthread_attr_setinheritsched(&at3, PTHREAD_INHERIT_SCHED);
pthread_attr_setschedpolicy(&at3, policy);
pthread_attr_setschedparam(&at3, &schedparam3);
//iret1 = pthread_create( &thread1, &at1, monitor_function,NULL);
iret2 = pthread_create( &thread2,&at1, routine1,NULL);
iret3 = pthread_create( &thread3,&at2, routine2,NULL);
//iret4 = pthread_create( &thread4,&at3, NULL,NULL);
pthread_join( thread2, NULL);
pthread_join( thread3, NULL);
//pthread_join( thread1, NULL);
printf("\nThread create 1 returns: %d\n",iret1);
printf("Thread create 2 returns: %d\n",iret2);
printf("Thread create 3 returns: %d\n",iret3);
exit(0);
//fp1= fopen("file1.txt", "w");
//fp2= fopen("file2.txt", "w");
}