Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////// 1
- #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 *funkcja1(void *a);
- void *funkcja2(void *a);
- void *funkcja3(void *a);
- int liczby1[10], liczby2[10];
- pthread_t watek1, watek2, watek3;
- int main(){
- srand(time(NULL));
- pthread_create(&watek1, NULL, funkcja1, NULL);
- pthread_create(&watek2, NULL, funkcja2, NULL);
- pthread_create(&watek3, NULL, funkcja3, NULL);
- printf("Watek 1 zaczyna losowac liczby\n");
- pthread_join(watek1, NULL);
- printf("Watek 2 zaczyna losowac liczby\n");
- pthread_join(watek2, NULL);
- pthread_join(watek3, NULL);
- return 0;
- }
- void *funkcja1(void *a){
- int i;
- for(i = 0; i < 10; i++){
- int a = rand()%100;
- while(a%2!=0)
- {
- a = rand()%100;
- }
- liczby1[i] = a;
- sleep(3);
- }
- return 0;
- }
- void *funkcja2(void *a){
- int i;
- for(i = 0; i < 10; i++){
- int a = rand()%100;
- while(a%2==0)
- {
- a = rand()%100;
- }
- liczby2[i] = a;
- sleep(2);
- }
- return 0;
- }
- void *funkcja3(void *a){
- sleep(2*10+3*10);
- int i, suma = 0;
- printf("Watek 1 wylosowal liczby: ");
- for(i = 0; i < 10; i++) {
- printf("%d ", liczby1[i]);
- suma += liczby1[i];
- }
- printf("\n");
- printf("Watek 2 wylosowal liczby: ");
- for(i = 0; i < 10; i++) {
- printf("%d ", liczby2[i]);
- suma += liczby2[i];
- }
- printf("\n");
- printf("Suma liczb wynosi: %d\n", suma);
- return 0;
- }
- ////////////////// 2
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.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 *glowny1(void *a);
- void *glowny2(void *a);
- void *poboczny1(void *a);
- void *poboczny2(void *a);
- pthread_t g1, g2, p1, p2;
- int tok1, tok2, msg1, msg2;
- struct msgbuf {
- long mtype;
- char komunikat[10];
- };
- struct msgbuf bufor1, bufor2, bufor3, bufor4;
- struct msqid_ds *buf;
- void *glowny1(void *a){
- int i;
- for(i = 1; i <= 10; i++){
- bufor1.mtype = 1;
- char *m = "watek1";
- strcpy(bufor1.komunikat, m);
- msgsnd(msg1, &bufor1, sizeof(bufor1.komunikat), IPC_NOWAIT);
- sleep(3);
- }
- return 0;
- }
- void *glowny2(void *a){
- int i;
- for(i = 1; i <= 10; i++){
- bufor2.mtype = 2;
- char *m = "watek2";
- strcpy(bufor2.komunikat, m);
- msgsnd(msg2, &bufor2, sizeof(bufor2.komunikat), IPC_NOWAIT);
- sleep(2);
- }
- return 0;
- }
- void *poboczny1(void *a){
- int i;
- for(i = 1; i <= 10; i++){
- msgrcv(msg1, &bufor3, sizeof(bufor3.komunikat), 1, IPC_NOWAIT);
- printf("Komunikat odebrany przez poboczny 1: %s\n", bufor3.komunikat);
- sleep(3);
- }
- return 0;
- }
- void *poboczny2(void *a){
- int i;
- for(i = 1; i <= 10; i++){
- msgrcv(msg2, &bufor4, sizeof(bufor4.komunikat), 2, IPC_NOWAIT);
- printf("Komunikat odebrany przez poboczny 2: %s\n", bufor4.komunikat);
- sleep(2);
- }
- return 0;
- }
- int main(){
- tok1 = ftok("watek1",120);
- msg1 = msgget(tok1, IPC_CREAT|0660);
- tok2 = ftok("watek2",120);
- msg2 = msgget(tok2, IPC_CREAT|0660);
- pthread_create(&g1, NULL, glowny1, NULL);
- pthread_create(&g2, NULL, glowny2, NULL);
- pthread_create(&p1, NULL, poboczny1, NULL);
- pthread_create(&p2, NULL, poboczny2, NULL);
- pthread_join(g1, NULL);
- pthread_join(g2, NULL);
- pthread_join(p1, NULL);
- pthread_join(p2, NULL);
- msgctl(msg1, IPC_RMID, buf);
- msgctl(msg2, IPC_RMID, buf);
- return 0;
- }
- //////////////////////// 3
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <time.h>
- #include <pthread.h>
- #include <sched.h>
- #include <wait.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <unistd.h>
- void *funkcja1(void *a);
- void *funkcja2(void *a);
- void *funkcja3(void *a);
- pthread_t watek1, watek2, watek3;
- int fifo1, fifo2;
- int main(){
- srand(time(NULL));
- pthread_create(&watek1, NULL, funkcja1, NULL);
- pthread_create(&watek2, NULL, funkcja2, NULL);
- pthread_create(&watek3, NULL, funkcja3, NULL);
- fifo1 = mkfifo("programf1", 0600);
- if(fifo1==-1)
- perror("mkfifo1");
- fifo2 = mkfifo("programf2", 0600);
- if(fifo2==-1)
- perror("mkfifo2");
- pthread_join(watek1, NULL);
- sleep(1);
- pthread_join(watek2, NULL);
- pthread_join(watek3, NULL);
- unlink("programf1");
- unlink("programf2");
- return 0;
- }
- void *funkcja1(void *a){
- sleep(1);
- srand(time(NULL));
- int liczba, i, lacze1, lacze2, w1, w2;
- //char liczba2[10];
- for(i = 1; i <= 20; i++){
- liczba = rand() % (256-64) + 64;
- //sprintf(liczba2, "%d", liczba);
- lacze1 = open("programf1", O_WRONLY);
- lacze2 = open("programf2", O_WRONLY);
- if(write(lacze1, &liczba, sizeof(liczba)) == -1)
- printf("Blad write1\n");
- else w1 = 1;
- if(write(lacze2, &liczba, sizeof(liczba)) == -1)
- printf("Blad write2\n");
- else w2 = 1;
- if(w1 && w2) printf("%d. Wyslano %d do watkow\n", i, liczba);
- close(lacze1);
- close(lacze2);
- sleep(3);
- }
- return 0;
- }
- void *funkcja2(void *a){
- sleep(1);
- int liczba;
- int suma = 0, i, lacze;
- //char liczba2[10];
- for(i = 1; i <= 20; i++){
- lacze = open("programf1", O_RDONLY);
- if(read(lacze, &liczba, sizeof(liczba)) == -1)
- printf("Blad read1\n");
- suma += liczba;
- close(lacze);
- sleep(3);
- }
- printf("Suma liczb: %d\n", suma);
- return 0;
- }
- void *funkcja3(void *a){
- sleep(1);
- int liczba;
- int suma = 1, i, lacze;
- for(i = 1; i <= 20; i++){
- lacze = open("programf2", O_RDONLY);
- if(read(lacze, &liczba, sizeof(liczba)) == -1)
- printf("Blad read2\n");
- suma *= liczba;
- close(lacze);
- sleep(3);
- }
- printf("Iloczyn liczb: %d\n", suma);
- return 0;
- }
- ////////////////////// 4
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/shm.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- pthread_mutex_t xmut, xmut2, xmut3;
- pthread_cond_t zm_w, zm_w2, zm_w3;
- void *watek_glowny(void *p)
- {
- int i; int s = 0;
- int il = 1; int d;
- int key = ftok("4.c",123);
- int mem_idw = shmget(key,0,IPC_CREAT|0666);
- int *buf;
- buf = shmat(mem_idw,0,0);
- for (i = 0;i<5;i++)
- {
- pthread_cond_wait(&zm_w,&xmut); //czkam na wateki pomocnicze
- d = buf[1];
- s = s + d;
- printf("Watek glowny: Odebrano liczbe od Watek_1: %d, suma: %d\n",d,s);
- d = buf[2];
- il = il * d;
- printf("Watek glowny: Odebrano liczbe od Watek_2: %d, iloczyn: %d\n",d,il);
- pthread_cond_signal(&zm_w2); // wysylam ze juz odebralem
- }
- pthread_mutex_unlock(&xmut);
- pthread_mutex_unlock(&xmut2);
- pthread_mutex_unlock(&xmut3);
- printf("Watek glowny: Koniec\n");
- shmdt(buf);
- return 0;
- }
- void *watek_pom(void *p)
- {
- sleep(1);
- int i;
- int t[5];
- for (i = 0; i<5; i++)
- {
- t[i] = (10 ? (rand() % 10 +1) : 1);
- }
- int key = ftok("4.c",123);
- int mem_idw = shmget(key,0,IPC_CREAT|0666);
- int *buf;
- srand(123);
- buf = shmat(mem_idw,0,0);
- for (i =0;i<5;i++)
- {
- buf[1] = t[i]; //rand() %(10 - 1) + 1;
- printf("Watek 1: Wyslano liczbe: %d\n", buf[1]);
- pthread_cond_signal(&zm_w3); //wysyla do procesu 2 pomociniczego
- pthread_cond_wait(&zm_w2,&xmut2); //czeka na proces glowny
- }
- printf("Watek1: Koniec\n");
- shmdt(buf);
- return 0;
- }
- void *watek_pom_2(void *p)
- {
- int i;
- int t[5];
- for (i = 0; i<5; i++)
- {
- t[i] = (10 ? (rand() % 10 +1) : 1);
- }
- int key = ftok("4.c",123);
- int mem_idw = shmget(key,0,IPC_CREAT|0666);
- int *buf;
- buf = shmat(mem_idw,0,0);
- srand(432);
- for (i =0;i<5;i++)
- {
- pthread_cond_wait(&zm_w3,&xmut3); //czeka na proces 1 pomocniczy
- buf[2] = t[i]; // rand() %(10 - 1) + 1;
- printf("Watek 2: Wyslano liczbe: %d\n", buf[2]);
- pthread_cond_signal(&zm_w); //wysyla do procesu glownego
- }
- printf("Watek2: Koniec\n");
- shmdt(buf);
- return 0;
- }
- int main()
- {
- pthread_t w1, w2, w3;
- pthread_cond_init(&zm_w,NULL);
- pthread_cond_init(&zm_w2,NULL);
- pthread_cond_init(&zm_w3,NULL);
- int key = ftok("4.c",123);
- int mem_id = shmget(key,200,IPC_CREAT|0666);
- pthread_create(&w1,NULL,&watek_glowny,NULL);
- pthread_create(&w3,NULL,&watek_pom_2, NULL);
- pthread_create(&w2,NULL,&watek_pom, NULL);
- pthread_join(w1,NULL);
- pthread_join(w2,NULL);
- pthread_join(w3,NULL);
- shmctl(mem_id,IPC_RMID,0);
- }
- /////////////////////////////// 5_1
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/ipc.h>
- #include <sys/unistd.h>
- #include <pthread.h>
- void *funkcja1(void *a);
- pthread_t watek1;
- int fifo1;
- int main(){
- srand(time(NULL));
- pthread_create(&watek1, NULL, funkcja1, NULL);
- do {fifo1 = mkfifo("programf1", 0600);}
- while(fifo1 != -1);
- pthread_join(watek1, NULL);
- return 0;
- }
- void *funkcja1(void *a){
- sleep(1);
- int i, lacze;
- char komunikat[20];
- printf("Wysylam komunkaty\n");
- for(i = 1; i <=10; i++){
- sprintf(komunikat, "komunikat %d", i);
- lacze = open("programf1", O_WRONLY);
- write(lacze, komunikat, 20);
- close(lacze);
- sleep(3);
- }
- printf("Wyslano komunkaty\n");
- return 0;
- }
- //////////////////////////// 5_2
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/ipc.h>
- #include <sys/unistd.h>
- #include <pthread.h>
- void *funkcja1(void *a);
- pthread_t watek1;
- int fifo1;
- int main(){
- srand(time(NULL));
- pthread_create(&watek1, NULL, funkcja1, NULL);
- pthread_join(watek1, NULL);
- return 0;
- }
- void *funkcja1(void *a){
- sleep(1);
- int i, lacze;
- char komunikat[20];
- printf("Odbieram komunkaty\n");
- for(i = 1; i <=10; i++){
- lacze = open("programf1", O_RDONLY);
- read(lacze, komunikat, 20);
- close(lacze);
- printf("Odebrano: %s\n", komunikat);
- sleep(3);
- }
- printf("Odebrano komunkaty\n");
- unlink("programf1");
- return 0;
- }
- //////////////////////////// 6
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.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 *fglowna(void *a);
- void *fpoboczna1(void *a);
- void *fpoboczna2(void *a);
- pthread_t watek1, watek2, watek3;
- int tok1, msg1;
- struct msgbuf {
- long mtype;
- int liczba;
- };
- struct msgbuf bufor1, bufor2, bufor3, bufor4;
- struct msqid_ds *buf;
- int main(){
- tok1 = ftok("watek1",120);
- msg1 = msgget(tok1, IPC_CREAT|0660);
- pthread_create(&watek1, NULL, fglowna, NULL);
- pthread_create(&watek2, NULL, fpoboczna1, NULL);
- pthread_create(&watek3, NULL, fpoboczna2, NULL);
- pthread_join(watek1, NULL);
- pthread_join(watek2, NULL);
- pthread_join(watek3, NULL);
- msgctl(msg1, IPC_RMID, buf);
- return 0;
- }
- void *fglowna(void *a){
- srand(time(NULL));
- int liczba, i;
- for(i = 1; i <= 20; i++){
- liczba = rand() % (256-128) + 128;
- bufor1.mtype = 1;
- bufor1.liczba = liczba;
- bufor2.mtype = 2;
- bufor2.liczba = liczba;
- msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
- msgsnd(msg1, &bufor2, sizeof(bufor2.liczba), IPC_NOWAIT);
- printf("\n%d. Wyslano %d do watkow\n", i, liczba);
- sleep(3);
- }
- return 0;
- }
- void *fpoboczna1(void *a){
- int suma = 0, i;
- for(i = 1; i <= 20; i++){
- msgrcv(msg1, &bufor3, sizeof(bufor3.liczba), 1, 0);
- suma += bufor3.liczba;
- printf("%d. Poboczny1 odebral %d\n", i, bufor3.liczba);
- }
- printf("\nSuma liczb: %d, srednia arytmetyczna: %.2f\n", suma, (double)suma / 20.0);
- return 0;
- }
- void *fpoboczna2(void *a){
- int i;
- unsigned long long int iloczyn;
- for(i = 1; i <= 20; i++){
- msgrcv(msg1, &bufor4, sizeof(bufor4.liczba), 2, 0);
- iloczyn *= bufor4.liczba;
- printf("%d. Poboczny2 odebral %d\n", i, bufor4.liczba);
- }
- printf("\nIloczyn liczb: %llu\n", iloczyn);
- return 0;
- }
- //////////////////////////////// 7
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.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 *fglowna(void *a);
- void *fpoboczna1(void *a);
- void *fpoboczna2(void *a);
- int parzyste[10] = {2,4,6,8,10,12,14,16,18,20};
- int nieparzyste[10] = {1,3,5,7,9,11,13,15,17,19};
- int wynik1, wynik2;
- pthread_t watek1, watek2, watek3;
- int tok1, msg1;
- struct msgbuf {
- long mtype;
- char liczba[2];
- };
- struct msgbuf bufor1, bufor2, bufor3;
- struct msqid_ds *buf;
- int main(){
- tok1 = ftok("watek1",120);
- msg1 = msgget(tok1, IPC_CREAT|0660);
- pthread_create(&watek1, NULL, fglowna, NULL);
- pthread_create(&watek2, NULL, fpoboczna1, NULL);
- pthread_create(&watek3, NULL, fpoboczna2, NULL);
- pthread_join(watek1, NULL);
- pthread_join(watek2, NULL);
- pthread_join(watek3, NULL);
- msgctl(msg1, IPC_RMID, buf);
- return 0;
- }
- void *fpoboczna1(void *a){
- srand(time(NULL));
- int i;
- for(i = 0; i < 10; i++){
- bufor1.mtype = 1;
- sprintf(bufor1.liczba, "%d", parzyste[rand()%10]);
- msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
- printf("Poboczny 1 wysłał %s\n", bufor1.liczba);
- sleep(3);
- }
- return 0;
- }
- void *fpoboczna2(void *a){
- srand(time(NULL));
- int i;
- for(i = 0; i < 10; i++){
- bufor2.mtype = 1;
- sprintf(bufor2.liczba, "%d", nieparzyste[rand()%10]);
- msgsnd(msg1, &bufor2, sizeof(bufor2.liczba), IPC_NOWAIT);
- printf("Poboczny 2 wysłał %s\n", bufor2.liczba);
- sleep(2);
- }
- return 0;
- }
- void *fglowna(void *a){
- int i, suma = 0;
- for(i = 0; i < 20; i++){
- msgrcv(msg1, &bufor3, sizeof(bufor3.liczba), 1, 0);
- printf("Glowny odebral %s\n", bufor3.liczba);
- suma += atoi(bufor3.liczba);
- }
- printf("Suma liczb wynosi: %d\n", suma);
- return 0;
- }
- ///////////////////////////////////////// zad1
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.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>
- void *funkcja1(void *a);
- void *funkcja2(void *a);
- int tok1, msg1;
- pthread_t watek1, watek2;
- struct msgbuf {
- long mtype;
- int liczba;
- };
- struct msgbuf bufor1, bufor2;
- struct msqid_ds *buf;
- int globalna = 0;
- void *funkcja1(void *a){
- int liczba;
- scanf("%d", &liczba);
- bufor1.mtype = 1;
- bufor1.liczba = liczba;
- msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
- globalna = 1;
- while(liczba >= 0)
- {
- scanf("%d", &liczba);
- bufor1.mtype = 1;
- bufor1.liczba = liczba;
- msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
- globalna = 1;
- }
- return 0;
- }
- void *funkcja2(void *a){
- int liczba = 1;
- while(liczba >= 0)
- {
- if(globalna ==1)
- {
- msgrcv(msg1, &bufor2, sizeof(bufor2.liczba), 1, IPC_NOWAIT);
- liczba = bufor2.liczba;
- if(liczba >=0)printf("\nWatek 2: %d\n", liczba);
- globalna = 0;
- }
- }
- return 0;
- }
- int main(){
- tok1 = ftok("watek1",120);
- msg1 = msgget(tok1, IPC_CREAT|0660);
- pthread_create(&watek1, NULL, funkcja1, NULL);
- pthread_create(&watek2, NULL, funkcja2, NULL);
- pthread_join(watek1, NULL);
- pthread_join(watek2, NULL);
- msgctl(msg1, IPC_RMID, buf);
- return 0;
- }
- ////////////////////////// zad 2_klient
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define PORT 1090
- #define ADRES "127.0.0.1"
- void socket_name(int socket_descriptor)
- {
- struct sockaddr_in server = {
- .sin_family = AF_INET,
- .sin_port = PORT,
- .sin_addr = {INADDR_ANY}
- };
- if(bind(socket_descriptor, (struct sockaddr *)&server, sizeof(server))<0)
- perror("bind");
- }
- void get_message(int socket_descriptor)
- {
- int liczba;
- int suma = 0;
- struct sockaddr_in client;
- socklen_t adress_length = sizeof(client);
- do
- {
- int recive = recvfrom(socket_descriptor, &liczba, sizeof(liczba), 0 , (struct sockaddr *)&client, &adress_length);
- if(recive <0)
- perror("recvfrom");
- suma +=liczba;
- printf("%d\n", liczba);
- }while(liczba>=0);
- printf("Suma wynosi: %d", suma);
- }
- int main()
- {
- int socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
- if(socket_descriptor < 0)
- perror("socket");
- socket_name(socket_descriptor);
- get_message(socket_descriptor);
- if(close(socket_descriptor)<0)
- perror("close");
- return 0;
- }
- //////////////////////////// zad 2_serwer
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define PORT 1090
- #define ADRES "127.0.0.1"
- void send_message(int socket_descriptor)
- {
- struct in_addr network;
- if(!inet_aton(ADRES, &network))
- perror("inet_aton");
- struct sockaddr_in server =
- {
- .sin_family = AF_INET,
- .sin_port = PORT,
- .sin_addr = network
- };
- int liczba;
- do
- {
- scanf("%d", &liczba);
- if(sendto(socket_descriptor, &liczba, sizeof(liczba), 0, (struct sockaddr *)&server, sizeof(server))<0)
- {
- perror("sendto");
- }
- }while(liczba >=0);
- }
- int main()
- {
- 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;
- }
- //////////////////////// zad3
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.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 <stdio.h>
- void *funkcja1(void *a);
- void *funkcja2(void *a);
- int tok1, msg1;
- pthread_t watek1, watek2;
- struct msgbuf {
- long mtype;
- char liczba[21];
- };
- struct msgbuf bufor1, bufor2;
- struct msqid_ds *buf;
- int globalna = 0;
- void *funkcja1(void *a){
- char liczba[21];
- scanf("%s", liczba);
- char koniec[] = "koniec";
- bufor1.mtype = 1;
- strcpy(bufor1.liczba, liczba);
- msgsnd(msg1, &bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
- globalna = 1;
- while(strncmp(liczba, koniec,5)!=0)
- {
- if(strncmp(liczba, koniec,5 )==0)
- {
- break;
- }
- scanf("%s", liczba);
- bufor1.mtype = 1;
- strcpy(bufor1.liczba, liczba);
- msgsnd(msg1,&bufor1, sizeof(bufor1.liczba), IPC_NOWAIT);
- globalna = 1;
- }
- return 0;
- }
- void *funkcja2(void *a){
- char liczba[21];
- liczba[0] = 'm';
- char koniec[] = "koniec";
- while(strncmp(liczba, koniec,5)!=0)
- {
- if(globalna ==1)
- {
- if(strncmp(liczba, koniec,5 )==0)
- {
- break;
- }
- msgrcv(msg1, &bufor2, sizeof(bufor2.liczba), 1, IPC_NOWAIT);
- strcpy(liczba, bufor2.liczba);
- if(strcmp(liczba, "koniec" ))printf("\nWatek 2: %s\n", liczba);
- globalna = 0;
- }
- }
- return 0;
- }
- int main(){
- tok1 = ftok("watek2",120);
- msg1 = msgget(tok1, IPC_CREAT|0660);
- pthread_create(&watek1, NULL, funkcja1, NULL);
- pthread_create(&watek2, NULL, funkcja2, NULL);
- pthread_join(watek1, NULL);
- pthread_join(watek2, NULL);
- msgctl(msg1, IPC_RMID, buf);
- return 0;
- }
- //////////////////////////////// zad4 klient
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <arpa/inet.h>
- #define PORT 8080
- int main(int argc, char const *argv[])
- {
- struct sockaddr_in address;
- int sock = 0;
- struct sockaddr_in serv_addr;
- char *hello = "Hello from client";
- char buffer[1024] = {0};
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- // Convert IPv4 and IPv6 addresses from text to binary form
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
- {
- printf("\nInvalid address/ Address not supported \n");
- return -1;
- }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
- {
- printf("\nConnection Failed \n");
- return -1;
- }
- int liczba;
- do
- {
- scanf("%d", &liczba);
- send(sock, &liczba, sizeof(liczba),0);
- }while(liczba >=0);
- close(sock);
- return 0;
- }
- ////////////////////////// zad4 serwer
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define PORT 8080
- int main(int argc, char const *argv[])
- {
- int server_fd, new_socket;
- struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- char buffer[1024] = {0};
- char *hello = "Hello from server";
- // Creating socket file descriptor
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
- {
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- // Forcefully attaching socket to the port 8080
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
- &opt, sizeof(opt)))
- {
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- // Forcefully attaching socket to the port 8080
- if (bind(server_fd, (struct sockaddr *)&address,
- sizeof(address))<0)
- {
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- if (listen(server_fd, 3) < 0)
- {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
- (socklen_t*)&addrlen))<0)
- {
- perror("accept");
- exit(EXIT_FAILURE);
- }
- int liczba=2;
- do
- {
- read(new_socket, &liczba, 1024);
- printf("%d\n", liczba);
- }while(liczba >=0);
- close(server_fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement