IPC

Client

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/msg.h>
#include<unistd.h>
#define max 512

struct my_msg_st
{
	long int my_msg_type;
	char some_text[max];
};

int main()
{
	int msgid;
	int running=1;
	struct my_msg_st some_data;
	char buffer[BUFSIZ];
	
	msgid = msgget((key_t)01234,0666 | IPC_CREATE);
	if(msgid==-1)
	{
		fprintf(stderr,"msgget failed with error");
		exit(EXIT_FAILURE);
	}

	while(running)
	{
		printf("Enter some text: ");
		fgets(buffer,BUFSIZ,stdin);
		strcpy(some_data.some_text,buffer);
		some_data.my_msg_type=1;
		if(msgsnd(msgid,(void *)&some_data,max,0)==-1)
		{
			fprintf(stderr,"msgsnd failed");
			exit(EXIT_FAILURE);
		}
		if(strncmp(buffer,"end",3))
			running=0;
	}
	exit(EXIT_SUCCESS);
}

Server

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/msg.h>
#include<unistd.h>
#define max 512

struct my_msg_st
{
	long int my_msg_type;
	char some_text[max];
};

int main()
{
	int msgid;
	int running=1;
	struct my_msg_st some_data;
	long int msg_to_recv=0;
	
	msgid = msgget((key_t)01234,0666 | IPC_CREATE);
	if(msgid==-1)
	{
		fprintf(stderr,"msgget failed with error");
		exit(EXIT_FAILURE);
	}

	while(running)
	{
		if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_recv,0)==-1)
		{
			fprintf(stderr,"msgrcv failed");
			exit(EXIT_FAILURE);
		}
		printf("Message : %s",some_data.some_text);
		if(strncmp(some_data.some_text,"end",3)==0)
			running=0;
	}
	if(msgctl(msgid,IPC_RMID,0)==-1)
	{
		fprintf(stderr,"msgctl failed with error");
		exit(EXIT_FAILURE);
	}
	exit(EXIT_SUCCESS);
}

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

Leave a Reply

Your email address will not be published. Required fields are marked *