Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208207
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 824
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-12 21:40
个人简介

只有今天的埋头,才有明天的出头。

文章分类

全部博文(80)

文章存档

2014年(80)

我的朋友

分类: LINUX

2014-12-21 21:13:22

#include
#include
#include
#include
#include
int main()
{
pid_t result;
result = fork();
if(result==-1)
{
perror("fork:");
exit(1);
}
else if(result==0)
{
printf("Current value is %d. In child process, child PID = %d\n",result,getpid());
}
else 
{
printf("Current value is %d. In father process, father PID = %d\n",result,getppid());
}
return 0;
}
++++++++++++++++++++++++++++
/* mutex.c */
#include
#include
#include
#include
#include


/* 创建快速互斥锁 */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* 待观察计数变量 */
int lock_var;
timer_t end_time;


/* 线程一 */
void thread1(void)
{
int i = 0;
while(time(NULL) < end_time)
{
if(pthread_mutex_lock(&mutex)!=0)
{
perror("pthread_mutex_lock");
}
else
{
printf("pthread1:pthread1 lock the variable\n");
for(i =0;i < 2;i++)
{
sleep(1);
lock_var++;
}
}
/* 互斥锁解锁 */
if(pthread_mutex_unlock(&mutex)!=0)
{
perror("pthread_mutex_unlock");
}
else
{
printf("pthread1:pthread1 unlock the variable\n");
sleep(1);
}
}
}
/* 线程二 */
void thread2(void)
{
int ret;
while(time(NULL) < end_time)
{
/* 判断是否已经被上锁 */
ret = pthread_mutex_trylock(&mutex);
if(ret==EBUSY)
{
/* 忙 */
printf("pthread2:the variable is lock by pthread1\n");
}
else
{
/* 不忙 */
if(ret!=0)
{
perror("pthread_mutex_trylock");
exit(1);
}
else
{
printf("pthread2:pthread2 got lock. The variable is %d\n",lock_var);
}
/* 互斥锁解锁 */
if(pthread_mutex_unlock(&mutex)!=0)
{
perror("pthread_mutex_unlock");
}
else
{
printf("pthread2:pthread2 unlock the variable\n");

}
}
sleep(3);
}
}
int main()
{
pthread_t id1,id2;
int ret;
end_time = time(NULL) + 10;

/* 快速互斥锁的初始化 */
pthread_mutex_init(&mutex,NULL);
/* 分别创建线程1、2 */
ret = pthread_create(&id1,NULL,(void *)thread1,NULL);
if(ret != 0)
{
printf("Create pthread1 error\n");
exit(1);
}
ret = pthread_create(&id2,NULL,(void *)thread2,NULL);
if(ret != 0)
{
printf("Create pthread2 error\n");
exit(1);
}
/* 等待线程结束 */
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return 0;
}

++++++++++++++++++++++++++++
/* pthread.c */
#include
#include
#include
#include


/* 线程一 */
void thread1(void)
{
int i = 0;
while(i++ < 6)
{
printf("This is pthread1.\n");
if(i == 2)
pthread_exit(0);
sleep(2);
}
}
/* 线程二 */
void thread2(void)
{
int i = 0;
while(i++ < 3)
{
printf("This is pthread2.\n");
}
sleep(1);
pthread_exit(0);
}
int main()
{
pthread_t id1,id2;
int ret;


pthread_attr_t attr;
/* 初始化线程 */
pthread_attr_init(&attr);
/* 设置线程绑定属性 */
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
/* 设置线程分离属性 */
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
/* 分别创建线程1、2 */
ret = pthread_create(&id1,&attr,(void *)thread1,NULL);
if(ret != 0)
{
printf("Create pthread1 error\n");
exit(1);
}
ret = pthread_create(&id2,NULL,(void *)thread2,NULL);
if(ret != 0)
{
printf("Create pthread2 error\n");
exit(1);
}
/* 等待线程结束 */
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return 0;
}

+++++++++++++++++++++++++
/* sem.c */
#include
#include
#include
#include
#include
#include


/* 定义一个信号量 */
sem_t sem;
/* 待观察计数变量 */
int lock_var;
timer_t end_time;


/* 线程一 */
void thread1(void)
{
int i = 0;
while(time(NULL) < end_time)
{
/* P操作,使得sem减一 */
sem_wait(&sem);
for(i =0;i < 2;i++)
{
sleep(1);
lock_var++;
printf("lock_var = %d\n",lock_var);
}
printf("pthread1:lock_var = %d\n",lock_var);
/* V操作,使得sem加一 */
sem_post(&sem);
sleep(1);
}
}
/* 线程二 */
void thread2(void)
{
int ret;
while(time(NULL) < end_time)
{
/* P操作,使得sem减一 */
sem_wait(&sem);
printf("pthread2:pthread2 got lock;lock_var = %d\n",lock_var);
/* V操作,使得sem加一 */
sem_post(&sem);
sleep(3);
}
}
int main()
{
pthread_t id1,id2;
int ret;
end_time = time(NULL) + 10;

/* 初始化信号量为1 */
ret = sem_init(&sem,0,1);
if(ret != 0)
{
printf("sem_init error\n");
exit(1);
}
/* 分别创建线程1、2 */
ret = pthread_create(&id1,NULL,(void *)thread1,NULL);
if(ret != 0)
{
printf("Create pthread1 error\n");
exit(1);
}
ret = pthread_create(&id2,NULL,(void *)thread2,NULL);
if(ret != 0)
{
printf("Create pthread2 error\n");
exit(1);
}
/* 等待线程结束 */
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return 0;
}

+++++++++++++++++++++++++++++++++++++++++++
/* thread.c */
#include
#include
#include
#include


/* 线程一 */
void thread1(void)
{
int i = 0;
while(i++ < 3)
{
printf("This is pthread1.\n");
if(i == 2)
pthread_exit(0);
sleep(2);
}
}
/* 线程二 */
void thread2(void)
{
int i = 0;
while(i++ < 3)
{
printf("This is pthread2.\n");
}
pthread_exit(0);
}
int main()
{
pthread_t id1,id2;
int ret;
/* 分别创建线程1、2 */
ret = pthread_create(&id1,NULL,(void *)thread1,NULL);
if(ret != 0)
{
printf("Create pthread1 error\n");
exit(1);
}
ret = pthread_create(&id2,NULL,(void *)thread2,NULL);
if(ret != 0)
{
printf("Create pthread2 error\n");
exit(1);
}
/* 等待线程结束 */
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return 0;
}


阅读(2512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~