简体   繁体   中英

How to received ten message queues and print them in reverse order?

Main task - Write two programs: the first will send ten messages and the second, depending on the call param- eter will receive them in the same or reverse order, based on the user's choice.

I have two work programs: one sends 10 words in turn, the other receives and outputs. The problem is that I don't know how to make the second program display these words in the reverse order (starting from the last one) - i need to make a choice for the user in which order he wants!

//Program for receive messages :

#include<stdio.h>
 #include<sys/ipc.h>
 #include<sys/msg.h>
 #include<sys/types.h>

 struct msgbuf {
 long type;
 char mtext[1000];
 };

 void receive_message(int mqid,int argum)
 {
         int j=0;
 struct msgbuf buffer;

 for(int i = 0;i<10;i++){

 if(msgrcv(mqid,&buffer,sizeof(buffer.mtext),1,0)<0){
 perror("msgrcv");
 } else{

 printf("Received message [%d] : %s\n",i,buffer.mtext);

 }
 }
 }


 int main(int argc,char *argv[])
 {
 int key = ftok("ed",8);
 if(key<0)
 perror("ftok");

 int id = msgget(key,0600|IPC_CREAT|IPC_EXCL);
 if(id<0)
 perror("msgget");
 receive_message(id,argc);

 if(msgctl(id,IPC_RMID,0)<0)
 perror("msgctl");

 return 0;
 }

//Program for send messages

#include<stdio.h>
 #include<string.h>
 #include<sys/ipc.h>
 #include<sys/msg.h>
 #include<sys/types.h>

 #define TEXT_LENGTH 1000

 struct msgbuf {
 long type;
 char mtext[TEXT_LENGTH];
 };

 void send_message(int mqid)
 {
 struct msgbuf buffer;
int y;
 buffer.type = 1;

char message[100];

 for(int i = 0;i<10;i++){

puts("Enter word : ");
scanf("%s",&message);

memset(buffer.mtext,0,sizeof(buffer.mtext));
 strncpy(buffer.mtext,message,TEXT_LENGTH-1);

 if(msgsnd(mqid,&buffer,sizeof(buffer.mtext),0)<0)
 perror("msgsnd");
 }
 }

 int main(void) {
 int key = ftok("ed",8);
 if(key<0)
 perror("ftok");

 int id = msgget(key,0600);
 if(id<0)
 perror("msgget");

 send_message(id);

 return 0;
 }

there are no errors; the program simply displays 10 words that I enter

keep adding to tail of a linked list and then display from head to the tail if you wanna reverses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM