深入解释之前可以先看相应的笔记理解→【操作系统】第二章–进程的描述与控制–笔记与理解(2)
// 核心代码
void *producer(void *p) {
while(1) {
sem_wait(&space);
printf("Put a product\n");
sem_post(&prod);
}
return NULL;
}
void *consumer(void *p) {
while(1) {
sem_wait(&prod);
printf("Get a product\n");
sem_post(&space);
}
return NULL;
}
// 完整代码
#include
#include
#include
#include
#include
sem_t space,prod;
void *producer(void *p) {
while(1) {
sem_wait(&space);
printf("Put a product\n");
sem_post(&prod);
}
return NULL;
}
void *consumer(void *p) {
while(1) {
sem_wait(&prod);
printf("Get a product\n");
sem_post(&space);
}
return NULL;
}
int main(void) {
sem_init(&space,0,1);
sem_init(&prod,0,0);
pthread_t tid[2];
pthread_create(&tid[0],NULL,producer,NULL);
pthread_create(&tid[1],NULL,consumer,NULL);
sem_destroy(&space);
sem_destroy(&prod);
pthread_join(tid[0],NULL);
return 0;
}
// 核心代码
void *producer(void *p) {
while(1) {
sem_wait(&space);
printf("Put a product into Buffer[%d]\n",in);
in = (in+1)%N;
sem_post(&prod);
}
return NULL;
}
void *consumer(void *p) {
while(1) {
sem_wait(&prod);
printf("Get a product from Buffer[%d]\n",out);
out = (out+1)%N;
sem_post(&space);
}
return NULL;
}
// 完整代码
#include
#include
#include
#include
#include
#define N 10
sem_t space,prod;
int BUffer[N];
int in=0,out=0;
void *producer(void *p) {
while(1) {
sem_wait(&space);
printf("Put a product into Buffer[%d]\n",in);
in = (in+1)%N;
sem_post(&prod);
}
return NULL;
}
void *consumer(void *p) {
while(1) {
sem_wait(&prod);
printf("Get a product from Buffer[%d]\n",out);
out = (out+1)%N;
sem_post(&space);
}
return NULL;
}
int main(void) {
sem_init(&space,0,N);
sem_init(&prod,0,0);
pthread_t tid[2];
pthread_create(&tid[0],NULL,producer,NULL);
pthread_create(&tid[1],NULL,consumer,NULL);
sem_destroy(&space);
sem_destroy(&prod);
pthread_join(tid[0],NULL);
return 0;
}
// 核心代码
void *producer(void *p) {
while(1) {
sem_wait(&space);
sem_wait(&buf);
printf("Put a product into Buffer[%d]\n",in);
in = (in+1)%N;
sem_post(&prod);
sem_post(&buf);
}
return NULL;
}
void *consumer(void *p) {
while(1) {
sem_wait(&prod);
sem_wait(&buf);
printf("Get a product from Buffer[%d]\n",out);
out = (out+1)%N;
sem_post(&space);
sem_post(&buf);
}
return NULL;
}
// 完整代码
#include
#include
#include
#include
#include
#define M 8
#define N 10
sem_t space,prod,buf;
int Buffer[N];
int in = 0, out = 0;
void *producer(void *p) {
while(1) {
sem_wait(&space);
sem_wait(&buf);
printf("Put a product into Buffer[%d]\n",in);
in = (in+1)%N;
sem_post(&prod);
sem_post(&buf);
}
return NULL;
}
void *consumer(void *p) {
while(1) {
sem_wait(&prod);
sem_wait(&buf);
printf("Get a product from Buffer[%d]\n",out);
out = (out+1)%N;
sem_post(&space);
sem_post(&buf);
}
return NULL;
}
int main(void) {
sem_init(&space,0,N);
sem_init(&prod,0,0);
sem_init(&buf,0,1);
pthread_t tid[M],tid2[M];
for(int i = 0; i < M; i ++) {
pthread_create(&tid[i],NULL,producer,NULL);
}
for(int i = 0; i < M; i ++) {
pthread_create(&tid2[i],NULL,consumer,NULL);
}
sem_destroy(&space);
sem_destroy(&prod);
sem_destroy(&buf);
pthread_join(tid[0],NULL);
return 0;
}
// 核心代码
void *reader(void *p) {
sem_wait(&srcount);
readcount++;
if(readcount == 1) {
sem_wait(&sdata);
}
sem_post(&srcount);
printf("Reading..\n");
sem_wait(&srcount);
readcount--;
if(readcount == 0) {
sem_post(&sdata);
}
sem_post(&srcount);
return NULL;
}
void *writer(void *p) {
sem_wait(&sdata);
printf("Writing..\n");
sem_post(&sdata);
return NULL;
}
// 完整代码
#include
#include
#include
#include
#include
#define M 10
sem_t sdata,srcount;
int readcount = 0;
void *reader(void *p) {
sem_wait(&srcount);
readcount++;
if(readcount == 1) {
sem_wait(&sdata);
}
sem_post(&srcount);
printf("Reading..\n");
sem_wait(&srcount);
readcount--;
if(readcount == 0) {
sem_post(&sdata);
}
sem_post(&srcount);
return NULL;
}
void *writer(void *p) {
sem_wait(&sdata);
printf("Writing..\n");
sem_post(&sdata);
return NULL;
}
int main(void) {
sem_init(&sdata,0,1);
sem_init(&srcount,0,1);
pthread_t tid[M],tid2[M];
for(int i = 0; i < M; i ++) {
pthread_create(&tid[i],NULL,writer,NULL);
}
for(int i = 0; i < M; i ++) {
pthread_create(&tid2[i],NULL,reader,NULL);
}
sem_destroy(&sdata);
sem_destroy(&srcount);
pthread_join(tid[0],NULL);
return 0;
}
// 核心代码
void *reader(void *p) {
sem_wait(&sdata);
sem_wait(&srcount);
sem_post(&sdata);
printf("Reading..\n");
sem_post(&srcount);
return NULL;
}
void *writer(void *p) {
sem_wait(&sdata);
for(int i = 0; i < N; i++) {
sem_wait(&srcount);
}
printf("Writing..\n");
for(int i = 0; i < N; i++) {
sem_post(&srcount);
}
sem_post(&sdata);
return NULL;
}
// 完整代码
#include
#include
#include
#include
#include
#define M 10
sem_t sdata,srcount;
int readcount = 0;
void *reader(void *p) {
sem_wait(&srcount);
readcount++;
if(readcount == 1) {
sem_wait(&sdata);
}
sem_post(&srcount);
printf("Reading..\n");
sem_wait(&srcount);
readcount--;
if(readcount == 0) {
sem_post(&sdata);
}
sem_post(&srcount);
return NULL;
}
void *writer(void *p) {
sem_wait(&sdata);
printf("Writing..\n");
sem_post(&sdata);
return NULL;
}
int main(void) {
sem_init(&sdata,0,1);
sem_init(&srcount,0,1);
pthread_t tid[M],tid2[M];
for(int i = 0; i < M; i ++) {
pthread_create(&tid[i],NULL,writer,NULL);
}
for(int i = 0; i < M; i ++) {
pthread_create(&tid2[i],NULL,reader,NULL);
}
sem_destroy(&sdata);
sem_destroy(&srcount);
pthread_join(tid[0],NULL);
return 0;
}
#include
#include
#include
sem_t sfork[5];
void *philosopher(void*p) {
int id=(int)p;
while(1){
printf("Thinking...\n");
sem_wait(&sfork[id]);
sem_wait(&sfork[(id+1)%5]);
printf("Eating...\n");
sem_post(&sfork[id]);
sem_post(&sfork[(id+1)%5]);
}
return NULL;
}
int main() {
for(int i=0;i<5;i++)
sem_init(&sfork[i],0,1);
pthread_t tid[5]];
for(int i=0;i<5;i++)
pthread_create(&tid[i],NULL,philosopher,(void*)i);
for(int i=0;i<5;i++)
sem_destroy(&sfork);
pthread_join(tid[0],NULL);
}
#include
#include
#include
sem_t sfork[5];
void *philosopher(void*p) {
int id=(int)p;
while(1){
printf("Thinking...\n");
if(id%2==0){
sem_wait(&sfork[id]);
sem_wait(&sfork[(id+1)%5]);
}
else{
sem_wait(&sfork[(id+1)%5]);
sem_wait(&sfork[id]);
}
printf("Eating...\n");
sem_post(&sfork[id]);
sem_post(&sfork[(id+1)%5]);
}
return NULL;
}
int main() {
for(int i=0;i<5;i++)
sem_init(&sfork[i],0,1);
pthread_t tid[5]];
for(inti=0;i<5;i++)
pthread_create(&tid[i],NULL,philosopher,(void*)i);
for(inti=0;i<5;i++)
sem_destroy(&sfork);
pthread_join(tid[0],NULL);
}
#include
#include
#include
sem_t sfork[5];
void *philosopher(void*p) {
int id=(int)p;
while(1){
printf("Thinking...\n");
if(id%==4){
sem_wait(&sfork[0]);
sem_wait(&sfork[4]);
}
else{
sem_wait(&sfork[id]);
sem_wait(&sfork[id+1]);
}
printf("Eating...\n");
sem_post(&sfork[id]);
sem_post(&sfork[(id+1)%5]);
}
return NULL;
}
int main() {
for(int i=0;i<5;i++)
sem_init(&sfork[i],0,1);
pthread_ttid[5]];
for(int i=0;i<5;i++)
pthread_create(&tid[i],NULL,philosopher,(void*)i);
for(int i=0;i<5;i++)
sem_destroy(&sfork);
pthread_join(tid[0],NULL);
}