Last active
November 29, 2016 08:30
-
-
Save jirihnidek/cf931d17f40cc7ba2bc11d107eadc4c1 to your computer and use it in GitHub Desktop.
Test of forking (creating lot of child processes) 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 (fork-test) | |
# Source code for fork-test | |
set (fork-test_src fork-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) | |
# Set up fork-test executable | |
add_executable (fork-test ${fork-test_src}) |
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> | |
#define MICROSECONDS_IN_SECOND 1000000 | |
#define BUFF_SIZE 65535 /* bytes */ | |
#define TIME_DELAY 1 /* seconds */ | |
int main(int argc, char const *argv[]) | |
{ | |
struct timeval start_tv, end_tv; | |
int count, ret, i, proc_num = 10; | |
long int diff_time; | |
pid_t child_pid; | |
if (argc > 1) | |
{ | |
ret = sscanf(argv[1], "%d", &proc_num); | |
if (ret == 1) | |
{ | |
printf("argv[1]: %d\n", proc_num); | |
} | |
} | |
/* Start of experiment */ | |
gettimeofday(&start_tv, NULL); | |
/* Create lot of child processes */ | |
count = 0; | |
for(i = 0; i < proc_num; ++i) | |
{ | |
child_pid = fork(); | |
if (child_pid == 0) { | |
char *buff = (char*)malloc(BUFF_SIZE * sizeof(char)); | |
sleep(TIME_DELAY); | |
free(buff); | |
return EXIT_SUCCESS; | |
} else if (child_pid == -1) { | |
perror("fork()"); | |
} else { | |
/* Count total number of created processes */ | |
count++; | |
} | |
} | |
/* 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 can be changed using command: | |
* | |
* $ ulimit -u NEW_NUM | |
* | |
*/ | |
printf("%d of %d child processes created in %6.3f miliseconds\n", | |
count, proc_num, ((float)diff_time)/1000.0 ); | |
sleep(2 * TIME_DELAY); | |
/* code */ | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment