Hello I am working on a C program for an IPC, this is my assignment below: IPCs.
ID: 3678058 • Letter: H
Question
Hello I am working on a C program for an IPC, this is my assignment below: IPCs. In each case you are to write a server to initialize the IPC structure, and then fork a client (which you also write) to process the structure created. 1) Write a server and client for message queues. The server should enqueue several messages (the message data can be a string of characters), and the client should dequeue it and write the message data read to an output file. Please help me with this!! I am totally lost on this section of C code!
My assignment:
IPCs: this exercise has 2 parts. In each case you are to write a server to initialize the IPC structure, and then fork a client (which you also write) to process the structure created. 1) Write a server and client for message queues (use the semaphore code covered in the lecture and posted to Angel as a template). The server should enqueue several messages (the message data can be a string of characters), and the client should dequeue it and write the message data read to an output file. 2) Write a server and client for shared memory. The server should deposit some (character) data into shared memory and the client should read what is there, and write the results to an output file.
My Server code so far:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
void dieError(char *msg)
{
perror(msg);
exit(1);
}
typedef struct messageBuffer
{
long msgType; msg=0;
char message[MAXSIZE];
printf(" Enter the message" );
scanf("%c", msg);
strlen(msg);
};
int main()
{
int msgId;
int flag = IPC_CREAT | 0666;
key_t key;
struct messageBuffer buff;
size_t bufferLength;
FILE *fileptr;
key = 1234;
if ((msgId = msgget(key, flag )) < 0)
dieError("msgget");
buff.msgType = 1;
printf("Enter meesage: ");
scanf("%[^ ]",buff.message);
getchar();
bufferLength = strlen(buff.message) + 1 ;
if (msgsnd(msgId, &buff, bufferLength, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %s, %zu ", msgId, buff.msgType, buff.message, bufferLength);
dieError("message send");
}
else
printf("Message as been Sent ");
exit(0);
if ((msgId = msgget(key, 0666)) < 0)
break("message get()");
fileptr=fopen("output.txt","w");
if (msgrcv(msgId, &receiverBuffer, MAXSIZE, 1, 0) < 0)
die("message receive");
printf("%s ", receiverBuffer.message);
fprintf(fileptr,"%s",receiverBuffer.message);
exit(0);
}
My reciever code so far:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
//error occured.. closing server
void dieError(char *msg)
{
perror(msg);
exit(1);
}
//storing message
typedef struct messageBuffer
{
long msgType;
char message[MAXSIZE];
};
int main()
{
//variables declared
int msgId;
key_t key;
struct messageBuffer receiverBuffer;
FILE *fileptr;
//setting key
key = 1234;
if ((msgId = msgget(key, 0666)) < 0)
break("message get()");
//opening file
fileptr=fopen("output.txt","w");
//receiving message
if (msgrcv(msgId, &receiverBuffer, MAXSIZE, 1, 0) < 0)
die("message receive");
//recieved message
printf("%s ", receiverBuffer.message);
//writing message to output file
fprintf(fileptr,"%s",receiverBuffer.message);
exit(0);
}
Explanation / Answer
// shared memory
Get back if any queries
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.