
作业社区
探索学习新天地,共享知识资源!
胡汉三66 的学生作业:
#include #include #include #include //设计一个结构 struct perosn ,包含名字与年龄,在子线程中定义结构体变量,并传递给主线程进行打印 struct person{ char name[30]; int age; }; static struct person per; void *do_pthread(void *arg) { strcpy(per.name,"zhu yuanzhang");//结构体字符串定义 需要用 strcpy函数 per.age = 36; pthread_exit((void *)&per); } int main(void) { int err; pthread_t tid = 0; struct person *perm = NULL; err = pthread_create(&tid,NULL,do_pthread,NULL); if (err != 0) { fprintf(stderr,"[ERROR] pthread_create () : %s\n",strerror(err)); } printf("tid = %ld\n",tid); pthread_join(tid,(void **)&perm);//perm 是一级指针,&perm===>二级指针,&perm保存一级指针地址 printf("NAME = %s\n",perm->name);//perm 是一级指针 printf("AGE = %d\n",perm->age);//perm 是一级指针 return 0; } 【图片】





大禹123 的学生作业:
#include #include #include #include #include static int number = 0;//共享变量 static int total_of_consume = 0;//共享变量 static int total_of_produce = 0; static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int total_product_num = 0; static bool is_product_finished = false; void *product_thread_handler(void *arg) { int cnt = atoi((char *)arg); int i,tmp; for(i = 0;i < cnt;i++){ pthread_mutex_lock(&mtx); printf("生产线程 [%ld] 生产一个产品,库存数量为:%d\n",pthread_self(),++number); total_product_num ++; pthread_mutex_unlock(&mtx); pthread_cond_broadcast(&cond); // 唤醒所有消费者线程 } is_product_finished = total_product_num >=total_of_produce; pthread_exit((void *)0); } //消费者线程 void *consume_thread_handler(void *arg) { bool done = false; for (;;){ //如果生产任务完成了,且库存数量为0,那么直接结束 if(is_product_finished && number == 0) pthread_exit((void *)0); pthread_mutex_lock(&mtx); while(number == 0 && !is_product_finished ) // 当产品数量为 0时,且没有完成生产任务时候,让线程阻塞,并释放锁,这里一般设置循环,防止没有重新获取到锁 pthread_cond_wait(&cond,&mtx); while(number > 0){ total_of_consume++; // 消费产品总数 printf("消费者线程[%ld]消费一个产品,库存数量为:%d\n",pthread_self(), --number); done = total_of_consume >= total_of_produce; // 判断消费者数量与产品数量 } pthread_mutex_unlock(&mtx); // 消费者消费完成之后,释放锁 if (done) break; } pthread_exit((void *)0); } int main(int argc,char *argv[]) { pthread_t product_tids[argc-1]; pthread_t consumer_tids[argc-1]; int i; int err; for (i = 1;i < argc;i++){ //生产线程 total_of_produce += atoi(argv[i]); // 生产数量的总和 err = pthread_create(&product_tids[i-1],NULL,product_thread_handler,(void *)argv[i]); if (err != 0){ perror("[ERROR] pthread_create(): "); exit(EXIT_FAILURE); } //消费者线程 err = pthread_create(&consumer_tids[i-1],NULL,consume_thread_handler,NULL); if (err != 0){ perror("[ERROR] pthread_create(): "); exit(EXIT_FAILURE); } } for (i = 0;i < argc-1;i++){ pthread_join(product_tids[i], NULL); pthread_join(consumer_tids[i], NULL); } printf("任务结束\n"); return 0; } 【图片】





大禹123 的学生作业:
#include #include #include #include #include #include static int product_num = 0; //共享变量,仓库中的数量 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; void *do_thread(void *arg) { for(int i=0; i 0) { ++total_consume; fprintf(stdout, "消费了一个产品,库存产品数量为%d\n", --product_num); done = total_product == total_consume ? true : false; sleep(1); } if(done) break; pthread_mutex_unlock(&mtx); } for(int i =1; i





大禹123 的学生作业:
#include #include #include #include #include typedef struct perosn{ char name[20]; int age; }perosn_t; void *do_thread(void *arg) { static perosn_t pers; pers.age = 20; strcpy(pers.name, "zhangsan"); printf("child thread do!\n"); pthread_exit((void *)&pers); } int main() { int err; pthread_t threadid; perosn_t *p_pers; err = pthread_create(&threadid, NULL, do_thread, NULL); if(err!=0) { fprintf(stderr,"[ERROR] pthread_create(): \n", strerror(err)); } pthread_join(threadid, (void **)&p_pers); printf("person name is %s, age is %d\n",p_pers->name, p_pers->age); sleep(1); return 0; }





大禹123 的学生作业:
#include #include #include #include #include void *do_thread_a(void *arg) { printf("child thread_a is runing\n"); pthread_exit(NULL); } void *do_thread_b(void *arg) { printf("child thread_b is runing\n"); pthread_exit(NULL); } int main() { int err_a,err_b; pthread_t threadid_a,threadid_b; err_a = pthread_create(&threadid_a, NULL, do_thread_a, NULL); if(err_a!=0) { fprintf(stderr,"[ERROR] pthread_create(): \n", strerror(err_a)); } err_b = pthread_create(&threadid_b, NULL, do_thread_b, NULL); if(err_b!=0) { fprintf(stderr,"[ERROR] pthread_create(): \n", strerror(err_b)); } printf("threadid_a is: %ld\n", threadid_a); printf("threadid_b is: %ld\n", threadid_b); pthread_detach(threadid_a); pthread_detach(threadid_b); sleep(1); return 0; }





大禹123 的学生作业:
#include #include #include #include //线程执行函数,由操作系统调度执行 void *do_thread_a(void *args) { printf("thread_a is running!\n"); } void *do_thread_b(void *args) { printf("thread_b is running!\n"); } int main() { int err_a, err_b; pthread_t threadid_a, threadid_b; err_a = pthread_create(&threadid_a, NULL, do_thread_a,NULL); if(err_a!=0) { fprintf(stderr,"[ERROR] phread_create: \n", strerror(err_a)); exit(EXIT_FAILURE); } err_b = pthread_create(&threadid_b, NULL, do_thread_b,NULL); if(err_b!=0) { fprintf(stderr,"[ERROR] phread_create: \n", strerror(err_b)); exit(EXIT_FAILURE); } printf("threadid_a =%ld\n", threadid_a); printf("threadid_b =%ld\n", threadid_b); return 0; }




