diff --git a/ex2/T1.c b/ex2/T1.c new file mode 100644 index 0000000000000000000000000000000000000000..90c71ab26fd9991ea37aba67ed639f7a273371f8 --- /dev/null +++ b/ex2/T1.c @@ -0,0 +1,40 @@ +#include<stdio.h> +#include<sys/mman.h> +#include<stdlib.h> +#include<fcntl.h> +#include<unistd.h> +#include<sys/wait.h> +#include<string.h> + +#define SIZE 10000 +#define SCRATCH_FILE "./shared-file" + +void swap(int *xp, int *yp); +void sort(int arr[], int size); + +int main() { + int arr[SIZE]; + for(int i = SIZE - 1; i >= 0; i--) + arr[(SIZE - 1) - i] = i % 2; + + // TODO: Fork another process here + // TODO: One process should calculate the median + // TODO: The other process should calculate the mean + // TODO: Use shm_open, ftruncate, mmap, and munmap to communicate over mapped files + // TODO: Print both calculated results and involved PIDs + + return 0; +} + +void swap(int *xp, int *yp) { + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void sort(int *arr, int size) { + for (int i = 0; i < size - 1; i++) + for (int j = 0; j < size - i - 1; j++) + if (arr[j] > arr[j + 1]) + swap(&arr[j], &arr[j + 1]); +} \ No newline at end of file diff --git a/ex2/T2.c b/ex2/T2.c new file mode 100644 index 0000000000000000000000000000000000000000..0c88f38b7f38e9092167ec7b8670a37f09c3e8a2 --- /dev/null +++ b/ex2/T2.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <pthread.h> + +#define SIZE 10000 + +void swap(int *xp, int *yp); +void sort(int arr[], int size); + +// Function prototypes for thread tasks +// TODO: Declare functions for calculating mean and median + +int main() { + int arr[SIZE]; + for(int i = SIZE - 1; i >= 0; i--) + arr[(SIZE - 1) - i] = i % 2; + + // TODO: Implement your solution here + // TODO: Create threads for calculating mean and median + // TODO: Display the result (mean, median) + // TODO: Print TIDs of involved threads + + return 0; +} + +void swap(int *xp, int *yp) { + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void sort(int *arr, int size) { + for (int i = 0; i < size - 1; i++) + for (int j = 0; j < size - i - 1; j++) + if (arr[j] > arr[j + 1]) + swap(&arr[j], &arr[j + 1]); +} \ No newline at end of file diff --git a/ex2/T3.c b/ex2/T3.c new file mode 100644 index 0000000000000000000000000000000000000000..a8a154e70d7bd8da4578c8c4c659d1f2fecff4e9 --- /dev/null +++ b/ex2/T3.c @@ -0,0 +1,12 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <pthread.h> +#include <sys/types.h> +#include <sys/wait.h> + +int main() { + // TODO: Fork multiple processes here + // TODO: Each process with a different number of threads + // TODO: Each thread should print the PID and TID +} \ No newline at end of file diff --git a/ex2/T4.c b/ex2/T4.c new file mode 100644 index 0000000000000000000000000000000000000000..d07b2d8aec32ae421d43596360b3b12585fbad79 --- /dev/null +++ b/ex2/T4.c @@ -0,0 +1,38 @@ +#include<stdio.h> +#include<stdlib.h> +#include<unistd.h> +#include<pthread.h> +#define SIZE 10000 +#define NUM_THREADS 5 +int result; +pthread_mutex_t lock; + +struct args +{ + int *arr; + int start; + int end; +}; + +void * accumulate(void * in) +{ + //TODO implement accumlate + return NULL; +} +int main () +{ + + result=0; + int arr[SIZE]; + for(int i=0;i<SIZE;i++) + arr[i]=1; + + pthread_t threads[NUM_THREADS-1]; + struct args thread_args[NUM_THREADS-1]; + // TODO create a team of thread, each thread must take SIZE/NUM_THREADS to accumulate + // TODO use struct args to pass arguments to the accumulate function + // TODO main thread must participate in the calculation + // TODO make sure all threads finised + printf("sum is %d\n", result); +} + diff --git a/ex2/T5.c b/ex2/T5.c new file mode 100644 index 0000000000000000000000000000000000000000..72b1a647315d1bc5ec6cf6dcdd649b06f8f58fc5 --- /dev/null +++ b/ex2/T5.c @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define SIZE 5 +struct process{ + int process_id; + int arrival_time; + int execution_time; + int start_time; +}; + +void init_processes(struct process *); +void display(struct process *); +void display_average_waiting_time(struct process *); +void schedule_FCFS(struct process *); + +int main() +{ + struct process processes[SIZE]; + init_processes(processes); + + printf("Orignial input (processes)\n"); + display(processes); + + schedule_FCFS(processes); + printf("Execution according to FCFS policy\n"); + display(processes); + display_average_waiting_time(processes); + +} +void init_processes(struct process * input) +{ + srand(100); + for(int i=0;i<SIZE;i++) + { + input[i].arrival_time = rand()%10; + input[i].execution_time = rand()%10; + input[i].process_id= i; + input[i].start_time=-1; + } +} +void display(struct process * input){ + printf("process<id, arr, exe, start>:"); + for(int i=0;i<SIZE;i++) + { + printf("<%d,%d,%d,%d> ", input[i].process_id, + input[i].arrival_time, input[i].execution_time, input[i].start_time); + } + printf("\n"); +} + +void schedule_FCFS(struct process * arr) +{ + //TODO + printf("Not implemented Yet!\n"); +} +void schedule_SJF(struct process * arr) +{ + //TODO + printf("Not implemented Yet!\n"); +} +void schedule_LJF(struct process * arr) +{ + //TODO + printf("Not implemented Yet!\n"); +} +void display_average_waiting_time(struct process * arr) +{ + //TODO + printf("Not implemented Yet!\n"); +} diff --git a/ex2/os-exercise-2.pdf b/ex2/os-exercise-2.pdf new file mode 100644 index 0000000000000000000000000000000000000000..6e11bfe0742c36cf039ce996ff10ec29a165db6a Binary files /dev/null and b/ex2/os-exercise-2.pdf differ