IPC mechanism using message queues in C - c

I am trying to implement a IPC mechanism for telephone conversation using message queues in C. I have created two .c files, one for caller and one for receiver. In each .c file i have created two threads , one for sending messages and another for receiving messages. Each thread creates its message queue. The send message thread from caller and the receive message thread from receiver share the same queue and same for the other.
The message queues are being created but whenever i enter some message to be sent , it fails. The msgsnd(-,-,-,-) always returns -1.
The caller.c file is as follows :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[50];
}SEND_BUFFER,RECEIVE_BUFFER;
int send_msgQ_id, receive_msgQ_id;
key_t send_key,receive_key;
void * send_message(void * a)
{
send_key = ftok("Caller.c", 'B');
if(send_key==-1)
{
printf("\n caller send key error");
exit(1);
}
send_msgQ_id = msgget(send_key, 0666 | IPC_CREAT);
if(send_msgQ_id==-1)
{
printf("\n caller send msgget error");
exit(1);
}
printf("\n Enter lines of text, ^D to quit:\n");
while(fgets(SEND_BUFFER.mtext, sizeof(SEND_BUFFER.mtext), stdin) != NULL)
{
SEND_BUFFER.mtype = 0;
int len = strlen(SEND_BUFFER.mtext);
if (SEND_BUFFER.mtext[len-1] == '\n')
SEND_BUFFER.mtext[len-1] = '\0';
printf("\n Attemping to send %s\n", SEND_BUFFER.mtext);
if(msgsnd(send_msgQ_id, &SEND_BUFFER, len+1, 0)==-1)
printf("\n msg sendign error\n");
}
int i =0;
while(i<9999)
i++;
msgctl(send_msgQ_id, IPC_RMID, NULL);
return;
}
void * receive_message(void * a)
{
receive_key = ftok("Receiver.c", 'B');
if(receive_key==-1)
{
printf("\n caller receive key error");
exit(1);
}
receive_msgQ_id = msgget(receive_key, 0777 | IPC_CREAT);
if(receive_msgQ_id==-1)
{
printf("\n caller receive msgget error");
exit(1);
}
printf("\n Ready to receive ");
while(1)
{
if( msgrcv(receive_msgQ_id, &RECEIVE_BUFFER, sizeof(RECEIVE_BUFFER.mtext), 0, 0)!=-1)
printf("Received : %s\n", RECEIVE_BUFFER.mtext);
}
return;
}
void initialize()
{
pthread_t send_thread,receive_thread;
pthread_create(&send_thread,NULL,send_message,NULL);
pthread_create(&receive_thread,NULL,receive_message,NULL);
pthread_join(send_thread,NULL);
pthread_join(receive_thread,NULL);
return;
}
int main()
{
printf("\n\n *** Caller Program ***\n");
initialize();
return 0;
}
The receiver.c file is as follows (it is similar to the caller.c file) :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[50];
}SEND_BUFFER,RECEIVE_BUFFER;
int send_msgQ_id, receive_msgQ_id;
key_t send_key,receive_key;
void * send_message(void * a)
{
send_key = ftok("Receiver.c", 'B');
if(send_key==-1)
{
printf("\n receiver send key error");
exit(1);
}
send_msgQ_id = msgget(send_key, 0777);
if(send_msgQ_id==-1)
{
printf("\n receiver msgget error ");
exit(1);
}
printf("\n Enter lines of text, ^D to quit:\n");
SEND_BUFFER.mtype = 0;
while(fgets(SEND_BUFFER.mtext, sizeof(SEND_BUFFER.mtext), stdin) != NULL)
{
int len = strlen(SEND_BUFFER.mtext);
if (SEND_BUFFER.mtext[len-1] == '\n')
SEND_BUFFER.mtext[len-1] = '\0';
msgsnd(send_msgQ_id, &SEND_BUFFER, len+1, 0);
}
int i =0;
while(i<9999)
i++;
msgctl(send_msgQ_id, IPC_RMID, NULL);
return;
}
void * receive_message(void * a)
{
receive_key = ftok("Caller.c", 'B');
if(receive_key==-1)
{
printf("\n receiver receiver key error");
exit(1);
}
receive_msgQ_id = msgget(receive_key, 0666);
if(receive_msgQ_id==-1)
{
printf("\n msgget error");
exit(1);
}
while(1)
{
if(msgrcv(receive_msgQ_id, &RECEIVE_BUFFER, sizeof(RECEIVE_BUFFER.mtext), 0, 0)==-1)
printf("\n msg receiving error");
else
printf("Received : %s\n", RECEIVE_BUFFER.mtext);
}
return;
}
void initialize()
{
pthread_t send_thread,receive_thread;
pthread_create(&receive_thread,NULL,receive_message,NULL);
pthread_create(&send_thread,NULL,send_message,NULL);
pthread_join(send_thread,NULL);
pthread_join(receive_thread,NULL);
return;
}
int main()
{
printf("\n\n *** Receiver Program ***\n");
initialize();
return 0;
}
I need that both the caller and sender can receive and send messages at their free will.

The errno you're getting indicates you're passing an invalid argument to msgsnd. According to the man page for msgsnd:
[EINVAL]
The value of msqid is not a valid message queue identifier, or the value of mtype is less than 1; or the value of msgsz is less than 0 or greater than the system-imposed limit.
Blockquote
My guess would be that msqid is the offending parameter. Either check the value in a debugger or print it out.
EDIT: Mike Wilkins spotted it. His comment:
mtype is likely the issue (not msqid). mtype needs to be > 0. The code
in the OP specifically sets it to 0

Related

C Unix msgsnd not sending some messages

I have written a program in c that creates a child process for receiving messages and the parent sends messages. It will not receive messages from its parent process, this is by design and my reason for using MSG_EXCEPT. So it is intended to have 2 instances of the program running, and they can send and receive messages. The issue is that the program only sends some messages, not all, and I have no clue why...
Also, I have to use gcc -D_GNU_SOURCE chat.c -o chat to compile, otherwise it has an error about MSG_EXCEPT. Does anyone know a better way to get MSG_EXCEPT to work without using those compiler flags? Something in code would be preferable, so that this can be portable.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
typedef struct messageBuffer {
long type;
char text[256];
} MsgBuf;
typedef struct MessageStruct {
MsgBuf message;
int success;
} Message;
void handler(int sig){
_exit(0);
}
int open_queue( key_t keyval )
{
int qid;
if((qid = msgget( keyval, IPC_CREAT | 0666 )) == -1)
{
return(-1);
}
return qid;
}
void SendMessage(int qid, int msgtype, char* msgtxt)
{
MsgBuf msg;
time_t t;
msg.type = msgtype;
snprintf(msg.text, sizeof(msg.text), "%s", msgtxt);
time(&t);
if(msgsnd(qid, (void*)&msg, sizeof(msg.text), IPC_NOWAIT) == -1)
{
perror("msgsnd error");
exit(EXIT_FAILURE);
}
}
Message ReceiveMessage(int qid, int msgtype)
{
Message msg;
msg.success = 1;
if(msgrcv(qid, (void*)&msg.message, sizeof(msg.message.text), msgtype, IPC_NOWAIT | MSG_NOERROR | MSG_EXCEPT) == -1)
{
if (errno != ENOMSG)
{
perror("msgrcv");
exit(EXIT_FAILURE);
}
else
msg.success = 0;
}
return msg;
}
void ClearCurrentConsoleLine()
{
printf("\x1b[1F"); // Move to beginning of previous line
printf("\x1b[2K"); // Clear entire line
}
int main(void)
{
pid_t pid;
pid_t ppid = getpid();
int msgkey = 6666;
char str[256];
Message msg;
char* writemsg = "Write your message below:\n";
pid = fork();
int qid = open_queue(msgkey);
if(qid == -1)
{
perror("msgget");
exit(EXIT_FAILURE);
}
if(pid < 0)
{
perror("Forking error!");
abort();
}
else if(pid == 0)
{
signal(SIGCONT,handler);
while(1)
{
msg = ReceiveMessage(qid, ppid);
if(msg.success)
{
ClearCurrentConsoleLine();
printf("message: %ld: %s\n", msg.message.type, msg.message.text);
printf("%s", writemsg);
}
}
exit(0);
}
while(1)
{
printf("%s", writemsg);
fgets(str, sizeof(str), stdin);
int n = strlen(str);
if(str[n-1] == '\n')
str[n-1] = '\0';
ClearCurrentConsoleLine();
ClearCurrentConsoleLine();
printf("Me: %s\n", str);
if(strcmp(str, "exit") == 0)
{
printf("exiting\n");
break;
}
SendMessage(qid, ppid, str);
}
printf("Killing: %d\n", pid);
kill(pid,SIGCONT);
exit(0);
}
MSG_EXCEPT should not be used here, just remove this flag
MSG_EXCEPT
Used with msgtyp greater than 0 to read the first message
in the queue with message type that differs from msgtyp.
Turns out there was just an issue with that specific message queue. So I just closed the message queue and started a new one and all fixed. In regards to the MSG_EXCEPT, I need to use that because I don't want to just get any message currently in the queue, as I would with 0, I want to get any message that is not submitted by myself, any message except one with the key that I am using to send them. Another way I could do this would be to have 2 message queues per process, one for acquiring the key and message queue of another chat process, and one for sending messages between that other chat process, but this would increase complexity and this is just meant to be a simple implementation.

Sleeping barber - shared memory queue not working?

few days ago I started working on the sleeping barber problem, got some issues with segmentation fault But they have been solved here
Even though I fixed the missing parts, I still have a problem. I need to use FIFO queue, and create shared memory for it. I get no errors when creating it. Running the client should let me put clientAmount of clients into the queue, from which barber should be getting them. Every client is described by his process id. But when I try to do so, client program shows that clients have been added to queue:
2101 entered the queue
2099 entered the queue
2104 entered the queue
2097 entered the queue
2103 entered the queue
2095 entered the queue
2102 entered the queue
2098 entered the queue
2096 entered the queue
but when I run the barber code, all I get is:
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
Queue empty, I fall asleep
I'm waking up
I'm not really sure what to do here.
The code for client:
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "functions.h"
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
int numberOfCuts;
int isCut;
int isDone;
void status(int f);
int main(int argc,char* argv[])
{
if(argc < 3){
printf("Error while executing program, invalid amount of arguments");
return 0;
}
sem_t *barber;
sem_t *queue;
sem_t *client;
sem_t *pillow;
int clientsAmount;
int sharedmem, waitRoomSize;
struct Queue* waitroom;
void *space;
int i;
signal(SIGUSR1, status);
clientsAmount = atoi(argv[1]);
numberOfCuts = atoi(argv[4]);
barber = sem_open("/B", O_RDWR);
if((barber == SEM_FAILED)){
perror("Error while getting semaphore for barber");
exit(1);
}
queue = sem_open("/Q", O_RDWR);
if((queue == SEM_FAILED)) {
perror("Error while creating semaphore for queue");
exit(1);
}
client = sem_open("/C", O_RDWR);
if(client == SEM_FAILED){
perror("Error while creating semaphore for pillow");
exit(0);
}
sharedmem = shm_open("QueueMem", O_RDWR, 0666);
if(sharedmem==-1){
perror("Error while getting shared memory");
exit(1);
}
space = mmap(NULL, sizeof(waitroom), PROT_READ | PROT_WRITE, MAP_SHARED, sharedmem, 0);
if((space == MAP_FAILED)){
perror("Error while mapping memory");
exit(1);
}
waitroom = (struct Queue*) space;
for(i = 0; i< clientsAmount; i++){
if(fork() == 0){
int isCut = 0;
int id = getpid();
printf("%d entered the queue \n", id);
sem_wait(queue);
sem_post(queue);
if( push(waitroom, id)==-1){
printf("Queue is full, leaving...");
exit(0);
}else {
push(waitroom, id);
sem_wait(pillow);
printf("%d: Barber is sleeping, he needs to wake up", id);
int x;
sem_getvalue(barber, &x);
if(x==0){
sem_post(barber);
while(x!= 0){
sem_post(barber);
printf("Barber is waking up to cut %d", id);
}
}
sem_post(pillow);
_exit(0);
}
}
}
sem_close(barber);
sem_unlink("/B");
sem_close(queue);
sem_unlink("/Q");
sem_close(client);
sem_unlink("/C");
}
void status(int f){
numberOfCuts--;
printf("Remaining cuts: %d", numberOfCuts);
isCut = 1;
while(!numberOfCuts)
{
printf("Leaving the barber");
isDone =1;
}
}
The barber code:
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "functions.h"
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
#define BARBER "Barber"
int main(int argc,char* argv[])
{
if(argc < 2){
printf("Error while executing program, invalid amount of arguments");
return 0;
}
sem_t *barber;
sem_t *queue;
sem_t *client;
int seats;
int sharedmem, waitRoomSize;
struct Queue* waitroom;
queue = sem_open("/Q", O_CREAT | O_RDWR, 0666, 1);
if((queue == SEM_FAILED)) {
printf("Error while creating semaphore for queue");
exit(1);
}
barber= sem_open("/B", O_CREAT | O_RDWR, 0666, 1);
if((barber == SEM_FAILED)){
printf("Error while creating semaphore for barber");
exit(1);
}
client = sem_open("/C", O_CREAT | O_RDWR, 0666, 0);
if(client == SEM_FAILED){
printf("Error while creating semaphore for pillow");
exit(0);
}
seats = atoi(argv[1]);
void *space;
sharedmem = shm_open("Queue",O_CREAT | O_RDWR, 0666);
if(sharedmem==-1){
printf("Error while getting shared memory");
exit(1);
}
waitRoomSize = ftruncate(sharedmem, sizeof(waitroom));
if((waitRoomSize ==-1)){
printf("Error while getting size");
exit(1);
}
space = mmap(NULL, sizeof(struct Queue), PROT_READ | PROT_WRITE, MAP_SHARED, sharedmem, 0);
if((space == MAP_FAILED)){
printf("Bład podczas mapowania pamiêci");
exit(1);
}
waitroom = (struct Queue*) space;
queueinit(waitroom, seats);
printf("semaphores created\n");
while(1)
{
sem_post(queue);
int x = isEmpty(waitroom);
sem_wait(queue);
if(x==1){
printf("Queue empty, I fall asleep\n");
sem_post(barber);
sem_wait(barber);
printf("I'm waking up\n");
} else {
sem_post(queue);
int id = get(waitroom);
sem_wait(queue);
printf("%d, please sit on the chair\n", id);
printf("Started cutting hair for %d\n", id);
sleep(2);
printf("Cutting done for :%d \n", id);
kill(id, SIGUSR1);
}
}
sem_close(barber);
sem_unlink("/B");
sem_close(queue);
sem_unlink("/Q");
sem_close(client);
sem_unlink("/C");
printf("senaphores unlinked");
}
The code for queue:
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "functions.h"
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
struct Queue{
int elems[500];
int size;
int queueIn;
int queueOut;
int isAsleep;
int mainPID;
int countCurrent;
};
void queueinit(struct Queue* q, int size){
q->size = size;
q->queueIn = q->queueOut = 0;
q->isAsleep = 0;
q->countCurrent = 0;
}
int push(struct Queue* q, int e){
if(q->queueIn == ((q->queueOut -1 + q->size) % q->size)){
return -1; //Queue full
}
q->elems[q->queueIn] = e;
q->queueIn = (q->queueIn + 1) % q->size;
return 0;
}
int get(struct Queue* q){
int e = q->elems[q->queueOut];
q->queueOut = (q->queueOut + 1) % q->size;
return e;
}
int isEmpty(struct Queue* q){
if(q->queueIn == q->queueOut)
return 1;
return 0;
}
void lock(sem_t* sem){
if(sem_wait(sem) == -1){
printf("Error while lockin semaphore");
exit(1);
}
}
void free_sem(sem_t* sem){
if(sem_post(sem) == -1){
printf("Error while releasing semaphore");
exit(1);
}
}
#endif // FUNCTIONS_H_INCLUDED
Any suggestion would be greatly appreciated
EDIT
As of now, few changed have been added:
Checking the return value of sem_wait and sem_post
Removed the second call for push(waitroom, id) in client
Took care of locking and unlocking semaphores, mainly swapped sem_wait with sem_post and vice versa
Got rid of the pillow semaphore
Now the program was working nearly fine, however the client code did not exit after all its children finished their work. Pressing [ENTER] works. So I took the given advice and created a new semaphore - p, which I lock instead of using pause() in client code, and I unlock it in signal handler status(). I also cahnged the value with which barber semaphore was initialized - no more double locking or freeing a semaphore.
I also tried to use abort() function instead of exit but it didn't work.
What happens now:
Barber code does start but nothing happens.
Client code starts and adds some values to queue, and immediately exits.
The updated code for barber:
#include <stdlib.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "functions.h"
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
#define BARBER "Barber"
int main(int argc,char* argv[])
{
if(argc < 2){
printf("Error while executing program, invalid amount of arguments");
return 0;
}
sem_t *barber;
sem_t *queue;
sem_t *p;
int seats;
int sharedmem, waitRoomSize;
struct Queue* waitroom;
queue = sem_open("/Q", O_CREAT | O_RDWR, 0666, 1);
if((queue == SEM_FAILED)) {
printf("Error while creating semaphore for queue");
exit(1);
}
barber= sem_open("/B", O_CREAT | O_RDWR, 0666, 0);
if((barber == SEM_FAILED)){
printf("Error while creating semaphore for barber");
exit(1);
}
p= sem_open("/P", O_CREAT | O_RDWR, 0666, 0);
if((p == SEM_FAILED)){
printf("Error while creating semaphore for barber");
exit(1);
}
seats = atoi(argv[1]);
void *space;
sharedmem = shm_open("QueueMem",O_CREAT | O_RDWR, 0666);
if(sharedmem==-1){
printf("Error while getting shared memory");
exit(1);
}
waitRoomSize = ftruncate(sharedmem, sizeof(waitroom));
if((waitRoomSize ==-1)){
printf("Error while getting size");
exit(1);
}
space = mmap(NULL, sizeof(struct Queue), PROT_READ | PROT_WRITE, MAP_SHARED, sharedmem, 0);
if((space == MAP_FAILED)){
printf("Error while mapping memory");
exit(1);
}
waitroom = (struct Queue*) space;
queueinit(waitroom, seats);
while(1)
{
lock(queue);
if(isEmpty(waitroom)==1){
printf("Queue empty, I fall asleep\n");
waitroom->isAsleep = 1;
free_sem(queue);
lock(barber);
printf("I'm waking up\n");
} else {
int id = get(waitroom);
free_sem(queue);
printf("%d, please sit on the chair\n", id);
printf("Started cutting hair for %d\n", id);
sleep(2);
printf("Cutting done for :%d \n", id);
kill(id, SIGUSR1);
}
}
sem_close(barber);
sem_unlink("/B");
sem_close(queue);
sem_unlink("/Q");
sem_close(p);
sem_unlink("/P");
//exit(0);
//printf("senaphores unlinked");
}
The updated code for client:
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "functions.h"
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
int numberOfCuts;
#include "functions.h"
int id;
sem_t *barber;
sem_t *queue;
sem_t *p;
int clientsAmount;
int sharedmem, waitRoomSize;
struct Queue* waitroom;
void *space;
void status(int f);
void handler(int f);
int main(int argc,char* argv[])
{
if(argc < 3){
printf("Error while executing program, invalid amount of arguments");
return 0;
}
int i;
signal(SIGUSR1, status);
signal(SIGINT, handler);
int pid = getpid();
clientsAmount = atoi(argv[1]);
numberOfCuts = atoi(argv[2]);
barber = sem_open("/B", O_RDWR);
if((barber == SEM_FAILED)){
perror("Error while getting semaphore for barber");
exit(1);
}
queue = sem_open("/Q", O_RDWR);
if((queue == SEM_FAILED)) {
perror("Error while creating semaphore for queue");
exit(1);
}
p = sem_open("/P", O_RDWR);
if((p == SEM_FAILED)) {
perror("Error while creating semaphore for queue");
exit(1);
}
sharedmem = shm_open("QueueMem", O_RDWR, 0666);
if(sharedmem==-1){
perror("Error while getting shared memory");
exit(1);
}
space = mmap(NULL, sizeof(waitroom), PROT_READ | PROT_WRITE, MAP_SHARED, sharedmem, 0);
if((space == MAP_FAILED)){
perror("Error while mapping memory");
exit(1);
}
waitroom = (struct Queue*) space;
for(i = 0; i< clientsAmount; i++) {
if(fork() == 0) {
id = getpid();
printf("%d entered the barbershop \n", id);
while(1) {
lock(queue);
if( push(waitroom, id)==-1 ) {
free_sem(queue);
printf("Queue is full, %d leaving...\n", id);
exit(0);
} else {
free_sem(queue);
printf("%d has entered the queue \n", id);
lock(queue);
int x;
x = waitroom->isAsleep;
if(x==1){
printf("%d: Barber is sleeping, he needs to wake up\n", id);
waitroom->isAsleep = 0;
free_sem(queue);
free_sem(barber);
printf("Barber is waking up to cut %d\n", id);
} else {
printf("Barber is cutting someone else, %d waiting for its turn", id);
free_sem(queue);
}
}
lock(p);
}
break;
}
}
//exit(0);
sem_close(barber);
sem_close(queue);
sem_close(p);
munmap(space, waitRoomSize);
exit(0);
}
void handler(int f) {
printf("Closing");
sem_close(barber);
sem_close(queue);
munmap(space, waitRoomSize);
exit(0);
}
void status(int f) {
numberOfCuts--;
free_sem(p);
printf("Remaining cuts for %d: %d\n", id, numberOfCuts);
if(!numberOfCuts) {
printf("%d is done cutting \n", id);
exit(0);
}
}
There are several mistakes in your program:
In the client code, you are calling sem_wait(pillow) and sem_post(pillow);, although the variable pillow has not been initialized. This causes undefined behavior. In order to initialize a semaphore, you can use the functions sem_init or sem_open.
In the client code, you release the mutex queue immediately after acquiring it. Instead, you should only release it when you are finished with the queue operation.
In the client code, you call push(waitroom, id) twice, the second call being immediately after the first call. This does not make sense.
In the barber's main loop, you are releasing the mutexes queue and barber without acquiring them beforehand, then acquire them afterwards. A mutex should normally first be acquired, then released, not the other way around. Use sem_wait for acquiring the mutex, sem_post for releasing it. EDIT: Meanwhile, I believe that you are using the semaphore barber for signalling purposes, not as a mutex. In that case, it is correct to call sem_post without having called sem_wait beforehand.
You are not checking the return value of sem_wait. For example, it is possible that the function fails due to being interrupted by a signal handler.
It is not safe to use the function printf in a signal handler. See this link for more information.
You are not waiting for the child processes to finish, before terminating the parent process.

Message queue is giving me an invalid argument

I'm having some trouble with my code. It should create a message queue and send a message, than wait some time for another program to receive that message and answer. The problem is, when I run it, I get an invalid argument both on the msgsnd and on the msgrcv.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/msg.h>
typedef struct my_msg{
long type;
char text[100];
char sqdr;
}message;
static void score(int messagge_id, char* A_B){
message send;
send.type=1;
strcpy(send.text, "Try to score");
send.sqdr = *A_B;
if((msgsnd(messagge_id, &send, sizeof(send), 0))<0)perror("Error msgsnd\n");
sleep(3);
if((msgrcv(messagge_id, &send, sizeof(send), 4, 0))==-1)perror("Error msgrcv 1\n");
int test=atoi(send.text);
printf("%d\n", test);
}
int main(){
int caso, key;
char team= 'A';
key=1234;
int msg_id=msgget(key, S_IRUSR|S_IWUSR);
printf("Try function score\n");
score(msg_id, &team);
printf("After score\n");
return 0;
}
You need to ensure that the message queue is created. You either use the key IPC_PRIVATE or you add IPC_CREAT to the flags. You also need to try to read the message correctly. You sent a 'type 1' message and attempted to read a 'type 4' message, so the read hangs.
This code also removes the message queue. That's not critical if it is a private queue (such queues are deleted when the program terminates), but it is important for queues using IPC_CREAT and a user-defined key. (I also changed the message text so that atoi() returned something more interesting — and convincing — than zero. The code also uses separate send and receive buffers so that we know the code is not cheating and reusing data already in the buffer.)
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <fcntl.h>
typedef struct my_msg
{
long type;
char text[100];
char sqdr;
} message;
static void score(int messagge_id)
{
message send;
message recv;
send.type = 1;
strcpy(send.text, "47 tries to score");
send.sqdr = 'A';
if ((msgsnd(messagge_id, &send, sizeof(send), 0)) < 0)
perror("Error msgsnd");
printf("Dozing...\n");
sleep(3);
printf("Unslumbering...\n");
if ((msgrcv(messagge_id, &recv, sizeof(recv), -4, 0)) == -1)
perror("Error msgrcv");
int test = atoi(recv.text);
printf("%d\n", test);
}
int main(void)
{
int key = 1234;
int flags = S_IRUSR|S_IWUSR|IPC_CREAT;
// int key = IPC_PRIVATE;
// int flags = S_IRUSR|S_IWUSR;
int msg_id = msgget(key, flags);
if (msg_id < 0)
perror("Error msgget");
else
{
printf("Try function score\n");
score(msg_id);
printf("After score\n");
if (msgctl(msg_id, IPC_RMID, 0) < 0)
perror("Error msgctl");
}
return 0;
}
Sample output:
Try function score
Dozing...
Unslumbering...
47
After score
There's a 3 second pause between 'Dozing' and 'Unslumbering', of course.
use like this:-
if((msgsnd(messagge_id, (void *)&send, sizeof(send), 0))<0)perror("Error msgsnd\n");
if((msgrcv(messagge_id, (void *)&send, sizeof(send), 4, 0))==-1)perror("Error msgrcv 1\n");

Server and Client in C using a Message Queue (no socket code)

I am trying to implement threading, a binary semaphore, and message queues in a client and server program. Everything works, except for the reply in my server to send a response back to the client. When I uncomment the reply, the server loops infinitely after receiving a message.
They both use a message.h header file that is a struct containing:
contents (character), id(int), and mtype(int).
// server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include "message.h"
int sqid, cqid, pid;
msg m, n;
sem_t sem_var;
key_t key;
void requestSem();
void setSem();
void unlockSem();
void reply();
void readQueue();
void listener(void *ptr);
int main() {
key = ftok(".", 'z');
sqid = msgget(key, IPC_CREAT | 0600);
if(sqid < 0) {
perror("Error creating the message queue.\n");
}
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
char *msg1 = "Thread 1";
char *msg2 = "Thread 2";
char *msg3 = "Thread 3";
pthread_create(&thread1, NULL, (void *) &listener, (void *) msg1);
pthread_create(&thread2, NULL, (void *) &listener, (void *) msg2);
pthread_create(&thread3, NULL, (void *) &listener, (void *) msg3);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
}
// always listen for messages
void listener(void *ptr) {
while (1) {
requestSem();
setSem();
readQueue();
unlockSem();
//reply();
}
}
void readQueue()
{
// print the proccess ID and thread ID
printf("Process ID: %d, Thread ID: %d\n", getpid(), pthread_self());
// receive message
if(msgrcv(sqid,&m,sizeof(struct msg),0,0) < 0) {
perror("Error receiving a message.\n");
}
printf("Message was received...\n");
fprintf(stderr, "The contents are: %s\n", &m.contents);
fprintf(stderr, "The client's ID is: %d\n", (&m)->id);
fprintf(stderr, "The thread's ID is: %d\n", pthread_self());
}
void requestSem()
{
// request semaphore (lock)
sem_init(&sem_var, 1, 1);
printf("Semaphore requested.\n");
}
void setSem()
{
// lock semaphore
sem_wait(&sem_var);
printf("Semaphore set.\n");
}
void unlockSem()
{
// unlock semaphore
sem_post(&sem_var);
printf("Semaphore unlocked.\n");
}
void reply()
{
// send reply
printf("got here\n");
pid = (&m)->id;
cqid = msgget(pid, 0600);
n.contents = (&m)->contents;
n.mtype = 0;
n.id = getpid();
if(msgsnd(cqid, &n,sizeof(struct msg),0) < 0) {
perror("Msg send error");
}
msgctl(sqid,IPC_RMID,NULL);
}
// client.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include "message.h"
int main() {
// initialize variables
int mqid, rqid;
msg m;
key_t key;
// initialize message
int pid;
pid = getpid();
m.id = pid;
m.mtype = 1;
// create message queue and response queue
key = ftok(".", 'z');
mqid = msgget(key, 0600);
rqid = msgget(pid, IPC_CREAT | 0600);
if(mqid < 0) {
perror("Error creating the message queue.\n");
}
if (rqid < 0)
{
perror("Error creating the message queue.\n");
}
// loop forever, can send as many messages as you want
while(1) {
fprintf(stderr, "The ID of the client is: %d\n", pid);
// prompt for user input
printf("Enter one character to send: \n");
scanf("%s", &m.contents);
// send message
if(msgsnd(mqid, &m,sizeof(struct msg), 1) < 0) {
perror("Error sending the message.");
}
//msgctl(mqid,IPC_RMID,NULL);
printf("Message was sent successfully!\n");
// receive response
printf("Waiting for a reply...\n");
if(msgrcv(rqid,&m,sizeof(msg), 0, 0) < 0)
{
perror("Error receiving a message.\n");
}
printf("Message was received.\n");
fprintf(stderr, "The contents are: %s\n", &m.contents);
}
}
Thank you in advance for any help. Everything functions as it's supposed to when reply is commented out within listener in the server. Uncommenting it creates an infinite loop.
the reason the server never exits is because each thread has a
while(1)
loop and no way to exit that loop

Usage of message queue fork + execlp + ftok

The main purpose here is to create a message queue by executing thise command : ./create_msg_queue fileForQueue
We want to create the file fileForQueue if it doesn't exist.
Regarding my code, if the file doesn't exist, I get this error
ftok: No such file or directory
So how can I have this file create BEFORE ftok() is called with the file name ?
Provided code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
// IPC and KEYS -------------
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
//---------------------------
#define _XOPEN_SOURCE
// message structure
typedef struct {
// id message
long type;
// Size : 12 bytes
double mesure;
pid_t pidClient;
} message_t;
int main (int argc, char * argv []) {
key_t key;
message_t message;
int file;
int pid;
// Check arg number
if (argc != 2) {
fprintf(stderr, "Syntaxe : %s fichier_clé\n",argv[0]);
exit(EXIT_FAILURE);
}
// Create the empty file given in parameter
if ((pid = vfork()) == -1)
{
perror("fork");
exit(1);
}
if (pid)
{
// code replacement to create the FILE
execlp("touch", "touch", argv[1], NULL);
perror("execlp");
exit(1);
}else
{
// Trying to wait for the forked() process to finish its file creation
wait (NULL);
// I GET ERROR HERE IF THE FILE DON'T EXIST BEFORE I LAUNCH THE PROGRAM
// Create key with the file given in parameter and then created
if ((key = ftok(argv[1], 0)) == -1) {
// errNo value
perror("ftok");
exit(EXIT_FAILURE);
}
// Create message queue
if ((file = msgget(key, IPC_CREAT | 0666)) == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
// Registering message content
message.type = 1;
message.mesure = -1;
message.pidClient = getpid();
// Sending message
if (msgsnd(file, (void *) & message, 12, 0) <0) {
perror("msgsnd");
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}
Thanks in advance for help. I struggle :)
key_t ftok(const char *path, int id);
ftok(argv[1], 0))
Only the low-order 8-bits of id are significant. The behavior of
ftok() is unspecified if these bits are 0.
Also note that you have to
#define _XOPEN_SOURCE
at the very begin

Resources