Skip to content
Snippets Groups Projects
Commit 92a55ca2 authored by antux18's avatar antux18
Browse files

Fichiers ex2.

parent b876a179
Branches
No related merge requests found
ex2/T1.c 0 → 100644
#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
ex2/T2.c 0 → 100644
#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
ex2/T3.c 0 → 100644
#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
ex2/T4.c 0 → 100644
#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);
}
ex2/T5.c 0 → 100644
#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");
}
File added
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment