关于thread-fork/data细节实验确认(十一)

来源:互联网 发布:word判断矩阵 编辑:程序博客网 时间:2024/05/22 08:22

NOTE : 前一篇理论部分戳我

一:题目

请确定是否线程中key所关联的数据只有线程自身能够访问?在线程中fork之后的程序执行流是?

二:审题

  解决上面两个疑问,除了百度google等伸手的手段,还有一种就是做实验验证,设计一段代码能合理的解决上面两个疑问。在另一个不绑定keythread-data 的线程中获取key所关联的数据,并在线程中执行fork,观察程序输出。

三:代码展示

[root@localhost dhuang]# vim 12_3.c#include <stdio.h>#include <err.h>#include <string.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>typedef struct {char * a ;char *b;} CHAR;static pthread_key_t key;static pthread_once_t init_done = PTHREAD_ONCE_INIT;void thread_init(void){        pthread_key_create(&key, free);}void * thr_fn(void *arg){        pid_t pid;        CHAR * buffer;        if((buffer=malloc(sizeof(CHAR))) == NULL)                errx(1,"error in malloc\n");            pthread_once(&init_done, thread_init);        if((buffer->a=malloc(10)) == NULL)                errx(1,"error in malloc\n");        strncpy(buffer->a,"hello",6);        if((buffer->b=malloc(3)) == NULL)                errx(1,"error in malloc\n");        strncpy(buffer->b,"hi",3);        pthread_setspecific(key,buffer->a);        printf("thread :%s,%s\n",buffer->a,buffer->b);        if((pid=fork())<0)                errx(1,"error in fork\n");        if(pid==0)                printf("current process is child\n");        else                return((void *)buffer);}int  main (void){        int err;        pthread_t tid;                                                                                                       CHAR * buffer;                                                                                                       err=pthread_create(&tid,NULL,thr_fn,NULL);                                                                           if(err!=0)                                                                                                                   errx(1, "can’t create thread\n");                                                                                                                                                                                                                                                                                                                 if(pthread_getspecific(key) == NULL)                                                                                         printf("It's true that the key is only valid to it's own thread\n");                                                         pthread_join(tid,(void **)&buffer);                                                                                  sleep(20);                                                                                                           printf("%s\n",buffer->a);                                                                                            printf("%s\n",buffer->b);                                                                                            return 0;                                                                                                    }    

四:结果分析

[root@localhost dhuang]# ./12_3 It's true that the key is only valid to it's own threadthread :hello,hicurrent process is childhi[root@localhost dhuang]# 

  第一行打印信息和最后printf("%s\n",buffer->a);没有输出buffer->b的正常输出形成了交叉对比反映了第一个问题,只有和绑定keythread才能访问thread相关的数据。然后current process is child反映了子线程fork之后的子进程执行流仅仅到该子线程自身函数结束。
  笔者读英文版APUE的方式,已经很认真仔细了,针对众多细节进行验证。当然不必多言,但行好事。

NOTE: 如有疑问,请及时指正。
  

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 露龈笑怎么办 子宫出血怎么办 涨停怎么办? 抬钱不还怎么办 商铺烂尾怎么办 衣冠冢怎么办 失神怎么办 搜狗打不出字怎么办 五险一金没交满怎么办 墨锭用后裂怎么办 脚上长硬皮很疼怎么办 豆腐渣怎么办 手弄到淮山痒怎么办 牡丹卡怎么办 脚上长小红点痒怎么办 腿上起红点很痒怎么办 有血痣怎么办 长痘痘很疼怎么办 洗完脸脸干爆皮怎么办 手被烫到怎么办 一笑嘴歪怎么办 盘丝洞耗蓝怎么办 脸多肉怎么办 怕死怎么办 铁兰变绿怎么办 手被夹破皮怎么办 胎死腹中怎么办 遇到困难怎么办 枪打出头鸟怎么办 飞蚊症怎么办 肺火旺长痘怎么办 胆囊息肉怎么办 欠网贷还不上怎么办 网贷还不上怎么办 猫拉完屎爱蹭怎么办 炒肉粘锅怎么办 mastercard怎么办 网商贷还不上怎么办 缺钱怎么办 上火怎么办? 长立事牙疼怎么办