linux下的信号量操作示例
2011-02-25 14:30
344 查看
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <pthread.h>
#include <errno.h>
#include <vector>
#include <iostream>
using namespace std;
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) */
};
//工作线程函数
void * workproc(void * param)
{
int sid = *(int *)param; //信号量ID
pthread_t tid = pthread_self(); //线程ID
cout <<"thread " <<tid <<"/t opt 1 is ok!" <<endl; //第一步操作执行完成
//信号量减1
sembuf b;
b.sem_num = 0;
b.sem_op = -1;
b.sem_flg = 0;
int ret = semop(sid, &b, 1);
if (ret != 0)
return (void *)-1;
//等待信号量变成0
cout <<"thread " <<tid <<"/t begin wait for all thread ok!" <<endl;
b.sem_op = 0;
ret = semop(sid, &b, 1);
if (ret != 0)
return (void *)-1;
cout <<"thread " <<tid <<"/t end wait for all thread ok!" <<endl;
//执行第二步操作
cout <<"thread " <<tid <<"/t opt 2 is ok!" <<endl;
return 0;
}
int main()
{
//创建信号量
int sid = semget(IPC_PRIVATE, 1, IPC_CREAT|0660);
if (sid == -1)
{
cout <<"create sem error!" <<endl;
return -1;
}
//设置信号量的初始值为5
semun un;
un.val = 5;
int ret = semctl(sid, 0, SETVAL, un);
if (ret == -1)
return -1;
//启动一组工作线程
vector<pthread_t> ids; //保存启动的线程ID数组
pthread_t id;
for (int i = 0; i < 5; ++i)
{
pthread_create(&id, NULL, workproc, &sid);
ids.push_back(id);
}
//等待所有的工作线程结束
int num = ids.size();
for (int i = 0; i < num ; ++i)
{
pthread_join(ids[i], NULL);
}
//删除信号量
semctl(sid, 1, IPC_RMID);
return 1;
}
#include <sys/ipc.h>
#include <sys/sem.h>
#include <pthread.h>
#include <errno.h>
#include <vector>
#include <iostream>
using namespace std;
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) */
};
//工作线程函数
void * workproc(void * param)
{
int sid = *(int *)param; //信号量ID
pthread_t tid = pthread_self(); //线程ID
cout <<"thread " <<tid <<"/t opt 1 is ok!" <<endl; //第一步操作执行完成
//信号量减1
sembuf b;
b.sem_num = 0;
b.sem_op = -1;
b.sem_flg = 0;
int ret = semop(sid, &b, 1);
if (ret != 0)
return (void *)-1;
//等待信号量变成0
cout <<"thread " <<tid <<"/t begin wait for all thread ok!" <<endl;
b.sem_op = 0;
ret = semop(sid, &b, 1);
if (ret != 0)
return (void *)-1;
cout <<"thread " <<tid <<"/t end wait for all thread ok!" <<endl;
//执行第二步操作
cout <<"thread " <<tid <<"/t opt 2 is ok!" <<endl;
return 0;
}
int main()
{
//创建信号量
int sid = semget(IPC_PRIVATE, 1, IPC_CREAT|0660);
if (sid == -1)
{
cout <<"create sem error!" <<endl;
return -1;
}
//设置信号量的初始值为5
semun un;
un.val = 5;
int ret = semctl(sid, 0, SETVAL, un);
if (ret == -1)
return -1;
//启动一组工作线程
vector<pthread_t> ids; //保存启动的线程ID数组
pthread_t id;
for (int i = 0; i < 5; ++i)
{
pthread_create(&id, NULL, workproc, &sid);
ids.push_back(id);
}
//等待所有的工作线程结束
int num = ids.size();
for (int i = 0; i < num ; ++i)
{
pthread_join(ids[i], NULL);
}
//删除信号量
semctl(sid, 1, IPC_RMID);
return 1;
}
相关文章推荐
- 操作系统中关于信号量操作的代码示例(Linux + windows)
- 操作系统中关于信号量操作的代码示例(Linux + windows)
- linux互斥操作——信号量
- 在Linux下信号量的semop操作的sembu结构体中sem_flag的设置值的含义
- 在Linux下编译Google leveldb数据库及在C++中操作示例 标签: linux数据库googlec++makefile浏览器 2012-03-14 09:15 955人阅读 评论(0)
- Linux下基于POSIX标准的共享内存操作示例
- Linux下基于POSIX标准的共享内存操作示例
- linux下posix有名信号量的简单使用示例
- 关于操作系统里面的P-V操作的信号量跟Linux下的无名信号量的区别!
- linux 中信号量 down_interruptible 和 down操作
- Linux下连接MySQL数据库的操作示例
- 在Linux下编译Google leveldb数据库及在C++中操作示例
- 在Linux下编译Google leveldb数据库及在C++中操作示例
- Linux下基于POSIX标准的共享内存操作示例
- Linux route命令详解和使用示例(查看和操作IP路由表)
- <linux线程>POSIX无名信号量的基本操作---两个线程间的通信
- Linux route命令详解和使用示例(查看和操作IP路由表)
- Linux文件操作函数open close read write等示例
- Linux下基于POSIX标准的共享内存操作示例
- <linux进程>system-V信号量的基本操作