F
fabio
1.
#include <stdio.h>
2.
#include <stdlib.h>
3.
#include <string.h>
4.
#include <pthread.h>
5.
#include <sys/types.h>
6.
#include <sys/stat.h>
7.
#include <fcntl.h>
8.
#include <errno.h>
9.
10.
11.
pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
12.
13.
14.
15.
void th_funz(void* arg)
16.
{
17.
int rc;
18.
int fd;
19.
FILE* f;
20.
char* point;
21.
int nread = 1;
22.
23.
int *id = (int*) arg;
24.
25.
26.
rc = pthread_mutex_lock(&file_lock);
27.
if (rc) perror("pthread_mutex_lock");
28.
29.
printf("th %d comes..\n", *id);
30.
sleep(4);
31.
32.
rc = pthread_mutex_unlock(&file_lock);
33.
if (rc) perror("pthread_mutex_unlock");
34.
35.
}
36.
37.
38.
int main()
39.
{
40.
41.
pthread_t th1, th2, th3;
42.
int rc;
43.
int uno= 1, due = 2, tre= 3;
44.
45.
rc = pthread_create(&th1, NULL, th_funz, (void*) &uno);
46.
if (rc < 0) perror("pthread_create()");
47.
48.
rc = pthread_create(&th2, NULL, th_funz, (void*) &due);
49.
if (rc < 0) perror("pthread_create()");
50.
51.
rc = pthread_create(&th3, NULL, th_funz, (void*) &tre);
52.
if (rc < 0) perror("pthread_create()");
53.
54.
pthread_join(th1, NULL);
55.
pthread_join(th2, NULL);
56.
pthread_join(th3, NULL);
57.
58.
return 0;
59.
60.
}
61.
62.
when i debug this sample program with gdb and set a breakpoint into pthread_mutex_lock() line
63.
i got sigtrap
gdb) b prova.c:26
64.
Breakpoint 1 at 0x8048547: file prova.c, line 26.
65.
(gdb) r
66.
Starting program: /progetto/fd_threads/prova
67.
[Thread debugging using libthread_db enabled]
68.
[New Thread -1208653568 (LWP 20212)]
69.
[New Thread -1209922640 (LWP 20215)]
70.
[Switching to Thread -1209922640 (LWP 20215)]
71.
72.
Breakpoint 1, th_funz (arg=0xbfb86994) at prova.c:26
73.
26 rc = pthread_mutex_lock(&file_lock);
74.
(gdb) n
75.
[New Thread -1218311248 (LWP 20216)]
76.
77.
Program received signal SIGTRAP, Trace/breakpoint trap.
78.
[Switching to Thread -1208653568 (LWP 20212)]
my gdb version is 6.3-debian
thx everybody!
#include <stdio.h>
2.
#include <stdlib.h>
3.
#include <string.h>
4.
#include <pthread.h>
5.
#include <sys/types.h>
6.
#include <sys/stat.h>
7.
#include <fcntl.h>
8.
#include <errno.h>
9.
10.
11.
pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
12.
13.
14.
15.
void th_funz(void* arg)
16.
{
17.
int rc;
18.
int fd;
19.
FILE* f;
20.
char* point;
21.
int nread = 1;
22.
23.
int *id = (int*) arg;
24.
25.
26.
rc = pthread_mutex_lock(&file_lock);
27.
if (rc) perror("pthread_mutex_lock");
28.
29.
printf("th %d comes..\n", *id);
30.
sleep(4);
31.
32.
rc = pthread_mutex_unlock(&file_lock);
33.
if (rc) perror("pthread_mutex_unlock");
34.
35.
}
36.
37.
38.
int main()
39.
{
40.
41.
pthread_t th1, th2, th3;
42.
int rc;
43.
int uno= 1, due = 2, tre= 3;
44.
45.
rc = pthread_create(&th1, NULL, th_funz, (void*) &uno);
46.
if (rc < 0) perror("pthread_create()");
47.
48.
rc = pthread_create(&th2, NULL, th_funz, (void*) &due);
49.
if (rc < 0) perror("pthread_create()");
50.
51.
rc = pthread_create(&th3, NULL, th_funz, (void*) &tre);
52.
if (rc < 0) perror("pthread_create()");
53.
54.
pthread_join(th1, NULL);
55.
pthread_join(th2, NULL);
56.
pthread_join(th3, NULL);
57.
58.
return 0;
59.
60.
}
61.
62.
when i debug this sample program with gdb and set a breakpoint into pthread_mutex_lock() line
63.
i got sigtrap
64.
Breakpoint 1 at 0x8048547: file prova.c, line 26.
65.
(gdb) r
66.
Starting program: /progetto/fd_threads/prova
67.
[Thread debugging using libthread_db enabled]
68.
[New Thread -1208653568 (LWP 20212)]
69.
[New Thread -1209922640 (LWP 20215)]
70.
[Switching to Thread -1209922640 (LWP 20215)]
71.
72.
Breakpoint 1, th_funz (arg=0xbfb86994) at prova.c:26
73.
26 rc = pthread_mutex_lock(&file_lock);
74.
(gdb) n
75.
[New Thread -1218311248 (LWP 20216)]
76.
77.
Program received signal SIGTRAP, Trace/breakpoint trap.
78.
[Switching to Thread -1208653568 (LWP 20212)]
my gdb version is 6.3-debian
thx everybody!