diff options
[SP/P09] Solved exercises
7 files changed, 188 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice09/ex1.c | 18 | ||||
| -rw-r--r-- | Year 3/System programming/Practice09/ex2.c | 26 | ||||
| -rw-r--r-- | Year 3/System programming/Practice09/ex3.c | 46 | ||||
| -rw-r--r-- | Year 3/System programming/Practice09/ex4.c | 48 | ||||
| -rw-r--r-- | Year 3/System programming/Practice09/ex5-1.c | 28 | ||||
| -rw-r--r-- | Year 3/System programming/Practice09/ex5-2.c | 21 | ||||
| -rw-r--r-- | Year 3/System programming/Practice09/ex5.h | 1 |
diff --git a/Year 3/System programming/Practice09/ex1.c b/Year 3/System programming/Practice09/ex1.c new file mode 100644 index 0000000..36ed844 --- /dev/null +++ b/Year 3/System programming/Practice09/ex1.c @@ -0,0 +1,18 @@ +#include <fcntl.h> +#include <sys/stat.h> +#include <mqueue.h> +#include <stdio.h> + +int main() { + mqd_t mq = mq_open("/spqueue", O_RDWR | O_CREAT, 0700); + + struct mq_attr mqattrs; + mq_getattr(mq, &mqattrs); + + mq_close(mq); + mq_unlink("/spqueue"); + + printf("%ld %ld %ld %ld\n", mqattrs.mq_flags, mqattrs.mq_maxmsg, mqattrs.mq_msgsize, mqattrs.mq_curmsgs); + + return 0; +} diff --git a/Year 3/System programming/Practice09/ex2.c b/Year 3/System programming/Practice09/ex2.c new file mode 100644 index 0000000..20c0c10 --- /dev/null +++ b/Year 3/System programming/Practice09/ex2.c @@ -0,0 +1,26 @@ +#include <fcntl.h> +#include <sys/stat.h> +#include <mqueue.h> +#include <stdio.h> +#include <string.h> + +int main() { + struct mq_attr mqattrs = { + .mq_maxmsg = 5, + .mq_msgsize = 1024, + }; + mqd_t mq = mq_open("/spqueue", O_RDWR | O_CREAT, 0700, &mqattrs); + + char msg[] = "hello world"; + mq_send(mq, msg, strlen(msg), 0); + + char rmsg[1024] = { '\0' }; + mq_receive(mq, rmsg, 1024, NULL); + + printf("%s\n", rmsg); + + mq_close(mq); + mq_unlink("/spqueue"); + + return 0; +} diff --git a/Year 3/System programming/Practice09/ex3.c b/Year 3/System programming/Practice09/ex3.c new file mode 100644 index 0000000..7188491 --- /dev/null +++ b/Year 3/System programming/Practice09/ex3.c @@ -0,0 +1,46 @@ +#include <sys/wait.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <mqueue.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int main(int argc, char* argv[]) { + struct mq_attr mqattrs = { + .mq_maxmsg = 10, + .mq_msgsize = 128, + }; + mqd_t mq = mq_open("/spqueue", O_RDWR | O_CREAT, 0700, &mqattrs); + + char buf[128]; + + pid_t fp = fork(); + if (fp == 0) { // child + int file = open(argv[1], O_RDONLY); + int amount = 0; + while ((amount = read(file, buf, 128))) { + if (amount < 128) buf[amount] = '\0'; + mq_send(mq, buf, 128, 0); + } + close(file); + } + else { + while (!waitpid(fp, NULL, WNOHANG)) { + mq_receive(mq, buf, 128, NULL); + printf("%s", buf); + } + + mq_getattr(mq, &mqattrs); + while (mqattrs.mq_curmsgs-- > 0) { + mq_receive(mq, buf, 128, NULL); + printf("%s", buf); + } + } + + mq_close(mq); + if (fp != 0) + mq_unlink("/spqueue"); + + return 0; +} diff --git a/Year 3/System programming/Practice09/ex4.c b/Year 3/System programming/Practice09/ex4.c new file mode 100644 index 0000000..3e6f871 --- /dev/null +++ b/Year 3/System programming/Practice09/ex4.c @@ -0,0 +1,48 @@ +#include <sys/wait.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <mqueue.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int main(int argc, char* argv[]) { + struct mq_attr mqattrs = { + .mq_maxmsg = 10, + .mq_msgsize = 128, + }; + mqd_t mq = mq_open("/spqueue", O_RDWR | O_CREAT, 0700, &mqattrs); + + char buf[128]; + + pid_t fp = fork(); + if (fp == 0) { // child + int amount; + while ((amount = read(0, buf, 128))) { + if (amount < 128) buf[amount] = '\0'; + mq_send(mq, buf, 128, 0); + } + } + else { + int fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0755); + + while (!waitpid(fp, NULL, WNOHANG)) { + mq_receive(mq, buf, 128, NULL); + write(fd, buf, strlen(buf)); + } + + mq_getattr(mq, &mqattrs); + while (mqattrs.mq_curmsgs-- > 0) { + mq_receive(mq, buf, 128, NULL); + write(fd, buf, strlen(buf)); + } + + close(fd); + } + + mq_close(mq); + if (fp != 0) + mq_unlink("/spqueue"); + + return 0; +} diff --git a/Year 3/System programming/Practice09/ex5-1.c b/Year 3/System programming/Practice09/ex5-1.c new file mode 100644 index 0000000..716a940 --- /dev/null +++ b/Year 3/System programming/Practice09/ex5-1.c @@ -0,0 +1,28 @@ +#include <error.h> +#include <stdio.h> +#include <unistd.h> +#include <mqueue.h> + +#include "ex5.h" + +int main(int argc, char* argv[]) { + const struct mq_attr mqattr = { + .mq_maxmsg = 10, + .mq_msgsize = 64, + }; + mqd_t mq = mq_open(queueName, O_WRONLY | O_CREAT, 0700, &mqattr); + + int fd = open(argv[1], O_RDONLY); + char buf[mqattr.mq_msgsize]; + int amount; + while ((amount = read(fd, buf, mqattr.mq_msgsize))) { + if (amount < mqattr.mq_msgsize) buf[amount] = '\0'; + mq_send(mq, buf, mqattr.mq_msgsize, 0); + } + mq_send(mq, "", 1, 0); + + close(fd); + mq_close(mq); + mq_unlink(queueName); + return 0; +} diff --git a/Year 3/System programming/Practice09/ex5-2.c b/Year 3/System programming/Practice09/ex5-2.c new file mode 100644 index 0000000..1b5c83c --- /dev/null +++ b/Year 3/System programming/Practice09/ex5-2.c @@ -0,0 +1,21 @@ +#include <string.h> +#include <stdio.h> +#include <unistd.h> +#include <mqueue.h> + +#include "ex5.h" + +int main(int argc, char* argv[]) { + mqd_t mq = mq_open(queueName, O_RDONLY); + + char buf[64]; + while (1) { + mq_receive(mq, buf, 64, NULL); + if (buf[0] == '\0') break; + write(1, buf, strlen(buf)); + } + + mq_close(mq); + mq_unlink(queueName); + return 0; +} diff --git a/Year 3/System programming/Practice09/ex5.h b/Year 3/System programming/Practice09/ex5.h new file mode 100644 index 0000000..92b1fea --- /dev/null +++ b/Year 3/System programming/Practice09/ex5.h @@ -0,0 +1 @@ +const char queueName[] = "/spqueue"; |
