//Pierwszy program
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
int main(int argc,char **argv)
{
char bufor[50];
int file_read,file_open,file_open_p,file_read_p;
srand(time(0));
if (argc != 3)
{
printf ("%s <file name>", argv[0]);
return 1;
}
mkfifo("potok",0640);
file_open=open(argv[2],O_WRONLY | O_CREAT,0640);
if (file_open==-1)
{
perror("write_only_file error");
exit(EXIT_FAILURE);
}
file_read=open(argv[1],O_RDONLY,0640);
if (file_read==-1)
{
perror("read_only_file error");
exit(EXIT_FAILURE);
}
switch (fork())
{
case -1:
perror("fork error");
exit(EXIT_FAILURE);
case 0:
file_open_p=open("potok",O_RDONLY,0640);
if (file_open_p==-1)
{
perror("write_p error");
exit(EXIT_FAILURE);
}
while(read(file_open_p,&bufor,25)>0)
{
write(STDOUT_FILENO, &bufor, 25);
write(file_open,&bufor,25);
}
close(file_open);
close(file_open_p);
printf("\n");
_exit(EXIT_SUCCESS);
break;
default:
file_read_p=open("potok",O_WRONLY | O_CREAT,0640);
if (file_read_p==-1)
{
perror("read_p error");
exit(EXIT_FAILURE);
}
while(read(file_read,&bufor,50)>0)
{
sleep(rand()%3);
write(file_read_p,&bufor,50);
}
close(file_read);
close(file_read_p);
wait(NULL);
exit(EXIT_SUCCESS);
break;
};
return 0;
}
//Drugi program w dwoch osobnyach plikach do uruchamiania na dwoch konsolach
//producent
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
int main(int argc,char **argv)
{
char bufor[50];
int file_read,file_read_p;
srand(time(0));
if (argc != 2)
{
printf ("%s <file name>", argv[0]);
return 1;
}
file_read=open(argv[1],O_RDONLY,0640);
if (file_read==-1)
{
perror("read_only_file error");
exit(EXIT_FAILURE);
}
switch (fork())
{
case -1:
perror("fork error");
exit(EXIT_FAILURE);
case 0:
file_read_p=open("potok",O_WRONLY | O_CREAT,0640);
if (file_read_p==-1)
{
perror("read_p error");
exit(EXIT_FAILURE);
}
while(read(file_read,&bufor,50)>0)
{
sleep(rand()%3);
write(file_read_p,&bufor,50);
}
close(file_read);
close(file_read_p);
wait(NULL);
exit(EXIT_SUCCESS);
break;
default:;
};
return 0;
}
//konsument
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
int main(int argc,char **argv)
{
char bufor[50];
int file_open,file_open_p;
srand(time(0));
if (argc != 2)
{
printf ("%s <file name>", argv[0]);
return 1;
}
file_open=open(argv[1],O_WRONLY | O_CREAT,0640);
if (file_open==-1)
{
perror("write_only_file error");
exit(EXIT_FAILURE);
}
switch (fork())
{
case -1:
perror("fork error");
exit(EXIT_FAILURE);
case 0:
file_open_p=open("potok",O_RDONLY,0640);
if (file_open_p==-1)
{
perror("write_p error");
exit(EXIT_FAILURE);
}
while(read(file_open_p,&bufor,25)>0)
{
write(STDOUT_FILENO, &bufor, 25);
write(file_open,&bufor,25);
}
close(file_open);
close(file_open_p);
printf("\n");
_exit(EXIT_SUCCESS);
break;
default:
break;
};
return 0;
}
/makefile nie wiem do konca czy dobry
Fifo: zadanie5.c
gcc zadanie5.c -o Fifo.o && ./Fifo.o wejscie.txt wyjscie.txt
clean: potok
rm potok
Potok:
mkfifo -m 644 potok
ProdKons: producent.c
gcc producent.c -o Prod.o && ./Prod.o wejscie1.txt
gcc konsument.c -o Kons.o && ./Kons.o wyjscie1.txt
cleano:
rm -f *.o