我无法调试以下程序,因为我家里没有linux。我无法执行该程序,但在实验室会话中编译程序时,我遇到了两个错误。谁能帮我吗?如果有人能够在调试后执行该程序,请发布输出。
代码如下:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#define BUFFER_SIZE 10
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
int buffer[BUFFER_SIZE];
pthread_t ptid,ctid;
pthread_attr_t attr;
void *producer(void *param);
void *consumer(void *param);
int counter;
int main()
{
init();
pthread_create(&ptid, &attr, producer, NULL);
pthread_create(&ctid, &attr, consumer, NULL);
pthread_join(ptid,NULL);
pthread_join(ctid,NULL);
return 0;
}
void init()
{
pthread_mutex_init(&mutex, NULL);
pthread_attr_init(&attr);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
counter = 0;
for(int j=0;j<BUFFER_SIZE;j++)
{ buffer[j] = 0;}
}
void *producer(void *param)
{
int item;
while(1)
{
item=item+1;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
if(insert_item(item))
{
perror("Producer error.");
return 1;
}
else
{
printf("Producer produced %d\n", item);
}
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void *consumer(void *param)
{
int item;
while(1)
{
sleep(5);
sem_wait(&full);
pthread_mutex_lock(&mutex);
if(remove_item(item))
{
perror( "Consumer error.");
}
else
{
printf("Consumer consumed %d\n", item);
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int insert_item(int item)
{
if(counter < BUFFER_SIZE)
{
buffer[counter] = item;
counter++;
return 0;
}
else
return -1;
}
int remove_item(int item)
{
if(counter > 0)
{
item = buffer[(counter-1)];
counter--;
return ;
}
else
return -1;
}
进行更改和更正后,我现在得到以下输出,这不是所需的输出.......
[root@localhost sree58]# gcc producerconsumer.c -lpthread -lrt
producerconsumer.c: In function ‘producer’:
producerconsumer.c:54: warning: return makes pointer from integer without a cast
[root@localhost sree58]# ./a.out
Producer produced 1804289383
Producer produced 846930886
Producer produced 1681692777
Producer produced 1714636915
Producer produced 1957747793
Producer produced 424238335
Producer produced 719885386
Producer produced 1649760492
Producer produced 596516649
Producer produced 1189641421
Consumer error.: Success
Producer produced 1025202362
Consumer error.: Illegal seek
Producer produced 1350490027
Consumer error.: Illegal seek
Producer produced 783368690
Consumer error.: Illegal seek
Producer produced 1102520059
Consumer error.: Illegal seek
Producer produced 2044897763
Consumer error.: Illegal seek
Producer produced 1967513926
Consumer error.: Illegal seek
Producer produced 1365180540
Consumer error.: Illegal seek
Producer produced 1540383426
Consumer error.: Illegal seek
Producer produced 304089172
Consumer error.: Illegal seek
Producer produced 1303455736
Consumer error.: Illegal seek
Producer produced 35005211
Consumer error.: Illegal seek
Producer produced 521595368
Consumer error.: Illegal seek
Producer produced 294702567
Consumer error.: Illegal seek
Producer produced 1726956429
请某人指导我......
答案 0 :(得分:6)
首先,让我们解决这个问题(帮助自己的工具):since i don't have linux at home
转到Oracle的网站并下载VirtualBox,然后转到您最喜爱的Linux发行版的网站并下载.iso
图像(例如,如果您感觉Minty,您可以获得iso { {3}})
然后你可以按照说明在虚拟机中设置和“安装”iso瞧!你家里有Linux。 ;)
其次,我将解决这个问题:i got two errors
真的?你怎么编译这个?使用gcc -Wall
运行代码时,我看到一大堆警告和一个错误。一个错误告诉您如何在消息中修复它:
38:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
38:3: note: use option -std=c99 or -std=gnu99 to compile your code
添加该命令gcc -Wall -std=c99
,现在由于缺少pthread库而看到未定义的引用:
undefined reference to `pthread_create'
通过链接:
来解决这个问题gcc -Wall -std=c99 <yourfile>.c -lpthread
现在你所拥有的是一堆(重要的)警告:
In function ‘main’:
22:3: warning: implicit declaration of function ‘init’ [-Wimplicit-function-declaration]
At top level:
31:6: warning: conflicting types for ‘init’ [enabled by default]
22:3: note: previous implicit declaration of ‘init’ was here
In function ‘producer’:
51:5: warning: implicit declaration of function ‘insert_item’ [-Wimplicit-function-declaration]
54:7: warning: return makes pointer from integer without a cast [enabled by default]
In function ‘consumer’:
71:1: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
74:5: warning: implicit declaration of function ‘remove_item’ [-Wimplicit-function-declaration]
In function ‘remove_item’:
106:5: warning: ‘return’ with no value, in function returning non-void [enabled by default]
In function ‘consumer’:
74:19: warning: ‘item’ is used uninitialized in this function [-Wuninitialized]
答案 1 :(得分:1)
我在实验室会话中编译程序,我有两个错误.....可以 有人帮帮我????
1)在for(int j=0;j<BUFFER_SIZE;j++);
中删除for循环初始声明,或使用选项-std=c99
进行编译
2)与pthread库链接。
gcc main.c -lpthread
还有一堆警告,注意修理它们。
- 编辑 -
在你的remove_item()中,改变return;返回0;
这是输出
Producer produced 4001537
Producer produced 4001538
Producer produced 4001539
Producer produced 4001540
Producer produced 4001541
Producer produced 4001542
Producer produced 4001543
Producer produced 4001544
Producer produced 4001545
Producer produced 4001546
Consumer consumed 4001536
Producer produced 4001547
Consumer consumed 4001536
Producer produced 4001548
Consumer consumed 4001536
Producer produced 4001549
Consumer consumed 4001536
Producer produced 4001550
Consumer consumed 4001536
Producer produced 4001551
Consumer consumed 4001536
Producer produced 4001552
Consumer consumed 4001536
Producer produced 4001553
Consumer consumed 4001536
Producer produced 4001554
Consumer consumed 4001536
Producer produced 4001555
Consumer consumed 4001536
;;;;;;;;
;;;;;;;;
;;;;;;;;
;;;;;;;;