excuse me, what does locking and calling from the same function mean?
here is my code:
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
void _sleep(unsigned int t){
struct timeval tm;
tm.tv_sec = t;
tm.tv_usec = 0;
select(0,NULL,NULL,NULL,&tm);
}
void* t1(void* a){
pid_t t = getpid();
printf("pid = %d\n",t);
printf("thread %d begins\n",pthread_self());
// sleep(10);
_sleep(10);
printf("thread %d ends\n",pthread_self());
}
void* t2(void* a){
pid_t t = getpid();
printf("pid = %d\n",t);
int i = 0;
printf("thread %d begins\n",pthread_self());
for(; i < 10 ;++i){
printf("thread %d is running\n",pthread_self());
//sleep(1);
_sleep(1);
}
}
int main(){
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,NULL,t1,NULL);
pthread_create(&tid2,NULL,t2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
}
and the result in my redhat 9 is:
pid = 2732
thread 1082399936 begins
pid = 2732
thread 1090788416 begins
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1082399936 ends
It seems the call to select() only sleeps the calling thread.
but in fedoracore6 the select() call sleeps the whole process. Last friday, I found this problem in office, but now I can't access that machine,so I can't give you the test result. But I'm sure the call to select() will sleep the whole process accords to my test in last friday.