Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //6-2-2
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <iostream>
- #include <signal.h>
- using namespace std;
- int *chan;
- int resultself[2];
- int resultopponent[2];
- int score[2] = {0,0};
- int sum(int x1,int x2)
- {
- if(x1==x2)return 0;
- else return x1+x2;
- }
- void hdl(int sig)
- {
- for(int i=1;i<=8;i++)
- {
- resultopponent[0]=chan[i*2-1];
- resultopponent[1]=chan[i*2];
- resultself[0] = chan[(i+8)*2-1];
- resultself[1] = chan[(i+8)*2];
- if(sum(resultself[0],resultself[1]) && sum(resultopponent[0],resultopponent[1]))
- {
- if(sum(resultself[0],resultself[1])>sum(resultopponent[0],resultopponent[1]))
- {
- score[0]++;
- }
- else if(sum(resultself[0],resultself[1])<sum(resultopponent[0],resultopponent[1]))
- {
- score[1]++;
- }
- }
- else if(!sum(resultself[0],resultself[1]) && !sum(resultopponent[0],resultopponent[1]))
- {
- }
- else if(!sum(resultself[0],resultself[1]))
- {
- score[0]++;
- }
- else if(!sum(resultopponent[0],resultopponent[1]))
- {
- score[1]++;
- }
- fprintf(stdout,"\nВы:%d Противник:%d -> В этом ходу Вы: %d;%d Противник:%d;%d",score[0],score[1],resultself[0],resultself[1],resultopponent[0],resultopponent[1]) ;
- }
- }
- int main()
- {
- int k1,k2;
- int fd[2];
- srand(time(NULL));
- mkfifo("connect1",S_IFIFO|0666) ;
- fd[0]= open("connect1",O_RDONLY) ;
- fd[1]= open("connect2",O_WRONLY);
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_flags = SA_SIGINFO;
- act.sa_handler = hdl;
- sigaction(SIGUSR1,&act,NULL);
- int shmid = shmget(2,2000,IPC_CREAT|0666);
- if(shmid<0){cout<<"ERROR, Разделяемая память не выделена";return 0;}
- chan = (int *)shmat(shmid,NULL,0);
- for(int i=9;i<=16;i++)
- {
- k1=rand()%6+1;
- k2=rand()%6+1;
- resultself[0] = k1;
- resultself[1] = k2;
- chan[i*2-1]=resultself[0];
- chan[i*2]=resultself[1];
- }
- int pid;
- int pi =getpid();
- write(fd[1],&pi,4) ;
- read(fd[0],&pid,4) ;
- sleep(2);
- kill(pid,SIGUSR1);
- sleep(1);
- close(fd[0]) ;
- close(fd[1]) ;
- unlink("connect1") ;
- unlink("connect2") ;
- return 1 ;
- }
- //6-2-1
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <iostream>
- #include <signal.h>
- using namespace std;
- int *chan;
- int resultself[2];
- int resultopponent[2] ;
- int score[2] = {0,0};
- int sum(int x1,int x2)
- {
- if(x1==x2)return 0;
- else return x1+x2;
- }
- void hdl(int sig)
- {
- for(int i=9;i<=16;i++)
- {
- resultopponent[0]=chan[i*2-1];
- resultopponent[1]=chan[i*2];
- resultself[0] = chan[(i-8)*2-1];
- resultself[1] = chan[(i-8)*2];
- if(sum(resultself[0],resultself[1]) && sum(resultopponent[0],resultopponent[1]))
- {
- if(sum(resultself[0],resultself[1])>sum(resultopponent[0],resultopponent[1]))
- {
- score[0]++;
- }
- else if(sum(resultself[0],resultself[1])<sum(resultopponent[0],resultopponent[1]))
- {
- score[1]++;
- }
- }
- else if(!sum(resultself[0],resultself[1]) && !sum(resultopponent[0],resultopponent[1]))
- {
- }
- else if(!sum(resultself[0],resultself[1]))
- {
- score[0]++;
- }
- else if(!sum(resultopponent[0],resultopponent[1]))
- {
- score[1]++;
- }
- fprintf(stdout,"\nВы:%d Противник:%d -> В этом ходу Вы: %d;%d Противник:%d;%d",score[0],score[1],resultself[0],resultself[1],resultopponent[0],resultopponent[1]) ;
- }
- }
- int main()
- {
- srand(time(NULL));
- int fd[2];
- int k1,k2;
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_flags = SA_SIGINFO;
- act.sa_handler = hdl;
- sigaction(SIGUSR1,&act,NULL);
- mkfifo("connect2",S_IFIFO|0666) ;
- fd[0]= open("connect1",O_WRONLY);
- fd[1]= open("connect2",O_RDONLY) ;
- int shmid = shmget(2,2000,IPC_CREAT|0666);
- if(shmid<0){cout<<"ERROR, Разделяемая память не выделена";return 0;}
- chan = (int *)shmat(shmid,NULL,0);
- for(int i=1;i<=8;i++)
- {
- k1=rand()%6+1;
- k2=rand()%6+1;
- resultself[0] = k1;
- resultself[1] = k2;
- chan[i*2-1]=resultself[0];
- chan[i*2]=resultself[1];
- }
- int pid;
- int pi =getpid();
- write(fd[0],&pi,4) ;
- read(fd[1],&pid,4) ;
- sleep(2);
- kill(pid,SIGUSR1);
- sleep(1);
- close(fd[0]) ;
- close(fd[1]) ;
- unlink("connect1") ;
- unlink("connect2") ;
- return 1 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement