Hi Ravikumar,
* *
> And from some other sources I understand that there is a one-one mapping
> between threads created by library API and kernel threads.
>
>
I think kernel threads here do not seem to indicate conventional kernel
threads. There are kernel thread which handle interrupts, and there is
user-context kernel code on behalf of user threads. I think what you are
mentioning here is the latter, which is not regarded as kernel "thread,"
even though they are part of kernel.
> Now my questions are :
> 1. I have Program and I've created two threads say T1 and T2 using Posix
> Thread Library. see below
> *void* function2()
> {
> while(1)
> {
> printf("I'm Thread 2\n");
> sleep(1);
> }
> }
> void* function1()
> {
> printf("I'm Thread 1\n");
> char buf[1024];
> read(1,buf,1023); // Thread 1 will block here
> }
> int main(int argc,char *argv[])
> {
>
> pthread_t pid1,pid2;
> pthread_create(&pid1,NULL,function1,NULL);
> pthread_create(&pid2,NULL,function2,NULL);
> while(1)
> {
> printf("I'm Main Thread\n");
> sleep(1);
> }
> }
>
> *
> According my understanding there will be three tasks will be created in the
> kernel after I ran this program. One is for main process say P and other two
> for T1 and T2.
> And T1 & T2 share the address space of P. Is my understanding is correct?
>
>
Yeah, that's correct. pthread_create() calls
create_thread<http://www.google.com/codesearch/p?hl=en#5ge3gHPB4K4/gnu/glibc/glibc-2.3.4.tar.bz2%7CTTlJV-niWY8/glibc-2.3.4/nptl/sysdeps/pthread/createthread.c&q=create_thread&exact_package=http://ftp.gnu.org/gnu/glibc/glibc-2.3.4.tar.bz2>()
in glibc, and the clone flag contains CLONE_VM flag in calling do_clone().
> 2.Regarding the scheduling.
>
*read(1,buf,1023); // Thread 1 will block here
*The integer 1 for fd means reading standard output of itself, which usually
does not make sense.
Even reading from stdin should stall at this point because it is synchronous
read.
Sangman