msgrcv函数
程序能顺利编译,在运行时红色部分会出现错误:c_msgrcv: Argument list too long,我找了好久也不知道哪错了#include"stdio.h"
#include"assert.h"
#define mask 0xff
#include"unistd.h"
#include"sys/types.h"
#include"sys/ipc.h"
#define msgtyp_1 1
#define msgtyp_2 2
#define BUFSIZE 196
void print_(int position,int *buf)
{
int *p = buf;
int i = 0,k = 0,address = 0;
printf("packet %d header:\n ",position);
printf("Sync byte :0x%.4x\n", buf[0]);
printf("transport_error_indicator :0x%.4x\n",buf[1]);
printf("payload_unit_start_indicator :0x%.4x\n",buf[2]);
printf("transport priority :0x%.4x\n",buf[3]);
printf("PID :0x%.4x\n",buf[4]);
printf("transport_scrambling_control :0x%.4x\n",buf[5]);
printf("adaptation_field_control :0x%.4x\n",buf[6]);
printf("continuity_counter :0x%.4x\n",buf[7]);
printf("Other content\n");
printf("%.4x :",address&mask);
for(i = 8;i < 196;i++)
{
if(k > 14)
{
printf("\n%.4x :",address = address+k&mask);
k = 0;
}
printf("%.2x ",buf[i]&mask);
k++;
}
}
void send_head( char *buf,int *buf1,unsigned int position)
{
buf1[0] = mask & buf[0];
buf1[1] = mask &(buf[1]&128)>>7;
buf1[2] = mask &(buf[1]&64)>>6;
buf1[3] = (buf[1]&32)>>5&mask;
buf1[4] = (((buf[1]&31)<<8)+buf[2])&mask;
buf1[5] = mask&(buf[3]&192)>>6;
buf1[6] = ((buf[3]&48)>>4)&mask;
buf1[7] =((buf[3]&15))&mask;
}
void send_other(char *buf,int *buf1)
{
int i;
for(i = 8;i < 196;i++)
{
buf1[i] = buf[i-8];
}
}
int analyze(unsigned int position,int *buf1)
{
char buf[188] = {0};
FILE *fp1;
unsigned int count = 0;
fp1 = fopen("./test.trp","rb");
assert(NULL != fp1);
clearerr(fp1);
while(fread(buf,1,1,fp1) == 1 && buf[0] != 0x47)
;//NULL statement
fseek(fp1,-1,SEEK_CUR);
while(fread(buf,1,188,fp1)== 188 )
{
if(count == position)
{
send_head(buf,buf1,position);
send_other(buf,buf1);
return ;
}
else ++count;
}
if(feof == 0)
perror("read");
else
buf1[0] = -1;
}
struct msgp
{
long msgtyp;
int buf[BUFSIZE];
}msg;
main()
{
pid_t pc;
key_t key;
int qid;
if((key = ftok("./",'A')) < 0)
{
perror("ftok");
exit(1);
}
if((qid = msgget(key,IPC_CREAT | 0666)) < 0)
{
perror("msgget");
exit(1);
}
pc = fork();
if(pc < 0)
{
printf("fock error\n");
exit(1);
}
else if(pc == 0 )
{
int pos;
while(1)
{
if(msgrcv(qid,&msg,4*BUFSIZE,msgtyp_1,0) < 0)
{
perror("c_msgrcv");
exit(1);
}
pos = msg.buf[0];
analyze(pos,msg.buf);
msg.msgtyp = msgtyp_2;
if(msgsnd(qid,&msg,sizeof(msg),0) < 0)
{
perror("c_msgsnd");
exit(1);
}
}
}
else
{
int pos;
int i;
while(1)
{
printf("cmd>");
scanf("%u",&pos);//the number pos start from 0
//send the number of position
msg.msgtyp = msgtyp_1;
msg.buf[0] = pos;
if(msgsnd(qid,&msg,sizeof(msg)-4,0) < 0)
{
perror("p_msgsnd");
exit(1);
}
//recevie data,if msg.buf[0]== -1,pass the position range
if(msgrcv(qid,&msg,sizeof(msg)-4,msgtyp_2,0) < 0)
{
perror("p_msgrcv");
exit(1);
}
if(msg.buf[0] == -1)
printf("The number of position pass the range\n");
else
{
print_(pos,msg.buf);
}
}
}
}