- 论坛徽章:
- 0
|
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <pthread.h>;
- #include <time.h>;
- #include <string.h>;
- #include <sys/stat.h>;
- #include <sys/types.h>;
- #include <errno.h>;
- #define BUFFER 512
- #define MAX_HOST 256
- struct rcmd_type{
- char *hostname;
- char *time;
- };
- void *_exec_program(void *arg) {
- int ret;
- char *buff;
- struct rcmd_type *host = (struct rcmd_type *)arg;
- printf("GET HOSTNAME: %s TIME: %s \n",host->;hostname,host->;time);
- ret =1;
- pthread_exit(&ret);
- }
- int main(int argc,char **argv)
- {
- pthread_t *thread;
- char *hostname[MAX_HOST];
- int i,j,host_num;
- int ret;
- struct rcmd_type host;
-
- host_num = get_host((char **)(&hostname));
- if(argc!=2)
- return 0;
-
- if(host_num<0)
- return 0;
-
- if((thread=(pthread_t *)malloc(sizeof(pthread_t)*host_num))==NULL){
- fprintf(stderr,"Out Of Memory!\n\a");
- exit(1);
- }
- host.time = argv[1];
- for(i=0,j=0;i<=host_num;i++,j++){
- host.hostname = NULL;
- host.hostname = hostname[i];
- if(pthread_create(&thread[j],NULL,_exec_program,(void *)&host)!=0)
- fprintf(stderr,"Create Thread[%d] Error:%s\n\a",i,strerror(errno));
- }
-
- ret = 0;
- for(i=0;i<=host_num;i++){
- if(pthread_join(thread[i],(void *)&ret)!=0)
- fprintf(stderr,"Thread[%d] Join Error:%s\n\a",i,strerror(errno));
- else {
- if(ret==0)
- continue;
- printf("Thread[%d] Exit\n\a",i);
- free(hostname[i]);
-
- }
- }
- free(thread);
- exit(0);
- }
复制代码
请斑竹帮我看看,怎么每次线程得到的参数有问题,或老是一样的或不全!
get_host 程序主要作用是创建服务器的数组,然后返回服务器数目 |
|