mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-07-08 13:56:08 +08:00
完成了第 18 章 多线程服务器端的实现
This commit is contained in:
52
ch18/semaphore.c
Normal file
52
ch18/semaphore.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
void *read(void *arg);
|
||||
void *accu(void *arg);
|
||||
static sem_t sem_one;
|
||||
static sem_t sem_two;
|
||||
static int num;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
pthread_t id_t1, id_t2;
|
||||
sem_init(&sem_one, 0, 0);
|
||||
sem_init(&sem_two, 0, 1);
|
||||
|
||||
pthread_create(&id_t1, NULL, read, NULL);
|
||||
pthread_create(&id_t2, NULL, accu, NULL);
|
||||
|
||||
pthread_join(id_t1, NULL);
|
||||
pthread_join(id_t2, NULL);
|
||||
|
||||
sem_destroy(&sem_one);
|
||||
sem_destroy(&sem_two);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *read(void *arg)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
fputs("Input num: ", stdout);
|
||||
|
||||
sem_wait(&sem_two);
|
||||
scanf("%d", &num);
|
||||
sem_post(&sem_one);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
void *accu(void *arg)
|
||||
{
|
||||
int sum = 0, i;
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
sem_wait(&sem_one);
|
||||
sum += num;
|
||||
sem_post(&sem_two);
|
||||
}
|
||||
printf("Result: %d \n", sum);
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user