Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <errno.h>
- #include <sys/shm.h>
- #include <fcntl.h>
- #include <string.h>
- #include <time.h>
- #define SCRIPT "\
- #/bin/bash \n\
- LIMIT=`ulimit -u` \n\
- ACTIVE=`ps -u palerat | wc -l`\n\
- echo $LIMIT > max\n\
- echo $ACTIVE >> max\n\
- "
- //USTAWIENIA
- char * myfifo = "/tmp/kit";
- int main(int argc, char *argv[]) {
- system(SCRIPT);
- system("clear");
- if (argc != 2) {
- printf("[KONSUMENT %d] Nieprawidlowa liczba argumentow!\n", getpid());
- exit(EXIT_FAILURE);
- }
- FILE *file;
- file = fopen("max", "r");
- int limit, active;
- fscanf(file, "%d", &limit);
- fscanf(file, "%d", &active);
- printf("Maksymalna liczba procesow: %d, Liczba aktywnych procesow: %d\n", limit, active);
- if (atoi(argv[1]) >= limit-active) {
- printf("[KONSUMENT %d] Podana ilosc procesow przekracza maksymalna liczbe procesow do utworzenia!\n", getpid());
- exit(0);
- }
- if (atoi(argv[1]) <= 0) {
- printf("[KONSUMENT %d] Podana ilosc prcesow jest nieodpowiednia!\n", getpid());
- exit(0);
- }
- int i, pid;
- for (i = 1; i < atoi(argv[1]); i++) {
- if((pid = fork()) < 0) {
- printf("Blad fork.\n");
- exit(1);
- } else if(pid != 0) break;
- }
- char sign;
- char nameOfFile[20];
- int check;
- int fd1, fd2;
- sprintf(nameOfFile, "K/k_%d", getpid());
- FILE* f = fopen(nameOfFile, "w");
- if(f == NULL) {
- printf("[KONSUMENT %d] Blad otwarcia pliku.\n", getpid());
- exit (1);
- }
- sleep(1);
- fd1 = open(myfifo, O_RDONLY | O_NONBLOCK);
- if (fd1 == -1) {
- printf("[KONSUMENT %d] Blad otwierania kolejki!", getpid());
- exit(EXIT_FAILURE);
- }
- int x = 0;
- while(sign != EOF) {
- usleep(200);
- int readstatus = read(fd1, &sign, sizeof(char));
- if(readstatus==0) break;
- if(readstatus==-1) {
- printf("[KONSUMENT %d] Blad przy czytaniu.\n", getpid());
- exit(EXIT_FAILURE);
- }
- fprintf(f, "%c", sign);
- x++;
- }
- printf("[KONSUMENT %d] Wczytano znakow: %d\n", getpid(), x);
- close(fd1);
- unlink(myfifo);
- fclose(f);
- int z;
- wait(&z);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement