Created
November 25, 2016 14:35
-
-
Save jirihnidek/5b94074591e9ed11fba02569bce50379 to your computer and use it in GitHub Desktop.
Test of threads at Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Main CMakeFile.txt | |
# Minimal version of CMake | |
cmake_minimum_required (VERSION 2.6) | |
# Build type | |
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) | |
message(STATUS "Setting build type to 'Debug' as none was specified.") | |
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE) | |
# Set the possible values of build type for cmake-gui | |
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release") | |
endif () | |
# Define project name | |
project (pthread-test) | |
# Pthread is *prefered* (only supported) thread library | |
set (CMAKE_THREAD_PREFER_PTHREAD) | |
# Try to find required packages | |
find_package (Threads REQUIRED) | |
# Source code for pthread-test | |
set (pthread-test_src pthread-test.c) | |
# Compiler flags | |
if (CMAKE_COMPILER_IS_GNUCC) | |
set (CMAKE_C_FLAGS "-D_REETRANT -Wall -Wextra -pedantic -Wno-long-long") | |
if (CMAKE_BUILD_TYPE STREQUAL "Debug") | |
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") | |
elseif( CMAKE_BUILD_TYPE STREQUAL "Release" ) | |
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -O3 -fno-strict-aliasing") | |
endif () | |
endif (CMAKE_COMPILER_IS_GNUCC) | |
# Basic libraries used by verse_server | |
set ( pthread-test_libs ${CMAKE_THREAD_LIBS_INIT} ) | |
# Set up pthread-test executable | |
add_executable (pthread-test ${pthread-test_src}) | |
target_link_libraries (pthread-test | |
${pthread-test_libs} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/time.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
#define MICROSECONDS_IN_SECOND 1000000 | |
#define BUFF_SIZE 65535 /* bytes */ | |
#define TIME_DELAY 1 /* seconds */ | |
#define THREADSTACK 65536 | |
void *dummy_thread_func(void *arg) | |
{ | |
char *buff = NULL; | |
(void)arg; | |
buff = (char*)malloc(BUFF_SIZE * sizeof(char)); | |
sleep(TIME_DELAY); | |
free(buff); | |
pthread_exit(NULL); | |
return NULL; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
struct timeval start_tv, end_tv; | |
int count, ret, i, thread_num = 10; | |
long int diff_time; | |
pthread_t *thread_array; /* Array of threads */ | |
pthread_attr_t attrs; | |
pthread_attr_init(&attrs); | |
pthread_attr_setstacksize(&attrs, THREADSTACK); | |
if (argc > 1) | |
{ | |
ret = sscanf(argv[1], "%d", &thread_num); | |
if (ret == 1) | |
{ | |
printf("argv[1]: %d\n", thread_num); | |
} | |
} | |
thread_array = (pthread_t*)malloc(thread_num * sizeof(pthread_t)); | |
if(thread_array == NULL) | |
{ | |
perror("malloc()"); | |
return EXIT_FAILURE; | |
} | |
/* Start of experiment */ | |
gettimeofday(&start_tv, NULL); | |
/* Create lot of child processes */ | |
count = 0; | |
for(i = 0; i < thread_num; ++i) { | |
ret = pthread_create(&thread_array[i], &attrs, dummy_thread_func, NULL); | |
if (ret == 0) { | |
count++; | |
} else { | |
perror("pthread_create()"); | |
thread_array[i] = 0; | |
} | |
} | |
/* End of experiment */ | |
gettimeofday(&end_tv, NULL); | |
diff_time = MICROSECONDS_IN_SECOND * (end_tv.tv_sec - start_tv.tv_sec) + | |
(end_tv.tv_usec - start_tv.tv_usec); | |
/* Maximum number of user processes/threads can be changed using command: | |
* | |
* $ ulimit -u NEW_NUM | |
* | |
*/ | |
printf("%d of %d child threads created in %6.3f miliseconds\n", | |
count, thread_num, ((float)diff_time)/1000.0 ); | |
/* Wait for threads to finish hard work. */ | |
for(i = 0; i < thread_num; ++i) { | |
pthread_join(thread_array[i], NULL); | |
} | |
free(thread_array); | |
/* code */ | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment