Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stwórz w programie dwa wątki, które wypiszą swój identyfikator i identyfikator procesu
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- void *fun_watek1(void * p);
- void *fun_watek2(void * p);
- int main() {
- pthread_t watek1;
- pthread_t watek2;
- pthread_create(&watek1, NULL, fun_watek1, (void *)&watek1);
- pthread_create(&watek2, NULL, fun_watek2, (void *)&watek2);
- pthread_join(watek1, NULL);
- pthread_join(watek2, NULL);
- return 0;
- }
- void *fun_watek1(void * p) {
- printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
- return 0;
- }
- void *fun_watek2(void * p) {
- printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
- return 0;
- }
- Stwórz dwa wątki w programie. Każdemu z nich przekaż przez parametr funkcji dwie liczby. Pierw-szy wątek niech policzy sumę tych liczb, drugi różnicę. Obie wartości należy wypisać na ekranw wątkach.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- void *fun_watek1(void * p);
- void *fun_watek2(void * p);
- struct liczby{
- int x;
- int y;
- };
- struct liczby message;
- int main() {
- message.x = 4;
- message.y = 2;
- pthread_t watek1;
- pthread_t watek2;
- pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
- pthread_create(&watek2, NULL, fun_watek2, (void *)&message);
- pthread_join(watek1, NULL);
- pthread_join(watek2, NULL);
- return 0;
- }
- void *fun_watek1(void * p) {
- printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
- struct liczby * l = (struct liczby*) p;
- printf("%d + %d = %d\n",l->x, l->y, l->x+l->y);
- return 0;
- }
- void *fun_watek2(void * p) {
- printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
- struct liczby * l = (struct liczby*) p;
- printf("%d - %d = %d\n",l->x, l->y, l->x-l->y);
- return 0;
- }
- Napisz program, w którym stworzysz jeden wątek łączny i jeden wątek rozdzielny oraz zademon-strujesz różnicę w działaniu tych wątków.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- void *fun_watek1(void * p);
- void *fun_watek2(void * p);
- char* message;
- pthread_attr_t attr;
- int main() {
- pthread_t watek1;
- pthread_t watek2;
- pthread_attr_init(&attr);
- pthread_create(&watek1, &attr, fun_watek1, (void *)&message);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- pthread_create(&watek2, &attr, fun_watek2, (void *)&message);
- pthread_join(watek1, NULL);
- //tu
- pthread_join(watek2, NULL);
- pthread_attr_destroy(&attr);
- return 0;
- }
- void *fun_watek1(void * p) {
- printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
- return 0;
- }
- void *fun_watek2(void * p) {
- printf("Watek 2: %lu, PID: %d\n", pthread_self(), getpid());
- return 0;
- }
- Zademonstruj działanie funkcjipthread_kill() wysyłając do wątku sygnał, dla którego będzie onmiał własną procedurę obsługi. Do podmiany procedury obsługi wykorzystaj funkcjęsigaction().
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <time.h>
- void *fun_watek1(void * p);
- char* message;
- int main() {
- pthread_t watek1;
- pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
- sleep(2);
- pthread_kill(watek1, SIGHUP);
- pthread_join(watek1, NULL);
- return 0;
- }
- void *fun_watek1(void * p) {
- int koniec = 0;
- void syghup(int a)
- {
- printf("Otrzymalem sygnal. Koniec\n");
- koniec=1;
- }
- signal(SIGHUP, &syghup);
- printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
- for(;;){
- if(koniec==1) break;
- };
- return 0;
- }
- Zademonstruj sposób użycia semafora, którego wartość początkowa jest większa od jeden
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <time.h>
- static sigset_t signal_mask;
- void *fun_watek1(void * p);
- char* message;
- int rc;
- int main() {
- sigemptyset (&signal_mask);
- sigaddset (&signal_mask, SIGHUP);
- rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
- if (rc != 0) {
- printf("Blad blokowania sygnalu");
- }
- pthread_t watek1;
- rc = pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
- if (rc != 0) {
- printf("Blad tworzenia");
- }
- sleep(2);
- pthread_kill(watek1, SIGHUP);
- pthread_join(watek1, NULL);
- return 0;
- }
- void *fun_watek1(void * p) {
- int sig;
- printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
- sigwait(&signal_mask, &sig);
- printf("Sygnal %d Koniec\n", sig);
- return 0;
- }
- Zademonstruj działanie funkcji sprzątających
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <time.h>
- void cleanupHandler(void *arg)
- {
- printf("Funkcja sprzatajaca\n");
- }
- void *fun_watek1(void * p) {
- printf("Watek 1: %lu, PID: %d\n", pthread_self(), getpid());
- pthread_cleanup_push(cleanupHandler, NULL);
- while (1) {
- pthread_testcancel();
- sleep(1);
- }
- pthread_cleanup_pop(0);
- return 0;
- }
- char* message;
- int rc;
- int main() {
- pthread_t watek1;
- rc = pthread_create(&watek1, NULL, fun_watek1, (void *)&message);
- if (rc != 0) {
- printf("Blad tworzenia");
- }
- sleep(2);
- rc = pthread_cancel(watek1);
- if (rc != 0) {
- printf("Blad blokowania sygnalu");
- }
- sleep(2);
- //pthread_join(watek1, NULL);
- return 0;
- }
- Napisz program, w którym stworzysz 20 wątków wykonujących tę samą czynność. W momenciekiedy jeden z nich ją zakończy pozostałe powinny być anulowane w sposób asynchroniczny.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <time.h>
- void *fun_watek(void * p) {
- int oldtype, rc;
- int x=2,l;
- printf("Watek: %lu, PID: %d\n", pthread_self(), getpid());
- rc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
- if (rc != 0) {
- printf("Blad ustawienia cancel");
- }
- for(l=0; l<1000000; l++){
- x*=2;
- }
- printf("Koniec watku %lu\n",pthread_self());
- return 0;
- }
- int rc;
- int main() {
- int i;
- pthread_t watek[20];
- for(i=0; i<20;i++){
- rc = pthread_create(&watek[i], NULL, fun_watek, NULL);
- if (rc != 0) {
- printf("Blad tworzenia");
- }
- }
- //sleep(6);
- pthread_join(watek[0], NULL);
- for(i=0; i<20; i++) pthread_cancel(watek[i]);
- printf("Koniec programu\n");
- return 0;
- }
- Napisz program, który sprawdzi, czy z deskryptorem standardowego wejścia (stdin_filenolubwartość0) jest związany terminal. Jeśli tak, to program powinien wypisać nazwę pliku urządzeniatego terminala, oraz kilka jego bieżących ustawień.
- #include <termios.h>
- #include <unistd.h>
- #include <stdio.h>
- int main (void)
- {
- struct termios temp;
- int atr_l, atr_i, atr_o, atr_c;
- char * ciag;
- if(tcgetattr(STDIN_FILENO, &temp) != 0)
- {
- perror("tcgetattr");
- return -1;
- }
- atr_i=temp.c_iflag;
- atr_o=temp.c_oflag;
- atr_c=temp.c_cflag;
- atr_l=temp.c_lflag;
- if(isatty(STDIN_FILENO) == 1)
- {
- printf("Deskryptor odnosi sie do terminala\n");
- ciag = ttyname(STDIN_FILENO);
- if(ciag != NULL)
- {
- printf("nazwa pliku terminala: %s\n", ciag);
- printf("ECHO: \t\t%s\n", atr_l & ECHO?"włączone":"wyłączone");
- printf("ICANON: \t%s\n", atr_l & ICANON?"włączone":"wyłączone");
- printf("\n");
- printf("INPCK: \t%s\n", atr_i & INPCK?"włączone":"wyłączone");
- printf("IGNCR: \t%s\n", atr_i & IGNCR?"włączone":"wyłączone");
- printf("\n");
- printf("OPOST: \t%s\n", atr_o & OPOST?"włączone":"wyłączone");
- printf("OFILL: \t%s\n", atr_o & OFILL?"włączone":"wyłączone");
- printf("\n");
- printf("CREAD: \t\t%s\n", atr_c & CREAD?"włączone":"wyłączone");
- printf("CLOCAL: \t%s\n", atr_c & CLOCAL?"włączone":"wyłączone");
- }
- else
- {
- perror("ttyname");
- return -1;
- }
- }
- else
- {
- printf("Dekstryptor nie odnosi sie.");
- perror("isatty: ");
- return -1;
- }
- return 0;
- }
- Napisz program, który wyłączy echo, czyli wypisywanie znaków wprowadzonych z klawiatury naekran. Po ponownym uruchomieniu program powinien przywrócić echo.
- #include <termios.h>
- #include <unistd.h>
- #include <stdio.h>
- int main (void)
- {
- struct termios temp;
- int atr;
- if(tcgetattr(STDIN_FILENO, &temp) != 0)
- {
- perror("tcgetattr: ");
- return -1;
- }
- atr=temp.c_lflag & ECHO;
- if(atr == 0)
- {
- printf("ECHO ON \n");
- temp.c_lflag = temp.c_lflag | ECHO;
- if(tcsetattr(STDIN_FILENO, TCSANOW, &temp) != 0)
- {
- perror("tcsetattr: ");
- return -1;
- }
- }
- else
- {
- printf("ECHO OFF \n");
- temp.c_lflag = temp.c_lflag & ~(ECHO);
- if(tcsetattr(STDIN_FILENO, TCSANOW, &temp) != 0)
- {
- perror("tcsetattr: ");
- return -1;
- }
- }
- return 0;
- }
- Napisz program, po którego uruchomieniu terminal będzie wypisywał wszystkie informacje dużymiliterami (niezależnie od ustawienia CapsLock). Efekt ten powinno usuwać ponowne uruchomienieprogramu.
- #include <termios.h>
- #include <unistd.h>
- #include <stdio.h>
- int main (void)
- {
- struct termios term;
- int x;
- tcgetattr(STDIN_FILENO, &term);
- x=term.c_oflag & OLCUC;
- if(x == 0)
- {
- term.c_oflag |= OLCUC;
- tcsetattr(STDIN_FILENO, TCSANOW, &term);
- }
- else
- {
- term.c_oflag &= ~(OLCUC);
- tcsetattr(STDIN_FILENO, TCSANOW, &term);
- }
- return 0;
- }
- Napisz program, który przepisze z istniejącego pliku tekstowego czterdzieści znaków z początkui czterdzieści znaków z końca do nowego pliku.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- int main()
- {
- int desk = open("emg", 0666);
- if(desk == -1) perror("open");
- int fd = creat("z", S_IRWXU);
- if(fd == -1) perror("create");
- int tekst[41];
- int a = read(desk, (void *)tekst, 40 * sizeof(char));
- if(a == -1) perror("read");
- else
- printf("Odczytano %d znaków\n", a);
- a = write(fd, tekst, 40 * sizeof(char));
- if(a == -1) perror("write");
- else printf("Zapisano %d bajtów\n", a);
- lseek(desk, -(sizeof(int) * 40), SEEK_END);
- a = read(desk, (void *)tekst, 40 * sizeof(char));
- if(a == -1) perror("read2");
- else printf("Odczytano %d bajtów", a);
- lseek(fd, 1, SEEK_END);
- a = write(fd, tekst, 40 * sizeof(char));
- if(a == -1) perror("write2");
- else printf("Zapisano %d bajtów\n", a);
- if(desk != -1) close(desk);
- if(fd != -1) close(fd);
- return 0;
- }
- Stwórz plik tekstowy wielkości 512 bajtów, z „dziurą” w środku.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- int d1 = creat("z2", S_IRWXU);
- if(d1 == -1) perror("Creat");
- int d2 = dup(d1);
- if(d2 == -1) printf("Nie udało się skopiować deskryptora pliku\n");
- else printf("Skopiowano deskryptor pliku\n");
- char t1[] ="Zapis t1\n";
- int a = write(d1, t1, strlen(t1));
- if(a == -1) perror("write1");
- else printf("Zapisano %d\n", a);
- char t2[] = "Zapis t2\n";
- a = write(d2, t2, strlen(t2));
- if(a == -1) perror("Write2");
- else printf("Zapisano d2 %d\n", a);
- int d3;
- close(d3);
- if(dup2(d1, d3) == -1) printf("Nie udało się skopiować desk pliku\n");
- else printf("Skopiowano desk pliku\n");
- char t3[] = "Zapis t3";
- a = write(d3, t3, strlen(t3));
- if(a == -1) perror("Write3");
- else printf("Zapisano d3 %d\n", a);
- close(d3);
- close(d2);
- close(d1);
- return 0;
- }
- Zademonstruj działanie funkcji replikującej deskryptory plików.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/file.h>
- #include <stdlib.h>
- #include <stdio.h>
- void *zap(void *);
- int d;
- int main()
- {
- d = creat("z3", S_IRWXU);
- pthread_t tid[20];
- int i;
- for(i = 0; i < 20; i++)
- {
- pthread_create(&tid[i], NULL, zap, (void *)&i);
- pthread_join(tid[i], NULL);
- }
- close(d);
- return 0;
- }
- void *zap(void * p)
- {
- int a;
- memcpy((void *)&a, p, sizeof(int));
- printf("Wątek %d\n", a);
- char c = a + 48;
- flock(d, LOCK_EX | LOCK_NB);
- int r = write(d, &c, sizeof(char));
- if(r == -1) printf("Nie udało się zapisać w wątku %d\n", a);
- else printf("Zapisanow w wątku %d %d bajtów\n", a, r);
- flock(d, LOCK_UN);
- return NULL;
- }
- Napisz program, który przeczyta i wypisze na ekran treść pliku tekstowego o dowolnej wielkości.Nazwę pliku należy przekazywać jako argument wywołania programu
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- int main(int argc, char *argv[])
- {
- if(argc != 3)
- {
- printf("Podaj jako parametr ścieżkę do pliku\n");
- exit(0);
- }
- //printf("%s", argv[1]);
- char *plik = (char *)calloc(strlen(argv[1]), sizeof(char));
- strcpy(plik, argv[1]);
- char *plik2 = (char *)calloc(strlen(argv[2]), sizeof(char));
- strcpy(plik2, argv[2]);
- char p[] = "Pliki: ";
- char newLine[] = "\n";
- write(1, p, strlen(p));
- write(1, plik, strlen(plik));
- write(1, plik2, strlen(plik2));
- write(1, newLine, strlen(newLine));
- int d = open(plik, 0444);
- if(d == -1) perror("open\n");
- free(plik);
- off_t roz = lseek(d, 0, SEEK_END);
- printf("Rozmiar pliku %d bajtów\n", (int)roz);
- char *tmp = (char *)malloc(roz);
- lseek(d, 0, SEEK_SET);
- int r = read(d, tmp, roz);
- if(r == -1) perror("read");
- else printf("Odczytano %d bajtów\n\n", r);
- r = write(1, tmp, roz);
- if(r == -1) perror("write wys");
- else printf("\nWyświetlono %d bajtów\n", r);
- int d1 = creat(plik2, S_IRWXU);
- if(d1 == -1) perror("creat");
- free(plik2);
- r = write(d1, tmp, roz);
- if(r == -1) perror("write zap");
- else printf("\nZapisono %d bajtów\n", r);
- free(tmp);
- if(d != -1) close(d);
- if(d1 != -1) close(d1);
- return 0;
- }
- Zmodyfikuj przykładowe programy tak, aby serwer odsyłał do klienta komunikat potwierdzającyodebranie komunikatu.
- //client
- #include<stdio.h>
- #include<unistd.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<string.h>
- #include<netinet/ip.h>
- #include<arpa/inet.h>
- #define SERVER_PORT 1096
- struct sockaddr_in server_address =
- {
- .sin_family = AF_INET,
- .sin_port = SERVER_PORT,
- .sin_addr = {INADDR_ANY}
- };
- void get_and_print_message(int socket_descriptor)
- {
- char buffer[512];
- socklen_t address_length = sizeof(server_address);
- int received_bytes = recvfrom(socket_descriptor,(void *)buffer,sizeof(buffer),0,(struct sockaddr*)&server_address,&address_length);
- if(received_bytes<0)
- perror("recvfrom");
- else
- {
- buffer[received_bytes]='\0';
- puts(buffer);
- }
- }
- void send_message(int socket_descriptor)
- {
- const char *message = "Komunikat przesłany przez sieć.";
- if(sendto(socket_descriptor,message,strlen(message),0,(struct sockaddr *)&server_address,sizeof(server_address))<0)
- perror("sendto");
- }
- SERVER
- int main(void)
- {
- int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
- if(socket_descriptor<0)
- perror("socket");
- send_message(socket_descriptor);
- get_and_print_message(socket_descriptor);
- if(close(socket_descriptor)<0)
- perror("close");
- return 0;
- }
- //server
- #include<stdio.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/ip.h>
- #include<string.h>
- #define SERVER_PORT 1096
- #define SERVER_IP_ADDRESS "127.0.0.1"
- struct sockaddr_in client_address;
- struct sockaddr_in server_address =
- {
- .sin_family = AF_INET,
- .sin_port = SERVER_PORT,
- .sin_addr = {INADDR_ANY}
- };
- void name_socket(int socket_descriptor)
- {
- if(bind(socket_descriptor,(struct sockaddr*)&server_address,sizeof(server_address))<0)
- perror("bind");
- }
- void get_and_print_message(int socket_descriptor)
- {
- char buffer[512];
- socklen_t address_length = sizeof(client_address);
- int received_bytes = recvfrom(socket_descriptor,(void *)buffer,sizeof(buffer), 0,(struct sockaddr*)&client_address,&address_length);
- if(received_bytes<0)
- perror("recvfrom");
- else
- {
- buffer[received_bytes]='\0';
- puts(buffer);
- }
- }
- void send_message(int socket_descriptor)
- {
- const char *message = "Komunikat zostal odebrany.";
- if(sendto(socket_descriptor,message,strlen(message),0,(struct sockaddr *)&client_address,sizeof(server_address))<0)
- perror("sendto");
- }
- int main(void)
- {
- int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
- if(socket_descriptor<0)
- perror("socket");
- name_socket(socket_descriptor);
- get_and_print_message(socket_descriptor);
- send_message(socket_descriptor);
- if(close(socket_descriptor)<0)
- perror("close");
- return 0;
- }
- Napisz programy, które będą realizował polecenie zawarte w zadaniu pierwszym, ale w oparciuo protokółTCP/IP.
- KLIENT
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <sys/sendfile.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- struct sockaddr_in do_kogo_addr, moj_addr;
- int socket_fd, socket_accept;
- char moje_ip[20]="127.0.0.1";
- char odbiorca_ip[20]="127.0.0.1";
- int main(void){
- int desk = open("plik.gts", 0666);
- if(desk == -1) perror("open");
- else printf(" opened file ");
- socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(socket_fd == -1){
- perror("socket: ");
- return -1;
- }
- else printf("\nSocket");
- do_kogo_addr.sin_family = AF_INET;
- do_kogo_addr.sin_port = htons(4000);
- do_kogo_addr.sin_addr.s_addr = inet_addr(odbiorca_ip);
- if ( connect(socket_fd, (struct sockaddr*)&do_kogo_addr, sizeof(struct sockaddr)) == -1){
- perror("connect: ");
- close(socket_fd);
- return -1;
- }
- else printf("\nconnect");
- char bufor[100]="wiadomosc od klienta do servera";
- int costam=0;
- int numbytes = 0;
- do{
- costam=sendfile(socket_fd, desk,NULL,50);
- //numbytes += send(socket_fd,&bufor,sizeof(bufor),0);
- }while(costam==50);
- close(socket_fd);
- close(desk);
- return 0;
- }
- SERVER
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <sys/sendfile.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- struct sockaddr_in od_kogo_addr, moj_addr;
- int socket_fd, socket_accept;
- char moje_ip[20]="127.0.0.1";
- char odbiorca_ip[20]="127.0.0.1";
- int main(void){
- socklen_t _size;
- int desk2 = creat("plik2.gts", 0666);
- if(desk2 == -1) perror("open");
- else printf(" opened file ");
- socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(socket_fd == -1){
- perror("socket: ");
- return -1;
- }
- else printf("\n socket");
- moj_addr.sin_family = AF_INET;
- moj_addr.sin_port = htons(4000);
- moj_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- if ( (bind(socket_fd, (struct sockaddr*)&moj_addr, sizeof(struct sockaddr))) == -1){
- close(socket_fd);
- perror("bind: ");
- }
- else printf("\n bind: ");
- if(listen(socket_fd, SOMAXCONN) == -1) {
- close(socket_fd);
- perror("listen: ");
- }
- else printf("\n listen: ");
- _size = sizeof(struct sockaddr_in);
- socket_accept = accept (socket_fd, (struct sockaddr*)&od_kogo_addr, &_size);
- if(socket_accept == -1){
- close(socket_fd);
- close(socket_accept);
- perror("\naccept: ");
- return -1;
- }
- else printf("accept\n");
- char bufor[100];
- int numbytes=1;
- int a=0;
- //do{
- numbytes = recv(socket_accept, bufor, sizeof(bufor), 0);
- printf("\n %d,\n %s\n\n",numbytes,bufor);
- a += write(desk2, bufor, 100 * sizeof(char));
- //}while(numbytes != 0);
- close(desk2);
- return 0;
- }
- Stwórz serwer współbieżny, który będzie obsługiwał połączenia od wielu klientów, również napi-sanych przez Ciebie - mogą one przesyłać np. pseudolosowe liczby do serwera, który będzie jewyświetlał na ekranie. Połączenia powinny być obsługiwane przez procesy potomne. Aby uniknąćtworzenia procesów zombie, proces macierzysty powinien ignorować sygnały o zakończeniu proce-sów potomnych. Użyj protokołu połączeniowego.8.Wykonaj polecenie z poprzedniego zadania, używając tym razem wątków zamiast procesów.
- KLIENT
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFLEN 512
- #define NPACK 3
- #define PORT 54321
- #define SRV_IP "127.0.0.1"
- void blad(char *s)
- {
- perror(s);
- exit(1);
- }
- int main(void)
- {
- struct sockaddr_in si_other;
- int gniazdo, i;
- socklen_t slen = sizeof(struct sockaddr);
- char buf[BUFLEN];
- if ((gniazdo = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
- blad("socket: ");
- memset((char *) &si_other, 0, sizeof(si_other));
- si_other.sin_family = AF_INET;
- si_other.sin_port = htons(PORT);
- si_other.sin_addr.s_addr = inet_addr(SRV_IP);
- connect(gniazdo, (struct sockaddr*)&si_other, sizeof(struct sockaddr));
- for (i = 0; i < NPACK; i++) {
- //wysylanie
- printf("Wiadomosc nr: %d\n ", i+1);
- scanf("%s", buf);
- printf("\n");
- if (sendto(gniazdo, buf, BUFLEN, 0, (struct sockaddr *)&si_other, slen) == -1)
- blad("sendto: ");
- }
- close(gniazdo);
- return 0;
- }
- SERVER
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #define BUFLEN 512
- #define NPACK 3
- #define PORT 54321
- int gniazdo, i;
- socklen_t slen = sizeof(struct sockaddr);
- char buf[BUFLEN];
- struct sockaddr_in si_me, si_other;
- void blad(char *s)
- {
- perror(s);
- exit(1);
- }
- void *odbierz(void *arg)
- {
- for (i = 0; i < NPACK; i++) {
- //odbieranie
- if (recvfrom(gniazdo, buf, BUFLEN, 0,(struct sockaddr*) &si_other, &slen) == -1)
- blad("perror: ");
- printf("Wiadomosc od %s:%d\n %s\n\n",
- inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
- }
- }
- int main(void)
- {
- pthread_t klient_watek;
- if ((gniazdo = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
- blad("socket: ");
- memset((char *) &si_me, 0, sizeof(si_me));
- si_me.sin_family = AF_INET;
- si_me.sin_port = htons(PORT);
- si_me.sin_addr.s_addr = htonl(INADDR_ANY);
- if (bind(gniazdo, (struct sockaddr*)&si_me, sizeof(si_me)) == -1)
- blad("bind: ");
- int j = 0;
- for(j = 0; j < 2; j++) {
- listen(gniazdo, 1);
- int list = accept(gniazdo, (struct sockaddr*) &si_other, &slen);
- if ( pthread_create( &klient_watek, NULL, odbierz, NULL))
- blad("pthread_create: ");
- if ( pthread_join ( klient_watek, NULL))
- blad("pthread_join: ");
- }
- close(gniazdo);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement