From 5316ebb25048427708b6415d13d47a01a2615e41 Mon Sep 17 00:00:00 2001 From: Jelena Dokic Date: Mon, 10 Feb 2020 13:18:17 +0100 Subject: [PATCH] syscalls --- .gitignore | 11 ++++++++ compile.py | 11 ++++++++ fork.c | 24 ++++++++++++++++++ mmap_munmap.c | 36 +++++++++++++++++++++++++++ page_fault.c | 34 +++++++++++++++++++++++++ read.c | 27 ++++++++++++++++++++ recv.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ run.py | 33 ++++++++++++++++++++++++ send.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ thread.c | 28 +++++++++++++++++++++ write.c | 28 +++++++++++++++++++++ 11 files changed, 361 insertions(+) create mode 100644 .gitignore create mode 100644 compile.py create mode 100644 fork.c create mode 100644 mmap_munmap.c create mode 100644 page_fault.c create mode 100644 read.c create mode 100644 recv.c create mode 100644 run.py create mode 100644 send.c create mode 100644 thread.c create mode 100644 write.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..781f3ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.vscode +a.out +fork +mmap_munmap +page_fault +read +recv +select* +send +thread +write \ No newline at end of file diff --git a/compile.py b/compile.py new file mode 100644 index 0000000..d126f9d --- /dev/null +++ b/compile.py @@ -0,0 +1,11 @@ +import subprocess + +subprocess.call(["gcc", "-o", "thread", "thread.c", "-lpthread"]) +subprocess.call(["gcc", "-o", "fork", "fork.c"]) +subprocess.call(["gcc", "-o", "read", "read.c"]) +subprocess.call(["gcc", "-o", "write", "write.c"]) +subprocess.call(["gcc", "-o", "mmap_munmap", "mmap_munmap.c"]) +subprocess.call(["gcc", "-o", "page_fault", "page_fault.c"]) +subprocess.call(["gcc", "-o", "send", "send.c"]) +subprocess.call(["gcc", "-o", "recv", "recv.c"]) +subprocess.call(["gcc", "-o", "select", "select.c"]) diff --git a/fork.c b/fork.c new file mode 100644 index 0000000..48dbdf9 --- /dev/null +++ b/fork.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include + +int main (int argc, char ** argv) +{ + int i, n; + n = atoi(argv[1]); + + struct timeval stop, start; + gettimeofday(&start, NULL); + + // 2^n procesa + for(i = 0; i < n; i++){ + syscall(SYS_fork); + } + gettimeofday(&stop, NULL); + + printf("fork %d %luus\n", n, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + return 0; +} \ No newline at end of file diff --git a/mmap_munmap.c b/mmap_munmap.c new file mode 100644 index 0000000..2c59cbc --- /dev/null +++ b/mmap_munmap.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int args, char **argv) { + + int i, n, pages; + + n = atoi(argv[1]); + pages = atoi(argv[2]); + size_t pagesize = getpagesize(); + + struct timeval stop, start; + gettimeofday(&start, NULL); + + for(i = 0 ; i < n; i++){ + char* region = mmap( + (void*) pagesize, + pages * pagesize, + PROT_READ|PROT_WRITE|PROT_EXEC, + MAP_ANON|MAP_PRIVATE, + 0, + 0 + ); + munmap(region, pages * pagesize); + } + + gettimeofday(&stop, NULL); + printf("mmap_munmap %d %d %luus\n", n, pages, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + + return 0; +} \ No newline at end of file diff --git a/page_fault.c b/page_fault.c new file mode 100644 index 0000000..0f3bcfb --- /dev/null +++ b/page_fault.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int args, char **argv) +{ + // printf("%d\n", getpid()); + + int i, n; + n = atoi(argv[1]); + + long pagesize = sysconf(_SC_PAGESIZE); + + struct timeval stop, start; + gettimeofday(&start, NULL); + for(i = 0; i < n; i++){ + unsigned char *p = malloc(pagesize + 1); + // Page fault + p[0] = 0; + } + + gettimeofday(&stop, NULL); + printf("page_fault %d %luus\n", n, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + + // ps -o min_flt,maj_flt + // sleep da bih mogla da pokrenem ps dok se ne ubije proces + // sleep(30); + + return 0; +} \ No newline at end of file diff --git a/read.c b/read.c new file mode 100644 index 0000000..2fb01ff --- /dev/null +++ b/read.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include + +int main (int argc, char ** argv) { + int i, r, n; + char c; + + n = atoi(argv[1]); + + struct timeval stop, start; + gettimeofday(&start, NULL); + + FILE *f = fopen("/dev/zero", "r"); + for(i = 0 ; i < n; i++){ + syscall(SYS_read,fileno(f), &c, 1); + } + + gettimeofday(&stop, NULL); + printf("read %d %luus\n", n, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + + fclose(f); + + return 0; +} \ No newline at end of file diff --git a/recv.c b/recv.c new file mode 100644 index 0000000..c169f32 --- /dev/null +++ b/recv.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX 80 +#define PORT 8080 +#define SA struct sockaddr + +void func(int sockfd) +{ + char buff[MAX]; + int n; + + struct timeval stop, start; + gettimeofday(&start, NULL); + while (1) { + bzero(buff, MAX); + + recv(sockfd, buff, sizeof(buff), 0); + // puts(buff); + if (strncmp("exit", buff, 4) == 0) { + break; + } + } + gettimeofday(&stop, NULL); + printf("recv %luus\n", (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); +} + +int main() +{ + int sockfd, connfd, len; + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + printf("socket creation failed...\n"); + exit(0); + } + bzero(&servaddr, sizeof(servaddr)); + + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(PORT); + + if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { + printf("socket bind failed...\n"); + exit(0); + } + + if ((listen(sockfd, 5)) != 0) { + printf("Listen failed...\n"); + exit(0); + } + len = sizeof(cli); + + connfd = accept(sockfd, (SA*)&cli, &len); + if (connfd < 0) { + printf("server acccept failed...\n"); + exit(0); + } + + func(connfd); + close(sockfd); +} diff --git a/run.py b/run.py new file mode 100644 index 0000000..e9a9165 --- /dev/null +++ b/run.py @@ -0,0 +1,33 @@ +import io +import subprocess + +# THREAD number_of_threads +subprocess.call(["./thread", "100000"]) + +# FORK number_of_forks +proc = subprocess.Popen(["./fork", "8"], stdout=subprocess.PIPE) +for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): + pass +print(line, end='') + +# READ number_of_reads +subprocess.call(["./read", "10000000"]) + +# WRITE number_of_writtes text +subprocess.call(["./write", "10000000", "a"]) + +# MMAP_MUNMAP number_of_maps/unmaps number_of_pages +subprocess.call(["./mmap_munmap", "1000000", "1000"]) +subprocess.call(["./mmap_munmap", "1000000", "1"]) + +# PAGE_FAULT number_of_faults +subprocess.call(["./page_fault", "100000"]) + +# RECV +subprocess.Popen(["./recv"]) # background process + +# SEND number_of_messages text +subprocess.call(["./send", "100000", "a"]) + +# SELECT number of messages text +subprocess.call(["./select", "100000"]) diff --git a/send.c b/send.c new file mode 100644 index 0000000..915d67a --- /dev/null +++ b/send.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include + +#define MAX 80 +#define PORT 8080 +#define SA struct sockaddr + +void func(int sockfd, int n, char *text) +{ + char buff[MAX]; + int i; + + struct timeval stop, start; + gettimeofday(&start, NULL); + + for (i = 0; i < n; i++) { + bzero(buff, sizeof(buff)); + + strcpy(buff,text); + send(sockfd, buff, sizeof(buff), 0); + } + strcpy(buff,"exit"); + send(sockfd, buff, sizeof(buff), 0); + + gettimeofday(&stop, NULL); + printf("send %d %s %luus\n", n, text, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); +} + +int main(int argc, char ** argv) +{ + int sockfd, connfd; + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + printf("socket creation failed...\n"); + exit(0); + } + bzero(&servaddr, sizeof(servaddr)); + + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); + servaddr.sin_port = htons(PORT); + + if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) { + printf("connection with the server failed...\n"); + exit(0); + } + + int n = atoi(argv[1]); + char *text = argv[2]; + func(sockfd, n, text); + + close(sockfd); +} diff --git a/thread.c b/thread.c new file mode 100644 index 0000000..37bd66c --- /dev/null +++ b/thread.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +void *threadFun(void *vargp) { + return NULL; +} + +int main(int argc, char ** argv) { + pthread_t thread_id; + int i, n; + n = atoi(argv[1]); + + struct timeval stop, start; + gettimeofday(&start, NULL); + + for(i = 0; i < n; i++) { + pthread_create(&thread_id, NULL, threadFun, NULL); + pthread_join(thread_id, NULL); + } + + gettimeofday(&stop, NULL); + printf("thread %d %luus\n", n, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + + return(0); +} diff --git a/write.c b/write.c new file mode 100644 index 0000000..37c96d1 --- /dev/null +++ b/write.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include + +int main (int argc, char ** argv) { + int i, r, n; + + n = atoi(argv[1]); + char *text = argv[2]; + + FILE *f = fopen("/dev/null", "w"); + + struct timeval stop, start; + gettimeofday(&start, NULL); + + for(i = 0 ; i < n; i++){ + syscall(SYS_write,fileno(f), text, strlen(text)); + } + + gettimeofday(&stop, NULL); + printf("write %d %luus\n", n, (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + + fclose(f); + return 0; +} \ No newline at end of file