Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. //6-2-2
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <sys/shm.h>
  10. #include <sys/ipc.h>
  11. #include <iostream>
  12. #include <signal.h>
  13. using namespace std;
  14.  
  15. int *chan;
  16. int resultself[2];
  17. int resultopponent[2];
  18. int score[2] = {0,0};
  19. int sum(int x1,int x2)
  20. {
  21. if(x1==x2)return 0;
  22. else return x1+x2;
  23. }
  24.  
  25. void hdl(int sig)
  26. {
  27. for(int i=1;i<=8;i++)
  28. {
  29. resultopponent[0]=chan[i*2-1];
  30. resultopponent[1]=chan[i*2];
  31. resultself[0] = chan[(i+8)*2-1];
  32. resultself[1] = chan[(i+8)*2];
  33. if(sum(resultself[0],resultself[1]) && sum(resultopponent[0],resultopponent[1]))
  34. {
  35. if(sum(resultself[0],resultself[1])>sum(resultopponent[0],resultopponent[1]))
  36. {
  37. score[0]++;
  38. }
  39. else if(sum(resultself[0],resultself[1])<sum(resultopponent[0],resultopponent[1]))
  40. {
  41. score[1]++;
  42. }
  43. }
  44. else if(!sum(resultself[0],resultself[1]) && !sum(resultopponent[0],resultopponent[1]))
  45. {
  46.  
  47. }
  48. else if(!sum(resultself[0],resultself[1]))
  49. {
  50. score[0]++;
  51. }
  52. else if(!sum(resultopponent[0],resultopponent[1]))
  53. {
  54. score[1]++;
  55. }
  56.  
  57. fprintf(stdout,"\nВы:%d Противник:%d -> В этом ходу Вы: %d;%d Противник:%d;%d",score[0],score[1],resultself[0],resultself[1],resultopponent[0],resultopponent[1]) ;
  58. }
  59. }
  60. int main()
  61. {
  62.  
  63. int k1,k2;
  64. int fd[2];
  65. srand(time(NULL));
  66.  
  67.  
  68. mkfifo("connect1",S_IFIFO|0666) ;
  69. fd[0]= open("connect1",O_RDONLY) ;
  70. fd[1]= open("connect2",O_WRONLY);
  71.  
  72. struct sigaction act;
  73. sigemptyset(&act.sa_mask);
  74. act.sa_flags = SA_SIGINFO;
  75. act.sa_handler = hdl;
  76. sigaction(SIGUSR1,&act,NULL);
  77.  
  78.  
  79. int shmid = shmget(2,2000,IPC_CREAT|0666);
  80. if(shmid<0){cout<<"ERROR, Разделяемая память не выделена";return 0;}
  81.  
  82. chan = (int *)shmat(shmid,NULL,0);
  83. for(int i=9;i<=16;i++)
  84. {
  85. k1=rand()%6+1;
  86. k2=rand()%6+1;
  87. resultself[0] = k1;
  88. resultself[1] = k2;
  89.  
  90. chan[i*2-1]=resultself[0];
  91. chan[i*2]=resultself[1];
  92.  
  93. }
  94. int pid;
  95. int pi =getpid();
  96. write(fd[1],&pi,4) ;
  97. read(fd[0],&pid,4) ;
  98. sleep(2);
  99. kill(pid,SIGUSR1);
  100. sleep(1);
  101.  
  102.  
  103. close(fd[0]) ;
  104. close(fd[1]) ;
  105. unlink("connect1") ;
  106. unlink("connect2") ;
  107. return 1 ;
  108. }
  109.  
  110. //6-2-1
  111.  
  112. #include <stdio.h>
  113. #include <unistd.h>
  114. #include <fcntl.h>
  115. #include <sys/stat.h>
  116. #include <stdlib.h>
  117. #include <time.h>
  118. #include <sys/shm.h>
  119. #include <sys/ipc.h>
  120. #include <iostream>
  121. #include <signal.h>
  122. using namespace std;
  123.  
  124. int *chan;
  125. int resultself[2];
  126. int resultopponent[2] ;
  127.  
  128.  
  129.  
  130. int score[2] = {0,0};
  131.  
  132. int sum(int x1,int x2)
  133. {
  134. if(x1==x2)return 0;
  135. else return x1+x2;
  136. }
  137. void hdl(int sig)
  138. {
  139. for(int i=9;i<=16;i++)
  140. {
  141. resultopponent[0]=chan[i*2-1];
  142. resultopponent[1]=chan[i*2];
  143. resultself[0] = chan[(i-8)*2-1];
  144. resultself[1] = chan[(i-8)*2];
  145. if(sum(resultself[0],resultself[1]) && sum(resultopponent[0],resultopponent[1]))
  146. {
  147. if(sum(resultself[0],resultself[1])>sum(resultopponent[0],resultopponent[1]))
  148. {
  149. score[0]++;
  150. }
  151. else if(sum(resultself[0],resultself[1])<sum(resultopponent[0],resultopponent[1]))
  152. {
  153. score[1]++;
  154. }
  155. }
  156. else if(!sum(resultself[0],resultself[1]) && !sum(resultopponent[0],resultopponent[1]))
  157. {
  158.  
  159. }
  160. else if(!sum(resultself[0],resultself[1]))
  161. {
  162. score[0]++;
  163. }
  164. else if(!sum(resultopponent[0],resultopponent[1]))
  165. {
  166. score[1]++;
  167. }
  168.  
  169. fprintf(stdout,"\nВы:%d Противник:%d -> В этом ходу Вы: %d;%d Противник:%d;%d",score[0],score[1],resultself[0],resultself[1],resultopponent[0],resultopponent[1]) ;
  170. }
  171. }
  172. int main()
  173. {
  174.  
  175. srand(time(NULL));
  176. int fd[2];
  177. int k1,k2;
  178.  
  179. struct sigaction act;
  180. sigemptyset(&act.sa_mask);
  181. act.sa_flags = SA_SIGINFO;
  182. act.sa_handler = hdl;
  183. sigaction(SIGUSR1,&act,NULL);
  184. mkfifo("connect2",S_IFIFO|0666) ;
  185. fd[0]= open("connect1",O_WRONLY);
  186. fd[1]= open("connect2",O_RDONLY) ;
  187.  
  188.  
  189. int shmid = shmget(2,2000,IPC_CREAT|0666);
  190. if(shmid<0){cout<<"ERROR, Разделяемая память не выделена";return 0;}
  191.  
  192. chan = (int *)shmat(shmid,NULL,0);
  193. for(int i=1;i<=8;i++)
  194. {
  195. k1=rand()%6+1;
  196. k2=rand()%6+1;
  197. resultself[0] = k1;
  198. resultself[1] = k2;
  199.  
  200. chan[i*2-1]=resultself[0];
  201. chan[i*2]=resultself[1];
  202.  
  203. }
  204. int pid;
  205. int pi =getpid();
  206. write(fd[0],&pi,4) ;
  207. read(fd[1],&pid,4) ;
  208. sleep(2);
  209. kill(pid,SIGUSR1);
  210. sleep(1);
  211. close(fd[0]) ;
  212. close(fd[1]) ;
  213. unlink("connect1") ;
  214. unlink("connect2") ;
  215.  
  216. return 1 ;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement