|
| 1 | +#include <semaphore.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <sys/shm.h> |
| 6 | +#include <sys/stat.h> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <sys/types.h> |
| 9 | +#include <unistd.h> |
| 10 | +#include <sys/types.h> |
| 11 | +#include <sys/wait.h> |
| 12 | +#include <time.h> |
| 13 | +#include <errno.h> |
| 14 | +#include "../include/struct.h" |
| 15 | +#include"../include/helpers.h" |
| 16 | +int main(int argc, char **argv) |
| 17 | +{ |
| 18 | + //Initialize Random generator based on pid so each process has a different rand |
| 19 | + srand(time(NULL) - getpid()); |
| 20 | + clock_t start, end; |
| 21 | + //Temporarily create a string to open a log file |
| 22 | + char *proc_id = malloc(sizeof(char) * 100); |
| 23 | + if(proc_id==NULL){ |
| 24 | + perror("malloc at proc_id"); |
| 25 | + exit(20); |
| 26 | + } |
| 27 | + sprintf(proc_id, "./logs/%d_log.txt", getpid()); |
| 28 | + |
| 29 | + //Delete previous log file in case proccess id is the same as a previous one |
| 30 | + if(remove(proc_id)==0){ |
| 31 | + printf("Removed File succcessfully\n"); |
| 32 | + } |
| 33 | + |
| 34 | + FILE *logs; |
| 35 | + //Get necessary integers from arguments |
| 36 | + int id = atoi(argv[0]); //Id for memory block |
| 37 | + int max_sec = atoi(argv[1]); //Number of sections |
| 38 | + int max_line = atoi(argv[2]); //Number of chars in max line |
| 39 | + int loop = atoi(argv[3]); //Total requests to be sent |
| 40 | + int total_lines = atoi(argv[4]); //Total lines in each segment |
| 41 | + char *line; |
| 42 | + if (id == 0) |
| 43 | + { |
| 44 | + perror("Error at getting shm id from parent"); |
| 45 | + exit(14); |
| 46 | + } |
| 47 | + //Attach memory |
| 48 | + shared_obj *memory = (shared_obj *)shmat(id, NULL, 0); |
| 49 | + if (memory == (void *)-1) |
| 50 | + { |
| 51 | + perror("Attachment."); |
| 52 | + exit(15); |
| 53 | + } |
| 54 | + // Attach shared string |
| 55 | + char *shared_string = (char *)shmat(memory->ID_str, NULL, 0); |
| 56 | + if(shared_string==(char *)-1){ |
| 57 | + perror("Error at attaching child to shared_string"); |
| 58 | + exit(16); |
| 59 | + } |
| 60 | + |
| 61 | + //Open created semaphores for usage |
| 62 | + sem_t *write_server = sem_open(WR_SERV, 0); |
| 63 | + sem_t *ready = sem_open(READY, 0); |
| 64 | + sem_t *critical = sem_open(CS, 0); |
| 65 | + sem_t *queue = sem_open(QUEUE, 0); |
| 66 | + |
| 67 | + //Variables needed |
| 68 | + int new = 1,old_req=-1,req_line,req_sec; |
| 69 | + |
| 70 | + while (loop > 0) |
| 71 | + { |
| 72 | + //Create a new request |
| 73 | + if (new == 1) |
| 74 | + { |
| 75 | + get_request(old_req, &req_line, &req_sec, total_lines, max_line, max_sec); |
| 76 | + start = clock(); |
| 77 | + old_req = req_sec; |
| 78 | + printf("Client: %d made request <%d,%d>\n", getpid(), req_line, req_sec); |
| 79 | + fflush(stdout); |
| 80 | + //Make new 0 so that if the loop come backs we dont create a new request and discard current |
| 81 | + new = 0; |
| 82 | + } |
| 83 | + |
| 84 | + //In case requested segment is current segment go ahead and read |
| 85 | + if (req_sec == memory->curr_seg) |
| 86 | + { |
| 87 | + end = clock(); //Stop Clock |
| 88 | + line = get_line(shared_string, req_line); //Read requested line |
| 89 | + logs = fopen(proc_id, "a"); //Open log file in log folder |
| 90 | + fprintf(logs, "Response Time:%fs,<%d,%d>,Line:%s\n", ((double)(end - start)) / CLOCKS_PER_SEC, req_line, req_sec, line); //Write in logs |
| 91 | + printf("Client: %d got request <%d,%d>\n", getpid(), req_line, req_sec); //Print to let user know that request got completed |
| 92 | + loop--; //One request done |
| 93 | + new = 1; //Create new request |
| 94 | + fclose(logs); |
| 95 | + usleep(20000); //Sleep for 20ms |
| 96 | + continue; |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + //In case current section available isn't requested section wait for critical section |
| 101 | + //to change the variable in memory |
| 102 | + sem_wait(critical); |
| 103 | + printf("Client: %d got queued\n", getpid()); |
| 104 | + fflush(stdout); |
| 105 | + //If u are first to enter Queue make your requested segment the next to be put |
| 106 | + if (memory->in_q == 0) |
| 107 | + { |
| 108 | + memory->next_seg = req_sec; |
| 109 | + printf("Server: Next segment will be %d\n", memory->next_seg); |
| 110 | + fflush(stdout); |
| 111 | + } |
| 112 | + |
| 113 | + //Increase the number of processes waiting in queue |
| 114 | + memory->in_q++; |
| 115 | + if (memory->in_q == (N - memory->dead)) |
| 116 | + { |
| 117 | + memory->in_q--; |
| 118 | + //In case the Queue is now full with all alive processes call server to change segment |
| 119 | + sem_post(write_server); |
| 120 | + sem_wait(ready); |
| 121 | + //Give critical to the next process |
| 122 | + sem_post(critical); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + //If you Queue isn't Full give back critical and wait in Queue |
| 127 | + sem_post(critical); |
| 128 | + sem_wait(queue); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + //process exits and waits for critical to update number of dead processes |
| 134 | + sem_wait(critical); |
| 135 | + memory->dead++; |
| 136 | + |
| 137 | + //In Case Queue is now full because all other processes are waiting in Queue or all processes are dead |
| 138 | + //change Segment and unblock |
| 139 | + |
| 140 | + if (memory->in_q == N - memory->dead) |
| 141 | + { |
| 142 | + //Post to server so that segment updates |
| 143 | + sem_post(write_server); |
| 144 | + sem_wait(ready); |
| 145 | + |
| 146 | + } |
| 147 | + //Give back critical |
| 148 | + sem_post(critical); |
| 149 | + |
| 150 | + //Close open File and exit |
| 151 | + free(proc_id); |
| 152 | + return 0; |
| 153 | +} |
0 commit comments