Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //2 beégetett állomás
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <wait.h>
- #include <signal.h>
- #include <sys/stat.h>
- struct bike{
- char bike_id[2];
- char bike_station_id[2];
- char bike_status[10];
- };
- struct rent{
- char bike_id[2];
- char user_name[10];
- char user_action[15];
- char action_time[40];
- };
- struct uzenet {
- long mtype;
- char mtext [ 1024 ];
- };
- void p_handler(int signumber){
- }
- void gy1_handler(int signumber){
- if (signumber == SIGUSR1){
- printf("Child 1, signal 1 \n");
- }
- if (signumber == SIGUSR2){
- printf("Child 1, signal 2 \n");
- }
- }
- void gy2_handler(int signumber){
- if (signumber == SIGUSR1){
- printf("Child 2, signal 1 \n");
- }
- if (signumber == SIGUSR2){
- printf("Child 2, signal 2 \n");
- }
- }
- void listAllBike(){
- struct bike bike1;
- int f = open("OP_BIKE", O_RDONLY);
- while (read(f, &bike1, sizeof(struct bike))) {
- printf("%s,%s,%s\n",bike1.bike_id,bike1.bike_station_id,bike1.bike_status);
- }
- close(f);
- }
- void listStationBike(){
- struct bike bike1;
- char expectedStationId[2];
- printf("Melyik allomas?\n");
- scanf("%s", expectedStationId);
- printf("\n");
- int f = open("OP_BIKE", O_RDONLY);
- while (read(f, &bike1, sizeof(struct bike))) {
- if(strcmp(expectedStationId, bike1.bike_station_id) == 0){
- printf("%s,%s,%s\n",bike1.bike_id,bike1.bike_station_id,bike1.bike_status);
- close(f);
- return;
- }
- }
- printf("Nem letezo allomas.");
- close(f);
- }
- void deleteBadBike(){
- struct bike bikes[100];
- struct bike tmpBike;
- int n=0;
- int i=0;
- int f = open("OP_BIKE", O_RDONLY);
- while (read(f, &tmpBike, sizeof(struct bike))) {
- if(strcmp("rossz",tmpBike.bike_status) != 0){
- bikes[n] = tmpBike;
- n++;
- }
- }
- close(f);
- creat("OP_BIKE",S_IRUSR|S_IWUSR);
- f = open("OP_BIKE",O_WRONLY|O_APPEND);
- for(i=0;i<n;++i){
- write(f, &bikes[i], sizeof(struct bike));
- }
- close(f);
- }
- void addBike(){
- struct bike bike1;
- struct bike tmpBike;
- printf("\n Bicikli id: ");
- scanf("%s", bike1.bike_id);
- printf("\n Bicikli állomás id: ");
- scanf("%s", bike1.bike_station_id);
- if( (strcmp(bike1.bike_station_id, "1") !=0 ) && (strcmp(bike1.bike_station_id, "2") !=0 )){
- printf("Hibas allomas nev! \n");
- return;
- }
- printf("\n Bicikli állapot: ");
- scanf("%s", bike1.bike_status);
- int f2 = open("OP_BIKE",O_RDONLY);
- while (read(f2, &tmpBike, sizeof(struct bike))) {
- if((strcmp(bike1.bike_id, tmpBike.bike_id) ==0) && (strcmp(bike1.bike_station_id, tmpBike.bike_station_id) ==0)){
- printf("Létezik már bicikli ilyen ID-val ezen az állomáson. \n");
- return;
- }
- }
- close(f2);
- int f = open("OP_BIKE",O_WRONLY|O_APPEND);
- write(f, &bike1, sizeof(struct bike));
- close(f);
- }
- void rentBike(pid_t allomas1, pid_t allomas2, int fid){
- char bikeId[2];
- printf("Kolcsonzendo bicikli id-ja: ");
- scanf("%s",bikeId);
- char stationId[2];
- printf("Kolcsonzendo bicikli állomás id-ja: ");
- scanf("%s",stationId);
- int fd = open("fifo.ftc",O_WRONLY);
- write(fd, bikeId, sizeof(bikeId));
- close(fd);
- printf("Szulo beleirta az adatokat a csobe, szol a gyereknek. \n");
- if(strcmp(stationId, "1") == 0) kill(allomas1, SIGUSR1);
- if(strcmp(stationId, "2") == 0) kill(allomas2, SIGUSR1);
- if( (strcmp(stationId, "1") != 0) && (strcmp(stationId, "2") != 0)) {
- printf("Hibás állomás név!\n");
- return;
- }
- pause();
- printf("Szulo folytatja\n");
- }
- void childRent(char stationId[2]){
- char bikeId[2];
- int fd=open("fifo.ftc",O_RDONLY);
- read(fd,bikeId,sizeof(bikeId));
- close(fd);
- printf("Gyerek olvasta uzenet: %s \n",bikeId);
- int f = open("OP_BIKE.RENT",O_WRONLY|O_APPEND);
- int f2 = open("OP_BIKE",O_RDONLY);
- struct rent tmpRent;
- struct bike bikes[100];
- struct bike actualBike;
- struct bike tmpBike;
- char userName[10];
- printf("\nKolcsonzo neve:");
- scanf("%s",userName);
- printf("\n");
- int i=0;
- int n=0;
- int exist = 0;
- //ide kell még egy állomástól kapott id.
- while (read(f2, &tmpBike, sizeof(struct bike))) {
- if(strcmp(bikeId, tmpBike.bike_id) ==0){
- actualBike=tmpBike;
- exist=1;
- } else {
- bikes[n] = tmpBike;
- n++;
- }
- }
- if(exist == 0){
- printf("Nem letezo bicikli! \n");
- return;
- }
- if(strcmp(actualBike.bike_status,"szabad") != 0) {
- printf("Nem lehet kikolcsonozni! \n");
- return;
- }
- strcpy(actualBike.bike_status,"foglalt");
- bikes[n]=actualBike;
- close(f2);
- creat("OP_BIKE",S_IRUSR|S_IWUSR);
- f2 = open("OP_BIKE",O_WRONLY|O_APPEND);
- for(i=0; i<=n; i++){
- write(f2, &bikes[i], sizeof(struct bike));
- }
- strcpy(tmpRent.bike_id, actualBike.bike_id);
- strcpy(tmpRent.user_action, "kolcsonzes");
- strcpy(tmpRent.user_name, userName);
- time_t t = time(NULL);
- strcpy(tmpRent.action_time, ctime(&t));
- write(f, &tmpRent, sizeof(struct rent));
- }
- void getBikeBack(){
- int f = open("OP_BIKE.RENT",O_WRONLY|O_APPEND);
- int f2 = open("OP_BIKE",O_RDONLY);
- struct rent tmpRent;
- struct bike bikes[100];
- struct bike actualBike;
- struct bike tmpBike;
- char bikeId[2];
- char userName[10];
- printf("Visszahozando bicikli id-ja: ");
- scanf("%s",bikeId);
- printf("\nVisszahozo neve:");
- scanf("%s",userName);
- printf("\n");
- int i=0;
- int n=0;
- while (read(f2, &tmpBike, sizeof(struct bike))) {
- if(strcmp(bikeId, tmpBike.bike_id) ==0){
- actualBike=tmpBike;
- } else {
- bikes[n] = tmpBike;
- n++;
- }
- }
- if(strcmp(actualBike.bike_status,"foglalt") != 0) {
- printf("Nem lehet visszahozni!");
- return;
- }
- strcpy(actualBike.bike_status,"szabad");
- bikes[n]=actualBike;
- close(f2);
- creat("OP_BIKE",S_IRUSR|S_IWUSR);
- f2 = open("OP_BIKE",O_WRONLY|O_APPEND);
- for(i=0; i<=n; i++){
- write(f2, &bikes[i], sizeof(struct bike));
- }
- strcpy(tmpRent.bike_id, actualBike.bike_id);
- strcpy(tmpRent.user_action, "visszahozatal");
- strcpy(tmpRent.user_name, userName);
- time_t t = time(NULL);
- strcpy(tmpRent.action_time, ctime(&t));
- write(f, &tmpRent, sizeof(struct rent));
- }
- void listAllRent(){
- struct rent rent1;
- int f = open("OP_BIKE.RENT", O_RDONLY);
- while (read(f, &rent1, sizeof(struct rent))) {
- printf("%s,%s,%s,%s\n",rent1.bike_id, rent1.user_name, rent1.user_action,rent1.action_time);
- }
- close(f);
- }
- void listSpecificRent(){
- char expectedUserName[10];
- printf("Felhasznalo neve: ");
- scanf("%s", expectedUserName);
- struct rent rent1;
- int f = open("OP_BIKE.RENT", O_RDONLY);
- while (read(f, &rent1, sizeof(struct rent))) {
- if(strcmp(expectedUserName, rent1.user_name) == 0)
- printf("%s,%s,%s\n",rent1.bike_id, rent1.user_name, rent1.user_action);
- }
- close(f);
- }
- void program_exit(pid_t allomas1, pid_t allomas2){
- unlink("fifo.ftc");
- kill(allomas1, SIGKILL);
- kill(allomas2, SIGKILL);
- kill(getpid(), SIGKILL);
- //exit(0);
- }
- int main(int argc,char ** argv)
- {
- creat("OP_BIKE",S_IRUSR|S_IWUSR);
- creat("OP_BIKE.RENT",S_IRUSR|S_IWUSR);
- pid_t allomas1;
- pid_t allomas2;
- pid_t szulo = getpid();
- int fid=mkfifo("fifo.ftc", S_IRUSR|S_IWUSR );
- if (fid==-1)
- {
- printf("Error number: %i",errno);
- exit(EXIT_FAILURE);
- }
- int uzenetsor, status;
- key_t kulcs;
- kulcs = ftok(argv[0],1);
- uzenetsor = msgget( kulcs, 0600 | IPC_CREAT );
- if ( uzenetsor < 0 ) {
- perror("msgget");
- return 1;
- }
- allomas1 = fork();
- if(getppid() == szulo) allomas2 = fork();
- while(1){
- if(getpid() == szulo){
- signal(SIGUSR1,p_handler);
- signal(SIGUSR2,p_handler);
- int n;
- 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");
- scanf ("%d",&n);
- switch(n){
- case 0: {program_exit(allomas1, allomas2); break;}
- case 1: {listAllBike(); break;}
- case 2: {listStationBike(); break;}
- case 3: {deleteBadBike(); break;}
- case 4: {addBike(); break;}
- case 5: {rentBike(allomas1, allomas2, fid); break;}
- case 6: {getBikeBack(); break;}
- case 7: {listAllRent(); break;}
- case 8: {listSpecificRent(); break;}
- case 9: {kill(allomas2, SIGUSR2); break; }
- default: printf("Hibas megadott ertek.");
- }
- } else{
- //nevcso, uzenetsor
- if(allomas2 > 0){
- //allomas1 gyerek
- signal(SIGUSR1,gy1_handler);
- signal(SIGUSR2,gy1_handler);
- pause();
- //childRent("1");
- printf("Gyerek1 vegzett, visszajelez a szulonek.\n");
- kill(getppid(), SIGUSR1);
- } else{
- //allomas2 gyerek
- printf("alma");
- signal(SIGUSR1,gy2_handler);
- signal(SIGUSR2,gy2_handler);
- pause();
- //childRent("2");
- printf("Gyerek2 vegzett, visszajelez a szulonek.\n");
- kill(getppid(), SIGUSR1);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement