这只是更大模拟的开始。这个想法是拥有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 00:00 0 [堆] 7fa2d4000000-7fa2d4021000 rw-p 00000000 00:00 0 7fa2d4021000-7fa2d8000000 --- p 00000000 00:00 0 7fa2dc000000-7fa2dc021000 rw-p 00000000 00:00 0 7fa2dc021000-7fa2e0000000 --- p 00000000 00:00 0 7fa2e2287000-7fa2e229c000 r-xp 00000000 07:00 4805 / lib / x86_64- linux-gnu / 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 4805 / lib / x86_64- linux-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.15.so 7fa2e3851000-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_64-linux-gnu/libc-2.15.so 7fa2e3857000-7fa2e385c000 rw-p 00000000 00:00 0 7fa2e385c000-7fa2e3874000 r-xp 00000000 07:00 4864 /lib/x86_64-linux-gnu/libpthread-2.15.so 7fa2e3874000-7fa2e3a73000 --- p 00018000 07:00 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 7fa2e3a74000-7fa2e3a75000 rw-p 00018000 07: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:00 4764 /lib/x86_64-linux-gnu/ld-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.so 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 [stack] 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");
}