This commit is contained in:
Jelena Dokic 2020-02-10 13:18:17 +01:00
commit 5316ebb250
11 changed files with 361 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
.vscode
a.out
fork
mmap_munmap
page_fault
read
recv
select*
send
thread
write

11
compile.py Normal file
View File

@ -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"])

24
fork.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
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;
}

36
mmap_munmap.c Normal file
View File

@ -0,0 +1,36 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
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;
}

34
page_fault.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
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 <pid>
// sleep da bih mogla da pokrenem ps dok se ne ubije proces
// sleep(30);
return 0;
}

27
read.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>
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;
}

69
recv.c Normal file
View File

@ -0,0 +1,69 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#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);
}

33
run.py Normal file
View File

@ -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"])

60
send.c Normal file
View File

@ -0,0 +1,60 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#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);
}

28
thread.c Normal file
View File

@ -0,0 +1,28 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
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);
}

28
write.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>
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;
}