Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <semaphore.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <time.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <sys/mman.h>
  13. #include <limits.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <sys/ipc.h>
  17. #include <sys/shm.h>
  18. #include <sys/sem.h>
  19.  
  20. FILE *file = NULL;
  21. int shared_counter=0;
  22. int *riders=NULL;
  23. int *id=NULL;
  24. int *boarders=NULL;
  25. int *molo=NULL;
  26.  
  27. sem_t *sem1=NULL;
  28. sem_t *sem2=NULL;
  29. sem_t *sem3=NULL;
  30. sem_t *sem4=NULL;
  31. sem_t *sem5=NULL;
  32.  
  33.  
  34. void create_sems()
  35. {
  36.     if((sem1 = sem_open("xduris05_1",O_CREAT | O_EXCL , 0666 , 0)) == SEM_FAILED ){
  37.         printf("%s\n", "ERROR");
  38.     }
  39.     if((sem2 = sem_open("xduris05_2",O_CREAT | O_EXCL , 0666 , 1)) == SEM_FAILED ){
  40.         printf("%s\n", "ERROR");
  41.     }
  42.     if((sem3 = sem_open("xduris05_3",O_CREAT | O_EXCL , 0666 , 0)) == SEM_FAILED ){
  43.         printf("%s\n", "ERROR");
  44.     }
  45.     if((sem4 = sem_open("xduris05_4",O_CREAT | O_EXCL , 0666 , 0)) == SEM_FAILED ){
  46.         printf("%s\n", "ERROR");
  47.     }
  48.     if((sem5 = sem_open("xduris05_5",O_CREAT | O_EXCL , 0666 , 1)) == SEM_FAILED ){
  49.         printf("%s\n", "ERROR");
  50.     }
  51. }
  52.  
  53. void clean_sems()
  54. {
  55.     shmctl(shared_counter,IPC_RMID,NULL);
  56.     shmdt(id);
  57.  
  58.     sem_close(sem1);
  59.     sem_close(sem2);
  60.     sem_close(sem3);
  61.     sem_close(sem4);
  62.     sem_close(sem5);
  63.  
  64.     sem_unlink("xduris05_1");
  65.     sem_unlink("xduris05_2");
  66.     sem_unlink("xduris05_3");
  67.     sem_unlink("xduris05_4");
  68.     sem_unlink("xduris05_5");
  69.  
  70.     fclose(file);
  71. }
  72.  
  73.  
  74. int main(int argc, char **argv)
  75. {
  76.     int p,h,s,r,w,c;
  77.     char *ptr;
  78.     if (argc != 7)
  79.     {
  80.         fprintf(stderr, "Wrong number of arguments\n");
  81.         exit(1);
  82.     }
  83.     p=strtol(argv[1],&ptr,10);
  84.     if( p < 2 || (p % 2) != 0)
  85.     {
  86.         fprintf(stderr, "Wrong number of riders\n");
  87.         exit(1);
  88.     }
  89.     h=strtol(argv[2],&ptr,10);
  90.     if( h < 0 || h > 2000)
  91.     {
  92.         fprintf(stderr, "Wrong time\n");
  93.         exit(1);
  94.     }
  95.     s=strtol(argv[3],&ptr,10);
  96.     if( s < 0 || s > 2000)
  97.     {
  98.         fprintf(stderr, "Wrong time\n");
  99.         exit(1);
  100.     }
  101.     r=strtol(argv[4],&ptr,10);
  102.     if( r < 0 || r > 2000)
  103.     {
  104.         fprintf(stderr, "Wrong time of ride\n");
  105.         exit(1);
  106.     }
  107.     w=strtol(argv[5],&ptr,10);
  108.     if( w < 20 || w > 2000)
  109.     {
  110.         fprintf(stderr, "Wrong wait time\n");
  111.         exit(1);
  112.     }
  113.     c=strtol(argv[6],&ptr,10);
  114.     if( c < 5 )
  115.     {
  116.         fprintf(stderr, "Wrong capacity\n");
  117.         exit(1);
  118.     }
  119. file = fopen("proj2.out", "w");
  120.     if ((file = fopen("proj2.out", "w")) == NULL)
  121.     {                      
  122.         fprintf(stderr, "Opening file error");
  123.         exit(1);
  124.     }
  125.  
  126. setbuf(file, NULL);
  127. shared_counter = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
  128. id = shmat(shared_counter,NULL,1);
  129. molo = mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED,0,0);
  130. riders = mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED,0,0);
  131. *id = 1;
  132. *molo=0;
  133. *riders=0;
  134. create_sems();
  135.  
  136. pid_t serf = fork();
  137. if(serf == 0)
  138. {
  139.     printf("Generovanie serfov\n");
  140.     exit(0);
  141. }
  142. else if(serf > 0);
  143. else
  144. {
  145.     fprintf(stderr, "FORK ERROR\n");
  146. }
  147.  
  148. pid_t hackers = fork();
  149. if(hackers == 0)
  150. {
  151.     printf("Generovanie hackerov\n");
  152.     exit(0);
  153. }
  154. else if(hackers > 0);
  155. else
  156. {
  157.     fprintf(stderr, "FORK ERROR\n");
  158. }
  159.  
  160. munmap(molo,sizeof(int));
  161. munmap(riders,sizeof(int));
  162.  
  163. fprintf(file,"%d , %d , %d , %d , %d , %d\n",p,h,s,r,w,c);
  164. clean_sems();
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement