ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The structure below is an example of what this user-defined buffer might look like:
struct mymsg {
long mtype; /* message type */
char mtext[MSGSZ]; /* message text of length MSGSZ */
}
So can I change this to:
typedef struct msgbuf {
int mSlots[5];
int mError[5];
char mtext[MSGSZ];
} message_buf;
or is the struct unchangeble.
The man in linux gives:
the calling process allocates a structure of the following general form :
struct msgbuf {
long mtype; /* message type must be >0*/
char mtext[1]; /* message data */
}
Yah You can change, but first member should be Long int ( mtype).
Speciality in Linux message Queues is that Mtype. It allows Multiplexing of messages into a message queue.
But at the other end while you are reading you should use the same Format structure. Other wise logically you may face problems.
I hope this clarifies your doubt.
Bye,
Eshwar.
Ya you can send but add long mtype as the first element of your message.
Recieve that data at the other end with the same structure format. It has to work try it.
bye,
Eshwar.
Here's the message queue part of a program I wrote to switch on/off two LED's connected to a serial port. Maybe you can use this. Your thing sound similar to me. If you would like to see the entire program, just send me an e-mail through my forum profile, and I'll mail you back with the .tar.gz of the program attached.
It has one daemon process that sits waiting for a SIGUSR1 signal from the client program. The client process puts a message on the queue, and then kills the daemon with SIGUSR1, The signal handler of the daemon gets the message from the queue and sets/unsets the lines of the serial port. This way the daemon uses exactly 0% CPU when there's nothing to do.
Code:
Note that:
- Both my client and the daemon link with this, each calling only the functions
useful for them. (the daemon creates and remove the queue, the client
"connects")
- functions like out_error() used here are in some other c-file of my program,
which just send the string either to syslog or stderr depending on some
flag set somewhere else in the program.
- I used ftok() to generate a IPC key in getipckey(). This needs a path to
some file unique for the project. I used a lockfile here that my program
uses. The function getlockfilename() is in another c-file in my project.
It just returns the path of the lock-file.
- The size parameter for the syscalls msgrcv() and msgsnd() should not
include the long int message type identifier. I solved this here using a
struct inside the actual message-struct.
Code:
/* msgq.h */
#ifndef MSGQ_H
#define MSGQ_H
#define MSGTYPE 19031968
#define MSGSIZE (sizeof(action_t))
/* LED actions */
#define NONE 0
#define OFF 1
#define ON 2
typedef struct {
unsigned int led1 :2;
unsigned int led2 :2;
} action_t;
typedef struct {
long int message_type;
action_t action;
} msg_t;
int connect_new_msg_queue(void);
int connect_msg_queue(void);
void remove_msg_queue(void);
int get_message(action_t *action);
int put_message(const action_t *action);
#endif
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.