Usage sample of unix semaphore
2010-01-10 14:04
393 查看
There are two types of semaphore on unix system:
-- System V信号量(命名信号量)
#include <sys/sem.h>
int semctl(int sem_id, int sem_num, int command, ...);
int semget(key_t key, int num_sems, int sem_flags);
int semop(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops);
-- POSIX无名信号量
int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_wait(sem_t * sem);
int sem_trywait(sem_t * sem);
int sem_post(sem_t * sem);
int sem_getvalue(sem_t * sem, int * sval);
int sem_destroy(sem_t * sem);
无名信号量不能用在进程间通信, 命名信号量可以用在进程间通信。
-- Sample program of POXIS semaphore --
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
int data;
sem_t sem;
void *HandleDataWrite(void *args)
{
while(true)
{
sleep(2);
sem_post(&sem);
data=100;
}
return NULL;
}
void *HandleDataRead(void *args)
{
while(true)
{
sleep(3);
sem_wait(&sem);
printf("Data=%d/n",data);
}
return NULL;
}
int main(void){
pthread_t tread,twrite;
sem_init(&sem,true,0);
pthread_create(&tread,NULL,HandleDataRead,NULL);
pthread_create(&twrite,NULL,HandleDataWrite,NULL);
pthread_join(tread,NULL);
pthread_join(twrite,NULL);
}
-- System V信号量(命名信号量)
#include <sys/sem.h>
int semctl(int sem_id, int sem_num, int command, ...);
int semget(key_t key, int num_sems, int sem_flags);
int semop(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops);
-- POSIX无名信号量
int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_wait(sem_t * sem);
int sem_trywait(sem_t * sem);
int sem_post(sem_t * sem);
int sem_getvalue(sem_t * sem, int * sval);
int sem_destroy(sem_t * sem);
无名信号量不能用在进程间通信, 命名信号量可以用在进程间通信。
-- Sample program of POXIS semaphore --
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
int data;
sem_t sem;
void *HandleDataWrite(void *args)
{
while(true)
{
sleep(2);
sem_post(&sem);
data=100;
}
return NULL;
}
void *HandleDataRead(void *args)
{
while(true)
{
sleep(3);
sem_wait(&sem);
printf("Data=%d/n",data);
}
return NULL;
}
int main(void){
pthread_t tread,twrite;
sem_init(&sem,true,0);
pthread_create(&tread,NULL,HandleDataRead,NULL);
pthread_create(&twrite,NULL,HandleDataWrite,NULL);
pthread_join(tread,NULL);
pthread_join(twrite,NULL);
}
相关文章推荐
- Usage sample of unix mutex and conditional
- Usage sample of unix Message Queues
- Usage sample of unix pipe
- Usage sample of unix signal to handle function call with a timeout value.
- Usage sample of SSL
- C#: the sample usage of c# template
- (OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitte
- Usage of AWR And ASH
- 内存泄露 Analyzing the memory usage of your Android application
- curl tutorial with examples of usage
- simple usage of tcpdump
- The design of Unix Operating System (1) overview of System V
- UNIX tips: Learn 10 good UNIX usage habits
- Overall Process of Cobble Stone Crusher Usage
- The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)
- some usage of define
- #Sample of the Day 检查文件是否被使用
- Usage of Pseudocode
- Usage and Configuration of the Oracle shared Server
- Usage of the @ (at) sign in ASP.NET