[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
fopen/malloc MT bug
From: |
Wojciech Piechowski |
Subject: |
fopen/malloc MT bug |
Date: |
Wed, 18 Apr 2001 00:42:01 +0200 (CEST) |
hello!
I have not found this one in archives.
In a multithreaded program (Linux 2.2.15, glibc 2.2-5, gcc 2.96) I noticed
the following problem: memory gets corrupted when one thread calls
malloc() and another calls fopen() at the same time. Everything goes fine
when I replace fopen() with open().
Please let me know if you know some other workarounds, a real solution or
anything about this bug (or is it my mistake?) because replacing
fopen() with open() is not what I really need.
This test program drops core:
/* compile with:
gcc -o test test.cc -g -lpthread -lmcheck -lrt
Some includes (and possibly linking with librt) are redundant and come
from tests with open() and attempts with async I/O but they shouldn't
bother.
*/
#define _REENTRANT
#define _THREAD_SAFE
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <malloc.h>
#include <mcheck.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void *allocator (void *arg) {
while (true) {
new char [(int)arg];
if ((int)arg==10) {
FILE *in = fopen ("/etc/passwd","r");
if (in)
fclose (in);
}
}
return 0;
}
main () {
pthread_t t;
mcheck_pedantic (0);
pthread_create (&t, 0, allocator, (void*)10);
pthread_create (&t, 0, allocator, (void*)100);
pthread_create (&t, 0, allocator, (void*)30);
pthread_create (&t, 0, allocator, (void*)80);
pthread_create (&t, 0, allocator, (void*)1000);
pthread_create (&t, 0, allocator, (void*)3000);
pthread_create (&t, 0, allocator, (void*)5000);
pthread_create (&t, 0, allocator, (void*)4);
allocator ((void*)99);
return 0;
}
--
-- Wojciech Piechowski - address@hidden - Gadu-Gadu 494156 --
- fopen/malloc MT bug,
Wojciech Piechowski <=