AmidamaruZXC

Untitled

May 8th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/queue.h>
  8.  
  9. struct node {
  10.     pid_t client_pid;
  11.     long n;
  12.     TAILQ_ENTRY(node) nodes;
  13. };
  14.  
  15. struct request_msg_buf {
  16.     long mtype;
  17.     struct {
  18.         pid_t client_pid;
  19.         long n;
  20.     } info;
  21. };
  22.  
  23. struct response_msg_buf {
  24.     long mtype;
  25. };
  26.  
  27. int main() {  
  28.     TAILQ_HEAD(head_s, node) head;
  29.     TAILQ_INIT(&head);
  30.  
  31.     int msqid;
  32.  
  33.     char pathname[] = "server.c";
  34.  
  35.     key_t key;
  36.  
  37.     long n = 0;
  38.  
  39.     struct request_msg_buf request;
  40.     struct response_msg_buf response;
  41.  
  42.     if ((key = ftok(pathname, 0)) < 0) {
  43.         printf("Can't generate key\n");
  44.         exit(-1);
  45.     }
  46.  
  47.     if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0) {
  48.         printf("Can't get msqid\n");
  49.         exit(-1);
  50.     }
  51.  
  52.     while (1) {
  53.         if (msgrcv(msqid, (struct msgbuf*) &request, sizeof(request.info), 1, 0) < 0) {
  54.             printf("Can't receive message\n");
  55.             exit(-1);
  56.         }
  57.  
  58.         printf("A request for access was received:\n\tProcess PID: %d; N: %ld\n", request.info.client_pid, request.info.n);
  59.  
  60.         if (request.info.n >= 0) {
  61.             n += request.info.n;
  62.             response.mtype = request.info.client_pid;
  63.             msgsnd(msqid, (struct msgbuf*) &response, 0, 0);
  64.            
  65.             printf("Process with PID %d freed up %ld resources\n", request.info.client_pid, request.info.n);
  66.         } else {
  67.             struct node* new_node = malloc(sizeof(struct node));
  68.             new_node->client_pid = request.info.client_pid;
  69.             new_node->n = request.info.n;
  70.             TAILQ_INSERT_TAIL(&head, new_node, nodes);
  71.  
  72.             printf("Process with PID %d is waiting for %ld resources...\n", request.info.client_pid, -request.info.n);
  73.         }
  74.  
  75.         struct node* first_node = TAILQ_FIRST(&head);
  76.         while (!TAILQ_EMPTY(&head) && -first_node->n <= n) {
  77.             long client_pid = first_node->client_pid;
  78.             long needed_n = -first_node->n;
  79.            
  80.             n -= needed_n;
  81.             TAILQ_REMOVE(&head, first_node, nodes);
  82.  
  83.             response.mtype = client_pid;
  84.             msgsnd(msqid, (struct msgbuf*) &response, 0, 0);
  85.  
  86.             printf("Process with PID %ld is provided with needed resources\n", client_pid);
  87.         }
  88.  
  89.         printf("Current free resources:%ld\n", n);
  90.     }
  91.     return 0;
  92. }
Add Comment
Please, Sign In to add comment