Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LAB 7
- // KOMPILOWAĆ Z -lpthread !!! //
- // GCC -pthread
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sched.h>
- #include <time.h>
- void *thread_function(void *data){
- int old, number;
- srand(time(NULL));
- number = *(int *)data;
- // ASYNCHRONOUS lub DEFERRED
- int return_code = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&old);
- if(return_code!=0)
- printf("pthread_setcanceltype() error: %d\n",return_code);
- printf("Przekazana liczba: %d\n", number);
- return NULL;
- }
- int main(int argc, char **argv){
- int number = 0;
- if(argc==2){
- number = atoi(argv[1]);
- }
- pthread_t thread_id;
- int return_code = pthread_create(&thread_id,NULL,thread_function,(void *)&number);
- if(return_code!=0)
- printf("pthread_create() error: %d\n", return_code);
- //if(sched_yield()<0)
- // perror("sched_yield");
- return_code = pthread_cancel(thread_id);
- if(return_code!=0)
- printf("pthread_cancel() error: %d\n", return_code);
- return_code = pthread_join(thread_id,NULL); // coś jak wait
- if(return_code!=0)
- printf("pthread_join() error: %d\n",return_code);
- return 0;
- }
- // LAB 8.1
- #include <termios.h>
- #include <unistd.h>
- #include <stdio.h>
- void echo_on(struct termios term){
- printf("ECHO ON \n");
- term.c_lflag = term.c_lflag | ECHO;
- if(tcsetattr(STDIN_FILENO, TCSANOW, &term) != 0)
- perror("tcsetattr: ");
- }
- void echo_off(struct termios term){
- printf("ECHO OFF \n");
- term.c_lflag = term.c_lflag & ~(ECHO);
- if(tcsetattr(STDIN_FILENO, TCSANOW, &term) != 0)
- perror("tcsetattr: ");
- }
- int main (void){
- struct termios term;
- if(tcgetattr(STDIN_FILENO, &term) != 0){
- perror("tcgetattr: ");
- return -1;
- }
- if((term.c_lflag & ECHO) == 0)
- echo_on(term);
- else
- echo_off(term);
- return 0;
- }
- // LAB 8.2
- #include<stdio.h>
- #include<stdlib.h>
- #include<termios.h>
- #include<unistd.h>
- static struct termios oldSettings, newSettings;
- void openKeyboard (void){
- tcgetattr(0, &oldSettings);
- newSettings = oldSettings;
- newSettings.c_lflag &= ~ICANON;
- newSettings.c_lflag &= ~ECHO;
- newSettings.c_lflag &= ~ISIG;
- newSettings.c_cc[VMIN] = 1;
- newSettings.c_cc[VTIME] = 0;
- tcsetattr(0, TCSANOW, &newSettings);
- }
- void closeKeyboard (void){
- tcsetattr(0, TCSANOW, &oldSettings);
- }
- int main (int argc, char* argv[]){
- openKeyboard();
- closeKeyboard();
- return 0;
- }
- // LAB 9.1
- // touch test.txt - to create file //
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- void print_file(char *string){
- int i;
- for(i=0; i<getpagesize(); i++)
- printf("%c",string[i]);
- puts("");
- }
- int main(){
- int descriptor = open("test.txt",O_RDWR, 0600);
- if(descriptor==-1){
- perror("open");
- return descriptor;
- }
- char *string = mmap(NULL,getpagesize(),PROT_READ,MAP_PRIVATE,descriptor,0);
- if(string==(char *)-1){
- perror("mmap");
- return -1;
- }
- printf("%s\n", string);
- if(munmap(string,getpagesize())==-1)
- perror("muunmap");
- if(close(descriptor)==01)
- perror("close");
- return 0;
- }
- // LAB 9.2
- #include <sys/types.h>
- #include <sys/file.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <string.h>
- int d;
- void *funkcja(void *data){
- int number = 0;
- number = *(int *)data;
- char c = 'A'+number;
- struct flock f;
- f.l_type = F_WRLCK;
- f.l_whence = SEEK_SET;
- f.l_start = number * sizeof(char);
- f.l_len = sizeof(char);
- if(fcntl(d, F_SETLKW, &f) == -1)
- perror("fcntl");
- printf("Zapisuje: %c\n", c);
- if(write(d, &c, sizeof(char) == -1))
- printf("Blad zapisu\n");
- f.l_type = F_UNLCK;
- if(fcntl(d, F_SETLKW, &f) == -1)
- perror("fcntl2");
- return NULL;
- }
- int main(){
- d = creat ("tst", 0600);
- pthread_t pthread[5];
- int i = 0;
- for(i=0; i<5; i++)
- if(pthread_create(&pthread[i],NULL,funkcja,(void*)&i) != 0)
- perror("pthread_create");
- for(i=0; i<5; i++)
- if(pthread_join(pthread[i], NULL) != 0)
- perror("pthread_join");
- if(close(d) == -1)
- perror("close");
- return 0;
- }
- // LAB 10k
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <string.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #define SERVER_PORT 1096
- #define SERVER_IP_ADDRESS "127.0.0.1"
- void send_message(int socket_descriptor){
- struct in_addr network_address;
- if(!inet_aton(SERVER_IP_ADDRESS, &network_address))
- perror("inet_aton");
- struct sockaddr_in server_address = {
- .sin_family = AF_INET,
- .sin_port = SERVER_PORT,
- .sin_addr = network_address
- };
- char *message = ("Siemanko witam");
- if(sendto(socket_descriptor, message, strlen(message), 0, (struct sockaddr*)&server_address, sizeof(server_address)) < 0)
- perror("sendto");
- return;
- }
- int main(void){
- int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
- if(socket_descriptor < 0)
- perror("socket");
- send_message(socket_descriptor);
- if(close(socket_descriptor) < 0)
- perror("close");
- return 0;
- }
- // LAB 10s
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/ip.h>
- #define PORT 1096
- void name_socket(int socket_descriptor){
- struct sockaddr_in server_address = {
- .sin_family = AF_INET,
- .sin_port = PORT,
- .sin_addr = {0}
- };
- 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];
- struct sockaddr_in client_address;
- 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);
- }
- }
- 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);
- if(close(socket_descriptor)<0)
- perror("close");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement