chunalt787 28 Junior Poster in Training

I am new to message queues and IPC in general. I am trying to have a process establish the queue and send a message. Then I fork and the child will read the message. The goal is to measure the execution time for different sized messages.

The msgsnd is returning a -1 and I have no idea as to why its not sending anything. Any help is appreciated. Thanks in advance

int main() {



	//establish the character array to be passed around
	int msgSize = 1024;

	char charArray[msgSize];
	
	clock_t timeStampBegin;

	clock_t timeStampEnd;



	key_t key = 1234; /* key to be passed to msgget() */ 

	int msqid; /* return value from msgget() */ 



	//establish the message queue and make sure that it properly established

	if ((msqid = msgget(key, IPC_CREAT)) == 1) {

		perror("msgget: msgget failed");

		//exit(1);

	} else {

		(void) fprintf(stderr, "msgget succeeded\n"); 

	}
	
	//Take an initial timestamp

	timeStampBegin = clock();
	//printf("\nTimeStampBegin = %d\n", timeStampBegin);
	
	//int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)

	int amountSent = msgsnd(1234, &charArray, msgSize, 0);
	printf("%d bytes were sent", amountSent);



	if(fork() == 0) {

	//this is the child, this is the one that we want to recieve the info.
		char recievedArray[100000];

		if((size_t)msgSize == msgrcv(1234, &recievedArray, msgSize, 0, 0))
			printf("Mesage was recieved");
		timeStampEnd = clock();
		//printf("Time for transit of size %d = %f", msgSize, (double)(timeStampEnd-timeStampBegin));

	}
	
	printf("\n");
	
	return 0;



}

Output

msgget succeeded
-1 bytes were sent
-1 bytes were sent
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.