所以我有这个主要功能,bank1.c:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<signal.h>
#include<sys/stat.h>
#include"receive_message.h"
int desks = 10;
int N = 2;
int running = 0;
pthread_t tid[10];
int queue[10];
int balance = 20;
pthread_mutex_t lock;
void* getMessage(void *arg)
{
int i;
char* message = malloc(sizeof(char)*30);
char** m_point = &message;
key_t key;
pthread_mutex_lock(&lock);
pthread_t id = pthread_self();
pid_t pid;
int j = 10;
for(i = 0; i < N; i++)
{
if(pthread_equal(id, tid[i]))
{
key = 10 + i * 5;
printf("\n Thread %d processing\n", i);
m_point = receive_message(key, m_point);
printf(" Message received: %s\n", *m_point);
}
}
free(message);
pthread_mutex_unlock(&lock);
running = running - 1;
return NULL;
}
int main(void)
{
int i = 0;
int err;
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
while(i < N)
{
running = running + 1;
pthread_create(&(tid[i]), NULL, &getMessage, NULL);
i++;
}
while(running != 0)
{
//sleep(2);
}
printf(" RUN OK\n");
return 0;
}
我意识到我需要在其他文件中使用函数“receive_message”,所以我试图从中创建一个单独的文件receive_message.c:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<signal.h>
#include<sys/stat.h>
#define MSGSZ 128
struct msgbuf {
long mtype;
char mtext[200];
};
char** receive_message(key_t key, char** ret)
{
int msqid;
msqid = msgget((key_t) key, 0600 | IPC_CREAT);
struct msqid_ds buf;
struct msgbuf m_buf;
int rc = msgctl(msqid, IPC_STAT, &buf);
int msg = (uint)(buf.msg_qnum);
if (msg != 0){
if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
return ret;
}
if (msgrcv(msqid, &m_buf, sizeof m_buf.mtext, 0, 0) == -1) {
perror("msgrcv");
return ret;
}
}
strcpy(*ret, m_buf.mtext);
return ret;
}
使用此标题,receive_message.h:
#ifndef RECEIVE_MESSAGE_H_INCLUDED
#define RECEIVE_MESSAGE_H_INCLUDED
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[200]; /* message data */
};
char** receive_message(key_t key, char** ret)
#endif
现在当我尝试将其编译为gcc bank1.c receive_message.c -o bank1 -lpthread时,我得到了这些错误,我无法做出正面或反面:
bank1.c: In function ‘receive_message’:
bank1.c:15:1: error: parameter ‘desks’ is initialized
int desks = 10;
^
bank1.c:16:1: error: parameter ‘N’ is initialized
int N = 2;
^
bank1.c:17:1: error: parameter ‘running’ is initialized
int running = 0;
^
bank1.c:20:1: error: parameter ‘balance’ is initialized
int balance = 20;
^
bank1.c:24:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
bank1.c:55:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
In file included from bank1.c:11:0:
receive_message.h:9:8: error: old-style parameter declarations in prototyped function definition
char** receive_message(key_t key, char** ret)
^
bank1.c:87:1: error: expected ‘{’ at end of input
}
^
我认为错误是在头文件中,但我不确定。当receive_message.c中的代码位于bank1.c
时,文件按预期运行答案 0 :(得分:1)
不幸的是我不能评论所以这里是Michiel已经指出的补充:
请务必仅定义struct msgbuf
一次。现在它在头文件和.c文件中定义。
此外,您似乎还需要在.c文件中使用它,因此您可以在.c文件中将其声明为静态而不是在头文件中公开!