Skip to content
Snippets Groups Projects
Commit 479171ce authored by Marc Feger's avatar Marc Feger
Browse files

First Commit.

parents
Branches
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/C-Stuff.iml" filepath="$PROJECT_DIR$/.idea/C-Stuff.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
cmake_minimum_required(VERSION 3.10)
project(C-Stuff C)
set(CMAKE_C_STANDARD 11)
add_executable(Multithreaded_Search Multithreaded_Search/main.c)
add_executable(Fork_and_Pipe Fork_and_Pipe/main.c)
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]){
pid_t pid;
int conditions[2]; // write = 1, read = 0
pipe(conditions); // create a pipe
pid = fork(); // duplicate the process
if(pid == 0){
/*child*/
printf("Iam the child\n");
int qsum = 0;
for(int i = 0; i < strlen(argv[1]); i++){
qsum += argv[1][i] - '0';
}
close(conditions[0]); // -> child wants to write
write(conditions[1], &qsum, sizeof(qsum)); // -> write the qsum to the pipe
close(conditions[1]); // -> close the writing
}
else if (pid != -1){
/*parent*/
printf("Iam the parent\n");
int qsum = 0;
close(conditions[1]);
read(conditions[0], &qsum, sizeof(qsum));
close(conditions[0]);
printf("The qsum is: %d\n", qsum);
}
else {
perror("Error while forking");
return -1;
}
return 0;
}
\ No newline at end of file
/***
* @author: Marc Feger
* @mail: marc.feger@uni-duesseldorf.de
*/
#include <stdlib.h>
#include <time.h>
#include <printf.h>
#include <ntsid.h>
#include <pthread.h>
// SIZE > COUNT_THREADS is a must
#define SIZE 100000000
#define COUNT_THREADS 1
struct arguments {
int id;
int *array;
int *max;
};
typedef struct arguments arguments_s;
int *create_random_array() {
srand((unsigned int) time(NULL));
int *array = malloc(SIZE * sizeof(int));
for (int i = 0; i < SIZE; i++) {
array[i] = rand();
}
return array;
}
int get_maximum(const int *array, int from, int to) {
int max = 0;
for (int i = from; i < to; i++) {
if (array[i] > max) max = array[i];
}
return max;
}
void *worker(void *arg) {
arguments_s *args = (arguments_s *) arg;
int from = args->id * (SIZE / COUNT_THREADS);
int to = (args->id + 1) * (SIZE / COUNT_THREADS);
if (args->id == COUNT_THREADS - 1) to = SIZE;
args->max[args->id] = get_maximum(args->array, from, to);
return NULL;
}
int main(void) {
int *array = create_random_array();
int *max = malloc(SIZE * sizeof(int));
arguments_s args;
args.array = array;
args.max = max;
pthread_t threads[COUNT_THREADS];
clock_t start_t;
start_t = clock();
// create all threads
for (int i = 0; i < COUNT_THREADS; i++) {
args.id = i;
if (pthread_create(&threads[i], NULL, worker, (void *) &args) != 0) {
printf("pthread_create() did not create a thread");
}
if (pthread_join(threads[i], NULL) != 0) {
printf("pthread_join() did not join a thread");
}
}
int total_max = get_maximum(args.max, 0, COUNT_THREADS);
printf("The total maximum is: %d\n", total_max);
printf("It took %f clocks for %d threads to find the maximum of %d elements\n",
((double) (start_t - clock())) / CLOCKS_PER_SEC, COUNT_THREADS, SIZE);
free(array);
free(max);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment