20 semaphore 2
2017-01-24 14:47
405 查看
1. 示例一:线程使用信号量
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
#include "semaphore.h"
执行结果:
alex@alex-VirtualBox:~/Share/process/twenty$ gcc thread.c -lpthread
alex@alex-VirtualBox:~/Share/process/twenty$ ./a.out
this is main fun i=0
this is main fun i=1
this is main fun i=2
this is main fun i=3
this is main fun i=4
this is main fun i=5
this is main fun i=6
this is main fun i=7
this is main fun i=8
this is main fun i=9
this is fun j=0
this is fun j=1
this is fun j=2
this is fun j=3
this is fun j=4
this is fun j=5
this is fun j=6
this is fun j=7
this is fun j=8
this is fun j=9
2. 示例二,线程使用信号灯:
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
//#include "semaphore.h"
#include "sys/ipc.h"
#include "sys/sem.h"
//sem_t sem;
union semun
{
int val; /* Value for SETVAL*/
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};
int semid;
union semun mysemun;
struct sembuf mysembuf;
void *fun(void *var)//child thread code
{
int j;
//p wait
// sem_wait(&sem); //sleep
mysembuf.sem_op=-1;
semop(semid,&mysembuf,1);
for(j=0;j<10;j++)//second
{
usleep(100);
printf("this is fun j=%d\n",j);
}
}
int main()//main thread code
{
int i;
char str[]="hello linux\n";
pthread_t tid;
int ret;
semid=semget(IPC_PRIVATE,3,0777);
if(semid < 0)
{
printf("creat semaphore failure\n");
return -1;
}
printf("creat semaphore sucess,semid=%d\n",semid);
system("ipcs -s");
mysemun.val=0;
semctl(semid,0,SETVAL,mysemun);
//sem_init(&sem,0,0);//sem=0
mysembuf.sem_num=0;
mysembuf.sem_flg=0;
ret=pthread_create(&tid,NULL,fun,(void *)str);
if(ret<0)
{
printf("creat thread failure\n");
return -1;
}
for(i=0;i<10;i++)//first
{
usleep(100);
printf("this is main fun i=%d\n",i);
}
//v
// sem_post(&sem);
mysembuf.sem_op=1;
semop(semid,&mysembuf,1);
while(1);
return 0;
}
执行结果:
alex@alex-VirtualBox:~/Share/process/twenty$ gcc sem.c -lpthread
alex@alex-VirtualBox:~/Share/process/twenty$ ./a.out
creat semaphore sucess,semid=32768
------ Semaphore Arrays --------
key semid owner perms nsems
0x00000000 32768 alex 777 3
this is main fun i=0
this is main fun i=1
this is main fun i=2
this is main fun i=3
this is main fun i=4
this is main fun i=5
this is main fun i=6
this is main fun i=7
this is main fun i=8
this is main fun i=9
this is fun j=0
this is fun j=1
this is fun j=2
this is fun j=3
this is fun j=4
this is fun j=5
this is fun j=6
this is fun j=7
this is fun j=8
this is fun j=9
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
#include "semaphore.h"
#include "unistd.h"
sem_t sem; void *fun(void *var)//child thread code { int j; //p wait sem_wait(&sem); //sleep for(j=0;j<10;j++)//second { usleep(100); printf("this is fun j=%d\n",j); } } int main()//main thread code { int i; char str[]="hello linux\n"; pthread_t tid; int ret; sem_init(&sem,0,0);//sem=0 ret=pthread_create(&tid,NULL,fun,(void *)str); if(ret<0) { printf("creat thread failure\n"); return -1; } for(i=0;i<10;i++)//first { usleep(100); printf("this is main fun i=%d\n",i); } //v sem_post(&sem); while(1); return 0; }
执行结果:
alex@alex-VirtualBox:~/Share/process/twenty$ gcc thread.c -lpthread
alex@alex-VirtualBox:~/Share/process/twenty$ ./a.out
this is main fun i=0
this is main fun i=1
this is main fun i=2
this is main fun i=3
this is main fun i=4
this is main fun i=5
this is main fun i=6
this is main fun i=7
this is main fun i=8
this is main fun i=9
this is fun j=0
this is fun j=1
this is fun j=2
this is fun j=3
this is fun j=4
this is fun j=5
this is fun j=6
this is fun j=7
this is fun j=8
this is fun j=9
2. 示例二,线程使用信号灯:
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
//#include "semaphore.h"
#include "sys/ipc.h"
#include "sys/sem.h"
//sem_t sem;
union semun
{
int val; /* Value for SETVAL*/
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};
int semid;
union semun mysemun;
struct sembuf mysembuf;
void *fun(void *var)//child thread code
{
int j;
//p wait
// sem_wait(&sem); //sleep
mysembuf.sem_op=-1;
semop(semid,&mysembuf,1);
for(j=0;j<10;j++)//second
{
usleep(100);
printf("this is fun j=%d\n",j);
}
}
int main()//main thread code
{
int i;
char str[]="hello linux\n";
pthread_t tid;
int ret;
semid=semget(IPC_PRIVATE,3,0777);
if(semid < 0)
{
printf("creat semaphore failure\n");
return -1;
}
printf("creat semaphore sucess,semid=%d\n",semid);
system("ipcs -s");
mysemun.val=0;
semctl(semid,0,SETVAL,mysemun);
//sem_init(&sem,0,0);//sem=0
mysembuf.sem_num=0;
mysembuf.sem_flg=0;
ret=pthread_create(&tid,NULL,fun,(void *)str);
if(ret<0)
{
printf("creat thread failure\n");
return -1;
}
for(i=0;i<10;i++)//first
{
usleep(100);
printf("this is main fun i=%d\n",i);
}
//v
// sem_post(&sem);
mysembuf.sem_op=1;
semop(semid,&mysembuf,1);
while(1);
return 0;
}
执行结果:
alex@alex-VirtualBox:~/Share/process/twenty$ gcc sem.c -lpthread
alex@alex-VirtualBox:~/Share/process/twenty$ ./a.out
creat semaphore sucess,semid=32768
------ Semaphore Arrays --------
key semid owner perms nsems
0x00000000 32768 alex 777 3
this is main fun i=0
this is main fun i=1
this is main fun i=2
this is main fun i=3
this is main fun i=4
this is main fun i=5
this is main fun i=6
this is main fun i=7
this is main fun i=8
this is main fun i=9
this is fun j=0
this is fun j=1
this is fun j=2
this is fun j=3
this is fun j=4
this is fun j=5
this is fun j=6
this is fun j=7
this is fun j=8
this is fun j=9
相关文章推荐
- Android中ContextImpl源码分析(二)
- Android canvas drawBitmap方法详解及实例
- ubuntu14.04 安装 paramkio
- 将C#文档注释生成.chm帮助文档
- PHP基础之运算符(四)连接运算符、赋值运算符、三元表达式
- PhotoModeler Scanner教程
- ArcGIS ArcMap 问题(ArcMap闪退、cx_oracle安装不上)
- springmvc和mybatis整合-查询用户列表
- [CF Gym 100372B] Sergey and a pattern
- 用友善提供的4.5.1解压后,运行/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc -v出错
- Vert.x开发指南
- [爆栈热门 iOS 问题] 让 UILabel 顶端对齐
- STM32开源板全屏图片提速例程/RA8875/SPIFLASH/DMA
- jpa
- ld
- nginx安装与配置2(转载)
- 零基础入门深度学习(1):感知器,激活函数
- PHP基础之运算符(三)位逻辑运算符、位移运算符
- 好文链接转载收藏:(1)筷子定律:什么叫融会贯通?(2)周鸿祎:如何做好产品经理?(3)逃出你的肖申克?
- nodejs路由模块使用