semaphores, fifo, ipc, sockets added

This commit is contained in:
Dawid Pietrykowski 2023-02-10 17:00:31 +01:00
parent d5a3f7150c
commit 92168e0782
10 changed files with 820 additions and 1 deletions

68
processes/c2-cfifo.c Normal file
View File

@ -0,0 +1,68 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
const char *path = "/tmp/fifo-server-432423";
char path_pid_1[500];
char path_pid_2[500];
const char *suf_1 = "-1-";
const char *suf_2 = "-2-";
void handle_error(char *txt)
{
perror(txt);
exit(1);
}
int main()
{
int m_w, m_r;
int db_w, db_r;
char pid_str[20];
int m_fifo;
int pid = getpid();
strcpy(path_pid_1, path);
strcpy(path_pid_2, path);
strcpy(path_pid_1, strcat(path_pid_1, suf_1));
strcpy(path_pid_2, strcat(path_pid_2, suf_2));
sprintf(pid_str, "%d", pid);
strcpy(path_pid_1, strcat(path_pid_1, pid_str));
strcpy(path_pid_2, strcat(path_pid_2, pid_str));
if (access(path_pid_1, F_OK) == 0)
unlink(path_pid_1);
if (access(path_pid_2, F_OK) == 0)
unlink(path_pid_2);
if ((m_fifo = mkfifo(path_pid_1, 0600)) == -1)
handle_error("pid1 mkfifo");
if ((m_fifo = mkfifo(path_pid_2, 0600)) == -1)
handle_error("pid2 mkfifo");
if ((m_w = open(path, O_WRONLY)) == -1)
handle_error("main open");
printf("writing pid\n");
write(m_w, (void *)&pid, sizeof(pid_t));
if ((db_w = open(path_pid_1, O_WRONLY)) == -1)
handle_error("db_w open");
if ((db_r = open(path_pid_2, O_RDONLY)) == -1)
handle_error("db_r open");
double db = 2.21212;
printf("writing db\n");
write(db_w, (void *)&db, sizeof(double));
close(db_w);
printf("reading db\n");
read(db_r, (void *)&db, sizeof(double));
close(db_r);
printf("%f\n", db);
return 0;
}

73
processes/c2-cipc.c Normal file
View File

@ -0,0 +1,73 @@
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <string.h>
#include <sys/msg.h>
const char *main_key_file = "/tmp/IPC-SERVER-43412321";
struct Msg_pid
{
long mtype;
pid_t pid;
} typedef msg_pid;
struct Msg_data
{
long mtype;
double data;
} typedef msg_data;
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
int main()
{
if (access(main_key_file, F_OK) != 0)
handle_error("file access");
key_t main_key;
if ((main_key = ftok(main_key_file, 0)) == -1)
handle_error("ftok");
int m_msg_id;
if ((m_msg_id = msgget(main_key, 0600)) == -1)
handle_error("msgget");
pid_t pid = getpid();
key_t pid_key;
if ((pid_key = ftok(main_key_file, pid)) == -1)
handle_error("ftok data");
int d_msg_id;
if ((d_msg_id = msgget(pid_key, 0600 | IPC_CREAT)) == -1)
handle_error("msgget pid");
msg_pid pid_msg;
pid_msg.mtype = 1;
pid_msg.pid = getpid();
if (msgsnd(m_msg_id, (void *)&pid_msg, sizeof(pid_t), 0) == -1)
handle_error("msgsnd pid");
msg_data data_msg;
data_msg.mtype = 1;
data_msg.data = 22.211;
if (msgsnd(d_msg_id, (void *)&data_msg, sizeof(double), 0) == -1)
handle_error("msgsnd data");
if (msgrcv(d_msg_id, (void *)&data_msg, sizeof(double), 2, 0600) == -1)
handle_error("msgrcv data");
printf("%f\n", data_msg.data);
if (msgctl(d_msg_id, IPC_RMID, NULL) == -1)
handle_error("msgctl RMID");
return 0;
}

78
processes/c2-csem.c Normal file
View File

@ -0,0 +1,78 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdlib.h>
const char *path = "/tmp/sem.server341321";
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
char *buf;
int main()
{
key_t sem_key, shm_key;
struct sembuf sem;
sem.sem_flg = 0;
int sem_id, shm_id;
if (access(path, F_OK) != 0)
handle_error("file does not exist");
if ((sem_key = ftok(path, 0)) == -1)
handle_error("ftok sem");
if ((shm_key = ftok(path, 1)) == -1)
handle_error("ftok shm");
if ((sem_id = semget(sem_key, 2, 0600 | IPC_CREAT)) == -1)
handle_error("shmget");
if ((shm_id = shmget(shm_key, sizeof(1024), 0600 | IPC_CREAT)) == -1)
handle_error("shmget");
if ((buf = shmat(shm_id, 0, 0)) == NULL)
handle_error("shmat");
double d1 = 23;
// while (1)
{
sem.sem_num = 0;
sem.sem_op = -1;
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 1");
// printf("a\n");
memcpy(buf, &d1, sizeof(double));
printf("sent %f\n", d1);
sem.sem_num = 1;
sem.sem_op = 1;
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 2");
// printf("b\n");
}
{
sem.sem_num = 0;
sem.sem_op = -1;
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 1");
memcpy(&d1, buf, sizeof(double));
printf("received %f\n", d1);
sem.sem_num = 0;
sem.sem_op = 1;
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 1");
// printf("b\n");
}
if (shmdt(buf) == -1)
handle_error("shmdt");
return 0;
}

40
processes/c2-csock.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
const char *path = "/tmp/socket_test";
void handle_error(char *txt)
{
perror(txt);
exit(1);
}
int main()
{
int fd;
struct sockaddr_un my_addr, serv_addr;
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, path, sizeof(my_addr.sun_path) - 1);
memset(&serv_addr, 0, sizeof(struct sockaddr_un));
serv_addr.sun_family = AF_UNIX;
strncpy(serv_addr.sun_path, path, sizeof(serv_addr.sun_path) - 1);
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
handle_error("socket");
if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
handle_error("connect");
double d1 = 21.1;
write(fd, (void *)&d1, sizeof(double));
printf("%f\n", d1);
close(fd);
return 0;
}

78
processes/c2-sfifo.c Normal file
View File

@ -0,0 +1,78 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
const char *path = "/tmp/fifo-server-432423";
char path_pid_1[500];
char path_pid_2[500];
const char *suf_1 = "-1-";
const char *suf_2 = "-2-";
int m_fifo, m_w, m_r;
void handle_error(char *txt)
{
close(m_w);
close(m_r);
perror(txt);
exit(1);
}
int main()
{
int p;
if (access(path, F_OK) == 0)
unlink(path);
if ((m_fifo = mkfifo(path, 0600)) == -1)
handle_error("main mkfifo");
if ((m_r = open(path, O_RDONLY)) == -1 || (m_w = open(path, O_WRONLY)) == -1)
{
unlink(path);
handle_error("main open");
}
printf("otwieranie kolejki\n");
while (1)
{
pid_t client_pid;
read(m_r, (void *)&client_pid, sizeof(pid_t));
int pid;
int db_w, db_r;
if ((pid = fork()) == -1)
handle_error("fork");
if (pid == 0)
{
char pid_str[20];
strcpy(path_pid_1, path);
strcpy(path_pid_2, path);
strcpy(path_pid_1, strcat(path_pid_1, suf_1));
strcpy(path_pid_2, strcat(path_pid_2, suf_2));
sprintf(pid_str, "%d", client_pid);
strcpy(path_pid_1, strcat(path_pid_1, pid_str));
strcpy(path_pid_2, strcat(path_pid_2, pid_str));
if ((db_r = open(path_pid_1, O_RDONLY)) == -1)
handle_error("db_r open");
if ((db_w = open(path_pid_2, O_WRONLY)) == -1)
handle_error("db_w open");
double db;
read(db_r, (void *)&db, sizeof(double));
printf("%f\n", db);
db *= 2;
write(db_w, (void *)&db, sizeof(double));
printf("%f\n", db);
close(db_r);
close(db_w);
return;
}
}
return 0;
}

80
processes/c2-sipc.c Normal file
View File

@ -0,0 +1,80 @@
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <sys/msg.h>
const char *main_key_file = "/tmp/IPC-SERVER-43412321";
struct Msg_pid
{
long mtype;
pid_t pid;
} typedef msg_pid;
struct Msg_data
{
long mtype;
double data;
} typedef msg_data;
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
int main()
{
if (access(main_key_file, F_OK) == 0)
unlink(main_key_file);
if (mknod(main_key_file, 0600 | S_IFREG, NULL) != 0)
handle_error("main file creation");
key_t main_key;
if ((main_key = ftok(main_key_file, 0)) == -1)
handle_error("ftok");
int m_msg_id;
if ((m_msg_id = msgget(main_key, 0600 | IPC_CREAT)) == -1)
handle_error("msgget");
msg_pid pid_msg;
while (1)
{
if (msgrcv(m_msg_id, (void *)&pid_msg, sizeof(pid_t), 1, 0) == -1)
handle_error("msgrcv");
printf("%d\n", pid_msg.pid);
int pid;
if ((pid = fork()) == -1)
handle_error("fork");
if (pid == 0)
{
key_t pid_key;
if ((pid_key = ftok(main_key_file, pid_msg.pid)) == -1)
handle_error("ftok data");
int d_msg_id;
if ((d_msg_id = msgget(pid_key, 0600)) == -1)
handle_error("msgget data");
msg_data data_msg;
if (msgrcv(d_msg_id, (void *)&data_msg, sizeof(double), 1, 0600) == -1)
handle_error("msgrcv data");
printf("Received data %f\n", data_msg.data);
data_msg.mtype = 2;
data_msg.data *= data_msg.data;
if (msgsnd(d_msg_id, (void *)&data_msg, sizeof(double), 0) == -1)
handle_error("msgsnd data");
}
}
if (msgctl(m_msg_id, IPC_RMID, NULL) == -1)
handle_error("msgctl RMID");
return 0;
}

97
processes/c2-ssem.c Normal file
View File

@ -0,0 +1,97 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
const char *path = "/tmp/sem.server341321";
char *buf;
key_t sem_key, shm_key;
struct sembuf sem;
int sem_id, shm_id;
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
void exit_prog()
{
// shmdt(buf);
semctl(sem_id, 0, IPC_RMID);
semctl(sem_id, 1, IPC_RMID);
shmctl(shm_id, IPC_RMID, NULL);
unlink(path);
exit(0);
}
int main()
{
sem.sem_flg = 0;
signal(SIGINT, exit_prog);
if (access(path, F_OK) == 0)
unlink(path);
if (mknod(path, 0600 | S_IFREG, 0) == -1)
handle_error("mknod");
if ((sem_key = ftok(path, 0)) == -1)
handle_error("ftok sem");
if ((shm_key = ftok(path, 1)) == -1)
handle_error("ftok shm");
if ((sem_id = semget(sem_key, 2, 0600 | IPC_CREAT)) == -1)
handle_error("semget");
if ((shm_id = shmget(shm_key, sizeof(1024), 0600 | IPC_CREAT)) == -1)
handle_error("shmget");
if ((buf = shmat(shm_id, 0, 0)) == NULL)
handle_error("shmat");
u_short semset[] = {1, 0};
if (semctl(sem_id, 0, SETALL, semset) == -1)
handle_error("semctl set");
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 1");
double d1;
while (1)
{
{
sem.sem_num = 1;
sem.sem_op = -1;
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 1");
memcpy(&d1, buf, sizeof(double));
printf("received %f\n", d1);
// sem.sem_num = 0;
// sem.sem_op = 1;
// if (semop(sem_id, &sem, 1) == -1)
// handle_error("semop 2");
// printf("b\n");
}
{
// sem.sem_num = 1;
// sem.sem_op = -1;
// if (semop(sem_id, &sem, 1) == -1)
// handle_error("semop 1");
sleep(1);
d1 *= d1;
memcpy(buf, &d1, sizeof(double));
printf("sent %f\n", d1);
sem.sem_num = 0;
sem.sem_op = 1;
if (semop(sem_id, &sem, 1) == -1)
handle_error("semop 2");
printf("b\n");
}
}
return 0;
}

59
processes/c2-ssock.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
const char *path = "/tmp/socket_test";
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
int main()
{
int sfd, cfd;
socklen_t peer_addr_size;
struct sockaddr_un my_addr, peer_addr;
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1)
handle_error("socket");
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, path,
sizeof(my_addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *)&my_addr,
sizeof(my_addr)) == -1)
handle_error("bind");
if (listen(sfd, 1) == -1)
handle_error("listen");
/* Now we can accept incoming connections one
at a time using accept(2). */
peer_addr_size = sizeof(peer_addr);
cfd = accept(sfd, (struct sockaddr *)&peer_addr,
&peer_addr_size);
if (cfd == -1)
handle_error("accept");
double d1;
read(cfd, (void *)&d1, sizeof(double));
printf("%f\n", d1);
close(cfd);
/* Code to deal with incoming connection(s)... */
if (close(sfd) == -1)
handle_error("close");
if (unlink(path) == -1)
handle_error("unlink");
return 0;
}

View File

@ -24,9 +24,10 @@ int main(int argc, char *argv[])
{ {
// child // child
// char const *arg[3]; // char const *arg[3];
// arg[0] = "bc"; // arg[0] = "bc";
// arg[1] = "-l"; // arg[1] = "-l";
// arg[2] = NULL; // arg[2] = NULL;-
close(fpc[1]); close(fpc[1]);
close(fcp[0]); close(fcp[0]);
char *buf = malloc(sizeof(char) * 1000); char *buf = malloc(sizeof(char) * 1000);

245
processes/sebastian_sem.c Normal file
View File

@ -0,0 +1,245 @@
// SERVER
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <error.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <sys/shm.h>
#include <sys/sem.h>
typedef struct sembuf semafory;
char *path = "/tmp/semafory3";
int IDpamiec, IDsemaforw;
struct wiadomosc
{
int a;
char test[1024];
};
typedef struct wiadomosc msg;
msg *pamiec;
int main(int argc, char *argv[])
{
unlink(path);
key_t kluczPamiec, kluczSemaforw;
int plik;
semafory sem;
if ((plik = open(path, 0600 | O_CREAT)) == -1)
{
perror("Blad tworzenia pliku\n");
return -1;
}
close(plik);
if ((kluczPamiec = ftok(path, 1)) == -1)
{
perror("Blad tworzenia klucza pamieci\n");
return -1;
}
if ((kluczSemaforw = ftok(path, 2)) == -1)
{
perror("Blad tworzenia klucza semaforow\n");
return -1;
}
if ((IDpamiec = shmget(kluczPamiec, sizeof(msg) * 1024, 0600 | IPC_CREAT)) == -1)
{
perror("Blad tworzenia ID pamieci\n");
return -1;
}
pamiec = shmat(IDpamiec, (msg *)0, 0);
if ((IDsemaforw = semget(kluczSemaforw, 2, 0600 | IPC_CREAT)) == -1)
{
perror("Blad tworzenia ID semaforw\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
return -1;
}
u_short ustawSemafory[] = {1, 0};
if (semctl(IDsemaforw, 0, SETALL, ustawSemafory) == -1)
{
perror("Blad ustawienia wartosci poczatkowych\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
return -1;
}
while (1)
{
sem.sem_num = 1;
sem.sem_op = -1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
msg *pomoc;
pomoc = malloc(sizeof(msg) * 1024);
memcpy(pomoc, pamiec, sizeof(msg));
printf("Wiadomosc otrzymałem: %d, %s\n", pomoc->a, pomoc->test);
char *pomoc2;
pomoc2 = pomoc->test;
pomoc2 += strlen(pomoc2);
sprintf(pomoc2, "%d", pomoc->a);
memcpy(pamiec, pomoc, sizeof(msg));
sem.sem_num = 0;
sem.sem_op = 1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
sem.sem_num = 1;
sem.sem_op = -1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
memcpy(pomoc, pamiec, sizeof(msg));
printf("Ostateczna wiadomosc: %s\n", pomoc->test);
sem.sem_num = 0;
sem.sem_op = 1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
memset(pamiec, 0, sizeof(msg));
}
}
// KLIENT
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <error.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <sys/shm.h>
#include <sys/sem.h>
typedef struct sembuf semafory;
char *path = "/tmp/semafory3";
struct wiadomosc
{
int a;
char test[1024];
};
typedef struct wiadomosc msg;
int main(int argc, char *argv[])
{
int IDpamiec, IDsemaforw;
key_t kluczPamiec, kluczSemaforw;
int plik;
msg *pamiec;
semafory sem;
if ((kluczPamiec = ftok(path, 1)) == -1)
{
perror("Blad tworzenia klucza pamieci\n");
return -1;
}
if ((kluczSemaforw = ftok(path, 2)) == -1)
{
perror("Blad tworzenia klucza semaforow\n");
return -1;
}
if ((IDsemaforw = semget(kluczSemaforw, 2, 0600 | IPC_CREAT)) == -1)
{
perror("Blad tworzenia ID semaforw\n");
return -1;
}
sem.sem_num = 0;
sem.sem_op = -1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
if ((IDpamiec = shmget(kluczPamiec, sizeof(msg) * 1024, 0600 | IPC_CREAT)) == -1)
{
perror("Blad tworzenia ID pamieci\n");
return -1;
}
pamiec = shmat(IDpamiec, (msg *)0, 0);
msg *pomoc;
pomoc = malloc(sizeof(msg) * 1024);
pomoc->a = 5;
char *pomoc2;
pomoc2 = pomoc->test;
strcpy(pomoc2, "Jakis tekst");
printf("Wiadomosc to: %d, %s\n", pomoc->a, pomoc->test);
memcpy(pamiec, pomoc, sizeof(msg));
sem.sem_num = 1;
sem.sem_op = 1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
printf("Wysłałem wiadomosc\n");
sem.sem_num = 0;
sem.sem_op = -1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
memcpy(pomoc, pamiec, sizeof(msg));
printf("Otrzymalem sklejona wiadomosc %s\n", pomoc->test);
pomoc2 = pomoc->test;
pomoc2 += strlen(pomoc2);
strcpy(pomoc2, " doklejam cos 2 raz");
memcpy(pamiec, pomoc, sizeof(msg));
sem.sem_num = 1;
sem.sem_op = 1;
sem.sem_flg = 0;
if (semop(IDsemaforw, &sem, 1) == -1)
{
perror("Blad inicjalizacji semaforow\n");
shmdt(pamiec);
shmctl(IDpamiec, IPC_RMID, NULL);
semctl(IDsemaforw, 0, IPC_RMID, NULL);
return -1;
}
shmdt(pamiec);
}