Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/un.h>
  9. #include <signal.h>
  10. #include <time.h>
  11. //w moim programie przyjelam konwencje, ze brygardzista jest rodzicem, a robotnicy sa jego dziecmi
  12. #define SOCK_PATH "polaczenie"
  13. char* address;  //parametr a
  14. int number_of_workers; //parametr n
  15. char* message; //parametr m
  16. float time_stamp; //parametr t
  17. int id;
  18. int h1=0; //zmienna pomocnicza
  19. int h2=0;
  20. int h3=0;
  21. int h4=0;
  22. int h5=0;
  23. int pipeDescriptors[2];
  24.  
  25. void sig(int signum){
  26.     printf("WYSLANO SYGNAL\n");
  27. }
  28. void WorkerCreate(){
  29. char readChar;
  30.     while(1){
  31. printf("\n ZAWARTOSC DESKRYPTORA do pisania: %d do czytania %d\n\n", pipeDescriptors[0],pipeDescriptors[1]);
  32.  
  33.         read(pipeDescriptors[0],&readChar,sizeof(readChar));
  34.         printf("\nOdczytalem:   %c \n",readChar);
  35.         printf( "\n PID ROBOTNIKA: %d PID MOJEGO BRYGARDZISTY: %d\n", getpid(), getppid() );
  36. sleep(1);
  37.  
  38.         }
  39. }
  40. void TimeStampLoad(){
  41.  
  42.  
  43.     struct timespec tm1, tm2;
  44.     timer_t timer;
  45.         struct itimerspec its;
  46.     int seconds;
  47.     float nanoseconds;
  48.     struct sigevent sev;
  49.         sev.sigev_notify = SIGEV_SIGNAL;
  50.         sev.sigev_signo = SIGALRM;
  51.         timer_create(CLOCK_PROCESS_CPUTIME_ID, &sev, &timer);
  52.         seconds = time_stamp;
  53.         nanoseconds = (time_stamp - (float)seconds);
  54.  
  55.         its.it_value.tv_sec = seconds;
  56.         its.it_value.tv_nsec = nanoseconds * 1000000000.0;
  57.         its.it_interval.tv_sec = 0;
  58.         its.it_interval.tv_nsec = 0;
  59.  
  60.         timer_settime(timer, 0, &its, NULL);
  61.         timer_gettime(&timer, &its);
  62.  
  63. //char* full_message_with_stamp=(char*)calloc(50,1);
  64.         printf("WIADOMOSC: %s , jej czas: %ld . %ld\n", message,                its.it_value.tv_sec,its.it_value.tv_nsec);
  65.         tm1.tv_sec = seconds;
  66.         tm1.tv_nsec = nanoseconds * 100000000;
  67.     nanosleep(&tm1, &tm2);
  68.  
  69. }
  70. int main(int argc, char** argv)
  71. {
  72. //udp accept,
  73. //listen
  74. //
  75.  
  76. int options;
  77.  while ((options = getopt(argc, argv, "a:n:m:t:i:")) != -1) {
  78.         switch (options) {
  79.  
  80.             case 'a' :
  81.         h1=1;
  82.                 address=optarg;
  83.                 break;
  84.  
  85.             case 'n' :
  86.         h2=1;
  87.         number_of_workers=atoi(optarg);
  88.                                 break;
  89.  
  90.             case 'm' :
  91.  
  92.                 h3 = 1;
  93.  
  94.                 message = optarg;
  95.         case 'i':
  96.         h4=1;
  97.         id=atoi(optarg);
  98.  
  99.                 break;
  100.  
  101.             case 't' :
  102.         h5=1;
  103.                time_stamp= strtof(optarg, NULL);
  104.                 break;
  105.    
  106.  
  107.                  }
  108.     }
  109.  
  110.  
  111. //tworzenie socketu -> wykorzystanie strony: http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
  112. /*
  113.  int s, t, len;
  114.     struct sockaddr_un remote;
  115.     char str[100];
  116.  
  117.     if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  118.         perror(" BLAD FUNKCJI ----> SOCKET <----");
  119.         exit(1);
  120.     }
  121.  
  122.     printf("PROBA POLACZENIA ... \n");
  123.  
  124.     remote.sun_family = AF_UNIX;
  125.     strcpy(remote.sun_path, SOCK_PATH);
  126.     len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  127.     if (connect(s, (struct sockaddr *)&remote, len) == -1) {
  128.         perror("BLAD FUNKCJI ----> CONNECT <-----");
  129.         exit(1);
  130.     }
  131.  
  132.     printf("POLACZONO\n");
  133.  
  134.     while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
  135.         if (send(s, str, strlen(str), 0) == -1) {
  136.             perror("BLAD FUNKCJI ----> SEND <----");
  137.             exit(1);
  138.         }
  139.  
  140.         if ((t=recv(s, str, 100, 0)) > 0) {
  141.             str[t] = '\0';
  142.             printf("echo> %s", str);
  143.         } else {
  144.             if (t < 0) perror("BLAD FUNKCJI ----> RECV <----");
  145.             else printf("ARCHIWISTA ( SERWER) ZAKONCZYL SWOJE DZIALANIE \n");
  146.             exit(1);
  147.         }
  148.    */
  149.  
  150. int i=0;
  151.  
  152.  
  153. pipe(pipeDescriptors);
  154. int * pids_table;
  155. pids_table=(int *)malloc(number_of_workers+1);
  156. printf("JESTEM BRYGARDZISTA! MOJ PID: %d \n",getpid() );
  157.     for(i;i<=number_of_workers;i++){
  158.  
  159.         pids_table[i] = fork();
  160. printf("deskryptor: %d : a kolejny %d ",pipeDescriptors[0],pipeDescriptors[1]);
  161. sleep(2);
  162.         if(pids_table[i]==-1){
  163.             printf("\n BLAD FUNKCJI ----> FORK <----");
  164.         }
  165.  
  166.  
  167.  
  168.  
  169.  
  170. //if(killpg(getpid(),SIGUSR1)==-1){
  171. //printf("BLAD W FUNKCJI KILLPG ");
  172. //}
  173.  
  174.             if(pids_table[i]==0){
  175. close(pipeDescriptors[0]);
  176.  
  177.   write(pipeDescriptors[1],message,strlen(message));
  178. int value;
  179. waitpid(pids_table[i] , &value, 0);
  180. if(value) printf("\nFUNKCJA WRITE (pipe) ----> COS POSZLO NIE TAK :(( <----- \n");
  181.                 WorkerCreate();
  182. printf("\n ZAWARTOSC DESKRYPTORA do pisania: %d do czytania %d\n\n", pipeDescriptors[0],pipeDescriptors[1]);
  183.  
  184.  
  185.  
  186.  
  187.  
  188. //signal(SIGALRM, sig);
  189.  
  190.      
  191.                
  192.  
  193.             }
  194. close(pipeDescriptors[1]);
  195. }
  196. // }
  197.  
  198.    //close(s);
  199.  
  200.  
  201.     return 0;
  202.  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement