????N?????C?????????????????????e?????????????????????????#include????????????о???????????C?????????????????????????????????á?
?????????????????Щ????????????????????Щ???????????????У??????????????????????????????????????ж???????????????????ó???????????а?????????????????????????С????????????????????????????????????????????????????????涼??????????????????????????? #include ??
????????????????????????Щ????????????????и???????????????????????е????????????????????????????????????????????????????????????????£??????λ·??????????????
??????д??????????????????а????κε???????
int f()
{
while(1)
{
printf("hello ");
sleep(5);
}
}
int main()
{
creat("testfile"??0777);
// printf("hello world ");
unsigned long tid = 0;
int ret = pthread_create(&tid??0??&f??0);
if(ret)
{
printf("pthread_create failed ");
}
else
{
printf("tid = %lu "??tid);
pthread_join(tid??0);
}
return 0;
}
???????????????е?????printf??sleep??creat??pthread_create??he pthread_join???????????????????????????????????????£?
????//for printf
????#include<stdio>
????// for sleep
????#include<unistd.h>
????//for create
????#include <sys/types.h>
????#include <sys/stat.h>
????#include <fcntl.h>
????//for pthread_create ??pthread_join
????#include <pthread.h>
?????????????????????????
????root@libin:~/program/C/testlib/head# gcc -o test test.c
????test.c: In function ‘f’:
????test.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
????test.c: In function ‘main’:
????test.c:20: warning: incompatible implicit declaration of built-in function ‘printf’
????test.c:24: warning: incompatible implicit declaration of built-in function ‘printf’
????/tmp/cc2Rt0UO.o: In function `main':
????test.c:(.text+0x65): undefined reference to `pthread_create'
????test.c:(.text+0xa6): undefined reference to `pthread_join'
????collect2: ld returned 1 exit status
?????????????е????飬???????????pthread_create ??pthread_join????????????塣????????????????????libpthread.so??????-lpthread??????????? ?е????????????creat sleep ??printf???????????????????塣?????????Щ????????libc???У???libc?????????????????????libc??????
root@libin:~/program/C/testlib/head# ll
?????? 12
drwxr-xr-x 2 root root 4096 2012-07-28 10:41 ./
drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
-rw-r--r-- 1 root root 405 2012-07-28 10:40 test.c
root@libin:~/program/C/testlib/head# gcc -Wall -o test test.c -lpthread
test.c: In function ‘f’:
test.c:6: warning: implicit declaration of function ‘printf’
test.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:7: warning: implicit declaration of function ‘sleep’
test.c: In function ‘main’:
test.c:14: warning: implicit declaration of function ‘creat’
test.c:17: warning: implicit declaration of function ‘pthread_create’
test.c:20: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:24: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:25: warning: implicit declaration of function ‘pthread_join’
root@libin:~/program/C/testlib/head# ll
?????? 20
drwxr-xr-x 2 root root 4096 2012-07-28 10:41 ./
drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
-rwxr-xr-x 1 root root 7394 2012-07-28 10:41 test*
-rw-r--r-- 1 root root 405 2012-07-28 10:40 test.c
root@libin:~/program/C/testlib/head# ./test
tid = 3077905264
hello
hello
???????Щ???棬??????????????????test??????????????С?
?????????????????????а????κ??????????????????飬????????????????????????????????????????????????????????????????????????????????????????????????????????????顣
???????????????????????????????????£?????????????????????
????????????£??????д?????????????????????????????whatever?????????????????????????????д????????????????????θ??? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ???????????????? ??????????????????????????????????????????顣????????????????????????????????????漰?????????????????飬????κε???????????????OK??????????????????????
??????????????????????????????????????????????顣???????????????????????????????????顣???????а??????????????????????????????????????????????
???????sleep???????????????????pthread_create???????????????????????????????????????????????
int f()
{
while(1)
{
printf("hello ");
sleep(5??4);
}
}
int main()
{
creat("testfile"??0777);
// printf("hello world ");
unsigned long tid = 0;
int ret = pthread_create(&tid??0??&f);
if(ret)
{
printf("pthread_create failed ");
}
else
{
printf("tid = %lu "??tid);
pthread_join(tid??0);
}
return 0;
}
root@libin:~/program/C/testlib/head# gcc -Wall -o test test.c -lpthread
test.c: In function ‘f’:
test.c:6: warning: implicit declaration of function ‘printf’
test.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:7: warning: implicit declaration of function ‘sleep’
test.c: In function ‘main’:
test.c:14: warning: implicit declaration of function ‘creat’
test.c:17: warning: implicit declaration of function ‘pthread_create’
test.c:20: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:24: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:25: warning: implicit declaration of function ‘pthread_join’
?????????????????gcc??ī?????????????sleep???????????pthread_create??????????????????????????С?
????root@libin:~/program/C/testlib/head# ./test
????tid = 3079175024
????hello
????hello
????hello
????hello
????hello
????hello
????hello
??????????????Σ?????????????????п??????????????????????????????????????????????????顣
#include<unistd.h>
#include<pthread.h>
int f()
{
while(1)
{
printf("hello ");
sleep(5??4);
}
}
int main()
{
creat("testfile"??0777);
// printf("hello world ");
unsigned long tid = 0;
int ret = pthread_create(&tid??0??&f);
if(ret)
{
printf("pthread_create failed ");
}
else
{
printf("tid = %lu "??tid);
pthread_join(tid??0);
}
return 0;
}
???????±????????
root@libin:~/program/C/testlib/head# ll
?????? 12
drwxr-xr-x 2 root root 4096 2012-07-28 10:51 ./
drwxr-xr-x 5 root root 4096 2012-07-27 19:05 ../
-rw-r--r-- 1 root root 444 2012-07-28 10:51 test.c
root@libin:~/program/C/testlib/head# gcc -Wall -o test test.c -lpthread
test.c: In function ‘f’:
test.c:8: warning: implicit declaration of function ‘printf’
test.c:8: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:9: error: too many arguments to function ‘sleep’
test.c: In function ‘main’:
test.c:16: warning: implicit declaration of function ‘creat’
test.c:19: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘int (*)()’
test.c:19: error: too few arguments to function ‘pthread_create’
test.c:22: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:26: warning: incompatible implicit declaration of built-in function ‘printf’
????????????????????????????????????????????????????????????????????????????????troubleshooting?????????????????????????????????????а????ó????????????????????飬??????米?????????????飬??ó?????????????????????????????????????????????????????
?????????????????????????????????飬?????????????????????????????????????????棬troubleshooting?????????