//一个可以边欣赏边熟悉操作系统PV操作的代码
//如果不想浪费时间,仅看代码便能理解
//这是一个用于Unit/Linux的代码,不适合用于XP,如果有需要,可以进行修改
view plaincopy to clipboardprint?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t bin_sem;
void *thread_function1(void *arg)
{
printf("thread_function1--------------sem_wait/n");
sem_wait(&bin_sem);
printf("sem_wait/n");
while (1)
{
}
}
void *thread_function2(void *arg)
{
printf("thread_function2--------------sem_post/n");
sem_post(&bin_sem);
printf("sem_post/n");
while (1)
{
}
}
int main()
{
int res;
pthread_t a_thread;
void *thread_