vuoi
o PayPal
tutte le volte che vuoi
I tre processi interagiscono tramite scambio di messaggi nel seguente modo:
Il processo P1 legge gli input e effettua una chiamata al processo P2, passandogli via messaggio l'input letto e sospendendosi nel frattempo.
Il processo P1 manda gli input già passati per il modificatore al visualizzatore, senza sospendersi.
Il visualizzatore è continuamente in attesa di ricevere stringhe, le quali vengono poi mostrate a video.
// Programma che realizza tre processi
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define RTS 1
#define OKTS 2
#define RPC 3
#define VIEW 4
struct message {
int tipo;
char frase[80];
};
void sendsinc(int msgid, char stringa[80], int tipo);
void receivesinc(int msgid, char stringa[80], int tipo);
void sendasinc(int msgid, char stringa[80], int tipo);
int receiveasinc(int msgid, char stringa[80], int tipo);
void sendasinc(int msgid, char stringa[80], int tipo){
struct message m;
m.tipo=tipo;
strcpy(m.frase,stringa);
msgsnd(msgid,
(struct message *)&m,sizeof (m.frase), 0);}
int receiveasinc(int msgid, char stringa[80], int tipo){
struct message m;
int res;
res=msgrcv(msgid, (struct message *)&m,sizeof(m.frase), tipo, IPC_NOWAIT);
if (res!=-1)
strcpy(stringa, m.frase);
return res;
}
void sendsinc(int msgid, char stringa[80], int tipo){
struct message m1,m2,m3;
m1.tipo=RTS;
strcpy(m1.frase,"Request to Send");
msgsnd(msgid, (struct message *)&m1,sizeof(m1.frase), 0);
//printf("--- Invio di%s\n",m1.frase);
msgrcv(msgid, (struct message *)&m2,sizeof(m2.frase), OKTS, 0);
//printf("--- Ricezione di%s\n",m2.frase);
if (strcmp((const char *) m2.frase,"OK to Send")==0){
m3.tipo=tipo;
strcpy(m3.frase,stringa);
msgsnd(msgid, (struct msgbuf*)&m3, sizeof (m3.frase), 0);
//printf("--- Invio di%s\n",m3.frase);
}
}
void receivesinc(int msgid, char stringa[80], int tipo){
struct message m1,m2,m3;
msgrcv(msgid, (struct message *) &m1,sizeof(m1.frase), RTS, 0);
//printf("-----
Ricezione di%s\n",m1.frase);
if (strcmp((const char *) m1.frase,"Request to Send")==0){
m2.tipo=OKTS;
strcpy(m2.frase,"OK to Send");
msgsnd(msgid, (struct msgbuf*)&m2, sizeof (m2.frase), 0);
//printf("----- Invio di%s\n",m2.frase);
msgrcv(msgid, (struct message*)&m3, sizeof(m3.frase), tipo, 0);
//printf("----- Ricezione di%s\n",m3.frase);
strcpy(stringa, m3.frase);
}