Linux网络编程:生产者消费者问题
Linux网络编程:生产者消费者问题
Linux网络编程:生产者消费者问题
- /*************************************************
- * File name :
- * Description :
- * Author : sg131971@qq.com
- * Version : V1.0
- * Date :
- * Compiler : arm-linux-gcc-4.4.3
- * Target : mini2440(Linux-2.6.32)
- * History :
- * <author> <time> <version > <desc>
- *************************************************/
- #include <stdio.h>
- #include <pthread.h>
- #include <semaphore.h>
- void *producter_f(void *arg);
- void *consumer_f(void *arg);
- int buffer_has_item = 0;
- sem_t sem;
- int running = 1;
- /*************************************************
- * Function :
- * Description :
- * Calls :
- * Called By :
- * Input :
- * Output :
- * Return :
- *************************************************/
- int main(void)
- {
- pthread_t consumer_t;
- pthread_t producter_t;
- sem_init(&sem, 0, 16);
- pthread_create(&producter_t, NULL, (void *)producter_f, NULL);
- pthread_create(&consumer_t, NULL, (void *)consumer_f, NULL);
- sleep(1);
- running = 0;
- pthread_join(consumer_t, NULL);
- pthread_join(producter_t, NULL);
- sem_destroy(&sem);
- return 0;
- }
- /*************************************************
- * Function :
- * Description :
- * Calls :
- * Called By : main
- * Input :
- * Output :
- * Return :
- *************************************************/
- void *producter_f(void *arg)
- {
- int semval = 0;
- while (running)
- {
- usleep(10);
- sem_post(&sem);
- sem_getvalue(&sem, &semval);
- printf("生产,总数量:%d\n", semval);
- }
- }
- /*************************************************
- * Function :
- * Description :
- * Calls :
- * Called By : main
- * Input :
- * Output :
- * Return :
- *************************************************/
- void *consumer_f(void *arg)
- {
- int semval = 0;
- while (running)
- {
- usleep(1);
- sem_wait(&sem);
- sem_getvalue(&sem, &semval);
- printf("消费,总数量:%d\n", semval);
- }
- }
评论暂时关闭