?? heap_mq.c
字號:
/****************************************************************************/
/* */
/* Sam Siewert - 10/14/97 */
/* */
/* */
/****************************************************************************/
#include "msgQLib.h"
#include "mqueue.h"
#include "errnoLib.h"
#include "ioLib.h"
#define SNDRCV_MQ "send_receive_mq"
struct mq_attr mq_attr;
static mqd_t mymq;
/* receives pointer to heap, reads it, and deallocate heap memory */
void receiver(void)
{
char buffer[sizeof(void *)+sizeof(int)];
void *buffptr;
int prio;
int nbytes;
int count = 0;
int id;
while(1) {
/* read oldest, highest priority msg from the message queue */
printf("Reading %ld bytes\n", sizeof(void *));
if((nbytes = mq_receive(mymq, buffer, (size_t)(sizeof(void *)+sizeof(int)), &prio)) == ERROR)
/*
if((nbytes = mq_receive(mymq, (void *)&buffptr, (size_t)sizeof(void *), &prio)) == ERROR)
*/
{
perror("mq_receive");
}
else
{
memcpy(&buffptr, buffer, sizeof(void *));
memcpy((void *)&id, &(buffer[sizeof(void *)]), sizeof(int));
printf("receive: ptr msg 0x%X received with priority = %d, length = %d, id = %d\n", buffptr, prio, nbytes, id);
printf("contents of ptr = \n%s\n", (char *)buffptr);
free(buffptr);
printf("heap space memory freed\n");
}
}
}
static char imagebuff[4096];
void sender(void)
{
char buffer[sizeof(void *)+sizeof(int)];
void *buffptr;
int prio;
int nbytes;
int id = 999;
while(1) {
/* send malloc'd message with priority=30 */
buffptr = (void *)malloc(sizeof(imagebuff));
strcpy(buffptr, imagebuff);
printf("Message to send = %s\n", (char *)buffptr);
printf("Sending %ld bytes\n", sizeof(buffptr));
memcpy(buffer, &buffptr, sizeof(void *));
memcpy(&(buffer[sizeof(void *)]), (void *)&id, sizeof(int));
if((nbytes = mq_send(mymq, buffer, (size_t)(sizeof(void *)+sizeof(int)), 30)) == ERROR)
{
perror("mq_send");
}
else
{
printf("send: message ptr 0x%X successfully sent\n", buffptr);
}
taskDelay(3000);
}
}
static int sid, rid;
void heap_mq(void)
{
int i, j;
char pixel = 'A';
for(i=0;i<4096;i+=64) {
pixel = 'A';
for(j=i;j<i+64;j++) {
imagebuff[j] = (char)pixel++;
}
imagebuff[j-1] = '\n';
}
imagebuff[4095] = '\0';
imagebuff[63] = '\0';
printf("buffer =\n%s", imagebuff);
/* setup common message q attributes */
mq_attr.mq_maxmsg = 100;
mq_attr.mq_msgsize = sizeof(void *)+sizeof(int);
mq_attr.mq_flags = 0;
/* note that VxWorks does not deal with permissions? */
mymq = mq_open(SNDRCV_MQ, O_CREAT|O_RDWR, 0, &mq_attr);
if(mymq == (mqd_t)ERROR)
perror("mq_open");
/* receiver runs at a higher priority than the sender */
if((rid=taskSpawn("Receiver", 90, 0, 4000, receiver, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR) {
printf("Receiver task spawn failed\n");
}
else
printf("Receiver task spawned\n");
if((sid=taskSpawn("Sender", 100, 0, 4000, sender, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR) {
printf("Sender task spawn failed\n");
}
else
printf("Sender task spawned\n");
}
void shutdown(void)
{
mq_close(mymq);
taskDelete(sid);
taskDelete(rid);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -