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() 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