Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <signal.h>
- #include <time.h>
- //w moim programie przyjelam konwencje, ze brygardzista jest rodzicem, a robotnicy sa jego dziecmi
- #define SOCK_PATH "polaczenie"
- char* address; //parametr a
- int number_of_workers; //parametr n
- char* message; //parametr m
- float time_stamp; //parametr t
- int id;
- int h1=0; //zmienna pomocnicza
- int h2=0;
- int h3=0;
- int h4=0;
- int h5=0;
- int pipeDescriptors[2];
- void sig(int signum){
- printf("WYSLANO SYGNAL\n");
- }
- void WorkerCreate(){
- char readChar;
- while(1){
- printf("\n ZAWARTOSC DESKRYPTORA do pisania: %d do czytania %d\n\n", pipeDescriptors[0],pipeDescriptors[1]);
- read(pipeDescriptors[0],&readChar,sizeof(readChar));
- printf("\nOdczytalem: %c \n",readChar);
- printf( "\n PID ROBOTNIKA: %d PID MOJEGO BRYGARDZISTY: %d\n", getpid(), getppid() );
- sleep(1);
- }
- }
- void TimeStampLoad(){
- struct timespec tm1, tm2;
- timer_t timer;
- struct itimerspec its;
- int seconds;
- float nanoseconds;
- struct sigevent sev;
- sev.sigev_notify = SIGEV_SIGNAL;
- sev.sigev_signo = SIGALRM;
- timer_create(CLOCK_PROCESS_CPUTIME_ID, &sev, &timer);
- seconds = time_stamp;
- nanoseconds = (time_stamp - (float)seconds);
- its.it_value.tv_sec = seconds;
- its.it_value.tv_nsec = nanoseconds * 1000000000.0;
- its.it_interval.tv_sec = 0;
- its.it_interval.tv_nsec = 0;
- timer_settime(timer, 0, &its, NULL);
- timer_gettime(&timer, &its);
- //char* full_message_with_stamp=(char*)calloc(50,1);
- printf("WIADOMOSC: %s , jej czas: %ld . %ld\n", message, its.it_value.tv_sec,its.it_value.tv_nsec);
- tm1.tv_sec = seconds;
- tm1.tv_nsec = nanoseconds * 100000000;
- nanosleep(&tm1, &tm2);
- }
- int main(int argc, char** argv)
- {
- //udp accept,
- //listen
- //
- int options;
- while ((options = getopt(argc, argv, "a:n:m:t:i:")) != -1) {
- switch (options) {
- case 'a' :
- h1=1;
- address=optarg;
- break;
- case 'n' :
- h2=1;
- number_of_workers=atoi(optarg);
- break;
- case 'm' :
- h3 = 1;
- message = optarg;
- case 'i':
- h4=1;
- id=atoi(optarg);
- break;
- case 't' :
- h5=1;
- time_stamp= strtof(optarg, NULL);
- break;
- }
- }
- //tworzenie socketu -> wykorzystanie strony: http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
- /*
- int s, t, len;
- struct sockaddr_un remote;
- char str[100];
- if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
- perror(" BLAD FUNKCJI ----> SOCKET <----");
- exit(1);
- }
- printf("PROBA POLACZENIA ... \n");
- remote.sun_family = AF_UNIX;
- strcpy(remote.sun_path, SOCK_PATH);
- len = strlen(remote.sun_path) + sizeof(remote.sun_family);
- if (connect(s, (struct sockaddr *)&remote, len) == -1) {
- perror("BLAD FUNKCJI ----> CONNECT <-----");
- exit(1);
- }
- printf("POLACZONO\n");
- while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
- if (send(s, str, strlen(str), 0) == -1) {
- perror("BLAD FUNKCJI ----> SEND <----");
- exit(1);
- }
- if ((t=recv(s, str, 100, 0)) > 0) {
- str[t] = '\0';
- printf("echo> %s", str);
- } else {
- if (t < 0) perror("BLAD FUNKCJI ----> RECV <----");
- else printf("ARCHIWISTA ( SERWER) ZAKONCZYL SWOJE DZIALANIE \n");
- exit(1);
- }
- */
- int i=0;
- pipe(pipeDescriptors);
- int * pids_table;
- pids_table=(int *)malloc(number_of_workers+1);
- printf("JESTEM BRYGARDZISTA! MOJ PID: %d \n",getpid() );
- for(i;i<=number_of_workers;i++){
- pids_table[i] = fork();
- printf("deskryptor: %d : a kolejny %d ",pipeDescriptors[0],pipeDescriptors[1]);
- sleep(2);
- if(pids_table[i]==-1){
- printf("\n BLAD FUNKCJI ----> FORK <----");
- }
- //if(killpg(getpid(),SIGUSR1)==-1){
- //printf("BLAD W FUNKCJI KILLPG ");
- //}
- if(pids_table[i]==0){
- close(pipeDescriptors[0]);
- write(pipeDescriptors[1],message,strlen(message));
- int value;
- waitpid(pids_table[i] , &value, 0);
- if(value) printf("\nFUNKCJA WRITE (pipe) ----> COS POSZLO NIE TAK :(( <----- \n");
- WorkerCreate();
- printf("\n ZAWARTOSC DESKRYPTORA do pisania: %d do czytania %d\n\n", pipeDescriptors[0],pipeDescriptors[1]);
- //signal(SIGALRM, sig);
- }
- close(pipeDescriptors[1]);
- }
- // }
- //close(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement