i have been trying to send an array with message queues in c. It works with a normal string, but I don´t know how to do it with an array. this is my code so far with the string attempt. Do I need to seperate the writer and receiver? I also need to send the array two more times, but I can´t start before it sends the first time.
thanks for any help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/msg.h>
#define maxArr 100
//create buffer
struct msgbuffer{
long msgtype;
char msgtext [100];
int arr[maxArr];
}msg;
int main(int argc , char ** argv){
//needed variables
int returncode_msgget;
int returncode_msgrcv;
int i;
//create message queue
returncode_msgget = msgget(1234, IPC_CREAT |0600);
//generating random numbers
srand(time(NULL));
for (i = 0; i < 10; i++) {
msg.arr[i] = rand() %101;
printf("%d,", msg.arr[i]);
}
//sending message
msg.msgtype = 1;
strcpy(msg.msgtext,"test");
//checking if message was sent
if (msgsnd(returncode_msgget, &msg, sizeof(msg.msgtext), 0) == -1){
printf("message not sent\n");
perror("msgsnd()");
}
//buffer for message receiver
wait(NULL);
struct msgbuffer{
long mtype;
char mtext [100];
} msg;
// reaching message queue
//checking if message queue is reachable
msg.mtype = 1;
if ((returncode_msgget = msgget(1234, IPC_CREAT| 0600)) < 0) {
printf("could not reach Message queue\n");
perror("msgget()");
}
//checking if message was received
returncode_msgrcv = msgrcv(returncode_msgget, &msg, sizeof(msg.mtext), msg.mtype, MSG_NOERROR|IPC_NOWAIT);
if (returncode_msgrcv == -1) {
printf("message not received\n");
perror("msgrcv()");
//if message received, notification
}else {
printf ("Diese Nachricht wurde aus der Warteschlange gelesen : %s \n" , msg . mtext );
printf ("Die empfangene Nachricht ist %i Zeichen lang .\n" , returncode_msgrcv );
}
printf("test");
}
this was my attempt for sending an array which I saw on another post here:
//checking if message was sent
if (msgsnd(returncode_msgget, (int*) &msg.arr[i], sizeof(msg.arr[i]), 0) == -1){
printf("message not sent\n");
perror("msgsnd()");
}
Related
I am trying to run two processes which are basically sending and receiving messages using message queue(SYS V). While I can run two process on same terminal tab by keeping my receiver in background
./receiver &
and sender in the foreground.
./sender
which is working fine but causing all my prints from sender & receiver display on same tab.
If I try to run receiver on one terminal tab and sender on other terminal-tab, the processes are not working correctly, they fail to identify message queue exist on the system.
I am not sure if its the terminal issue, or my program issue, I am using MobaXterm terminal.
Added code below, Am I missing w.r.t running processes on two different terminals I would like to know.
receiver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define PATH "/tmp/CMN_KEY"
struct message_text {
int qid;
char buf [200];
};
struct message {
long message_type;
struct message_text message_text;
};
int main (int argc, char **argv)
{
key_t key;
int qid;
struct message message;
if ((key = ftok (PATH,'Z')) == -1) {
printf ("ftok");
exit (1);
}
if ((qid = msgget (key, IPC_CREAT | 0660)) == -1) {
printf ("msgget");
exit (1);
}
printf ("Receiver: Waiting for MSG!\n");
while (1) {
// read an incoming message
if (msgrcv (qid, &message, sizeof (struct message_text), 0, 0) == -1) {
printf ("msgrcv");
exit (1);
}
printf ("Receiver: MSG Received.\n");
// message from sender
int length = strlen (message.message_text.buf);
char buf [20];
sprintf (buf, " %d", length);
strcat (message.message_text.buf, buf);
int client_qid = message.message_text.qid;
message.message_text.qid = qid;
// send reply message to Sender
if (msgsnd (client_qid, &message, sizeof (struct message_text), 0) == -1) {
printf ("msgget");
exit (1);
}
printf ("Receiver: Response sent to Sender .\n");
}
}
sender.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define PATH "/tmp/CMN_KEY"
struct message_text {
int qid;
char buf [200];
};
struct message {
long message_type;
struct message_text message_text;
};
int main (int argc, char **argv)
{
key_t key;
int sender_qid, myqid;
struct message my_message, return_message;
// queue for receiving messages from receiver
if ((myqid = msgget (IPC_PRIVATE, 0660)) == -1) {
printf ("msgget: myqid");
exit (1);
}
printf("Sender created q with ID: %d\n" , myqid);
if ((key = ftok (PATH,'Z')) == -1) {
printf ("ftok");
exit (1);
}
if ((sender_qid = msgget (key, 0)) == -1) {
printf ("msgget: sender_qid");
exit (1);
}
my_message.message_type = 1;
my_message.message_text.qid = myqid;
printf ("Input a message: ");
while (fgets (my_message.message_text.buf, 198, stdin)) {
int length = strlen (my_message.message_text.buf);
if (my_message.message_text.buf [length - 1] == '\n')
my_message.message_text.buf [length - 1] = '\0';
// send message to Receiver
if (msgsnd (sender_qid, &my_message, sizeof (struct message_text), 0) == -1) {
printf ("client: msgsnd");
exit (1);
}
// read response from Receiver
if (msgrcv (myqid, &return_message, sizeof (struct message_text), 0, 0) == -1) {
printf ("client: msgrcv");
exit (1);
}
// Return message from Receiver
printf ("Return Message From Receiver: %s\n\n", return_message.message_text.buf);
printf ("type a one more message: ");
}
// remove message queue
if (msgctl (myqid, IPC_RMID, NULL) == -1) {
printf ("client: msgctl");
exit (1);
}
return
}
EDIT: the problem is that scanf (and other function I tried here) doesn't wait for the input, the program doesn't pause.
Using Ubuntu 18 on Virtual Box on Mac
I am writing a server/client using POSIX. I am not able to read keyboard input in client.c
char action_type[1];
printf("Chose action: T to request time, S to shut down\n");
scanf(" %c",action_type);
printf("%s", action_type);
if I put the same code as the first thing in main.c it works fine.
full code for server / client and commons is:
server
#include <stdio.h>
#include "mqueue.h"
#include "commons.h"
#include "errno.h"
#include "stdlib.h"
#include <string.h>
#include <time.h>
int status_closing = 0;
void send_time(char* clients_pid);
void send_activation(char* clients_pid);
//VARIABLES related to SERVERS QUEUE
mqd_t server; // server
struct mq_attr servers_attributes; // server creation attributes
struct mq_attr receiving_attributes; // server receiving attributes
// set up attributes
void set_servers_attributes(){
// set up server's attributes
servers_attributes.mq_maxmsg = QUEUE_SIZE;
servers_attributes.mq_msgsize = MESSAGE_SIZE;
printf("attributes set \n");
};
// open server
void open_servers_queue() {
server = mq_open (servers_path,
O_CREAT | O_RDWR | O_EXCL ,
0666, &servers_attributes);
if (server == -1) {
printf("failed to open server's queue\n");
printf(errno);
exit(-1);
} else {
printf("opened servers queue as: %d\n",server);
}
};
// check attributes
void check_attributes(){
if ((mq_getattr(server,&receiving_attributes)) == -1) {
printf("cannot read server's queue\n exit \n");
exit(-1);
}
};
void close_and_unlink_queue(){
printf("At exit closing and unlinking queue\n");
mq_close(server);
mq_unlink(servers_path);
};
int check_for_messages_in_the_queue(){
//printf("checking for messages\n");
int messages_in_queue;
messages_in_queue = receiving_attributes.mq_curmsgs;
//printf("there are %d message in the servers queue\n",messages_in_queue);
// printf("message in the queue!\n");
return messages_in_queue;
};
char* receive_message(){
char *receiving_buffer = malloc(sizeof(char)*MESSAGE_SIZE);
if ((mq_receive(server,receiving_buffer,MESSAGE_SIZE,NULL))>0){
return receiving_buffer;
}
else {
printf("Server failed to receive message");
mq_close(server);
mq_unlink(servers_path);
exit(-1);
}
};
void respond(char *message_type, char* clients_pid) {
printf("responding\n");
char* type_one = "1";
char* type_two = "2";
char *type_three ="3";
if (strcmp(message_type, type_one) == 0) {
printf("TYPE 1\n");
send_activation(clients_pid);
}
if (strcmp(message_type, type_two) == 0) {
printf("TYPE 2\n");
send_time(clients_pid);
}
if (strcmp(message_type, type_three) == 0) {
printf("type 3 - SHUTDOWN INITIATED\n");
}
}
void send_time(char* clients_pid) {
time_t mytime = time(NULL);
char *time_str = ctime(&mytime);
time_str[strlen(time_str)-1] = '\0';
char *clientpath[20];
int clients_pid_int = atoi(clients_pid);
sprintf(clientpath,"/%d",clients_pid_int);
printf("clients path: %s\n",clientpath);
mqd_t client;
client = mq_open(clientpath,O_RDWR , 0666, &servers_attributes);
if (client == -1) {
printf("failes opening client's queue \n");
exit(-1);
} else {
printf("connected to client's queue: %d\n",client);
}
};
void send_activation(char* clients_pid) {
char *clientpath[20];
int clients_pid_int = atoi(clients_pid);
sprintf(clientpath,"/%d",clients_pid_int);
printf("clients path: %s\n",clientpath);
mqd_t client;
client = mq_open(clientpath,O_RDWR , 0666, &servers_attributes);
if (client == -1) {
printf("failes opening client's queue \n");
exit(-1);
} else {
printf("connected to client's queue: %d\n",client);
char* activation = malloc(sizeof(char)*MESSAGE_SIZE);
char* activation_literal = "activation";
sprintf(activation,"%s",activation_literal);
int message_sent = mq_send(client,activation,MESSAGE_SIZE,0);
printf("message sent with: %d",message_sent);
}
};
int main() {
// clean remainings of previous trials
mq_close(server);
mq_unlink(servers_path);
// define atexit behaviour
atexit(close_and_unlink_queue);
//set servers attributes:
set_servers_attributes();
// open server's queue
open_servers_queue();
// receiving messages in the loop
int condition = 1;
while (1) {
check_attributes();
if (check_for_messages_in_the_queue() > 0) {
char *received_message = receive_message();
char *tok_one = strtok(received_message," ");
char *tok_two = strtok(NULL, " ");
//int clients_pid = atoi(tok_two);
respond(tok_one,tok_two);
} else if ((check_for_messages_in_the_queue() ==0) && (status_closing == 1)) {
printf("Server's queue is empty - work finished. closing down\n");
exit(0);
}
}
printf("Hello, World!\n");
return 0;
}
client
#include <stdio.h>
#include <mqueue.h>
#include <unistd.h>
#include "commons.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
mqd_t client;
mqd_t server;
char clientpath[20];
char* sending_buffer[MESSAGE_SIZE];
struct mq_attr clients_attributes;
struct mq_attr receiving_attributes;
void connect_to_server(){
server = mq_open(servers_path,O_WRONLY);
if(server == -1) {
printf("connection to server failed\n");
} else {
printf("connected to server with id: %d \n",server);
}
};
void set_clients_attributes(){
// deal with attributes
clients_attributes.mq_maxmsg = QUEUE_SIZE;
clients_attributes.mq_msgsize = MESSAGE_SIZE;
};
void create_clients_queue(){
// create clients path
pid_t client_pid = getpid();
sprintf(clientpath, "/%d", client_pid);
// open clients queue
client = mq_open(clientpath,O_RDONLY | O_CREAT | O_EXCL, 0666, &clients_attributes);
// printf(errno);
if (client == -1) {
printf("failes opening client's queue \n");
exit(-1);
} else {
printf("connected to client's queue: %d\n",client);
}
};
void register_at_server(){
message message;
message.mtype = 1;
int client_pid = getpid();
message.sender = client_pid;
char separator = ' ';
snprintf(sending_buffer,MESSAGE_SIZE,"%ld%c%d",message.mtype,separator,message.sender);
if ((mq_send(server,sending_buffer, MESSAGE_SIZE,0)) == -1) {
printf("failed to send registration request\n");
exit(-1);
}
else {
printf("%s",sending_buffer);
printf("sent registration request\n");
}
};
void close_and_unlink_queue(){
printf("At exit closing and unlinking queue\n");
mq_close(client);
mq_unlink(clientpath);
};
void check_attributes(){
if ((mq_getattr(client,&receiving_attributes)) == -1) {
printf("cannot read own's queue\n exit \n");
exit(-1);
}
};
int check_for_messages_in_the_queue(){
//printf("checking for messages\n");
int messages_in_queue;
messages_in_queue = receiving_attributes.mq_curmsgs;
//printf("there are %d message in the servers queue\n",messages_in_queue);
// printf("message in the queue!\n");
return messages_in_queue;
};
char* receive_message(){
char *receiving_buffer = malloc(sizeof(char)*MESSAGE_SIZE);
if ((mq_receive(client,receiving_buffer,MESSAGE_SIZE,NULL))>0){
return receiving_buffer;
}
else {
printf("Server failed to receive message");
mq_close(client);
mq_unlink(clientpath);
exit(-1);
}
};
void choose_action(){
char action_type[1];
printf("Chose action: T to request time, S to shut down\n");
scanf(" %c",action_type);
printf("%s", action_type);
};
int main() {
mq_close(client);
mq_unlink(clientpath);
connect_to_server();
set_clients_attributes();
create_clients_queue();
register_at_server();
int condition = 1;
int client_active = 0;
while (condition) {
check_attributes();
if (check_for_messages_in_the_queue() > 0) {
char *received_message = receive_message();
if ((strcmp(received_message, "activation")) == 0) {
client_active = 1;
condition = 0;
free(received_message);
}
}
}
char action_type[1];
printf("Chose action: T to request time, S to shut down\n");
scanf(" %c",action_type);
printf("%s", action_type);
printf("Hello, World!\n");
return 0;
}
commons
#ifndef SERVER_COMMONS_H
#define SERVER_COMMONS_H
#include <signal.h>
// define values of server queue attributes
//define message struct
typedef struct messgae {
char content[4096];
pid_t sender;
long mtype;
} message;
#define QUEUE_SIZE 10
#define MESSAGE_SIZE sizeof(message)
// define servers path
const char servers_path[] = "/server";
#endif //SERVER_COMMONS_H
``
this happens because the buffer is not empty and this function read from it.
to empty buffer just try this code:
while( getchar() != '\n');
it will empty the buffer and after it the functions will wait for input.
printf( "Enter a value :");
c = getchar( );
or
#include <stdio.h>
int main( ) {
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
I have created a msq to let two processes communicate to one another. The problem is that I'm having some issues since after a couple of time that I run my code some of the old messages of the previous executions show up. This bothers me because I need to perform controls on the current data and cant do it because of this. I tried irpcm -q msqid but it does not do anything except shutting down my program the next time I run it. I even tried hardcoding some keys thinking that it would help but nothing. Also tried to msgctl(msqid, IPC_RMID, 0) after I finished using the queue but nothing. Hope you can help me out and thanks in advance.
Here is my code:
sender.c
#define MAXSIZE 1024
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
void die(char *s)
{
perror(s);
exit(1);
}
int msqid1;
int msgflg = IPC_CREAT | 0666;
key_t keymq1;
struct msgbuf sbuf;
size_t buflen;
keymq1 = 668;
sbuf.mtype = 1;
if ((msqid1 = msgget(keymq1, msgflg )) < 0)
die("msgget");
sbuf.mtype = 1;
strcpy(sbuf.mtext,"my message");
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid1, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %s, %zu\n", msqid1, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");
}
else {
printf("Message sent\n");
}
receiver.c
#define MAXSIZE 1024
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
void die(char *s)
{
perror(s);
exit(1);
}
int msqid;
key_t keymq1;
struct msgbuf rcvbuffer;
keymq1 = 668;
if ((msqid = msgget(keymq1, 0666)) < 0)
die("msgget()");
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
die("msgrcv");
printf("Message received: %s\n", rcvbuffer.mtext);
To delete message queue with message queue Id use
ipcrm -q id
or delete the message queue using key value as
ipcrm -Q key_num
From man page "In order to delete such objects, you must be superuser, or the cre‐
ator or owner of the object."
Finally you can delete the message queue using IPC_RMID flag in msgctl() call as
main()
{
int total_mq,i,msqid;
struct msqid_ds ds; //dataStructure holding complete info for indexed message que
struct msginfo msginfo; //general buff copying data from MSG_INFO, has info of how many message que present right now
/* Obtain size of kernel 'entries' array */
total_mq = msgctl(0, MSG_INFO, (struct msqid_ds *) &msginfo); //copy kernel MSGQ_INFO to local buff
//returns count of active message Q
if (total_mq < 0)
{
perror("msgctl");
return 0;
}
printf("no of active message queue(KEY) : %d\n", total_mq+1);
/* Retrieve meaasge Queue id's */
for (i = 0; i <= total_mq; i++)
{
msqid = msgctl(i, MSG_STAT, &ds); //from each index using MSG_STAT -> ds, return msgqid
if (msqid <0 )
{
perror("msgctl");
return 0;
}
/* using msgqid remove the message queue */
if ( msgctl(msqid,IPC_RMID,0) < 0 )
{
perror("msgctl");
return 0;
}
}
printf("all message queues removed\n");
return 0;
}
before running above code, create some message queues and then delete those.
I am using C and putty to write a client/server program.
Both c files are on the same system.
I am currently having an issue with writing back to the client the frames it is using as well as printing out my frames. It prints out 3 0 9 8 but then it starts printing out 13456756 etc.
Here is what I have:
server:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int to_server; // to read from client
int from_server; // to write to client
int finish; // lets me know that client is done
int i,j,k,m,l; // because C needs this defined as int
int numClient;// number of clients
char temp[14];
int page_size = 128;
int pages_left;
int max_frames=10;
int used_frames =0;
int frameUpdate=0;
int freeframe[10] = {3,0,9,8,7,5,1,4,2,6}; //this is the array
int numpage=0;
int frames;
int check;
int option;
int byte;
int getPage;
int getOffset;
int physical_Addr;
int offset;
int req[3];
int again;
struct values{
char privFifo[14];
int memoryreq;
}cinput;
/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if((to_server=open("FIFO1", O_RDONLY))<0){
printf("cant open fifo to write");
}
//get number of clients
printf("\nHow many clients?\n");
scanf("%d", &numClient);
for(j =1; j <= numClient; j++){
read(to_server, &cinput, sizeof(cinput));
printf("\n\nFifo_%d \nMemory request %d", &cinput.privFifo, cinput.memoryreq);
req[j-1] = cinput.memoryreq;
if((mkfifo(cinput.privFifo,0666)<0 && errno != EEXIST))
{
perror("cant create privFifo");
exit(-1);
}
if((from_server=open(cinput.privFifo, O_WRONLY)<0)){
printf("cant open fifo to write");
}
// find number of pages need for request
numpage = cinput.memoryreq/page_size;
if((numpage * page_size) < cinput.memoryreq){
numpage++;
}
sleep(1);
printf("\nPages needed %d", numpage);
write(from_server, &numpage, sizeof(numpage));
printf("\n******Main Memory******");
for(m = used_frames; m < numpage; m++){
printf("\n* client: %d\tframe: %d", j, freeframe[m]);
frames = freeframe[m];
write(from_server, &frames, sizeof(frames));
}
used_frames = max_frames - used_frames;
pages_left = max_frames - numpage;
//this is where I try to print out the available frames
printf("\n Frames available:");
for(l = pages_left; l!= 0; l--){
check = max_frames - l;
printf(" %d", freeframe[check]);
max_frames = check;
}
close(from_server);
unlink(cinput.privFifo);
}
printf("\nDONE!!!");
close(to_server);
unlink("FIFO1");
client:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int to_server; // to write to server
int from_server;
char temp[14]; // server puts string here
int clientID;
//int frames;
int numpage;
int i;
struct values{
char privFifo[14];
int memoryreq;
}cinput;
if((to_server=open("FIFO1", O_WRONLY))<0)
printf("cant open fifo to write\n");
printf("writing data to to_server\n");
printf("Client: Please enter number of memory units: ");
scanf("%d", &cinput.memoryreq);
printf("%d", cinput.memoryreq);
clientID = getpid();
sprintf(cinput.privFifo, "Fifo_%d", getpid());
printf("\nFifo name is %s", &cinput.privFifo);
write(to_server, &cinput, sizeof(cinput));//write client pid and memUnit to server
sleep(2); //give time to send
printf("\nClient: Got the character sent, now waiting for response ");
if ((mkfifo(cinput.privFifo,0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if((from_server=open(cinput.privFifo, O_RDONLY))<0){
printf("cant open fifo to write");
}
read(from_server, &numpage, sizeof(numpage));
printf("\nFrames Occupied %d", numpage);
close(to_server);
close (from_server);
unlink(cinput.privFifo);
printf ("\nall done!\n");
}
Any help is greatly appreciated. Thank you.
I strongly suspect the problem is the line used_frames = max_frames - used_frames;. Since used_frames is initially 0, that sets it one past the end of the array for the second iteration, so you start printing values past the end of your frame array when you run for(m = used_frames; m < numpage; m++). (By the way: please indent properly.). But set a breakpoint and run in a debugger to be sure.
I have assignment when I need to write simple time server and a client using Linux message queue. The server opens a message queue and the client sends a request with his PID (message with type 1) and the server reads that message and sends a message with type of PID (taken from the message read). I put all the code below because I don't know where I made the mistake. I'm not Linux programming expert. Don't even know if I written server correct.
File that is included by server and client (I need to write it in this way).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#define QUEUE 100
#define PERM_ALL 0777
typedef struct my_msgbuf {
long mtype;
int pid;
} ClientMessage;
typedef struct my_msgbuf2 {
long mtype;
struct tm time;
} TimeMessage;
Server
int m_queue;
void cleanup(int signum) {
if (msgctl(m_queue, IPC_RMID, NULL) == -1) {
printf("Something happen on clean up\n");
exit(1);
}
exit(signum);
}
int main() {
ClientMessage pid_message;
TimeMessage t;
time_t clock;
struct tm *cur_time;
if ((m_queue = msgget(QUEUE, PERM_ALL | IPC_CREAT)) == -1) {
printf("Can't create and open message queue\n");
exit(1);
}
printf("created message queue = %d\n", m_queue);
fflush(stdout);
//t = malloc(sizeof(TimeMessage));
signal(SIGINT, cleanup);
while (1) {
if (msgrcv(m_queue, &pid_message, sizeof(pid_message.pid), 1, 0) == -1) {
break;
} else {
t.mtype = pid_message.pid;
clock = time(NULL);
cur_time = localtime(&clock);
memcpy(&t.time, cur_time, sizeof(struct tm));
msgsnd(m_queue, &t, sizeof(struct tm), 0);
}
}
cleanup(0);
}
Client
int main() {
int m_queue;
TimeMessage *t;
ClientMessage client;
if ((m_queue = msgget(QUEUE, PERM_ALL)) == -1) {
perror("Error in opening queue");
exit(1);
}
client.mtype = 1;
client.pid = getpid();
while (1) {
if (msgsnd(m_queue, &client, sizeof(client.pid), 0) == -1) {
perror("Error sending to queue");
exit(1);
} else {
if (msgrcv(m_queue, t, sizeof(struct tm), client.pid, 0) == -1) {
perror("Error reading from queue");
exit(1);
}
printf("time: %d:%d:%d\n", t->time.tm_hour, t->time.tm_min, t->time.tm_sec);
}
}
return 0;
}
Both program compile without errors but client return "Error reading from queue" msgrcv is returning -1.
After adding the perror it appears that you have got the error stating "Bad Address" (EFAULT) which means that "The address pointed to by msgp (buffer) isn't accessible". From the code it appears that there has been no memory allocated to TimeMessage *t so you can either allocate memory or just change it to TimeMessage t and pass &t instead of t to msgrcv. Also size should be sizeof t (assuming the change from *t to t, or sizeof(TimeMessage) for *t) instead of sizeof(struct tm) (& obviously you would change printf statement accordingly)
Hope this helps!