Screwface
09-09-2008, 17:26
Ciao a tutti. Avevo già postato qualche giorno fa una questione circa questo programmino che sto tentando di scrivere. E' saltato fuori un altro problema: infatti il programma prende correttamente in ingresso il messaggio, lo invia al secondo processo, che lo invia al terzo il quale lo memorizza su un file apposito. Se mi limito a questo tutto funziona perfettamente. Il problema nasce quando voglio mandare un messaggio di conferma dell'avvenuta operazione indietro al secondo processo: infatti, il primo inserimento di un nuovo messaggio va a buon fine, ma poi il secondo processo, quando si tratta di inserire altri messaggi, cessa di funzionare correttamente e in pratica viene saltato. Io sospetto che ci siano dei problemi con le wait che ho messo, ma ho provato a spostarle ovunque e il programma dà esiti ancor più disastrosi...:muro: Se qualcuno potesse darmi un consiglio sarebbe una manna dal cielo:)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>
#define MSGSZ 128
main ()
{
FILE *fp1, *fp2, *fp3;
char nomefile[70];
int fs_res;
size_t wr_res;
int remove_res;
int msqid, msqid1, msqid2, msqid3, msqidK;
int status;
int msgflg = IPC_CREAT | 0666;
key_t key;
key_t key1, key2, key3, keyK, keyK2;
size_t buf_length;
int pid_interfaccia;
int pid_upost, pid1, pid2, pid3;
int temp, temp1, temp2, temp3, temp4, temp5;
int risposta;
int i;
int c;
int maxmsgsz;
int msgsz;
struct msgbuf {
long mtype;
char mtext[MSGSZ];
long destinatario;
} *sbuf, *rbuf, *rbuf1, *rbuf2, *rbuf3, *rbufK, *rbufK2;
do
{
printf("\n\t\t1) Inviare un nuovo messaggio");
printf("\n\t\t2) Visualizzare i messaggi memorizzati");
printf("\n\t\t3) Eliminare i messaggi memorizzati");
printf("\n\t\t4) Uscire dal programma");
printf("\n\n\t\tScegliere un'opzione e premere INVIO\n");
sbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
do
{
scanf("%d", &risposta);
} while ((risposta < 1) || (risposta > 4));
key = ftok("/home/...../file.tmp", 'a');
if (msqid = msgget(key, msgflg) < 0)
{
perror("\nErrore nella msgget\n");
exit(1);
}
buf_length = MSGSZ;
switch(risposta) {
case 1:
{
sbuf->mtype = 1;
printf("\nInserire il numero del destinatario: ");
scanf("%d",&sbuf->destinatario);
if ((sbuf->destinatario < 1) || (sbuf->destinatario > 3))
{
printf("\nErrore: inserire un numero compreso fra 1 e 3\n");
exit(-1);
}
printf("\nInserire il messaggio (max 128 caratteri): ");
while ((c = getchar()) != '\n' && c != EOF);
for (i = 0; ((c = getchar()) != '\n'); i++){
sbuf->mtext[i] = c;
}
sbuf->mtext[strlen(sbuf->mtext)] = '\0';
break;
}
case 2:
{
sbuf->mtype = 2;
printf("\nInserire il numero dell'archivio: ");
scanf("%d", &sbuf->destinatario);
if ((sbuf->destinatario < 1) || (sbuf->destinatario > 3))
{
printf("\nErrore: inserire un numero compreso fra 1 e 3\n");
exit(-1);
}
strcpy(sbuf->mtext, "Richiesta visualizzazione archivio ");
break;
}
case 3:
{
sbuf->mtype = 3;
printf("\nInserire il numero dell'archivio da cancellare: ");
scanf("%d", &sbuf->destinatario);
if ((sbuf->destinatario < 1) || (sbuf->destinatario > 3))
{
printf("\nErrore: inserire un numero compreso fra 1 e 3\n");
exit(-1);
}
strcpy(sbuf->mtext, "Richiesta eliminazione archivio ");
break;
}
case 4:
{
exit(0);
}
} /*fine switch*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0)
{
perror("\nErrore nella msgsnd\n");
exit(1);
}
pid_upost = fork();
if (pid_upost == 0)
{
rbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbuf == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(1);
}
temp = msgrcv(msqid, &rbuf, MSGSZ, 0, MSG_NOERROR);
if (temp < 0)
{
perror("\nErrore nella msgrcv\n");
exit(1);
}
switch (rbuf->destinatario) {
case 1:
{
key1 = ftok("/home/...../file1.tmp", 'b');
if (msqid1 = msgget(key1, IPC_CREAT | 0666) < 0)
{
perror("\nErrore nella msgget\n");
exit(-1);
}
buf_length = MSGSZ;
if (msgsnd(msqid1, &rbuf, buf_length, IPC_NOWAIT) < 0)
{
perror("\nErrore nella msgsnd\n");
exit(1);
}
pid1 = fork();
if (pid1 == 0)
{
rbuf1 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbuf1 == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(-1);
}
temp1 = msgrcv(msqid1, &rbuf1, MSGSZ, 0, MSG_NOERROR);
if (temp1 < 0)
{
perror("\nErrore nella msgrcv\n");
exit(1);
}
if ((fp1 = fopen("/home/...../Scrivania/archivio1", "a")) == NULL)
{
printf("\nErrore nella fopen\n");
}
fprintf(fp1, "Destinatario: %d\n", rbuf1->destinatario);
fprintf(fp1, "Messaggio: %s\n\n", rbuf1->mtext);
fclose(fp1);
/*---------------FINO A QUI TUTTO FUNZIONA--------------------------------------------------------------*/
rbufK = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbufK == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(1);
}
if (rbuf1->mtype == 3)
{
if (remove_res = remove("archivio1") != 0)
{
strcpy(rbufK->mtext, "Impossibile rimuovere il file");
}
else
{
strcpy(rbufK->mtext, "Archivio 1 rimosso");
}
}
else
{
strcpy(rbufK->mtext, "Messaggio inserito correttamente");
}
keyK = ftok("/home/...../file4.tmp", 'e');
if (msqidK = msgget(keyK, IPC_CREAT | 0666) < 0)
{
perror("\nErrore nella msgget\n");
exit(1);
}
if (msgsnd(msqid1, &rbufK, buf_length, IPC_NOWAIT) < 0)
{
perror("\nErrore nella msgsnd\n");
exit(1);
}
}
break;
} /*fine case 1*/
case 2:
{
/*esattamente come sopra, ma scrivo sul file archivio2 e creo un processo pid2 */
} /*fine case 2 */
case 3:
{
/*anche qui come sopra */
} /*fine case 3 */
}/*fine switch*/
wait(&status);
if (msqidK = msgget(keyK, IPC_CREAT | 0666) < 0)
{
perror("\nErrore nella msgget\n");
exit(1);
}*/
rbufK2 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbufK2 == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(1);
}
temp4 = msgrcv(msqid1, &rbufK2, MSGSZ, 0, MSG_NOERROR);
if (temp4 < 0)
{
perror("\nErrore nella msgrcv\n");
exit(1);
}
else
{
printf("\nMESSAGGIO %s RICEVUTO \n", rbufK2->mtext);
}
}
wait(&status);
}
while (risposta != 4);
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>
#define MSGSZ 128
main ()
{
FILE *fp1, *fp2, *fp3;
char nomefile[70];
int fs_res;
size_t wr_res;
int remove_res;
int msqid, msqid1, msqid2, msqid3, msqidK;
int status;
int msgflg = IPC_CREAT | 0666;
key_t key;
key_t key1, key2, key3, keyK, keyK2;
size_t buf_length;
int pid_interfaccia;
int pid_upost, pid1, pid2, pid3;
int temp, temp1, temp2, temp3, temp4, temp5;
int risposta;
int i;
int c;
int maxmsgsz;
int msgsz;
struct msgbuf {
long mtype;
char mtext[MSGSZ];
long destinatario;
} *sbuf, *rbuf, *rbuf1, *rbuf2, *rbuf3, *rbufK, *rbufK2;
do
{
printf("\n\t\t1) Inviare un nuovo messaggio");
printf("\n\t\t2) Visualizzare i messaggi memorizzati");
printf("\n\t\t3) Eliminare i messaggi memorizzati");
printf("\n\t\t4) Uscire dal programma");
printf("\n\n\t\tScegliere un'opzione e premere INVIO\n");
sbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
do
{
scanf("%d", &risposta);
} while ((risposta < 1) || (risposta > 4));
key = ftok("/home/...../file.tmp", 'a');
if (msqid = msgget(key, msgflg) < 0)
{
perror("\nErrore nella msgget\n");
exit(1);
}
buf_length = MSGSZ;
switch(risposta) {
case 1:
{
sbuf->mtype = 1;
printf("\nInserire il numero del destinatario: ");
scanf("%d",&sbuf->destinatario);
if ((sbuf->destinatario < 1) || (sbuf->destinatario > 3))
{
printf("\nErrore: inserire un numero compreso fra 1 e 3\n");
exit(-1);
}
printf("\nInserire il messaggio (max 128 caratteri): ");
while ((c = getchar()) != '\n' && c != EOF);
for (i = 0; ((c = getchar()) != '\n'); i++){
sbuf->mtext[i] = c;
}
sbuf->mtext[strlen(sbuf->mtext)] = '\0';
break;
}
case 2:
{
sbuf->mtype = 2;
printf("\nInserire il numero dell'archivio: ");
scanf("%d", &sbuf->destinatario);
if ((sbuf->destinatario < 1) || (sbuf->destinatario > 3))
{
printf("\nErrore: inserire un numero compreso fra 1 e 3\n");
exit(-1);
}
strcpy(sbuf->mtext, "Richiesta visualizzazione archivio ");
break;
}
case 3:
{
sbuf->mtype = 3;
printf("\nInserire il numero dell'archivio da cancellare: ");
scanf("%d", &sbuf->destinatario);
if ((sbuf->destinatario < 1) || (sbuf->destinatario > 3))
{
printf("\nErrore: inserire un numero compreso fra 1 e 3\n");
exit(-1);
}
strcpy(sbuf->mtext, "Richiesta eliminazione archivio ");
break;
}
case 4:
{
exit(0);
}
} /*fine switch*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0)
{
perror("\nErrore nella msgsnd\n");
exit(1);
}
pid_upost = fork();
if (pid_upost == 0)
{
rbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbuf == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(1);
}
temp = msgrcv(msqid, &rbuf, MSGSZ, 0, MSG_NOERROR);
if (temp < 0)
{
perror("\nErrore nella msgrcv\n");
exit(1);
}
switch (rbuf->destinatario) {
case 1:
{
key1 = ftok("/home/...../file1.tmp", 'b');
if (msqid1 = msgget(key1, IPC_CREAT | 0666) < 0)
{
perror("\nErrore nella msgget\n");
exit(-1);
}
buf_length = MSGSZ;
if (msgsnd(msqid1, &rbuf, buf_length, IPC_NOWAIT) < 0)
{
perror("\nErrore nella msgsnd\n");
exit(1);
}
pid1 = fork();
if (pid1 == 0)
{
rbuf1 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbuf1 == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(-1);
}
temp1 = msgrcv(msqid1, &rbuf1, MSGSZ, 0, MSG_NOERROR);
if (temp1 < 0)
{
perror("\nErrore nella msgrcv\n");
exit(1);
}
if ((fp1 = fopen("/home/...../Scrivania/archivio1", "a")) == NULL)
{
printf("\nErrore nella fopen\n");
}
fprintf(fp1, "Destinatario: %d\n", rbuf1->destinatario);
fprintf(fp1, "Messaggio: %s\n\n", rbuf1->mtext);
fclose(fp1);
/*---------------FINO A QUI TUTTO FUNZIONA--------------------------------------------------------------*/
rbufK = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbufK == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(1);
}
if (rbuf1->mtype == 3)
{
if (remove_res = remove("archivio1") != 0)
{
strcpy(rbufK->mtext, "Impossibile rimuovere il file");
}
else
{
strcpy(rbufK->mtext, "Archivio 1 rimosso");
}
}
else
{
strcpy(rbufK->mtext, "Messaggio inserito correttamente");
}
keyK = ftok("/home/...../file4.tmp", 'e');
if (msqidK = msgget(keyK, IPC_CREAT | 0666) < 0)
{
perror("\nErrore nella msgget\n");
exit(1);
}
if (msgsnd(msqid1, &rbufK, buf_length, IPC_NOWAIT) < 0)
{
perror("\nErrore nella msgsnd\n");
exit(1);
}
}
break;
} /*fine case 1*/
case 2:
{
/*esattamente come sopra, ma scrivo sul file archivio2 e creo un processo pid2 */
} /*fine case 2 */
case 3:
{
/*anche qui come sopra */
} /*fine case 3 */
}/*fine switch*/
wait(&status);
if (msqidK = msgget(keyK, IPC_CREAT | 0666) < 0)
{
perror("\nErrore nella msgget\n");
exit(1);
}*/
rbufK2 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
if (rbufK2 == NULL) {
printf("\n\nErrore: impossibile allocare il messaggio");
exit(1);
}
temp4 = msgrcv(msqid1, &rbufK2, MSGSZ, 0, MSG_NOERROR);
if (temp4 < 0)
{
perror("\nErrore nella msgrcv\n");
exit(1);
}
else
{
printf("\nMESSAGGIO %s RICEVUTO \n", rbufK2->mtext);
}
}
wait(&status);
}
while (risposta != 4);
}