Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <string.h>
- void bet(int credit){
- int fd[2],fd2[2],fd3[2];
- pid_t p;
- int i,betNumber;
- int random=0;
- int creditoAtual;
- if(pipe(fd)== -1){
- perror("Pipe fail\n");
- exit(1);
- }
- if(pipe(fd2)==-1){
- perror("Pipe fail\n");
- exit(1);
- }
- if(pipe(fd3)==-3){
- perror("Pipe fail\n");
- exit(1);
- }
- if(credit>0){
- p = fork();
- if(p==0){
- close(fd[1]);
- read(fd[0],&i,sizeof(int));
- if(i==1){
- srand(3);
- betNumber = rand() %5;
- close(fd3[0]);
- write(fd3[1],&betNumber,sizeof(int));
- close(fd3[1]);
- }
- close(fd2[1]);
- read(fd2[0],&creditoAtual,sizeof(int));
- close(fd[0]);
- printf("Credito atual: %d\n",creditoAtual);
- exit(1);
- }else{
- random = rand() % 5;
- close(fd[0]);
- if(credit>0){
- i=1;
- }else{
- i=0;
- }
- write(fd[1],&i,sizeof(int));
- close(fd[1]);
- sleep(1);
- close(fd3[1]);
- read(fd3[0],&betNumber,sizeof(int));
- close(fd3[0]);
- if(betNumber==random){
- printf("Acertou!\n");
- creditoAtual = credit +10;
- }else{
- printf("Nao acertou :s !\n");
- creditoAtual = credit -5;
- }
- close(fd2[0]);
- write(fd2[1],&creditoAtual,sizeof(int));
- close(fd2[1]);
- wait(NULL);
- bet(creditoAtual);
- }
- }
- }
- int main(void){
- bet(20);
- printf("TERMINOU\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment