????——????????????????????Linux???????????????????????????
???????
????????????????????????????????????????????????????????????ж????????Щ??????????????????????????????Щ????????????н?????????QQ???????????????????
??????????POSIX?????????????????????д??????????1????
????1?????int????????g_Flag?????0??
????2?? ??????????????1?????“this is thread1”??????g_Flag?????1
????3?? ???????????????2?????“this is thread2”??????g_Flag?????2
????4?? ?????1????????2???????????
????5?? ?????????g_Flag??1???2???????2???1????????
??????????????????????£?????????????????????????????£?
????1???????????
????2?????????????
????3???й?????????????
????4????????????
????5????????????
????6???????????
????1???????????
?????????????????????????????????????????е????????????????????????????????????????????????????CPU???????????????λ??
?????????????????????????CPU??????????????λ???????????С??????????е??????λ???????????????????????к????????????????????????????ó???????????????????????????????????????????????????е?????????
????“????——????????С??λ?????——??????е?С??λ”
?????????ж??????????????????????????????2??????????????????????????????????е??????·?????????????????????????????????е??????????????????????????????????????????????????????????????????????л?????????????Ч??????Щ?????????Щ????????в???????????Щ?????????????????????????????y????
????2?????????????
?????????????????????????????????????Щ?????????????????????ɡ???????????????ж???????????????е??????????????????????????????????????????????????Linux?μ????????
?????????????????????????????????????”???”??????????????????????????Linux???£????????μ?????????????????????????????????????????????????Ρ?????κ?????Σ????????”????”??????????????????????????????е?????????????????????????????????????????????????????????????С?????????????????????????????????л????????????С???????л????????????????????????????????????????????????????30???????????????????????????????????н???????
??????????????????????????????????????????????????????ж???????????????????????????????????????У?????????????????????????????????????????????μ??????乲????????????????????????????????????????????????????????????????????????????????Щ?????е??????????????????????????е?????????????static????????п????????????????????????????Щ?????д????????????????????
?????????????????????????????????????????????????????????????????????????μ????
?????????ó?????????????ν????????????????壬?????????????????????????????????????????????????????????????????????????????????????????????????time consuming??????????μ???????????????????ε??????
???????CPU????????Ч????????????????????????CPU????????????????????????CPU???
??????????????????????????????????????????????????????????????????в??????????????????????????
????=============================
??????????????????????????????fork()???????????????clone()??????Richard Stevens????????????
????fork is expensive. Memory is copied from the parent to the child?? all descriptors are duplicated in the child?? and so on. Current implementations use a technique called copy-on-write?? which avoids a copy of the parent’s data space to the child until the child needs its own copy. But?? regardless of this optimization?? fork is expensive.
????IPC is required to pass information between the parent and child after the fork. Passing information from the parent to the child before the fork is easy?? since the child starts with a copy of the parent’s data space and with a copy of all the parent’s descriptors. But?? returning information from the child to the parent takes more work.
????Threads help with both problems. Threads are sometimes called lightweight processes since a thread is “lighter weight” than a process. That is?? thread creation can be 10–100 times faster than process creation.
????All threads within a process share the same global memory. This makes the sharing of information easy between the threads?? but along with this simplicity comes the problem of synchronization.
????=============================
????3???й????????????
????#include int pthread_create(pthread_t *tid?? const pthread_attr_t *attr?? void *(*func) (void *)?? void *arg);
????int pthread_join (pthread_t tid?? void ** status);
????pthread_t pthread_self (void);
????int pthread_detach (pthread_t tid);
????void pthread_exit (void *status);
????pthread_create?????????????????????0????????Exxx???????????
????pthread_t *tid?????id???????pthread_t??????????????????????pthread_create?????????*tid??????
????const pthread_attr_t *attr????????????????????????????????????С??????????????????????NULL???????????????????????????????????
????void *(*func) (void *)?????????func????????μ??????????????е??????
????void *arg????????е????????????????????????????????????????????С?
????pthread_join????????????????????????0????????Exxx???????????
????pthread_t tid??????????????ID
????void ** status????????NULL??????????????洢??status???????У????????status????????????????????????“?-???”????????
????pthread_self?????????????ID??
????pthread_detach?????????????????????????????????????????????????????????0????????Exxx????????????????????????????????????????????????????????????????????????????????????????ID??????????????????????????pthread_join??
??????????????????????????????????????????????к???????????????????????н??????????????
????pthread_exit??????????????????????????????????????pthread_join???????????????????
????void *status???????????????????
???????????Щ????????????????????????????????
????1?????int????????g_Flag?????0??
????2????????????????1?????“this is thread1”??????g_Flag?????1
????3?????????????????2?????“this is thread2”??????g_Flag?????2
??????3?????????????????pthread_create???????????????£?
/*
* 1?????int????????g_Flag?????0??
*
* 2????????????????1?????“this is thread1”??????g_Flag?????1
*
* 3?????????????????2?????“this is thread2”??????g_Flag?????2
*
*/
#include
#include
#include
#include
#includeint g_Flag=0;
void* thread1(void*);
void* thread2(void*);
/*
* when program is started?? a single thread is created?? called the initial thread or main thread.
* Additional threads are created by pthread_create.
* So we just need to create two thread in main().
*/
int main(int argc?? char** argv)
{
printf("enter mainn");
pthread_t tid1?? tid2;
int rc1=0?? rc2=0;
rc2 = pthread_create(&tid2?? NULL?? thread2?? NULL);
if(rc2 != 0)
printf("%s: %dn"??__func__?? strerror(rc2));
rc1 = pthread_create(&tid1?? NULL?? thread1?? &tid2);
if(rc1 != 0)
printf("%s: %dn"??__func__?? strerror(rc1));
printf("leave mainn");
exit(0);
}
/*
* thread1() will be execute by thread1?? after pthread_create()
* it will set g_Flag = 1;
*/
void* thread1(void* arg)
{
printf("enter thread1n");
printf("this is thread1?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
g_Flag = 1;
printf("this is thread1?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
printf("leave thread1n");
pthread_exit(0);
}
/*
* thread2() will be execute by thread2?? after pthread_create()
* it will set g_Flag = 2;
*/
void* thread2(void* arg)
{
printf("enter thread2n");
printf("this is thread2?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
g_Flag = 2;
printf("this is thread1?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
printf("leave thread2n");
pthread_exit(0);
}