Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // message-types.h
- #pragma once
- typedef struct message_from_client {
- long mtype;
- struct info_from_client {
- float a;
- int b;
- long pid;
- } info;
- } message_from_client;
- typedef struct message_from_server {
- long mtype;
- struct info_from_server {
- float c;
- } info;
- } message_from_server;
- // server.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include "message-types.h"
- int main()
- {
- int msqid;
- char pathname[] = "server.c";
- key_t key = ftok(pathname, 0);
- (void) umask(0000);
- if ((msqid = msgget(key, 0666 | IPC_CREAT | IPC_EXCL)) < 0) {
- if (errno == EEXIST) { printf("Error1s\n"); exit(-1); }
- else { printf("Error2s\n"); exit(-1); }
- }
- message_from_server snd;
- message_from_client rcv;
- int maxlen_c = sizeof(struct info_from_client);
- int maxlen_s = sizeof(struct info_from_server);
- while (1) {
- if (msgrcv(msqid, (message_from_client*) &rcv, maxlen_c, 1, 0) < 0) { printf("Error3s\n"); exit(-1); }
- printf("Server received from %ld, contents: a = %f, b = %d\n", rcv.info.pid, rcv.info.a, rcv.info.b);
- if (rcv.info.pid < 0)
- break;
- snd.mtype = rcv.info.pid;
- snd.info.c = (float) rcv.info.a * rcv.info.b;
- if (msgsnd(msqid, (message_from_server*) &snd, maxlen_s, 0) < 0) { printf("Error4s\n"); exit(-1); }
- printf("Server sent to %ld, result: c = %f\n", snd.mtype, snd.info.c);
- }
- if (msgctl(msqid, IPC_RMID, (struct msqid_ds*) NULL) < 0) { printf("Error5s\n"); exit(-1); }
- return 0;
- }
- // client.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <time.h>
- #include "message-types.h"
- int main() {
- char pathname[] = "server.c";
- key_t key = ftok(pathname, 0);
- int msqid;
- if ((msqid = msgget(key, 0)) < 0) { printf("Error1c\n"); exit(-1); }
- message_from_client snd;
- snd.mtype = 1;
- srand(time(0));
- snd.info.b = rand() % 512;
- snd.info.a = (float) rand() / RAND_MAX;
- snd.info.pid = (long) getpid();
- if (msgsnd(msqid, (message_from_client*) &snd, sizeof (struct info_from_client), 0) < 0) { printf("Error2c\n"); exit(-1); }
- printf ("Client %ld sent numbers a = %f, b = %d to the server\n", snd.info.pid, snd.info.a, snd.info.b);
- message_from_server rcv;
- if (msgrcv(msqid, (message_from_server*) &rcv, sizeof (struct info_from_server), (int) getpid(), 0) < 0) { printf("Error3c\n"); exit(-1); }
- printf ("Client %d received from the server, type: %ld, result: c = %f\n", (int) getpid(), rcv.mtype, rcv.info.c);
- return 0;
- }
- // killer.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include "message-types.h"
- int main() {
- char pathname[] = "server.c";
- key_t key = ftok(pathname, 0);
- int msqid;
- if ((msqid = msgget(key, 0)) < 0) { printf("Error1k\n"); exit(-1); }
- message_from_client snd;
- snd.mtype = 1;
- snd.info.pid = -1;
- if (msgsnd(msqid, (message_from_client*) &snd, sizeof (struct info_from_client), 0) < 0) { printf("Error2c\n"); exit(-1); }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment