Project

General

Profile

Bug #6554 » deadlock.c

Marcel Telka, 2016-01-14 03:26 PM

 
1
#include <sys/types.h>
2
#include <unistd.h>
3
#include <fcntl.h>
4
#include <stdio.h>
5
#include <errno.h>
6
#include <string.h>
7
#include <thread.h>
8
#include <unistd.h>
9

    
10
int f;
11

    
12
void *thread(void *arg)
13
{
14
	char buf[1];
15

    
16
	sleep(2);
17

    
18
	read(f, buf, 1);
19

    
20
	return NULL;
21
}
22

    
23
int
24
main(int argc, char *argv[])
25
{
26
	struct flock fl;
27

    
28
	if (argc != 2) {
29
		printf("Filename missing.\n");
30
		return -1;
31
	}
32

    
33
	f = open(argv[1], O_RDWR | O_CREAT, 0777);
34
	if (f == -1) {
35
		printf("open failed\n");
36
		return -1;
37
	}
38

    
39
	if (thr_create(NULL, 0, thread, NULL, THR_DETACHED, NULL) != 0) {
40
		printf("thr_create failed\n");
41
		return -1;
42
	}
43

    
44
	fl.l_type = F_RDLCK;
45
	fl.l_whence = SEEK_SET;
46
	fl.l_start = 0;
47
	fl.l_len = 1;
48

    
49
	if (fcntl(f, F_SETLK, &fl) == -1) {
50
		printf("fcntl failed: (%d) %s\n",
51
		    errno, strerror(errno));
52
		return 0;
53
	}
54

    
55
	return 0;
56
}
(2-2/2)