Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1.  
  2. //2 beégetett állomás
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <sys/ipc.h>
  12. #include <sys/msg.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include <wait.h>
  16. #include <signal.h>
  17. #include <sys/stat.h>
  18.  
  19. struct bike{
  20. char bike_id[2];
  21. char bike_station_id[2];
  22. char bike_status[10];
  23. };
  24. struct rent{
  25. char bike_id[2];
  26. char user_name[10];
  27. char user_action[15];
  28. char action_time[40];
  29. };
  30.  
  31.  
  32. struct uzenet {
  33. long mtype;
  34. char mtext [ 1024 ];
  35. };
  36.  
  37. void p_handler(int signumber){
  38. }
  39.  
  40. void gy1_handler(int signumber){
  41. if (signumber == SIGUSR1){
  42. printf("Child 1, signal 1 \n");
  43. }
  44.  
  45. if (signumber == SIGUSR2){
  46. printf("Child 1, signal 2 \n");
  47. }
  48. }
  49.  
  50. void gy2_handler(int signumber){
  51. if (signumber == SIGUSR1){
  52. printf("Child 2, signal 1 \n");
  53. }
  54.  
  55. if (signumber == SIGUSR2){
  56. printf("Child 2, signal 2 \n");
  57. }
  58. }
  59.  
  60. void listAllBike(){
  61. struct bike bike1;
  62. int f = open("OP_BIKE", O_RDONLY);
  63. while (read(f, &bike1, sizeof(struct bike))) {
  64. printf("%s,%s,%s\n",bike1.bike_id,bike1.bike_station_id,bike1.bike_status);
  65. }
  66. close(f);
  67. }
  68.  
  69. void listStationBike(){
  70. struct bike bike1;
  71. char expectedStationId[2];
  72. printf("Melyik allomas?\n");
  73. scanf("%s", expectedStationId);
  74.  
  75. printf("\n");
  76. int f = open("OP_BIKE", O_RDONLY);
  77. while (read(f, &bike1, sizeof(struct bike))) {
  78. if(strcmp(expectedStationId, bike1.bike_station_id) == 0){
  79. printf("%s,%s,%s\n",bike1.bike_id,bike1.bike_station_id,bike1.bike_status);
  80. close(f);
  81. return;
  82. }
  83. }
  84. printf("Nem letezo allomas.");
  85. close(f);
  86. }
  87.  
  88. void deleteBadBike(){
  89. struct bike bikes[100];
  90. struct bike tmpBike;
  91. int n=0;
  92. int i=0;
  93. int f = open("OP_BIKE", O_RDONLY);
  94. while (read(f, &tmpBike, sizeof(struct bike))) {
  95. if(strcmp("rossz",tmpBike.bike_status) != 0){
  96. bikes[n] = tmpBike;
  97. n++;
  98. }
  99. }
  100. close(f);
  101. creat("OP_BIKE",S_IRUSR|S_IWUSR);
  102. f = open("OP_BIKE",O_WRONLY|O_APPEND);
  103. for(i=0;i<n;++i){
  104. write(f, &bikes[i], sizeof(struct bike));
  105. }
  106. close(f);
  107. }
  108.  
  109. void addBike(){
  110. struct bike bike1;
  111. struct bike tmpBike;
  112. printf("\n Bicikli id: ");
  113. scanf("%s", bike1.bike_id);
  114. printf("\n Bicikli állomás id: ");
  115. scanf("%s", bike1.bike_station_id);
  116. if( (strcmp(bike1.bike_station_id, "1") !=0 ) && (strcmp(bike1.bike_station_id, "2") !=0 )){
  117. printf("Hibas allomas nev! \n");
  118. return;
  119. }
  120. printf("\n Bicikli állapot: ");
  121. scanf("%s", bike1.bike_status);
  122. int f2 = open("OP_BIKE",O_RDONLY);
  123. while (read(f2, &tmpBike, sizeof(struct bike))) {
  124. if((strcmp(bike1.bike_id, tmpBike.bike_id) ==0) && (strcmp(bike1.bike_station_id, tmpBike.bike_station_id) ==0)){
  125. printf("Létezik már bicikli ilyen ID-val ezen az állomáson. \n");
  126. return;
  127. }
  128. }
  129. close(f2);
  130. int f = open("OP_BIKE",O_WRONLY|O_APPEND);
  131. write(f, &bike1, sizeof(struct bike));
  132. close(f);
  133. }
  134.  
  135. void rentBike(pid_t allomas1, pid_t allomas2, int fid){
  136. char bikeId[2];
  137. printf("Kolcsonzendo bicikli id-ja: ");
  138. scanf("%s",bikeId);
  139.  
  140. char stationId[2];
  141. printf("Kolcsonzendo bicikli állomás id-ja: ");
  142. scanf("%s",stationId);
  143.  
  144. int fd = open("fifo.ftc",O_WRONLY);
  145. write(fd, bikeId, sizeof(bikeId));
  146. close(fd);
  147.  
  148. printf("Szulo beleirta az adatokat a csobe, szol a gyereknek. \n");
  149.  
  150. if(strcmp(stationId, "1") == 0) kill(allomas1, SIGUSR1);
  151. if(strcmp(stationId, "2") == 0) kill(allomas2, SIGUSR1);
  152. if( (strcmp(stationId, "1") != 0) && (strcmp(stationId, "2") != 0)) {
  153. printf("Hibás állomás név!\n");
  154. return;
  155. }
  156. pause();
  157. printf("Szulo folytatja\n");
  158. }
  159.  
  160. void childRent(char stationId[2]){
  161.  
  162.  
  163. char bikeId[2];
  164.  
  165. int fd=open("fifo.ftc",O_RDONLY);
  166. read(fd,bikeId,sizeof(bikeId));
  167. close(fd);
  168.  
  169. printf("Gyerek olvasta uzenet: %s \n",bikeId);
  170.  
  171. int f = open("OP_BIKE.RENT",O_WRONLY|O_APPEND);
  172. int f2 = open("OP_BIKE",O_RDONLY);
  173. struct rent tmpRent;
  174. struct bike bikes[100];
  175. struct bike actualBike;
  176. struct bike tmpBike;
  177. char userName[10];
  178. printf("\nKolcsonzo neve:");
  179. scanf("%s",userName);
  180. printf("\n");
  181. int i=0;
  182. int n=0;
  183. int exist = 0;
  184.  
  185. //ide kell még egy állomástól kapott id.
  186.  
  187. while (read(f2, &tmpBike, sizeof(struct bike))) {
  188. if(strcmp(bikeId, tmpBike.bike_id) ==0){
  189. actualBike=tmpBike;
  190. exist=1;
  191. } else {
  192. bikes[n] = tmpBike;
  193. n++;
  194. }
  195. }
  196.  
  197. if(exist == 0){
  198. printf("Nem letezo bicikli! \n");
  199. return;
  200. }
  201.  
  202. if(strcmp(actualBike.bike_status,"szabad") != 0) {
  203. printf("Nem lehet kikolcsonozni! \n");
  204. return;
  205. }
  206.  
  207. strcpy(actualBike.bike_status,"foglalt");
  208. bikes[n]=actualBike;
  209. close(f2);
  210. creat("OP_BIKE",S_IRUSR|S_IWUSR);
  211. f2 = open("OP_BIKE",O_WRONLY|O_APPEND);
  212. for(i=0; i<=n; i++){
  213. write(f2, &bikes[i], sizeof(struct bike));
  214. }
  215.  
  216. strcpy(tmpRent.bike_id, actualBike.bike_id);
  217. strcpy(tmpRent.user_action, "kolcsonzes");
  218. strcpy(tmpRent.user_name, userName);
  219. time_t t = time(NULL);
  220. strcpy(tmpRent.action_time, ctime(&t));
  221. write(f, &tmpRent, sizeof(struct rent));
  222. }
  223.  
  224. void getBikeBack(){
  225. int f = open("OP_BIKE.RENT",O_WRONLY|O_APPEND);
  226. int f2 = open("OP_BIKE",O_RDONLY);
  227. struct rent tmpRent;
  228. struct bike bikes[100];
  229. struct bike actualBike;
  230. struct bike tmpBike;
  231. char bikeId[2];
  232. char userName[10];
  233. printf("Visszahozando bicikli id-ja: ");
  234. scanf("%s",bikeId);
  235. printf("\nVisszahozo neve:");
  236. scanf("%s",userName);
  237. printf("\n");
  238. int i=0;
  239. int n=0;
  240. while (read(f2, &tmpBike, sizeof(struct bike))) {
  241. if(strcmp(bikeId, tmpBike.bike_id) ==0){
  242. actualBike=tmpBike;
  243. } else {
  244. bikes[n] = tmpBike;
  245. n++;
  246. }
  247. }
  248. if(strcmp(actualBike.bike_status,"foglalt") != 0) {
  249. printf("Nem lehet visszahozni!");
  250. return;
  251. }
  252. strcpy(actualBike.bike_status,"szabad");
  253. bikes[n]=actualBike;
  254. close(f2);
  255. creat("OP_BIKE",S_IRUSR|S_IWUSR);
  256. f2 = open("OP_BIKE",O_WRONLY|O_APPEND);
  257. for(i=0; i<=n; i++){
  258. write(f2, &bikes[i], sizeof(struct bike));
  259. }
  260.  
  261. strcpy(tmpRent.bike_id, actualBike.bike_id);
  262. strcpy(tmpRent.user_action, "visszahozatal");
  263. strcpy(tmpRent.user_name, userName);
  264. time_t t = time(NULL);
  265. strcpy(tmpRent.action_time, ctime(&t));
  266. write(f, &tmpRent, sizeof(struct rent));
  267. }
  268.  
  269. void listAllRent(){
  270. struct rent rent1;
  271. int f = open("OP_BIKE.RENT", O_RDONLY);
  272. while (read(f, &rent1, sizeof(struct rent))) {
  273. printf("%s,%s,%s,%s\n",rent1.bike_id, rent1.user_name, rent1.user_action,rent1.action_time);
  274. }
  275. close(f);
  276. }
  277.  
  278. void listSpecificRent(){
  279. char expectedUserName[10];
  280. printf("Felhasznalo neve: ");
  281. scanf("%s", expectedUserName);
  282. struct rent rent1;
  283. int f = open("OP_BIKE.RENT", O_RDONLY);
  284. while (read(f, &rent1, sizeof(struct rent))) {
  285. if(strcmp(expectedUserName, rent1.user_name) == 0)
  286. printf("%s,%s,%s\n",rent1.bike_id, rent1.user_name, rent1.user_action);
  287. }
  288. close(f);
  289. }
  290.  
  291. void program_exit(pid_t allomas1, pid_t allomas2){
  292. unlink("fifo.ftc");
  293. kill(allomas1, SIGKILL);
  294. kill(allomas2, SIGKILL);
  295. kill(getpid(), SIGKILL);
  296. //exit(0);
  297. }
  298.  
  299. int main(int argc,char ** argv)
  300. {
  301. creat("OP_BIKE",S_IRUSR|S_IWUSR);
  302. creat("OP_BIKE.RENT",S_IRUSR|S_IWUSR);
  303.  
  304. pid_t allomas1;
  305. pid_t allomas2;
  306. pid_t szulo = getpid();
  307.  
  308. int fid=mkfifo("fifo.ftc", S_IRUSR|S_IWUSR );
  309. if (fid==-1)
  310. {
  311. printf("Error number: %i",errno);
  312. exit(EXIT_FAILURE);
  313. }
  314.  
  315. int uzenetsor, status;
  316. key_t kulcs;
  317. kulcs = ftok(argv[0],1);
  318. uzenetsor = msgget( kulcs, 0600 | IPC_CREAT );
  319. if ( uzenetsor < 0 ) {
  320. perror("msgget");
  321. return 1;
  322. }
  323.  
  324. allomas1 = fork();
  325. if(getppid() == szulo) allomas2 = fork();
  326.  
  327.  
  328. while(1){
  329. if(getpid() == szulo){
  330. signal(SIGUSR1,p_handler);
  331. signal(SIGUSR2,p_handler);
  332.  
  333. int n;
  334. printf("\n 1-Osszes kerekpar listazasa \n 2-Egy allomas kerekparjainak listazasa \n 3-Rossz kerekparok torlese \n 4-Uj kerekpar hozzaadasa \n 5-Kerekpar kolcsonzese \n 6- Kerekpar visszahozasa \n 7-Osszes kolcsonzes listazasa \n 8-Adott szemely kolcsonzeseinek listazasa \n 0-Kilepes \n");
  335. scanf ("%d",&n);
  336. switch(n){
  337. case 0: {program_exit(allomas1, allomas2); break;}
  338. case 1: {listAllBike(); break;}
  339. case 2: {listStationBike(); break;}
  340. case 3: {deleteBadBike(); break;}
  341. case 4: {addBike(); break;}
  342. case 5: {rentBike(allomas1, allomas2, fid); break;}
  343. case 6: {getBikeBack(); break;}
  344. case 7: {listAllRent(); break;}
  345. case 8: {listSpecificRent(); break;}
  346. case 9: {kill(allomas2, SIGUSR2); break; }
  347. default: printf("Hibas megadott ertek.");
  348. }
  349. } else{
  350.  
  351. //nevcso, uzenetsor
  352.  
  353. if(allomas2 > 0){
  354. //allomas1 gyerek
  355. signal(SIGUSR1,gy1_handler);
  356. signal(SIGUSR2,gy1_handler);
  357.  
  358. pause();
  359. //childRent("1");
  360. printf("Gyerek1 vegzett, visszajelez a szulonek.\n");
  361.  
  362. kill(getppid(), SIGUSR1);
  363.  
  364. } else{
  365. //allomas2 gyerek
  366. printf("alma");
  367. signal(SIGUSR1,gy2_handler);
  368. signal(SIGUSR2,gy2_handler);
  369. pause();
  370.  
  371. //childRent("2");
  372. printf("Gyerek2 vegzett, visszajelez a szulonek.\n");
  373.  
  374. kill(getppid(), SIGUSR1);
  375. }
  376. }
  377.  
  378.  
  379.  
  380. }
  381.  
  382.  
  383. return 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement