#include "apue.h" #include <fcntl.h> #include <pthread.h>
char buf[]="abcdefghI1234567890"; char nn_read[100]; int fd;
void * thr_fn1(void *arg) { fd=open("./tmp",O_RDONLY); //lseek(fd,0,SEEK_SET); //read(fd,nn_read,10); pread(fd,nn_read,10,0); close(fd); printf("this is thr_fn1:%s\n",nn_read); } void * thr_fn2(void *arg) { fd=open("./tmp",O_RDONLY); // lseek(fd,4,SEEK_SET); // read(fd,nn_read,10); pread(fd,nn_read,10,4); // 使用原子操作 close(fd); printf("this is thr_fn2:%s\n",nn_read); }
int main(void) { pthread_t tid1,tid2; void *tret; fd=creat("./tmp",FILE_MODE); write(fd,buf,sizeof(buf));
pthread_create(&tid1,NULL,thr_fn1,NULL); pthread_create(&tid2,NULL,thr_fn2,NULL); pthread_join(tid1,&tret); pthread_join(tid2,&tret); exit(0);
}
|