Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <semaphore.h>
- #include <string.h>
- #include <stdbool.h>
- #define SHM_PATH "/shmue"
- #define PERMISSION (0600)
- #define SEM_SERVER "/semserver"
- #define SEM_CLIENT "/semclient"
- #define CLIENT_CONT "/client_cont"
- #define LINE_SIZE (100)
- sem_t *server_sem;
- sem_t *client_cont_sem;
- sem_t *client_sem;
- int shmfd = -1;
- typedef struct{
- int account_nr;
- int response;
- int log_in;
- }SHM_msg;
- SHM_msg *shared_message;
- void setupSharedMemory(void);
- void bail_out(int exit_code, char *error_msg);
- void free_resources(void);
- void check_login(void);
- void printSHM(void);
- void createSemaphores(void);
- void signal_handler(int signum);
- int main(int argc, char *argv[])
- {
- if(atexit(free_resources) != 0)
- {
- fprintf(stderr,"error at atexit.\n");
- }
- signal(SIGINT,signal_handler);
- createSemaphores();
- setupSharedMemory();
- while(1)
- {
- check_login();
- }
- return 1;
- }
- void signal_handler(int signum)
- {
- free_resources();
- exit(signum);
- }
- void createSemaphores() {
- client_sem = sem_open(SEM_CLIENT,O_CREAT, PERMISSION,1);
- server_sem = sem_open(SEM_SERVER,O_CREAT, PERMISSION,0);
- client_cont_sem = sem_open(CLIENT_CONT,O_CREAT, PERMISSION,0);
- if(client_sem == SEM_FAILED || server_sem == SEM_FAILED || client_cont_sem == SEM_FAILED) {
- printf("jdsds");
- }
- }
- /*
- void createSemaphores()
- {
- client_sem = sem_open(SEM_CLIENT, O_CREAT, PERMISSION,1);
- server_sem = sem_open(SEM_SERVER, O_CREAT, PERMISSION,0);
- client_cont_sem = sem_open(CLIENT_CONT, O_CREAT, PERMISSION,0);
- if(client_sem == SEM_FAILED || server_sem == SEM_FAILED || client_cont_sem == SEM_FAILED)
- {
- bail_out(EXIT_FAILURE, "error at creating semaphores");
- }
- }
- */
- void check_login(void)
- {
- if(shared_message->log_in == 1)
- {
- sem_wait(server_sem);
- printf("%d logged in!\n",shared_message->account_nr);
- printSHM();
- shared_message->response = 1;
- shared_message->log_in = -1;
- sleep(3);
- sem_post(client_cont_sem);
- }
- }
- void setupSharedMemory()
- {
- shmfd = shm_open(SHM_PATH, O_CREAT | O_RDWR, PERMISSION);
- if(shmfd == -1)
- {
- bail_out(EXIT_FAILURE,"could not create shm obj");
- }
- if(ftruncate(shmfd, sizeof(SHM_msg)) == -1)
- {
- bail_out(EXIT_FAILURE,"could not truncate memory");
- }
- shared_message = mmap(NULL, sizeof(SHM_msg), PROT_READ | PROT_WRITE, MAP_SHARED,shmfd,0);
- if(shared_message == MAP_FAILED)
- {
- bail_out(EXIT_FAILURE,"could not map memory");
- }
- shared_message->account_nr = -1;
- shared_message->response = -1;
- shared_message->log_in = -1;
- }
- void bail_out(int exit_code, char *error_msg)
- {
- fprintf(stderr,"Error in program: %s\n", error_msg);
- free_resources();
- exit(exit_code);
- }
- void free_resources()
- {
- printf("Free Resources\n");
- if(shmfd != -1)
- {
- if(close(shmfd) == -1)
- {
- fprintf(stderr,"could not close shm");
- }
- if(shm_unlink(SHM_PATH) == -1)
- {
- fprintf(stderr,"could not unlink shm");
- }
- if(munmap(shared_message,sizeof(SHM_msg)) == -1)
- {
- fprintf(stderr,"could not unmap shm");
- }
- printf("Resources freed.\n");
- }
- if(sem_close(client_sem) == -1)
- {
- fprintf(stderr,"could not close client_sem\n");
- }
- if(sem_unlink(SEM_CLIENT) == -1)
- {
- fprintf(stderr,"could not unlink SEM_CLIENT\n");
- }
- if(sem_close(server_sem) == -1)
- {
- fprintf(stderr,"could not close client_sem\n");
- }
- if(sem_unlink(SEM_SERVER) == -1)
- {
- fprintf(stderr,"could not unlink SEM_CLIENT\n");
- }
- if(sem_close(client_cont_sem) == -1)
- {
- fprintf(stderr,"could not close client_sem\n");
- }
- if(sem_unlink(CLIENT_CONT) == -1)
- {
- fprintf(stderr,"could not unlink SEM_CLIENT\n");
- }
- }
- void printSHM()
- {
- printf("Account nr: %d\n",shared_message->account_nr);
- printf("Response: %d\n",shared_message->response);
- printf("Login: %d\n",shared_message->log_in);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement