设为首页 加入收藏

TOP

Linux多进程多线程互斥同步例子
2014-11-24 08:29:41 来源: 作者: 【 】 浏览:0
Tags:Linux 进程 线程 同步 例子

Linux多进程多线程互斥同步例子,运行顺序:先运行进程1,再运行进程2。


进程1


#include
#include
#include


#include
#include
#include


#define DEBUG 1
#define SHARE_KEY 0x1234
#define THREAD_NUM 4


typedef struct{
pthread_mutex_t lock;
pthread_cond_t cond;
char msg[180];
int num;
}Share_Stuff;


static Share_Stuff* stuff[THREAD_NUM];


void* threadB(void *prm);



int main(int argc,char** argv)
{
void *share_addr=NULL;
pthread_t tid[THREAD_NUM];
int shmid = -1;
int ret=0;
int i=0;


#if DEBUG
printf("______%s______%s______\n",__DATE__,__TIME__);
#endif


////////////////////////////////////////////share menory////////////////////////////////////////////
shmid = shmget(SHARE_KEY,sizeof(Share_Stuff), 0666|IPC_CREAT);
if(shmid == -1){
printf("Create share memory fail! - %s\n",strerror(errno));
}


share_addr=(void*)shmat(shmid,(void*)0,0);
if(share_addr < 0){
printf("Get share memory address error! - %s\n",strerror(errno));
}


////////////////////////////////////////////get the addr////////////////////////////////////////////
#if DEBUG
printf("share_addr:%x\n",(unsigned int)share_addr);
#endif


for(;i stuff[i]=(Share_Stuff*)share_addr+i;
stuff[i]->num=i;
#if DEBUG
printf("stuff's addr:%x\n",(unsigned int)stuff[i]);
#endif
}


i=0;
////////////////////////////////////////////create_thread////////////////////////////////////////////
pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);


pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);

for(;i pthread_cond_init(&stuff[i]->cond, &cond_attr);
pthread_mutex_init(&stuff[i]->lock, &mutex_attr);
ret=pthread_create(&tid[i],0,threadB,(void*)stuff[i]);
if(ret!=0){
printf("ThreadA%d create error!\n",i);
}
}


pthread_condattr_destroy(&cond_attr);
pthread_mutexattr_destroy(&mutex_attr);


i=0;
////////////////////////////////////////////wait_for_done////////////////////////////////////////////
for(;i ret=pthread_join(tid[i],NULL);
}


#if DEBUG
printf("all thread done!\n");
#endif


return 0;
}



void* threadB(void *prm)
{
Share_Stuff *stuff;

#if DEBUG
printf("thread's prm:%x\n",(unsigned int)prm);
#endif


stuff=(Share_Stuff *)prm;
while(1){
sleep(1);
//printf("pthread_cond_wait\n");
pthread_cond_wait(&stuff->cond,&stuff->lock);
printf("message:%s",stuff->msg);
/*******************************************************/
sleep(3);
//printf("pthread_mutex_lock\n");
pthread_mutex_lock(&stuff->lock);
sprintf(stuff->msg,"threadA--%d\n",stuff->num);
// printf("pthread_cond_signal\n");
pthread_cond_signal(&stuff->cond);
//printf("pthread_mutex_unlock\n");
pthread_mutex_unlock(&stuff->lock);
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇sed 单行脚本快速参考 下一篇Linux 定时器timer使用

评论

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

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)