diff --git a/cpu/thd_ctx_mtx.c b/cpu/thd_ctx_mtx.c new file mode 100644 index 0000000..7dafb9a --- /dev/null +++ b/cpu/thd_ctx_mtx.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include "rasperf.h" +#define NUM_THREADS 2 +#define ITERATIONS 1000 + +pthread_mutex_t LOCK; +pthread_mutex_t TRIGGER; +pthread_cond_t COND; +volatile unsigned int cs_count = 0; + +// Uptil now TRIGGER WAS LOCKED AND THEN RELEASED +void * function(void *id){ + int t_id = *(int *)id; + + // Wait for trigger from parent + pthread_mutex_lock(&TRIGGER); + pthread_mutex_unlock(&TRIGGER); // So that both start at the same time + // Get lock + pthread_mutex_lock(&LOCK); //only one thread passes in + + // Wake up other thread if context switch count is greater than 0 + if (cs_count > 0){ + pthread_cond_signal(&COND); + } + + while(cs_count < ITERATIONS) { + + // Wait for other thread to wake up us + cs_count++; //Increment cs count + pthread_cond_wait(&COND, &LOCK); //release LOCK and wait for COND + + // wake up other thread + pthread_cond_signal(&COND); + } + pthread_mutex_unlock(&LOCK); + pthread_exit(NULL); +} + + +int main(){ + + pthread_t threads[NUM_THREADS]; + int rc, i,id1=1, id2=2; + time start, end; + int t; + + pthread_mutex_init(&LOCK, NULL); // to protect CS + pthread_mutex_init(&TRIGGER, NULL); //This is to start at the same time + pthread_cond_init(&COND, NULL); // to wake each other + + pthread_mutex_lock(&TRIGGER); //to stop the threads we are going to create + + + rc = pthread_create(&threads[0], NULL, function, (void*)&id1); //create first thread + if (rc){ + perror("Thread creation failed!"); + } + rc = pthread_create(&threads[1], NULL, function, (void*)&id2); //create second thread + if (rc){ + perror("Thread creation failed!"); + } + + getTime(start); // start time + pthread_mutex_unlock(&TRIGGER); // by releasing the trigger start the threads + + // Wait for threads to finish + for(i=0; iYiwM_6`t#L!r~-k;}~L+fUa7%g_3>v(KJO!*s=4how$yRpjCBqcki`*VPD$4 zP8^DagTq5AkW{2CLV#}}l@O&>q(UEvw5ez-T2-ol}w}c+kYJE?94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w.c b/cpu/thd_ctxsw.c new file mode 100644 index 0000000..593d101 --- /dev/null +++ b/cpu/thd_ctxsw.c @@ -0,0 +1,53 @@ +#define _GNU_SOURCE +#include +#include "rasperf.h" +#include +#include +#include +#include +#define STACK_SIZE 4096 +int pipe_fd[2], tid1, tid2; +char buffer[10]; +int reader(void* arg); +int writer(void* arg); +int controller(void *arg) +{ + void *tid1_stack = malloc(STACK_SIZE); + void *tid2_stack = malloc(STACK_SIZE); + tid1 = clone(&writer, tid1_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL); + tid2 = clone(&reader, tid2_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL); + return 0; +} +int reader(void* arg) +{ + read(pipe_fd[0], buffer, 1); + return 0; +} +int writer(void* arg) +{ + write(pipe_fd[1], "a", 1); + return 0; +} +int main(){ + + unsigned int regr; + time start, end; + unsigned int avg; + int status, i; + void *child_stack = malloc(STACK_SIZE); + int thread_pid; + + //asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); + //printf("regr: %x\n", regr); + + //initialize communication pipe + for (i = 0; i < 1000; i++) + { + getTime(start); + thread_pid = clone(&controller, child_stack+STACK_SIZE, SIGCHLD|CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES|CLONE_THREAD, NULL); + waitpid(thread_pid, 0, 0); + getTime(end); + printf("\nTime Taken %d", diff(end,start)); + } + return 0; +} diff --git a/cpu/thd_mtx b/cpu/thd_mtx new file mode 100755 index 0000000000000000000000000000000000000000..64fddf5805a22bbbbafd37264c06cffa85fe52e9 GIT binary patch literal 13615 zcmeHOeQX@n5r21QCzr3Y4GA~|;w2>1r1)??NKN?Kvwg`rwG-D|_$WB*vu_q#}wGZ2dS4j8`xn+=0mXWY# ze$P|X#nk?R9znig5f_2VoFRIb0W+dx_dT%V?GyR`i&P4Q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(EXIT_FAILURE);} while(0) +#define LISTEN_BACKLOG 5 +int main(int argc, char*argv[]) +{ + int socket_fd, rc; + if(argc<1){ + handle_error(":"); + } + //Retrieve the protocol number mapping + //from /etc/protocols file + struct protoent *proto_entry; + proto_entry = getprotobyname("tcp"); + + //Open an socket end-point for this TCP Server + socket_fd = socket(AF_INET, SOCK_STREAM, proto_entry->p_proto); + if(socket_fd == -1){ + handle_error("Errno: %d,Socket descriptor creation failed", errorno); + } + + //My address information to bind to + struct sockaddr sock_addr; + memset(&sock_addr, 0, sizeof(struct sockaddr)); + sock_addr.sa_family = AF_INET; + sock_addr.sin_addr.s_addr = htonl(INADDR_ANY); + sock_addr.sin_port = htons(8000); + + + rc = bind(sock_fd, &sock_addr, sizeof(sock_addr)); + if(rc == -1){ + handle_error("Errno: %d, Bind failure", errorno); + } + rc = listen(socket_fd, LISTEN_BACKLOG); + if(rc == -1){ + handle_error("Errno: %d, Listen Error", errorno); + } + + int addr_size = sizeof(struct sockaddr); + rc= accept(sock_fd, &sock_addr, &addr_size); + if( rc == -1){ + handle_error("Error: %d,Accept failed", errorno); + } +