设为首页 加入收藏

TOP

C指针原理(88)-LINUX应用(1)
2014-11-23 18:58:05 来源: 作者: 【 】 浏览:17
Tags:指针 原理 -LINUX 应用

一、在linux平台下,每个线程可有专用数据:

#include

#include
struct mydata{
int x;
char c[4];
};
pthread_t pthreada,pthreadb;
pthread_key_t datakey;//每个进程创建一次,不同的线程,同样名字的键指向不同的地方


void *cleanup_mydata(void *dataptr){//删除键时调用的
free((struct mydata*)dataptr);
}
void anum1(){
int rc;
struct mydata *mdata=(struct mydata*)malloc(sizeof(struct mydata));
mdata->x=1;
mdata->c[0]='a';
mdata->c[1]='\0';
rc=pthread_setspecific(datakey,(void*)mdata);//设置键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
sleep(1);
struct mydata *mmdata=(struct mydata*)pthread_getspecific(datakey);//取出键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
printf("-%d-%s\n",mmdata->x,mmdata->c);
fflush(stdout);
}
void bnum2(){
int rc;
struct mydata *mdata=(struct mydata*)malloc(sizeof(struct mydata));
mdata->x=2;
mdata->c[0]='b';
mdata->c[1]='\0';
rc=pthread_setspecific(datakey,(void*)mdata);//设置键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
sleep(1);
struct mydata *mmdata=(struct mydata*)pthread_getspecific(datakey);//取出键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
printf("-%d-%s\n",mmdata->x,mmdata->c);
fflush(stdout);
}


int main(void){


int rc;


rc=pthread_key_create(&datakey,cleanup_mydata);//为键删除时的清理函数
pthread_create(&pthreada,NULL,anum1,NULL);
pthread_create(&pthreadb,NULL,bnum2,NULL);
sleep(3);
pthread_join(pthreada,NULL);
pthread_join(pthreadb,NULL);
rc=pthread_key_delete(datakey); //仅删除键,但不删除值指向的内存,线程终止调用用户自定义的删除函数,本例中为cleanup_mydata

}

二、摸拟shell

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/


通过execlp函数来实现 ,execlp函数用于执行文件

其参数与说明为:

#include 
   
    

int execlp( const char * file, 
            const char * arg0, 
            const char * arg1,
            … 
            const char * argn, 
            NULL );
   

Arguments:

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C优化篇之减少运算量 下一篇C指针原理(89)-LINUX应用(2)-线..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: