Advertisement
tburton

SO - kolos_2 - programy

Jan 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.41 KB | None | 0 0
  1. // LAB 7
  2.  
  3. // KOMPILOWAĆ Z -lpthread !!! //
  4. // GCC -pthread
  5.  
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <sched.h>
  12. #include <time.h>
  13.  
  14. void *thread_function(void *data){
  15.     int old, number;
  16.     srand(time(NULL));
  17.     number = *(int *)data;
  18.         // ASYNCHRONOUS lub DEFERRED
  19.     int return_code = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&old);
  20.     if(return_code!=0)
  21.         printf("pthread_setcanceltype() error: %d\n",return_code);
  22.     printf("Przekazana liczba: %d\n", number);
  23.    
  24.     return NULL;
  25. }
  26.  
  27. int main(int argc, char **argv){
  28.     int number = 0;
  29.     if(argc==2){
  30.         number = atoi(argv[1]);
  31.     }
  32.     pthread_t thread_id;
  33.     int return_code = pthread_create(&thread_id,NULL,thread_function,(void *)&number);
  34.     if(return_code!=0)
  35.         printf("pthread_create() error: %d\n", return_code);
  36.     //if(sched_yield()<0)
  37.     //  perror("sched_yield");
  38.     return_code = pthread_cancel(thread_id);
  39.     if(return_code!=0)
  40.         printf("pthread_cancel() error: %d\n", return_code);
  41.     return_code = pthread_join(thread_id,NULL); // coś jak wait
  42.     if(return_code!=0)
  43.         printf("pthread_join() error: %d\n",return_code);
  44.  
  45.    
  46.     return 0;
  47. }
  48.  
  49.  
  50. // LAB 8.1
  51. #include <termios.h>
  52. #include <unistd.h>
  53. #include <stdio.h>
  54.  
  55. void echo_on(struct termios term){
  56.        printf("ECHO ON \n");
  57.        term.c_lflag = term.c_lflag | ECHO;  
  58.        if(tcsetattr(STDIN_FILENO, TCSANOW, &term) != 0)
  59.             perror("tcsetattr: ");
  60. }
  61.  
  62. void echo_off(struct termios term){
  63.        printf("ECHO OFF \n");
  64.        term.c_lflag = term.c_lflag & ~(ECHO);
  65.        if(tcsetattr(STDIN_FILENO, TCSANOW, &term) != 0)
  66.         perror("tcsetattr: ");
  67. }
  68.  
  69. int main (void){
  70.     struct termios term;
  71.    
  72.     if(tcgetattr(STDIN_FILENO, &term) != 0){
  73.         perror("tcgetattr: ");
  74.         return -1;
  75.     }
  76.  
  77.     if((term.c_lflag & ECHO) == 0)
  78.         echo_on(term);
  79.     else
  80.     echo_off(term);
  81.  
  82.     return 0;
  83. }
  84.  
  85.  
  86. // LAB 8.2
  87.  
  88. #include<stdio.h>
  89. #include<stdlib.h>
  90. #include<termios.h>
  91. #include<unistd.h>
  92.  
  93. static struct termios oldSettings, newSettings;
  94.  
  95. void openKeyboard (void){
  96.         tcgetattr(0, &oldSettings);
  97.         newSettings = oldSettings;
  98.         newSettings.c_lflag &= ~ICANON;
  99.         newSettings.c_lflag &= ~ECHO;
  100.         newSettings.c_lflag &= ~ISIG;
  101.         newSettings.c_cc[VMIN] = 1;
  102.         newSettings.c_cc[VTIME] = 0;
  103.  
  104.         tcsetattr(0, TCSANOW, &newSettings);
  105. }
  106.  
  107. void closeKeyboard (void){
  108.         tcsetattr(0, TCSANOW, &oldSettings);
  109. }
  110.  
  111.  
  112. int main (int argc, char* argv[]){
  113.  
  114.         openKeyboard();
  115.  
  116.         closeKeyboard();
  117.         return 0;
  118. }
  119.  
  120.  
  121.  
  122. // LAB 9.1
  123.  
  124. // touch test.txt - to create file //
  125.  
  126. #include <sys/mman.h>
  127. #include <sys/types.h>
  128. #include <sys/stat.h>
  129. #include <fcntl.h>
  130. #include <stdio.h>
  131. #include <unistd.h>
  132.  
  133. void print_file(char *string){
  134.     int i;
  135.     for(i=0; i<getpagesize(); i++)
  136.         printf("%c",string[i]);
  137.     puts("");  
  138. }
  139.  
  140. int main(){
  141.     int descriptor = open("test.txt",O_RDWR, 0600);
  142.     if(descriptor==-1){
  143.         perror("open");
  144.         return descriptor;
  145.     }
  146.  
  147.     char *string = mmap(NULL,getpagesize(),PROT_READ,MAP_PRIVATE,descriptor,0);
  148.     if(string==(char *)-1){
  149.         perror("mmap");
  150.         return -1;
  151.     }
  152.     printf("%s\n", string);
  153.    
  154.     if(munmap(string,getpagesize())==-1)
  155.         perror("muunmap");
  156.  
  157.     if(close(descriptor)==01)
  158.         perror("close");
  159.  
  160.     return 0;
  161. }
  162.  
  163.  
  164. // LAB 9.2
  165.  
  166. #include <sys/types.h>
  167. #include <sys/file.h>
  168. #include <fcntl.h>
  169. #include <pthread.h>
  170. #include <stdlib.h>
  171. #include <unistd.h>
  172. #include <errno.h>
  173. #include <stdio.h>
  174. #include <sys/stat.h>
  175. #include <string.h>
  176.  
  177.  
  178. int d;
  179.  
  180. void *funkcja(void *data){
  181.     int number = 0;
  182.     number = *(int *)data;
  183.     char c = 'A'+number;
  184.     struct flock f;
  185.     f.l_type = F_WRLCK;
  186.     f.l_whence = SEEK_SET;
  187.     f.l_start = number * sizeof(char);
  188.     f.l_len = sizeof(char);
  189.     if(fcntl(d, F_SETLKW, &f) == -1)
  190.         perror("fcntl");
  191.     printf("Zapisuje: %c\n", c);
  192.     if(write(d, &c, sizeof(char) == -1))
  193.         printf("Blad zapisu\n");
  194.     f.l_type = F_UNLCK;
  195.     if(fcntl(d, F_SETLKW, &f) == -1)
  196.         perror("fcntl2");
  197.     return NULL;
  198. }
  199.  
  200. int main(){
  201.     d = creat ("tst", 0600);
  202.     pthread_t pthread[5];
  203.     int i = 0;
  204.     for(i=0; i<5; i++)
  205.         if(pthread_create(&pthread[i],NULL,funkcja,(void*)&i) != 0)
  206.             perror("pthread_create");
  207.     for(i=0; i<5; i++)
  208.         if(pthread_join(pthread[i], NULL) != 0)
  209.             perror("pthread_join");
  210.  
  211.     if(close(d) == -1)
  212.         perror("close");
  213.  
  214.     return 0;
  215. }
  216.  
  217.  
  218. // LAB 10k
  219. #include <stdio.h>
  220. #include <unistd.h>
  221. #include <sys/socket.h>
  222. #include <sys/socket.h>
  223. #include <sys/types.h>
  224. #include <string.h>
  225. #include <netinet/ip.h>
  226. #include <arpa/inet.h>
  227.  
  228. #define SERVER_PORT 1096
  229. #define SERVER_IP_ADDRESS "127.0.0.1"
  230.  
  231. void send_message(int socket_descriptor){
  232.     struct in_addr network_address;
  233.     if(!inet_aton(SERVER_IP_ADDRESS, &network_address))
  234.         perror("inet_aton");
  235.  
  236.     struct sockaddr_in server_address = {
  237.         .sin_family = AF_INET,
  238.         .sin_port = SERVER_PORT,
  239.         .sin_addr = network_address
  240.     };
  241.    
  242.     char *message = ("Siemanko witam");
  243.     if(sendto(socket_descriptor, message, strlen(message), 0, (struct sockaddr*)&server_address, sizeof(server_address)) < 0)
  244.         perror("sendto");
  245.  
  246.     return;
  247. }
  248.  
  249. int main(void){
  250.     int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  251.     if(socket_descriptor < 0)
  252.         perror("socket");
  253.    
  254.     send_message(socket_descriptor);
  255.  
  256.     if(close(socket_descriptor) < 0)
  257.         perror("close");
  258.  
  259.  
  260.     return 0;
  261. }
  262.  
  263.  
  264. // LAB 10s
  265.  
  266. #include <stdio.h>
  267. #include <unistd.h>
  268. #include <sys/types.h>
  269. #include <sys/socket.h>
  270. #include <netinet/ip.h>
  271.  
  272. #define PORT 1096
  273.  
  274. void name_socket(int socket_descriptor){
  275.     struct sockaddr_in server_address = {
  276.         .sin_family = AF_INET,
  277.         .sin_port = PORT,
  278.         .sin_addr = {0}
  279.     };
  280.  
  281.     if(bind(socket_descriptor, (struct sockaddr*)&server_address, sizeof(server_address)) < 0)
  282.         perror("bind");
  283.  
  284. }
  285.  
  286. void get_and_print_message(int socket_descriptor){
  287.     char buffer[512];
  288.     struct sockaddr_in client_address;
  289.  
  290.     socklen_t address_length = sizeof(client_address);
  291.  
  292.     int received_bytes = recvfrom(socket_descriptor, (void *)buffer, sizeof(buffer), 0, (struct sockaddr*)&client_address, &address_length);
  293.     if(received_bytes < 0)
  294.         perror("recvfrom");
  295.     else {
  296.         buffer[received_bytes] = '0';
  297.         puts(buffer);
  298.     }
  299.  
  300. }
  301.  
  302.  
  303. int main(void){
  304.     int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  305.     if(socket_descriptor<0)
  306.         perror("socket");
  307.  
  308.     name_socket(socket_descriptor);
  309.     get_and_print_message(socket_descriptor);
  310.  
  311.     if(close(socket_descriptor)<0)
  312.         perror("close");
  313.  
  314.     return 0;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement