Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #shf
- //ZAD. 1
- //Program piszący
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <wait.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #define FIFO1 "/tmp/fifo.1"
- extern int errno;
- int main()
- {
- if(mknod(FIFO1,0666|S_IFIFO, 0)<0){
- perror("nie mozna stworzyc fifo");
- }
- char buff[]="ala ma kota a kot ma ale";
- int fifo;
- int flagi=O_WRONLY /*| O_NDELAY*/;
- if ((fifo = open(FIFO1, flagi)) < 0)
- perror("nie moze otworzyc fifo1 do pisania");
- if (strlen(buff) != write(fifo,buff,strlen(buff)))
- perror("blad zapisu do fifo");
- return 0;
- }
- //Program czytający
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <wait.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- extern int errno;
- #define FIFO1 "/tmp/fifo.1"
- int main()
- {
- char buff[1024];
- int fifo;
- int flagi=O_RDONLY /*| O_NDELAY*/ ;
- if ((fifo = open(FIFO1, flagi)) < 0)
- perror("nie moze otworzyc fifo1 do pisania");
- int n=read(fifo,buff,sizeof(buff));
- if (n<=0)
- perror("blad zapisu do fifo");
- else{
- buff[n]='\0';
- write(1,buff,n);
- printf("\n");
- }
- unlink(FIFO1);
- return 0;
- }
- //ZAD.2
- //KLIENT1
- Klient 1
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <wait.h>
- #include <errno.h>
- #define FIFO1 "/tmp/fifo.1" // do pisania dla klienta 1 i czytania dla klienta 2
- #define FIFO2 "/tmp/fifo.2" // do pisania dla klienta 2 i czytania dla klienta 1
- #define QUIT "\1"
- int pid_potomka,pid_rodzica,fifo_write;
- void oblsluga_zakonczenia_procesow(int nr_sig)
- {
- if(getpid()==pid_potomka){
- char buff[]="\1";
- if (strlen(buff) != write(fifo_write,buff,strlen(buff)))
- perror("blad zapisu do fifo");
- exit(0);
- }
- else if(getpid()==pid_rodzica){
- wait(NULL);
- unlink(FIFO1);
- unlink(FIFO2);
- exit(0);
- }
- }
- void oblsluga_zakonczenia_potomka(int nr_sig)
- {
- unlink(FIFO1);
- unlink(FIFO2);
- exit(0);
- }
- int main()
- {
- signal(SIGINT,oblsluga_zakonczenia_procesow);
- if(mknod(FIFO1,S_IFIFO|0666,0)==-1){
- perror("nie mozna stworzyc fifo1");
- }
- if(mknod(FIFO2,S_IFIFO|0666,0)==-1){
- perror("nie mozna stworzyc fifo1");
- }
- int childpid;
- if ((childpid = fork()) == -1)
- {
- perror("nie moge forknac");
- exit(1);
- }
- else
- if (childpid ==0 ) // POTOMEK PISZE
- {
- signal(SIGUSR1,oblsluga_zakonczenia_potomka);
- pid_potomka=getpid();
- int fifo;
- if((fifo=open(FIFO1,O_WRONLY))<0){
- perror("Nie mozna otworzyc kolejki do pisania");
- }
- fifo_write=fifo;
- char buff[1024];
- while(1==1){
- fgets(buff,sizeof(buff),stdin);
- if (strlen(buff) != write(fifo,buff,strlen(buff)))
- perror("blad zapisu do fifo");
- }
- }
- else // RODZIC CZYTA
- {
- signal(SIGUSR1,SIG_IGN);
- pid_rodzica=getpid();
- int fifo;
- if((fifo=open(FIFO2,O_RDONLY))<0){
- perror("Nie mozna otworzyc kolejki do czytania");
- }
- char buff[1024];
- while(1==1){
- int n=read(fifo,buff,sizeof(buff));
- if (n<=0){
- perror("blad odczytu z fifo");
- }
- else{
- buff[n]='\0';
- if (strcmp(QUIT, buff)==0){
- printf("Klient do rozmowy sie odlaczyl\n");
- break;
- }
- printf("Klient:");
- printf("%s",buff);
- }
- }
- kill(pid_potomka,SIGUSR1);
- wait(NULL);
- exit(0);
- }
- exit(0);
- return 0;
- }
- //KLIENT2
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <wait.h>
- #include <errno.h>
- #define FIFO1 "/tmp/fifo.1" // do pisania dla klienta 1 i czytania dla klienta 2
- #define FIFO2 "/tmp/fifo.2" // do pisania dla klienta 2 i czytania dla klienta 1
- #define QUIT "\1"
- int pid_potomka,pid_rodzica,fifo_write;
- void oblsluga_zakonczenia_procesow(int nr_sig)
- {
- if(getpid()==pid_potomka){
- char buff[]="\1";
- if (strlen(buff) != write(fifo_write,buff,strlen(buff)))
- perror("blad zapisu do fifo");
- exit(0);
- }
- else if(getpid()==pid_rodzica){
- wait(NULL);
- exit(0);
- }
- }
- void oblsluga_zakonczenia_potomka(int nr_sig)
- {
- exit(0);
- }
- int main()
- {
- signal(SIGINT,oblsluga_zakonczenia_procesow);
- int childpid;
- if ((childpid = fork()) == -1)
- {
- perror("nie moge forknac");
- exit(1);
- }
- else
- if (childpid ==0 ) // POTOMEK PISZE
- {
- signal(SIGUSR1,oblsluga_zakonczenia_potomka);
- pid_potomka=getpid();
- int fifo;
- if((fifo=open(FIFO2,O_WRONLY))<0){
- perror("Nie mozna otworzyc kolejki do pisania");
- }
- fifo_write=fifo;
- char buff[1024];
- while(1==1){
- fgets(buff,sizeof(buff),stdin);
- if (strlen(buff) != write(fifo,buff,strlen(buff)))
- perror("blad zapisu do fifo");
- }
- }
- else // RODZIC CZYTA
- {
- signal(SIGUSR1,SIG_IGN);
- pid_rodzica=getpid();
- int fifo;
- if((fifo=open(FIFO1,O_RDONLY))<0){
- perror("Nie mozna otworzyc kolejki do czytania");
- }
- char buff[1024];
- while(1==1){
- int n=read(fifo,buff,sizeof(buff));
- if (n<=0){
- perror("blad odczytu z fifo");
- sleep(10);
- }
- else{
- buff[n]='\0';
- if (strcmp(QUIT, buff)==0){
- printf("Klient do rozmowy sie odlaczyl\n");
- break;
- }
- printf("Klient:");
- printf("%s",buff);
- }
- }
- kill(pid_potomka,SIGUSR1);
- wait(NULL);
- exit(0);
- }
- exit(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement