当前位置: 技术问答>linux和unix
关于系统调用的问题
来源: 互联网 发布时间:2014-12-22
本文导语: 所创建的线程 为什么没有执行sub_function(),或者执行后子函数没有打印出 #include #include #include #include #include #include int num; pthread_t mythread; void *sub_function(){ printf("I'm a thread %d of the process that has been ...
所创建的线程 为什么没有执行sub_function(),或者执行后子函数没有打印出
#include
#include
#include
#include
#include
#include
int num;
pthread_t mythread;
void *sub_function(){
printf("I'm a thread %d of the process that has been created by forkn",getpid());
printf("pthread_create(%ld,NULL,*subfunction,NULL)n",mythread);
fflush(stdout);
return NULL;
}
int main() {
printf("I'm a main function,my PID is %dn",getpid());
for (num=0;num0){
printf("I'm a parent process,my ID is %d,and I receivea return value %d form my childrenn",getpid(),pid);
}
}
return 0;
}
#include
#include
#include
#include
#include
#include
int num;
pthread_t mythread;
void *sub_function(){
printf("I'm a thread %d of the process that has been created by forkn",getpid());
printf("pthread_create(%ld,NULL,*subfunction,NULL)n",mythread);
fflush(stdout);
return NULL;
}
int main() {
printf("I'm a main function,my PID is %dn",getpid());
for (num=0;num0){
printf("I'm a parent process,my ID is %d,and I receivea return value %d form my childrenn",getpid(),pid);
}
}
return 0;
}
|
主线程先于其它线程退出了,其它线程还没有来得及输出,进程就结束了。
|
主线程用pthread_join等一下就行了
|
因为你的主线程退出了,自线程就自动退出了,呵呵,在main的return 0前加上pthread_join(mythread, NULL);或者sleep(1);