Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #define WORDSIZE 64
- #define BUFFSIZE 2048
- #define SIZE 128
- #define MSGSIZE sizeof(msg)-sizeof(long)
- #define WRITER 2
- #define DIM 100
- /* struttura dei messaggi nella coda*/
- typedef struct {
- long type;
- char stop;
- char riga[BUFFSIZE];
- } msg;
- /* reader legge il contenuto del file indicato e lo passa al padre
- * tramite una pipe unidirezionale */
- void reader(int pipefd, char *url) {
- char buffer[BUFFSIZE];
- struct stat statbuf;
- FILE *input_fs, *output_fs;
- if( (input_fs = fopen(url, "r")) == NULL) {
- perror("fopen");
- exit(1);
- }
- if( (stat(url, &statbuf)==-1) || (!S_ISREG(statbuf.st_mode)) ){
- perror("stat");
- exit(1);
- }
- /*inserire qui controllo file*/
- if( (output_fs = fdopen(pipefd, "w")) == NULL) {
- perror("pipe");
- exit(1);
- }
- while(fgets(buffer, BUFFSIZE, input_fs)!=NULL) {
- fputs(buffer, output_fs);
- }
- fclose(input_fs);
- exit(0);
- }
- /* writer visualizza a video le righe ricevute dal padre */
- void writer(int qid) {
- msg messaggio;
- fprintf(stdout, "W> Sono il writer.\n");
- while(1) {
- msgrcv(qid, &messaggio, MSGSIZE, WRITER, 0);
- if(messaggio.stop == 1)
- break;
- fprintf(stdout, "W> %s\n", messaggio.riga);
- }
- fprintf(stdout, "END WRITER\n");
- exit(0);
- }
- /* ricerca una parola all'interno di una stringa, restituisce 0 se contenuta nella riga */
- int ricerca(char *key, char *stringa) {
- char * ret;
- if((ret = strstr(stringa, key))==NULL)
- return 1;
- return 0;
- }
- /* parent legge il contenuto ricevuto riga per riga e seleziona solo le
- * righe contenenti la parola indicata, passandole poi una alla volta al
- * writer tramite coda di messaggi */
- int main(int argc, char **argv) {
- /* dichiarazione delle variabili */
- char key[WORDSIZE]; //la parola da ricercare
- int qid;
- int pipefd[2];//0 lettura 1 scrittura
- int a = 0;
- msg messaggio;
- FILE *input_fs;
- char buffer[BUFFSIZE];
- messaggio.stop = 0;
- /* controllo sui parametri */
- if(argc!=3) {
- fprintf(stderr, "P> Utilizzo: %s <parola> <file-di-testo>\n", argv[0]);
- exit(1);
- }
- strcpy(key, argv[1]); //memorizzo la parola
- /* pipe unidirezionale*/
- if(pipe(pipefd)==-1) {
- perror("pipe");
- exit(1);
- }
- /* coda di messaggi */
- if((qid = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600))==-1) {
- perror("msgget");
- exit(1);
- }
- /* creazione dei processi figlio */
- if(fork()==0) {
- close(pipefd[0]);//chiude canale input sulla pipe nel re!der
- reader(pipefd[1], argv[2]);
- }
- if(fork()==0)
- writer(qid);
- /* lettura ed invio delle righe esatte */
- close(pipefd[1]);
- if( (input_fs = fdopen(pipefd[0], "r")) == NULL) {
- perror("pipe");
- exit(1);
- }
- while(1) {
- fgets(buffer, BUFFSIZE, input_fs);
- if(buffer == NULL || strcmp(buffer, "") == 0){
- printf("SANTI\n");
- break;
- }
- if( ricerca(argv[1], buffer) == 0) {
- strcpy(messaggio.riga, buffer);
- messaggio.type = WRITER;
- msgsnd(qid, &messaggio, MSGSIZE, 0);
- }
- }
- messaggio.stop = 1;
- messaggio.type = WRITER;
- msgsnd(qid, &messaggio, MSGSIZE, 0);
- /* chiusura delle strutture */
- wait(NULL);
- //wait(NULL);
- //msgctl(qid, IPC_RMID, NULL);
- fprintf(stdout, "P> Coda rimossa.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement