FIFOs: Full duplex communication between two independent processes. First process accepts sentences and writes on one pipe to be read by second process and second process counts number of characters, number of words and number of lines in accepted sentences, writes this output in a text file and writes the contents of the file on second pipe to be read by first process and displays on standard output.




//Program : os5b.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>

#define MAX_BUF 1024

int main()
{
    int fd,c=0;
    char *fifo1="fifo1";
    char *fifo2="fifo2";
    int fd1;
    int words=1,lines=1,chars=0;
    char buf1[MAX_BUF];
    mkfifo(fifo1,0666);
    fd=open(fifo1,O_RDWR);
    char str;
    printf("\nEnter the String:");
    while ((str=getchar())!='#')
    buf1[c++]=str;
    buf1[c]='\0';
    write(fd,buf1,sizeof(buf1));
    close(fd);
    unlink(fifo1);
    fd1=open(fifo2,O_RDWR);
    read(fd1,buf1,sizeof(buf1));
    printf("\nThe contents of file are %s\n",buf1);
    int i=0;
      while(buf1[i]!='\0')
          {
            if(buf1[i]==' '||buf1[i]=='\n')
             {words++;}
            else
             {chars++;}
             if(buf1[i]=='\n')
             { lines++;}
              i++;
           }
     printf("\n No of Words: %d",words);
     printf("\n No of Characters: %d",chars);
     printf("\n No of Lines: %d",lines);
    close(fd1);
    return 0;
}

//OUTPUT

[email protected]70:~/Desktop/os/5$ gcc os5b.c
[email protected]70:~/Desktop/os/5$ ./a.out

Enter the String:hello my name is Huzaif(Professional Cipher) the string ends with (hash sign) #         

The contents of file are hello my name is Huzaif(Professional Cipher) the string ends with (hash sign) 

 No of Words: 13
 No of Characters: 66
 No of Lines: [email protected]:~/Desktop/os/5$ 



FIFOs: Full duplex communication between two independent processes. First process accepts sentences and writes on one pipe to be read by second process and second process counts number of characters, number of words and number of lines in accepted sentences, writes this output in a text file and writes the contents of the file on second pipe to be read by first process and displays on standard output. FIFOs: Full duplex communication between two independent processes. First process accepts sentences and writes on one pipe to be read by second process and second process counts number of characters, number of words and number of lines in accepted sentences, writes this output in a text file and writes the contents of the file on second pipe to be read by first process and displays on standard output. Reviewed by Huzaif Sayyed on April 17, 2017 Rating: 5

No comments:

Powered by Blogger.