Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/sem.h>
  7. #include <errno.h>
  8. #include <sys/shm.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. #define SCRIPT "\
  14. #/bin/bash \n\
  15. LIMIT=`ulimit -u` \n\
  16. ACTIVE=`ps -u palerat | wc -l`\n\
  17. echo $LIMIT > max\n\
  18. echo $ACTIVE >> max\n\
  19. "
  20. //USTAWIENIA
  21. char * myfifo = "/tmp/kit";
  22.  
  23. int main(int argc, char *argv[]) {
  24.     system(SCRIPT);
  25.     system("clear");
  26.     if (argc != 2) {
  27.         printf("[KONSUMENT %d] Nieprawidlowa liczba argumentow!\n", getpid());
  28.         exit(EXIT_FAILURE);
  29.     }
  30.     FILE *file;
  31.     file = fopen("max", "r");
  32.     int limit, active;
  33.     fscanf(file, "%d", &limit);
  34.     fscanf(file, "%d", &active);
  35.     printf("Maksymalna liczba procesow: %d, Liczba aktywnych procesow: %d\n", limit, active);
  36.     if (atoi(argv[1]) >= limit-active) {
  37.         printf("[KONSUMENT %d] Podana ilosc procesow przekracza maksymalna liczbe procesow do utworzenia!\n", getpid());
  38.         exit(0);
  39.     }
  40.     if (atoi(argv[1]) <= 0) {
  41.         printf("[KONSUMENT %d] Podana ilosc prcesow jest nieodpowiednia!\n", getpid());
  42.         exit(0);
  43.     }
  44.     int i, pid;
  45.   for (i = 1; i < atoi(argv[1]); i++) {
  46.       if((pid = fork()) < 0) {
  47.           printf("Blad fork.\n");
  48.           exit(1);
  49.       } else if(pid != 0) break;
  50.   }
  51.     char sign;
  52.   char nameOfFile[20];
  53.     int check;
  54.     int fd1, fd2;
  55.     sprintf(nameOfFile, "K/k_%d", getpid());
  56.     FILE* f = fopen(nameOfFile, "w");
  57.   if(f == NULL) {
  58.     printf("[KONSUMENT %d] Blad otwarcia pliku.\n", getpid());
  59.     exit (1);
  60.   }
  61.     sleep(1);
  62.     fd1 = open(myfifo, O_RDONLY | O_NONBLOCK);
  63.     if (fd1 == -1) {
  64.       printf("[KONSUMENT %d] Blad otwierania kolejki!", getpid());
  65.       exit(EXIT_FAILURE);
  66.   }
  67.     int x = 0;
  68.     while(sign != EOF) {
  69.         usleep(200);
  70.         int readstatus = read(fd1, &sign, sizeof(char));
  71.         if(readstatus==0) break;
  72.         if(readstatus==-1) {
  73.             printf("[KONSUMENT %d] Blad przy czytaniu.\n", getpid());
  74.             exit(EXIT_FAILURE);
  75.         }
  76.         fprintf(f, "%c", sign);
  77.         x++;
  78.     }
  79.     printf("[KONSUMENT %d] Wczytano znakow: %d\n", getpid(), x);
  80.     close(fd1);
  81.     unlink(myfifo);
  82.     fclose(f);
  83.     int z;
  84.     wait(&z);
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement