void * do_get_read(void *vptr) { int fd, n; char line[MAXLINE]; struct file *fptr; fptr = (struct file *) vptr; fd = Tcp_connect(fptr->f_host, SERV); fptr->f_fd = fd; printf("do_get_read for %s, fd %d, thread %d\n", fptr->f_name, fd, fptr->f_tid); write_get_cmd(fptr); /* write() the GET command */ /* 4Read server's reply */ for ( ; ; ) { if ( (n = Read(fd, line, MAXLINE)) == 0) break; /* server closed connection */ printf("read %d bytes from %s\n", n, fptr->f_name); } printf("end-of-file on %s\n", fptr->f_name); Close(fd); fptr->f_flags = F_DONE; /* clears F_READING */ Pthread_mutex_lock(&ndone_mutex); ndone++; Pthread_cond_signal(&ndone_cond); Pthread_mutex_unlock(&ndone_mutex); return(fptr); /* terminate thread */ }
void thr_exit() { Pthread_mutex_lock(&m); done = 1; Pthread_cond_signal(&c); Pthread_mutex_unlock(&m); }
/* * _event_server_initialize * * perform metric server initialization */ static void _event_server_initialize(void) { int event_names_count; Pthread_mutex_lock(&event_server_init_lock); if (event_server_init) goto out; if (event_names) { event_names_count = List_count(event_names); event_connections = List_create((ListDelF)free); event_connections_index = Hash_create(event_names_count, (hash_key_f)hash_key_string, (hash_cmp_f)strcmp, (hash_del_f)list_destroy); } Signal(SIGPIPE, SIG_IGN); event_server_init++; Pthread_cond_signal(&event_server_init_cond); out: Pthread_mutex_unlock(&event_server_init_lock); }
/* * _cerebrod_listener_initialize * * perform listener initialization */ static void _cerebrod_listener_initialize(void) { int i; Pthread_mutex_lock(&listener_init_lock); if (listener_init) goto out; Pthread_mutex_lock(&listener_fds_lock); for (i = 0; i < conf.listen_message_config_len; i++) { if ((listener_fds[i] = _listener_setup_socket(i)) < 0) CEREBROD_EXIT(("listener fd setup failed")); } Pthread_mutex_unlock(&listener_fds_lock); if (!(clusterlist_handle = clusterlist_module_load())) CEREBROD_EXIT(("clusterlist_module_load")); if ((found_clusterlist_module = clusterlist_module_found(clusterlist_handle)) < 0) CEREBROD_EXIT(("clusterlist_module_found")); if (found_clusterlist_module) { if (clusterlist_module_setup(clusterlist_handle) < 0) CEREBROD_EXIT(("clusterlist_module_setup")); if (conf.debug && conf.listen_debug) { fprintf(stderr, "**************************************\n"); fprintf(stderr, "* Cerebro Clusterlist\n"); fprintf(stderr, "* -----------------------\n"); fprintf(stderr, "* Using Clusterlist: %s\n", clusterlist_module_name(clusterlist_handle)); fprintf(stderr, "**************************************\n"); } } cerebrod_listener_data_initialize(); for (i = 0; i < conf.forward_message_config_len; i++) { /* if the forward destination is local to the machine, don't forward */ if (conf.forward_message_config[i].ip_is_local) continue; if (_forwarding_setup(i) < 0) CEREBROD_EXIT(("forwarding setup failed")); } listener_init++; Pthread_cond_signal(&listener_init_cond); out: Pthread_mutex_unlock(&listener_init_lock); }
void produce(struct buf_t* bptr, int val) { Pthread_mutex_lock(&bptr->b_mutex); /* Wait if buffer is full */ while (bptr->b_nitems >= BUFFSIZE) Pthread_cond_wait(&bptr->b_cond_producer, &bptr->b_mutex); /* There is room, store the new value */ printf("produce %d\n", val); bptr->b_buf[bptr->b_nextput] = val; if (++bptr->b_nextput >= BUFFSIZE) bptr->b_nextput = 0; bptr->b_nitems++; /* Signal consumer */ Pthread_cond_signal(&bptr->b_cond_consumer); Pthread_mutex_unlock(&bptr->b_mutex); }
/* * _event_queue_monitor_initialize * * perform metric queue_monitor initialization */ static void _event_queue_monitor_initialize(void) { Pthread_mutex_lock(&event_queue_monitor_init_lock); if (event_queue_monitor_init) goto out; /* Note: * See comments in cerebrod_event_update about why the event_queue * is initialized there instead of here. */ event_queue_monitor_init++; Pthread_cond_signal(&event_queue_monitor_init_cond); out: Pthread_mutex_unlock(&event_queue_monitor_init_lock); }
int crew_start(crew_p crew,char* path,char* string){ Pthread_mutex_lock(&crew->mutex); while(crew->work_cnt>0){ Pthread_cond_wait(&crew->done,&crew->mutex); } work_p work = (work_p)malloc(sizeof(work)); errno =0; name_max = pathconf(path,_PC_NAME_MAX); if((int)name_max==-1){ if(errno==0) name_max = 2560; else{ printf("%s\n",strerror(errno)); exit(-1); } } path_max = pathconf(path,_PC_PATH_MAX); if((int)path_max==-1){ if(errno==0) path_max=102400; else{ printf("%s\n",strerror(errno)); exit(-1); } } path_max++; name_max++; work->string =string; work->path = (char*)malloc(path_max); strcpy(work->path,path); work->next = NULL; if(crew->first_work==NULL){ crew->first_work = work; crew->last_work = work; }else{ crew->last_work->next = work; crew->last_work = work; } crew->work_cnt ++; Pthread_cond_signal(&crew->go); while(crew->work_cnt>0){ Pthread_cond_wait(&crew->done,&crew->mutex); } return 0; }
void *thread_do_get_read(void *pv) { int fd, wfd, n; char line[MAXLINE], *filename; struct file *pf; pf = (struct file *)pv; fd = host_connect(pf->f_pchHost, WEBSERVPORT); if (fd < 0) err_sys("host_connect error"); pf->f_iFd = fd; pf->f_thrId = pthread_self(); printf("thread_do_get_read for %s, fd %d, thread %u\n", pf->f_pchName, pf->f_iFd, pf->f_thrId); thread_write_get_cmd(pf); filename = strrchr(pf->f_pchName, '/'); if (filename == NULL) filename = pf->f_pchName; else filename++; if ((wfd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) err_sys("open error"); /* read server's reply */ for (;;) { if ((n = Read(pf->f_iFd, line, sizeof(line))) == 0) break; /* server closed connection */ Writen(wfd, line, n); printf("read %d bytes from %s\n", n, pf->f_pchName); } printf("end-of-file on %s\n", pf->f_pchName); Close(pf->f_iFd); Close(wfd); pf->f_iFlags = F_DONE; Pthread_mutex_lock(&ndone_mutex); ndone++; Pthread_cond_signal(&ndone_cond); Pthread_mutex_unlock(&ndone_mutex); return pf; }
void cerebrod_queue_event(struct cerebro_event *event, unsigned int index) { struct cerebrod_event_to_send *ets; assert(event); assert(event_queue); Pthread_mutex_lock(&event_queue_lock); ets = (struct cerebrod_event_to_send *)Malloc(sizeof(struct cerebrod_event_to_send)); ets->event_name = event->event_name; ets->index = index; ets->event = event; List_append(event_queue, ets); Pthread_cond_signal(&event_queue_cond); Pthread_mutex_unlock(&event_queue_lock); }
int consume(struct buf_t* bptr) { int val; Pthread_mutex_lock(&bptr->b_mutex); /* Wait if buffer is empty */ while (bptr->b_nitems <= 0) Pthread_cond_wait(&bptr->b_cond_consumer, &bptr->b_mutex); /* There is data, fetch the value */ val = bptr->b_buf[bptr->b_nextget]; printf("consume %d\n", val); if (++bptr->b_nextget >= BUFFSIZE) bptr->b_nextget = 0; bptr->b_nitems--; /* Signal producer; it might be waiting for space to store */ Pthread_cond_signal(&bptr->b_cond_producer); Pthread_mutex_unlock(&bptr->b_mutex); return (val); }
void *produce(void *arg) { for(;;){ pthread_mutex_lock(&put.mutex);/* locked */ if(put.npro >= nitems){/* done */ pthread_mutex_unlock(&put.mutex); pthread_exit((void*)0) ; } buff[put.npro] = put.nval; put.npro++; put.nval++; Pthread_mutex_unlock(&put.mutex);/* unlocked */ Pthread_mutex_lock(&nready.mutex);/* lock for cond */ if(nready.nready == 0) Pthread_cond_signal(&nready.cond);/* send signal */ nready.nready ++; Pthread_mutex_unlock(&nready.mutex); *((int*)arg) += 1; } }
/* include prodcons */ void * produce(void *arg) { for ( ; ; ) { Pthread_mutex_lock(&put.mutex); if (put.nput >= nitems) { Pthread_mutex_unlock(&put.mutex); return(NULL); /* array is full, we're done */ } buff[put.nput] = put.nval; put.nput++; put.nval++; Pthread_mutex_unlock(&put.mutex); Pthread_mutex_lock(&nready.mutex); if (nready.nready == 0) Pthread_cond_signal(&nready.cond); nready.nready++; Pthread_mutex_unlock(&nready.mutex); *((int *) arg) += 1; } }
void *produce(void *arg) { for( ; ; ){/*向数组中放下一个条目*/ Pthread_mutex_lock(&put.mutex); if(put.nput >= nitems){ /*如果数目大于指定数目说明buff已满*/ Pthread_mutex_unlock(&put.mutex); return(NULL); } buff[put.nput] = put.nval; put.nput++; put.nval++; Pthread_mutex_unlock(&put.mutex); /*通知消费者*/ Pthread_mutex_lock(&nready.mutex); if(nready.nready == 0)/*如果nready计数器等于0*/ Pthread_cond_signal(&nready.cond);/*唤醒消费者线程*/ nready.nready++; Pthread_mutex_unlock(&nready.mutex); *((int*)arg) += 1; } }
void* work_loop(void* args){ worker_p mine = (worker_p)args; crew_p crew = mine->crew; struct stat file_stat; struct dirent *entry; work_p work; entry = (struct dirent*)malloc(sizeof(struct dirent)+name_max); Pthread_mutex_lock(&crew->mutex); while(crew->work_cnt==0){ Pthread_cond_wait(&crew->go,&crew->mutex); } Pthread_mutex_unlock(&crew->mutex); while(1){ Pthread_mutex_lock(&crew->mutex); while(crew->first_work==NULL) Pthread_cond_wait(&crew->go,&crew->mutex); printf("woker %d works on :#%lx ,conunt is %d\n", mine->index,crew->first_work,crew->work_cnt); work = crew->first_work; crew->first_work = work->next; if(crew->first_work==NULL) crew->last_work = NULL; printf("woker %d took #%lx ,leaave first is #%lx last is #%lx\n", mine->index,work,crew->first_work,crew->last_work); Pthread_mutex_unlock(&crew->mutex); if(lstat(work->path,&file_stat)<0){ printf("%s error: %s\n",work->path,strerror(errno)); } if(S_ISLNK(file_stat.st_mode)){ printf("%s is a link\n", work->path); } else if (S_ISDIR(file_stat.st_mode)){ DIR* directory = opendir(work->path); struct dirent* result; if(directory==NULL){ fprintf(stderr, "open %s errro:%s\n", work->path,strerror(errno)); continue; } while(1){ int status = readdir_r(directory,entry,&result); if(status!=0){ fprintf(stderr, "unable to open %s,error:%s\n", work->path,strerror(status)); break; } if(result==NULL) break; if(strcmp(entry->d_name,".")==0||strcmp(entry->d_name,"..")==0) continue; work_p new_work= (work_p)malloc(sizeof(work_t)); new_work->string = work->string; new_work->path = (char*) malloc(path_max); strcpy(new_work->path,work->path); strcat(new_work->path,"/"); strcat(new_work->path,entry->d_name); new_work->next= NULL; Pthread_mutex_lock(&crew->mutex); if(crew->first_work==NULL){ crew->first_work=new_work; crew->last_work = new_work; }else{ crew->last_work->next = new_work; crew->last_work = new_work; } crew->work_cnt++; Pthread_cond_signal(&crew->go); Pthread_mutex_unlock(&crew->mutex); } closedir(directory); } else if (S_ISREG(file_stat.st_mode)){ FILE* file; char buf[MAXLINE],*fbuf; if((file = fopen(work->path,"r"))==NULL){ fprintf(stderr, "can not open %s:%s\n", work->path,strerror(errno)); continue; } else{ while(1){ if((fbuf= fgets(buf,MAXLINE,file))==NULL){ if(feof(file)) break; else if(ferror(file)){ fprintf(stderr, "read %s error:%s\n", work->path,strerror(errno)); break; } } if(strstr(buf,work->string)!=NULL){ printf("found %s in %s\n",work->string,work->path); break; } } fclose(file); } } else{ fprintf(stderr, "%s's type is %s\n", work->path,S_ISFIFO(file_stat.st_mode)?"FIFO": S_ISCHR(file_stat.st_mode)?"CHAR": S_ISBLK(file_stat.st_mode)?"BLK": S_ISSOCK(file_stat.st_mode)?"SOCK":"unknown"); } free(work->path); free(work); Pthread_mutex_lock(&crew->mutex); crew->work_cnt--; if(crew->work_cnt<=0){ Pthread_cond_broadcast(&crew->done); Pthread_mutex_unlock(&crew->mutex); break; } Pthread_mutex_unlock(&crew->mutex); } free(entry); return NULL; }