线程同步(P\V操作)
来源:互联网 发布:传说 狐狸 知乎 编辑:程序博客网 时间:2024/05/22 03:52
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define N 64
char buf[N];
sem_t get, put;
void * thread_routine(void *arg)
{
while (1)
{
sem_wait(&get);
sleep(5);
printf("%s", buf);
sem_post(&put);
}
}
int main()
{
pthread_t thread;
void *thread_result;
if (sem_init(&get, 0, 0) == -1)
{
perror("sem_init get");
exit(-1);
}
if (sem_init(&put, 0, 1) == -1)
{
perror("sem_init r");
exit(-1);
}
if (pthread_create(&thread, NULL, thread_routine, NULL) != 0)
{
fprintf(stderr, "pthread_create %s\n", strerror(errno));
exit(-1);
}
do
{
sem_wait(&put);
fgets(buf, N, stdin);
sem_post(&get);
} while (strncmp(buf, "quit", 4));
exit(0);
}
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define N 64
char buf[N];
sem_t get, put;
void * thread_routine(void *arg)
{
while (1)
{
sem_wait(&get);
sleep(5);
printf("%s", buf);
sem_post(&put);
}
}
int main()
{
pthread_t thread;
void *thread_result;
if (sem_init(&get, 0, 0) == -1)
{
perror("sem_init get");
exit(-1);
}
if (sem_init(&put, 0, 1) == -1)
{
perror("sem_init r");
exit(-1);
}
if (pthread_create(&thread, NULL, thread_routine, NULL) != 0)
{
fprintf(stderr, "pthread_create %s\n", strerror(errno));
exit(-1);
}
do
{
sem_wait(&put);
fgets(buf, N, stdin);
sem_post(&get);
} while (strncmp(buf, "quit", 4));
exit(0);
}
- 线程同步(P\V操作)
- 信号量同步-P V 操作
- P-V操作(信号量的操作)
- 进程间同步与互斥---信号量P,V操作
- 关于P.V.操作
- P、V操作
- linux p v 操作
- p.v操作
- P、V操作
- 信号量P/V操作
- P,V操作理解
- P V操作
- P,V操作
- P操作和V操作
- 进程同步---生产者与消费者问题以及进程同步机制--信号量及P、V操作
- P-V操作理解析疑
- 进程间通信——信号量(P、V操作)
- 信号量的P操作和V操作
- 更新系统时间 Linux
- java界面中如何得到字符串的高度和宽度
- Android中string.xml使用
- 游标的使用,非常好的实用例子
- 错误解决:The type [X] is not accessible due to restriction on required library [Y].
- 线程同步(P\V操作)
- .net程序员求职简历
- a5站长论坛和s8站长论坛-网上兼职做任务赚钱的两大网站
- CRecordset类
- linux 终端操作mysql的一些指令积累
- Function解题报告
- EXTJS4.x之实战项目(9)SectionContentGridPanel栏目内容管理页的实现
- UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)
- C、C++写二进制数据到mysql的Blob字段