Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <string.h>
- #include <signal.h>
- #include <ctype.h>
- #define MAX 100
- int main (int argc, char* argv[]){
- int file_descriptor[2];
- pid_t pid1,pid2;
- char string[MAX];
- int n;
- int i;
- int len;
- char buff[33];
- if(pipe(file_descriptor)==0){
- pid1=fork();
- if(pid1!=0){
- //padre
- pid2=fork();
- if(pid2==0){
- //PRODUCER WRITES INTO PIPE
- close(file_descriptor[0]);
- while(1){
- sleep(1);
- printf("Write row of the file\n");
- fgets(string,MAX,stdin);
- len=strlen(string);
- sprintf(buff,"%d",len);
- write(file_descriptor[1],buff,33);
- n=write(file_descriptor[1],string,strlen(string));
- printf("I wrote %d bytes\n",n);
- if(strcmp(string,"end\n")==0){
- exit(0);
- }}
- }else{
- //CONSUMER READS FROM PIPE
- close(file_descriptor[1]);
- while(1){
- read(file_descriptor[0],buff,33);
- len=atoi(buff);
- n=read(file_descriptor[0],string,len);
- printf("I read %d bytes\n",n);
- string[len+1]='\0';
- for(i=0;i<strlen(string);i++){
- string[i]=toupper(string[i]);
- }
- printf("%s",string);
- if(strcmp(string,"END")==0){
- exit(0);
- }}
- }
- }
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement