From mboxrd@z Thu Jan 1 00:00:00 1970 From: Randi Botse Subject: Re: thread: reentrant question Date: Sat, 26 Dec 2009 12:32:01 +0700 Message-ID: <34e1241d0912252132y48945feek8c803ccb8625cbbc@mail.gmail.com> References: <34e1241d0912241041v60c031a2kc31fdb3b4ebbd317@mail.gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: QUOTED-PRINTABLE Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=ZdFj6IUSWy3ERSO3SCDb5DuNOfslLawMbWAsZ/fe/gE=; b=MrZuXcEyifbCq8io0B8MXcINXEVA2e5C+v2V7gRNGd46Pwl7VcAplkC8djea85EY9b FXHfSECiW0WSm4SecsGx+hRAKUaAMHUz9C/rTQoxLi+0OXXzStzIHzApCv2frCgWJ6Qj kIadxGs8GN8SkhEsQyZ2vg+dlUNw4OXC/zSLs= In-Reply-To: Sender: linux-c-programming-owner@vger.kernel.org List-ID: Content-Type: text/plain; charset="iso-8859-1" To: vinit dhatrak Cc: linux-c-programming@vger.kernel.org Hi Vinit, So, does this function actually reentrant when free() has not been called?, thanks for your explanation. Randi On 12/25/09, vinit dhatrak wrote: > Hi Randi, > > You are observing this because of malloc's behaviour. If you allocate > and free same-sized memory multiple times, then malloc tries to reuse > the memory. You can try this by putting malloc/free statements in a > loop. > > In your case, first thread gets created, it starts executing, it > allocates the memory, prints the address and frees it. After first > thread finishes its execution, second thread starts. It allocates the > memory so allocator reuses the same chunk and you get same address. > > If you put some sleep between malloc and free OR remove the free > statement itself as you did then you will get different addresses. So > this is really a timing issue when you do thread programming. > > Hope this helps you understand the issue. > > -Vinit > > On Fri, Dec 25, 2009 at 12:11 AM, Randi Botse > wrote: >> Hi all, >> >> I'm now learning thread reentrancy on Linux, so i write simple codes >> for demonstration: >> >> #include >> #include >> #include >> >> void *func(void *data) >> { >> =A0 =A0char *ptr; >> >> =A0 =A0ptr =3D malloc(8); >> =A0 =A0printf("thread%i -> ptr at: %p\n", *(int*) data, ptr); >> =A0 =A0free(ptr); >> =A0 =A0return NULL; >> } >> >> #define NTHREAD 5 >> >> int main(void) >> { >> =A0 =A0pthread_t thread[NTHREAD]; >> =A0 =A0int i; >> >> =A0 =A0for (i =3D 0; i < NTHREAD; i++) >> =A0 =A0 =A0 =A0if (pthread_create(&thread[i], NULL, func, (void*) &i= )) >> =A0 =A0 =A0 =A0 =A0 =A0return -1; >> =A0 =A0for (i =3D 0; i < NTHREAD; i++) >> =A0 =A0 =A0 =A0pthread_join(thread[i], NULL); >> =A0 =A0return 0; >> } >> >> >> I know the func() function is not reentrant, worst printf() debuggin= g >> told me that's each thread pointer to character (ptr) may has same >> address, like this output: >> >> thread1 -> ptr at: 0x9a80128 >> thread1 -> ptr at: 0x9a80138 >> thread2 -> ptr at: 0x9a801d8 >> thread4 -> ptr at: 0x9a801d8 >> thread4 -> ptr at: 0x9a801d8 >> >> I try to modify the func() by not free ptr to be: >> >> void *func(void *data) >> { >> =A0 =A0char *ptr; >> >> =A0 =A0ptr =3D malloc(8); >> =A0 =A0printf("thread%i -> ptr at: %p\n", *(int*) data, ptr); >> =A0 =A0/* free(ptr); */ >> =A0 =A0return NULL; >> } >> >> And the result is i always got different address of each thread's pt= r, >> such as: >> >> thread0 -> ptr at: 0x8db2098 >> thread1 -> ptr at: 0x8db2138 >> thread2 -> ptr at: 0x8db21d8 >> thread3 -> ptr at: 0x8db2278 >> thread4 -> ptr at: 0x8db2318 >> >> Can you explain me why this happen? how about my printf() debugging >> method? it's works for this demonstration? >> Thanks before. >> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0- Randi >> -- >> To unsubscribe from this list: send the line "unsubscribe >> linux-c-programming" in >> the body of a message to majordomo@vger.kernel.org >> More majordomo info at =A0http://vger.kernel.org/majordomo-info.html >> > -- To unsubscribe from this list: send the line "unsubscribe linux-c-progr= amming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html