Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <semaphore.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <time.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <limits.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- FILE *file = NULL;
- int shared_counter=0;
- int *riders=NULL;
- int *id=NULL;
- int *boarders=NULL;
- int *molo=NULL;
- sem_t *sem1=NULL;
- sem_t *sem2=NULL;
- sem_t *sem3=NULL;
- sem_t *sem4=NULL;
- sem_t *sem5=NULL;
- void create_sems()
- {
- if((sem1 = sem_open("xduris05_1",O_CREAT | O_EXCL , 0666 , 0)) == SEM_FAILED ){
- printf("%s\n", "ERROR");
- }
- if((sem2 = sem_open("xduris05_2",O_CREAT | O_EXCL , 0666 , 1)) == SEM_FAILED ){
- printf("%s\n", "ERROR");
- }
- if((sem3 = sem_open("xduris05_3",O_CREAT | O_EXCL , 0666 , 0)) == SEM_FAILED ){
- printf("%s\n", "ERROR");
- }
- if((sem4 = sem_open("xduris05_4",O_CREAT | O_EXCL , 0666 , 0)) == SEM_FAILED ){
- printf("%s\n", "ERROR");
- }
- if((sem5 = sem_open("xduris05_5",O_CREAT | O_EXCL , 0666 , 1)) == SEM_FAILED ){
- printf("%s\n", "ERROR");
- }
- }
- void clean_sems()
- {
- shmctl(shared_counter,IPC_RMID,NULL);
- shmdt(id);
- sem_close(sem1);
- sem_close(sem2);
- sem_close(sem3);
- sem_close(sem4);
- sem_close(sem5);
- sem_unlink("xduris05_1");
- sem_unlink("xduris05_2");
- sem_unlink("xduris05_3");
- sem_unlink("xduris05_4");
- sem_unlink("xduris05_5");
- fclose(file);
- }
- int main(int argc, char **argv)
- {
- int p,h,s,r,w,c;
- char *ptr;
- if (argc != 7)
- {
- fprintf(stderr, "Wrong number of arguments\n");
- exit(1);
- }
- p=strtol(argv[1],&ptr,10);
- if( p < 2 || (p % 2) != 0)
- {
- fprintf(stderr, "Wrong number of riders\n");
- exit(1);
- }
- h=strtol(argv[2],&ptr,10);
- if( h < 0 || h > 2000)
- {
- fprintf(stderr, "Wrong time\n");
- exit(1);
- }
- s=strtol(argv[3],&ptr,10);
- if( s < 0 || s > 2000)
- {
- fprintf(stderr, "Wrong time\n");
- exit(1);
- }
- r=strtol(argv[4],&ptr,10);
- if( r < 0 || r > 2000)
- {
- fprintf(stderr, "Wrong time of ride\n");
- exit(1);
- }
- w=strtol(argv[5],&ptr,10);
- if( w < 20 || w > 2000)
- {
- fprintf(stderr, "Wrong wait time\n");
- exit(1);
- }
- c=strtol(argv[6],&ptr,10);
- if( c < 5 )
- {
- fprintf(stderr, "Wrong capacity\n");
- exit(1);
- }
- file = fopen("proj2.out", "w");
- if ((file = fopen("proj2.out", "w")) == NULL)
- {
- fprintf(stderr, "Opening file error");
- exit(1);
- }
- setbuf(file, NULL);
- shared_counter = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
- id = shmat(shared_counter,NULL,1);
- molo = mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED,0,0);
- riders = mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED,0,0);
- *id = 1;
- *molo=0;
- *riders=0;
- create_sems();
- pid_t serf = fork();
- if(serf == 0)
- {
- printf("Generovanie serfov\n");
- exit(0);
- }
- else if(serf > 0);
- else
- {
- fprintf(stderr, "FORK ERROR\n");
- }
- pid_t hackers = fork();
- if(hackers == 0)
- {
- printf("Generovanie hackerov\n");
- exit(0);
- }
- else if(hackers > 0);
- else
- {
- fprintf(stderr, "FORK ERROR\n");
- }
- munmap(molo,sizeof(int));
- munmap(riders,sizeof(int));
- fprintf(file,"%d , %d , %d , %d , %d , %d\n",p,h,s,r,w,c);
- clean_sems();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement