Ik heb een C programma welke perfect werkt in allerlei Linux distributies, maar niet in FreeBSD.
Het probleem is dat de loops in de threads niet goed afsluiten. Daardoor lukt het niet om alle benodigde garbage collection functies te draaien en daarmee het programma goed af te sluiten. Iemand een idee wat ik fout doe?
Een voorbeeld code dat de situatie in mijn programma schets:
Uitvoer in linux:
Uitvoer van hetzelfde programma in FreeBSD:
Valgrind linux:
Valgrind FreeBSD:
gdb linux:
gdb FreeBSD:
Het probleem is dat de loops in de threads niet goed afsluiten. Daardoor lukt het niet om alle benodigde garbage collection functies te draaien en daarmee het programma goed af te sluiten. Iemand een idee wat ik fout doe?
Een voorbeeld code dat de situatie in mijn programma schets:
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
| #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include <pthread.h> #include <setjmp.h> #include <signal.h> pthread_t pth; static sigjmp_buf gc_cleanup; int main_loop = 1; int thread_loop = 1; int thread_running = 0; int gc_enable = 1; void *thread(void *param) { thread_running++; while(thread_loop) { sleep(1); printf("thread timeout\n"); } printf("thread stopped\n"); thread_running--; return NULL; } void gc_handler(int sig) { if(gc_enable) { gc_enable = 0; siglongjmp(gc_cleanup, sig); } } int main_gc() { thread_loop = 0; printf("gc\n"); while(thread_running > 0) { printf("waiting\n"); sleep(1); } main_loop = 0; return 0; } /* Initialize the catch all gc */ void gc_catch(void) { struct sigaction act, old; memset(&act, 0, sizeof(act)); act.sa_handler = gc_handler; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, &old); sigaction(SIGQUIT, &act, &old); sigaction(SIGTERM, &act, &old); if(sigsetjmp(gc_cleanup, 0) == 0) return; /* Call all GC functions */ main_gc(); } int main() { gc_catch(); pthread_create(&pth, NULL, &thread, (void *)NULL); while(main_loop) { sleep(1); } printf("program stopped\n"); return (EXIT_SUCCESS); } |
Uitvoer in linux:
code:
1
2
3
4
5
6
| ./test [CTRL-C] gc thread timeout thread stopped program stopped |
Uitvoer van hetzelfde programma in FreeBSD:
code:
1
2
3
4
5
| ./test [CTRL-C] gc waiting Segmentation fault |
Valgrind linux:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| ==13575== Memcheck, a memory error detector ==13575== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==13575== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==13575== Command: ./test ==13575== ^Cgc thread timeout thread stopped program stopped ==13575== ==13575== HEAP SUMMARY: ==13575== in use at exit: 0 bytes in 0 blocks ==13575== total heap usage: 1 allocs, 1 frees, 136 bytes allocated ==13575== ==13575== All heap blocks were freed -- no leaks are possible ==13575== ==13575== For counts of detected and suppressed errors, rerun with: -v ==13575== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 6) |
Valgrind FreeBSD:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| valgrind --dsymutil=yes --tool=memcheck --leak-check=full --track-origins=yes ./test ==923== Memcheck, a memory error detector ==923== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==923== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==923== Command: ./test ==923== ^C==923== ==923== Process terminating with default action of signal 11 (SIGSEGV): dumping core ==923== General Protection Fault ==923== at 0x1219F2A: ??? (in /lib/libthr.so.3) ==923== by 0x3804A267: ??? (in /usr/local/lib/valgrind/memcheck-amd64-freebsd) ==923== by 0x121ED5B: ??? (in /lib/libthr.so.3) ==923== by 0x400DC9: thread (in /data/Hobby/ICT/RPi/433.92/thread/test) ==923== by 0x12154A3: ??? (in /lib/libthr.so.3) ==923== ==923== HEAP SUMMARY: ==923== in use at exit: 6,444 bytes in 9 blocks ==923== total heap usage: 9 allocs, 0 frees, 6,444 bytes allocated ==923== ==923== LEAK SUMMARY: ==923== definitely lost: 0 bytes in 0 blocks ==923== indirectly lost: 0 bytes in 0 blocks ==923== possibly lost: 0 bytes in 0 blocks ==923== still reachable: 6,444 bytes in 9 blocks ==923== suppressed: 0 bytes in 0 blocks ==923== Reachable blocks (those to which a pointer was found) are not shown. ==923== To see them, rerun with: --leak-check=full --show-reachable=yes ==923== ==923== For counts of detected and suppressed errors, rerun with: -v ==923== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) Killed |
gdb linux:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1". [New Thread 0xb6e81470 (LWP 13594)] ^C Program received signal SIGINT, Interrupt. 0xb6f1ca20 in nanosleep () at ../sysdeps/unix/syscall-template.S:82 82 ../sysdeps/unix/syscall-template.S: No such file or directory. (gdb) signal 2 Continuing with signal SIGINT. gc thread timeout thread stopped program stopped [Thread 0xb6e81470 (LWP 13594) exited] [Inferior 1 (process 13593) exited normally] (gdb) |
gdb FreeBSD:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| GNU gdb 6.1.1 [FreeBSD] Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "amd64-marcel-freebsd"... (gdb) run Starting program: /data/Hobby/ICT/RPi/433.92/thread/test [New LWP 101232] ^C[New Thread 801406800 (LWP 101389/test)] Program received signal SIGINT, Interrupt. [Switching to Thread 801406800 (LWP 101389/test)] 0x000000080083089c in __error () from /lib/libthr.so.3 (gdb) signal 2 Continuing with signal SIGINT. gc Program received signal SIGSEGV, Segmentation fault. 0x0000000000000001 in ?? () (gdb) backtrace #0 0x0000000000000001 in ?? () #1 0x0000000000000000 in ?? () (gdb) |
[ Voor 6% gewijzigd door CurlyMo op 20-04-2014 23:53 . Reden: Stukje ingekort omdat het zelfs met een sleep al niet werkt. ]
Sinds de 2 dagen regel reageer ik hier niet meer