我是靠谱客的博主 俊逸白昼,最近开发中收集的这篇文章主要介绍linux下生产者与消费者问题代码,以及编译c代码时error:undefined reference to sem_wait 解决方法之一,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

//本文的详细讲解内容请大家下载word文档:http://download.csdn.net/detail/chenqiai0/4611801
#include <stdio.h>
#include <pthread.h>//线程
#include <semaphore.h>//因为用到了sem_init等
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int buffer[10]={0,0,0,0,0,0,0,0,0,0};
int i=0;
int j=0;
//信号量的数据类型为结构sem_t,它本质上是一个长整型的数
sem_t full;//信号量 
sem_t empty;
pthread_mutex_t mutex;//pthread_mutex_t是一个结构,是互斥锁


void producer(void)
{ 
  int m;
  while(i<10)
  {
   pthread_mutex_lock(&mutex);//声明开始用互斥锁上锁
   buffer[i]=i+1;
   i=i+1;
   printf("nthere are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9n");
   printf("nbuffer:");
   for(m=0;m<10;m++)
      printf("%3d",buffer[m]);
   printf("nthe number added by the producer is:");
   printf("%d", buffer[i]);
   printf("npointer is %d",i);
   sem_post(&full);//   int sem_post(sem_t * sem);------sem_post函数的作用是给信号量的值加上一个“1”,它是一个“原子操作"即同时对同一个信号量做加“1”操作的两个线程是不会冲突的
   pthread_mutex_unlock(&mutex);//声明开始用互斥锁解锁
  }
}

void consumer(void)
{ 
  int m;
  while(j<10)
  {
   int x;
   pthread_mutex_lock(&mutex);
   sem_wait(&full);// sem_wait函数也是一个原子操作,它的作用是从信号量的值减去一个“1”,但它永远会先等待该信号量为一个非零值才开始做减法。
   if(buffer[j]==0)
     break;
   x=buffer[j];
   buffer[j]=0;
   j=j+1;
   printf("nbuffer:");
   for(m=0;m<10;m++)
     printf("%3d",buffer[m]);
   printf("n %d is removed form the buffer by consumer",x);
   printf("nThe present pointer is %d",j);
   pthread_mutex_unlock(&mutex);
   sem_post(&empty);
  }
}


int main()
{
  pthread_t thread1,thread2,thread3,thread4;
  sem_init(&full,0,0);//int sem_init(sem_t *sem, int pshared, unsigned int value)---------sem_init() 初始化一个定位在 sem 的匿名信号量。value 参数指定信号量的初始值。 pshared 参数指明信号量是由进程内线程共享,还是由进程之间共享。如果 pshared 的值为 0,那么信号量将被进程内的线程共享,并且应该放置在所有线程都可见的地址上
  sem_init(&empty,0,10);
  pthread_create(&thread1,NULL,(void *)producer,NULL);//函数pthread_create用来创建一个线程
  pthread_create(&thread2,NULL,(void *)consumer,NULL);//函数pthread_join用来等待一个线程的结束
  pthread_join(thread1,NULL);
  pthread_join(thread2,NULL);
}



当我们在终端输入:gcc -o producer_consumer.out producer_consumer.c

就会出现错误:

producer_consumer.c:(.text+0xc9): undefined reference to `sem_post'
/tmp/ccvFyPLL.o: In function `consumer':
producer_consumer.c:(.text+0x108): undefined reference to `sem_wait'
producer_consumer.c:(.text+0x1c6): undefined reference to `sem_post'
/tmp/ccvFyPLL.o: In function `main':
producer_consumer.c:(.text+0x1fe): undefined reference to `sem_init'
producer_consumer.c:(.text+0x21a): undefined reference to `sem_init'
producer_consumer.c:(.text+0x23f): undefined reference to `pthread_create'
producer_consumer.c:(.text+0x264): undefined reference to `pthread_create'
producer_consumer.c:(.text+0x278): undefined reference to `pthread_join'
producer_consumer.c:(.text+0x28c): undefined reference to `pthread_join'

这个是因为pthread并非Linux系统的默认库,编译时注意加上-lpthread参数,以调用链接库

我们再一次在终端输入:gcc -o producer_consumer.out producer_consumer.c -lpthread

此时编译正确

执行:在终端输入:./producer_consumer.out

输出为:

there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  0  0  0  0  0  0  0  0  0
the number added by the producer is:0
pointer is 1
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  0  0  0  0  0  0  0  0
the number added by the producer is:0
pointer is 2
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  0  0  0  0  0  0  0
the number added by the producer is:0
pointer is 3
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  0  0  0  0  0  0
the number added by the producer is:0
pointer is 4
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  0  0  0  0  0
the number added by the producer is:0
pointer is 5
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  0  0  0  0
the number added by the producer is:0
pointer is 6
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  0  0  0
the number added by the producer is:0
pointer is 7
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  8  0  0
the number added by the producer is:0
pointer is 8
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  8  9  0
the number added by the producer is:0
pointer is 9
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  8  9 10
the number added by the producer is:10
pointer is 10
buffer:  0  2  3  4  5  6  7  8  9 10
 1 is removed form the buffer by consumer
The present pointer is 1
buffer:  0  0  3  4  5  6  7  8  9 10
 2 is removed form the buffer by consumer
The present pointer is 2
buffer:  0  0  0  4  5  6  7  8  9 10
 3 is removed form the buffer by consumer
The present pointer is 3
buffer:  0  0  0  0  5  6  7  8  9 10
 4 is removed form the buffer by consumer
The present pointer is 4
buffer:  0  0  0  0  0  6  7  8  9 10
 5 is removed form the buffer by consumer
The present pointer is 5
buffer:  0  0  0  0  0  0  7  8  9 10
 6 is removed form the buffer by consumer
The present pointer is 6
buffer:  0  0  0  0  0  0  0  8  9 10
 7 is removed form the buffer by consumer
The present pointer is 7
buffer:  0  0  0  0  0  0  0  0  9 10
 8 is removed form the buffer by consumer
The present pointer is 8
buffer:  0  0  0  0  0  0  0  0  0 10
 9 is removed form the buffer by consumer
The present pointer is 9
buffer:  0  0  0  0  0  0  0  0  0  0
 10 is removed form the buffer by consumer

最后

以上就是俊逸白昼为你收集整理的linux下生产者与消费者问题代码,以及编译c代码时error:undefined reference to sem_wait 解决方法之一的全部内容,希望文章能够帮你解决linux下生产者与消费者问题代码,以及编译c代码时error:undefined reference to sem_wait 解决方法之一所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部