Programma in C concorrente con tre processi distinti
Il seguente programma in C implementa un sistema concorrente che utilizza tre distinti processi per leggere, modificare e visualizzare stringhe. I processi interagiscono tramite passaggio di messaggi. Ecco una spiegazione dettagliata e il codice per ciascun processo.
Descrizione dei processi
- Processo P1: Legge input da tastiera e invia il dato letto al processo P2 per la modifica. Dopo l'invio, continua a mandare l'input già passato al visualizzatore.
- Processo P2: Riceve stringhe dal processo P1, le trasforma aggiungendo il simbolo ® alla fine, e le invia al processo P3.
- Processo P3: Visualizza continuamente le stringhe ricevute dal processo P2.
Interazione tra i processi
Il processo P1 invia le stringhe lette al processo P2 e attende la risposta. Nel frattempo, P1 continua a inoltrare le stringhe al processo P3 per la visualizzazione. Il processo P3, a sua volta, è in attesa continua per ricevere e mostrare le stringhe elaborate.
Codice del programma
Nel codice seguente, viene definita una struttura per i messaggi e varie funzioni per la gestione della comunicazione tra i 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);
msgrcv(msgid, (struct message *)&m2, sizeof(m2.frase), OKTS, 0);
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);
}
}
void receivesinc(int msgid, char stringa[80], int tipo) {
struct message m1, m2, m3;
msgrcv(msgid, (struct message *)&m1, sizeof(m1.frase), RTS, 0);
if (strcmp((const char *)m1.frase, "Request to Send") == 0) {
strcpy(m2.frase, "OK to Send");
m2.tipo = OKTS;
msgsnd(msgid, (struct message *)&m2, sizeof(m2.frase), 0);
msgrcv(msgid, (struct message *)&m3, sizeof(m3.frase), tipo, 0);
strcpy(stringa, m3.frase);
}
}
Questo codice fornisce le funzioni di base per la comunicazione tra i processi tramite messaggi, permettendo loro di sincronizzarsi e scambiarsi dati. Le funzioni `sendsinc` e `receivesinc` gestiscono l'invio e la ricezione sincrona di messaggi, mentre `sendasinc` e `receiveasinc` gestiscono la comunicazione asincrona.
-
Sistemi operativi - teoria completa
-
Sistemi operativi
-
Appunti di Sistemi operativi
-
Sistemi operativi - Appunti