Advertisement
Guest User

programma 1

a guest
Nov 30th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <ctype.h>
  9. #define MAX 100
  10.  
  11. int main (int argc, char* argv[]){
  12.  
  13. int file_descriptor[2];
  14. pid_t pid1,pid2;
  15. char string[MAX];
  16. int n;
  17. int i;
  18. int len;
  19. char buff[33];
  20.  
  21.  
  22.  
  23.  
  24. if(pipe(file_descriptor)==0){
  25.     pid1=fork();
  26.  
  27.    
  28.  
  29. if(pid1!=0){
  30.     //padre
  31.     pid2=fork();
  32.     if(pid2==0){
  33.         //PRODUCER WRITES INTO PIPE
  34.         close(file_descriptor[0]);
  35.         while(1){
  36.         sleep(1);
  37.         printf("Write row of the file\n");
  38.         fgets(string,MAX,stdin);
  39.         len=strlen(string);
  40.         sprintf(buff,"%d",len);
  41.         write(file_descriptor[1],buff,33);
  42.         n=write(file_descriptor[1],string,strlen(string));
  43.         printf("I wrote %d bytes\n",n);
  44.         if(strcmp(string,"end\n")==0){
  45.             exit(0);
  46.         }}
  47.         }else{
  48.         //CONSUMER READS FROM PIPE
  49.         close(file_descriptor[1]);
  50.         while(1){
  51.         read(file_descriptor[0],buff,33);
  52.         len=atoi(buff);
  53.         n=read(file_descriptor[0],string,len);
  54.         printf("I read %d bytes\n",n);
  55.         string[len+1]='\0';
  56.         for(i=0;i<strlen(string);i++){
  57.             string[i]=toupper(string[i]);
  58.  
  59.         }
  60.         printf("%s",string);
  61.         if(strcmp(string,"END")==0){
  62.             exit(0);
  63.         }}
  64.     }
  65. }
  66.  
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73. return(0);
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement