mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge branch 'master' into master
This commit is contained in:
commit
050e456c5c
4
.github/workflows/awesome_workflow.yml
vendored
4
.github/workflows/awesome_workflow.yml
vendored
@ -57,14 +57,14 @@ jobs:
|
|||||||
needs: [MainSequence]
|
needs: [MainSequence]
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest, macOS-latest]
|
os: [windows-latest, ubuntu-latest, macOS-latest]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- run: |
|
- run: |
|
||||||
cmake -B ./build -S .
|
cmake -B ./build -S .
|
||||||
cmake --build build
|
cmake --build build --config Release
|
||||||
- name: Label on PR fail
|
- name: Label on PR fail
|
||||||
uses: actions/github-script@v6
|
uses: actions/github-script@v6
|
||||||
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}
|
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
cmake_minimum_required(VERSION 3.22)
|
cmake_minimum_required(VERSION 3.22)
|
||||||
project(Algorithms_in_C
|
project(Algorithms_in_C
|
||||||
LANGUAGES C
|
LANGUAGES C
|
||||||
VERSION 1.0.0
|
VERSION 1.0.0
|
||||||
DESCRIPTION "Set of algorithms implemented in C."
|
DESCRIPTION "Set of algorithms implemented in C."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set compilation standards
|
# Set compilation standards
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED YES)
|
set(CMAKE_C_STANDARD_REQUIRED YES)
|
||||||
|
if (MSVC)
|
||||||
if(MSVC)
|
|
||||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||||
# add_compile_options(/Za)
|
# add_compile_options(/Za)
|
||||||
endif(MSVC)
|
endif (MSVC)
|
||||||
|
|
||||||
# check for math library
|
# check for math library
|
||||||
# addresses a bug when linking on OSX
|
# addresses a bug when linking on OSX
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
* [Rot13](https://github.com/TheAlgorithms/C/blob/HEAD/cipher/rot13.c)
|
* [Rot13](https://github.com/TheAlgorithms/C/blob/HEAD/cipher/rot13.c)
|
||||||
|
|
||||||
## Client Server
|
## Client Server
|
||||||
|
* [Bool](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/bool.h)
|
||||||
* [Client](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/client.c)
|
* [Client](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/client.c)
|
||||||
|
* [Fork](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/fork.h)
|
||||||
* [Remote Command Exec Udp Client](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/remote_command_exec_udp_client.c)
|
* [Remote Command Exec Udp Client](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/remote_command_exec_udp_client.c)
|
||||||
* [Remote Command Exec Udp Server](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/remote_command_exec_udp_server.c)
|
* [Remote Command Exec Udp Server](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/remote_command_exec_udp_server.c)
|
||||||
* [Server](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/server.c)
|
* [Server](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/server.c)
|
||||||
|
@ -6,6 +6,7 @@ else()
|
|||||||
CHECK_INCLUDE_FILE(arpa/inet.h ARPA_HEADERS)
|
CHECK_INCLUDE_FILE(arpa/inet.h ARPA_HEADERS)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
include(CheckSymbolExists)
|
||||||
if(ARPA_HEADERS OR WINSOCK_HEADER)
|
if(ARPA_HEADERS OR WINSOCK_HEADER)
|
||||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
@ -15,16 +16,21 @@ if(ARPA_HEADERS OR WINSOCK_HEADER)
|
|||||||
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
foreach( testsourcefile ${APP_SOURCES} )
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
# I used a simple string replace, to cut off .cpp.
|
# I used a simple string replace, to cut off .cpp.
|
||||||
string( REPLACE ".c" "" testname ${testsourcefile} )
|
string(REPLACE ".c" "" testname ${testsourcefile})
|
||||||
add_executable( ${testname} ${testsourcefile} )
|
|
||||||
|
|
||||||
if(OpenMP_C_FOUND)
|
if(NOT WIN32)
|
||||||
|
if(${testname} STREQUAL "fork" OR ${testname} STREQUAL "bool")
|
||||||
|
continue()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
add_executable(${testname} ${testsourcefile})
|
||||||
|
|
||||||
|
if (OpenMP_C_FOUND)
|
||||||
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
|
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
|
||||||
endif()
|
endif ()
|
||||||
if(MATH_LIBRARY)
|
if (MATH_LIBRARY)
|
||||||
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
|
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
|
||||||
endif()
|
endif ()
|
||||||
|
|
||||||
# if(HAS_UNISTD)
|
# if(HAS_UNISTD)
|
||||||
# target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
|
# target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
|
||||||
# endif()
|
# endif()
|
||||||
@ -34,7 +40,7 @@ if(ARPA_HEADERS OR WINSOCK_HEADER)
|
|||||||
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
|
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
|
||||||
# endif()
|
# endif()
|
||||||
|
|
||||||
if(WINSOCK_HEADER)
|
if (WINSOCK_HEADER)
|
||||||
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
|
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
55
client_server/bool.h
Normal file
55
client_server/bool.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
|
||||||
|
* Copyright (C) 2007 - INRIA
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 - 2016 - Scilab Enterprises
|
||||||
|
*
|
||||||
|
* This file is hereby licensed under the terms of the GNU GPL v2.0,
|
||||||
|
* pursuant to article 5.3.4 of the CeCILL v.2.1.
|
||||||
|
* This file was originally licensed under the terms of the CeCILL v2.1,
|
||||||
|
* and continues to be available under such terms.
|
||||||
|
* For more information, see the COPYING file which you should have received
|
||||||
|
* along with this program.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef __BOOL_H__
|
||||||
|
#define __BOOL_H__
|
||||||
|
|
||||||
|
/* define boolean type */
|
||||||
|
#ifdef BOOL
|
||||||
|
#undef BOOL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef TRUE
|
||||||
|
#undef TRUE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef FALSE
|
||||||
|
#undef FALSE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FALSE = 0,
|
||||||
|
TRUE = 1
|
||||||
|
} BOOL;
|
||||||
|
|
||||||
|
#else
|
||||||
|
/* Please notice that BOOL is defined in <windef.h> */
|
||||||
|
/* BUT windef.h includes all others windows include */
|
||||||
|
/* it is better to redefine as */
|
||||||
|
typedef int BOOL;
|
||||||
|
#define FALSE 0
|
||||||
|
#define TRUE 1
|
||||||
|
|
||||||
|
#endif
|
||||||
|
/* converts BOOL to bool */
|
||||||
|
#define BOOLtobool(w) ((w != FALSE) ? true : false)
|
||||||
|
|
||||||
|
/* converts bool to BOOL */
|
||||||
|
#define booltoBOOL(w) ((w == true) ? TRUE : FALSE)
|
||||||
|
|
||||||
|
#endif /* __BOOL_H__ */
|
||||||
|
/*--------------------------------------------------------------------------*/
|
298
client_server/fork.h
Normal file
298
client_server/fork.h
Normal file
@ -0,0 +1,298 @@
|
|||||||
|
/*
|
||||||
|
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
|
||||||
|
* Copyright (C) DIGITEO - 2010 - Allan CORNET
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 - 2016 - Scilab Enterprises
|
||||||
|
*
|
||||||
|
* This file is hereby licensed under the terms of the GNU GPL v2.0,
|
||||||
|
* pursuant to article 5.3.4 of the CeCILL v.2.1.
|
||||||
|
* This file was originally licensed under the terms of the CeCILL v2.1,
|
||||||
|
* and continues to be available under such terms.
|
||||||
|
* For more information, see the COPYING file which you should have received
|
||||||
|
* along with this program.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
#ifndef __FORK_H__
|
||||||
|
#define __FORK_H__
|
||||||
|
|
||||||
|
/* http://technet.microsoft.com/en-us/library/bb497007.aspx */
|
||||||
|
/* http://undocumented.ntinternals.net/ */
|
||||||
|
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
#include "bool.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* simulate fork on Windows
|
||||||
|
*/
|
||||||
|
int fork(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if symbols to simulate fork are present
|
||||||
|
* and load these symbols
|
||||||
|
*/
|
||||||
|
BOOL haveLoadedFunctionsForFork(void);
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef LONG NTSTATUS;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef struct _SYSTEM_HANDLE_INFORMATION
|
||||||
|
{
|
||||||
|
ULONG ProcessId;
|
||||||
|
UCHAR ObjectTypeNumber;
|
||||||
|
UCHAR Flags;
|
||||||
|
USHORT Handle;
|
||||||
|
PVOID Object;
|
||||||
|
ACCESS_MASK GrantedAccess;
|
||||||
|
} SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef struct _OBJECT_ATTRIBUTES
|
||||||
|
{
|
||||||
|
ULONG Length;
|
||||||
|
HANDLE RootDirectory;
|
||||||
|
PVOID /* really PUNICODE_STRING */ ObjectName;
|
||||||
|
ULONG Attributes;
|
||||||
|
PVOID SecurityDescriptor; /* type SECURITY_DESCRIPTOR */
|
||||||
|
PVOID SecurityQualityOfService; /* type SECURITY_QUALITY_OF_SERVICE */
|
||||||
|
} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef enum _MEMORY_INFORMATION_
|
||||||
|
{
|
||||||
|
MemoryBasicInformation,
|
||||||
|
MemoryWorkingSetList,
|
||||||
|
MemorySectionName,
|
||||||
|
MemoryBasicVlmInformation
|
||||||
|
} MEMORY_INFORMATION_CLASS;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef struct _CLIENT_ID
|
||||||
|
{
|
||||||
|
HANDLE UniqueProcess;
|
||||||
|
HANDLE UniqueThread;
|
||||||
|
} CLIENT_ID, *PCLIENT_ID;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef struct _USER_STACK
|
||||||
|
{
|
||||||
|
PVOID FixedStackBase;
|
||||||
|
PVOID FixedStackLimit;
|
||||||
|
PVOID ExpandableStackBase;
|
||||||
|
PVOID ExpandableStackLimit;
|
||||||
|
PVOID ExpandableStackBottom;
|
||||||
|
} USER_STACK, *PUSER_STACK;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef LONG KPRIORITY;
|
||||||
|
typedef ULONG_PTR KAFFINITY;
|
||||||
|
typedef KAFFINITY *PKAFFINITY;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef struct _THREAD_BASIC_INFORMATION
|
||||||
|
{
|
||||||
|
NTSTATUS ExitStatus;
|
||||||
|
PVOID TebBaseAddress;
|
||||||
|
CLIENT_ID ClientId;
|
||||||
|
KAFFINITY AffinityMask;
|
||||||
|
KPRIORITY Priority;
|
||||||
|
KPRIORITY BasePriority;
|
||||||
|
} THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef enum _SYSTEM_INFORMATION_CLASS
|
||||||
|
{
|
||||||
|
SystemHandleInformation = 0x10
|
||||||
|
} SYSTEM_INFORMATION_CLASS;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef NTSTATUS(NTAPI *ZwWriteVirtualMemory_t)(
|
||||||
|
IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN PVOID Buffer,
|
||||||
|
IN ULONG NumberOfBytesToWrite, OUT PULONG NumberOfBytesWritten OPTIONAL);
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef NTSTATUS(NTAPI *ZwCreateProcess_t)(
|
||||||
|
OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess,
|
||||||
|
IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE InheriteFromProcessHandle,
|
||||||
|
IN BOOLEAN InheritHandles, IN HANDLE SectionHandle OPTIONAL,
|
||||||
|
IN HANDLE DebugPort OPTIONAL, IN HANDLE ExceptionPort OPTIONAL);
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef NTSTATUS(WINAPI *ZwQuerySystemInformation_t)(
|
||||||
|
SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation,
|
||||||
|
ULONG SystemInformationLength, PULONG ReturnLength);
|
||||||
|
typedef NTSTATUS(NTAPI *ZwQueryVirtualMemory_t)(
|
||||||
|
IN HANDLE ProcessHandle, IN PVOID BaseAddress,
|
||||||
|
IN MEMORY_INFORMATION_CLASS MemoryInformationClass,
|
||||||
|
OUT PVOID MemoryInformation, IN ULONG MemoryInformationLength,
|
||||||
|
OUT PULONG ReturnLength OPTIONAL);
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef NTSTATUS(NTAPI *ZwGetContextThread_t)(IN HANDLE ThreadHandle,
|
||||||
|
OUT PCONTEXT Context);
|
||||||
|
typedef NTSTATUS(NTAPI *ZwCreateThread_t)(
|
||||||
|
OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess,
|
||||||
|
IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle,
|
||||||
|
OUT PCLIENT_ID ClientId, IN PCONTEXT ThreadContext,
|
||||||
|
IN PUSER_STACK UserStack, IN BOOLEAN CreateSuspended);
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
typedef NTSTATUS(NTAPI *ZwResumeThread_t)(IN HANDLE ThreadHandle,
|
||||||
|
OUT PULONG SuspendCount OPTIONAL);
|
||||||
|
typedef NTSTATUS(NTAPI *ZwClose_t)(IN HANDLE ObjectHandle);
|
||||||
|
typedef NTSTATUS(NTAPI *ZwQueryInformationThread_t)(
|
||||||
|
IN HANDLE ThreadHandle, IN THREAD_INFORMATION_CLASS ThreadInformationClass,
|
||||||
|
OUT PVOID ThreadInformation, IN ULONG ThreadInformationLength,
|
||||||
|
OUT PULONG ReturnLength OPTIONAL);
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
static ZwCreateProcess_t ZwCreateProcess = NULL;
|
||||||
|
static ZwQuerySystemInformation_t ZwQuerySystemInformation = NULL;
|
||||||
|
static ZwQueryVirtualMemory_t ZwQueryVirtualMemory = NULL;
|
||||||
|
static ZwCreateThread_t ZwCreateThread = NULL;
|
||||||
|
static ZwGetContextThread_t ZwGetContextThread = NULL;
|
||||||
|
static ZwResumeThread_t ZwResumeThread = NULL;
|
||||||
|
static ZwClose_t ZwClose = NULL;
|
||||||
|
static ZwQueryInformationThread_t ZwQueryInformationThread = NULL;
|
||||||
|
static ZwWriteVirtualMemory_t ZwWriteVirtualMemory = NULL;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
#define NtCurrentProcess() ((HANDLE)-1)
|
||||||
|
#define NtCurrentThread() ((HANDLE)-2)
|
||||||
|
/* we use really the Nt versions - so the following is just for completeness */
|
||||||
|
#define ZwCurrentProcess() NtCurrentProcess()
|
||||||
|
#define ZwCurrentThread() NtCurrentThread()
|
||||||
|
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
|
||||||
|
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
/* setjmp env for the jump back into the fork() function */
|
||||||
|
static jmp_buf jenv;
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
/* entry point for our child thread process - just longjmp into fork */
|
||||||
|
static int child_entry(void)
|
||||||
|
{
|
||||||
|
longjmp(jenv, 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
static BOOL haveLoadedFunctionsForFork(void)
|
||||||
|
{
|
||||||
|
HMODULE ntdll = GetModuleHandle("ntdll");
|
||||||
|
if (ntdll == NULL)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ZwCreateProcess && ZwQuerySystemInformation && ZwQueryVirtualMemory &&
|
||||||
|
ZwCreateThread && ZwGetContextThread && ZwResumeThread &&
|
||||||
|
ZwQueryInformationThread && ZwWriteVirtualMemory && ZwClose)
|
||||||
|
{
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ZwCreateProcess =
|
||||||
|
(ZwCreateProcess_t)GetProcAddress(ntdll, "ZwCreateProcess");
|
||||||
|
ZwQuerySystemInformation = (ZwQuerySystemInformation_t)GetProcAddress(
|
||||||
|
ntdll, "ZwQuerySystemInformation");
|
||||||
|
ZwQueryVirtualMemory =
|
||||||
|
(ZwQueryVirtualMemory_t)GetProcAddress(ntdll, "ZwQueryVirtualMemory");
|
||||||
|
ZwCreateThread = (ZwCreateThread_t)GetProcAddress(ntdll, "ZwCreateThread");
|
||||||
|
ZwGetContextThread =
|
||||||
|
(ZwGetContextThread_t)GetProcAddress(ntdll, "ZwGetContextThread");
|
||||||
|
ZwResumeThread = (ZwResumeThread_t)GetProcAddress(ntdll, "ZwResumeThread");
|
||||||
|
ZwQueryInformationThread = (ZwQueryInformationThread_t)GetProcAddress(
|
||||||
|
ntdll, "ZwQueryInformationThread");
|
||||||
|
ZwWriteVirtualMemory =
|
||||||
|
(ZwWriteVirtualMemory_t)GetProcAddress(ntdll, "ZwWriteVirtualMemory");
|
||||||
|
ZwClose = (ZwClose_t)GetProcAddress(ntdll, "ZwClose");
|
||||||
|
|
||||||
|
if (ZwCreateProcess && ZwQuerySystemInformation && ZwQueryVirtualMemory &&
|
||||||
|
ZwCreateThread && ZwGetContextThread && ZwResumeThread &&
|
||||||
|
ZwQueryInformationThread && ZwWriteVirtualMemory && ZwClose)
|
||||||
|
{
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ZwCreateProcess = NULL;
|
||||||
|
ZwQuerySystemInformation = NULL;
|
||||||
|
ZwQueryVirtualMemory = NULL;
|
||||||
|
ZwCreateThread = NULL;
|
||||||
|
ZwGetContextThread = NULL;
|
||||||
|
ZwResumeThread = NULL;
|
||||||
|
ZwQueryInformationThread = NULL;
|
||||||
|
ZwWriteVirtualMemory = NULL;
|
||||||
|
ZwClose = NULL;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
int fork(void)
|
||||||
|
{
|
||||||
|
HANDLE hProcess = 0, hThread = 0;
|
||||||
|
OBJECT_ATTRIBUTES oa = {sizeof(oa)};
|
||||||
|
MEMORY_BASIC_INFORMATION mbi;
|
||||||
|
CLIENT_ID cid;
|
||||||
|
USER_STACK stack;
|
||||||
|
PNT_TIB tib;
|
||||||
|
THREAD_BASIC_INFORMATION tbi;
|
||||||
|
|
||||||
|
CONTEXT context = {CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS |
|
||||||
|
CONTEXT_FLOATING_POINT};
|
||||||
|
|
||||||
|
if (setjmp(jenv) != 0)
|
||||||
|
{
|
||||||
|
return 0; /* return as a child */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* check whether the entry points are initilized and get them if necessary
|
||||||
|
*/
|
||||||
|
if (!ZwCreateProcess && !haveLoadedFunctionsForFork())
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* create forked process */
|
||||||
|
ZwCreateProcess(&hProcess, PROCESS_ALL_ACCESS, &oa, NtCurrentProcess(),
|
||||||
|
TRUE, 0, 0, 0);
|
||||||
|
|
||||||
|
/* set the Eip for the child process to our child function */
|
||||||
|
ZwGetContextThread(NtCurrentThread(), &context);
|
||||||
|
|
||||||
|
/* In x64 the Eip and Esp are not present, their x64 counterparts are Rip
|
||||||
|
and Rsp respectively.
|
||||||
|
*/
|
||||||
|
#if _WIN64
|
||||||
|
context.Rip = (ULONG)child_entry;
|
||||||
|
#else
|
||||||
|
context.Eip = (ULONG)child_entry;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if _WIN64
|
||||||
|
ZwQueryVirtualMemory(NtCurrentProcess(), (PVOID)context.Rsp,
|
||||||
|
MemoryBasicInformation, &mbi, sizeof mbi, 0);
|
||||||
|
#else
|
||||||
|
ZwQueryVirtualMemory(NtCurrentProcess(), (PVOID)context.Esp,
|
||||||
|
MemoryBasicInformation, &mbi, sizeof mbi, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
stack.FixedStackBase = 0;
|
||||||
|
stack.FixedStackLimit = 0;
|
||||||
|
stack.ExpandableStackBase = (PCHAR)mbi.BaseAddress + mbi.RegionSize;
|
||||||
|
stack.ExpandableStackLimit = mbi.BaseAddress;
|
||||||
|
stack.ExpandableStackBottom = mbi.AllocationBase;
|
||||||
|
|
||||||
|
/* create thread using the modified context and stack */
|
||||||
|
ZwCreateThread(&hThread, THREAD_ALL_ACCESS, &oa, hProcess, &cid, &context,
|
||||||
|
&stack, TRUE);
|
||||||
|
|
||||||
|
/* copy exception table */
|
||||||
|
ZwQueryInformationThread(NtCurrentThread(), ThreadMemoryPriority, &tbi,
|
||||||
|
sizeof tbi, 0);
|
||||||
|
tib = (PNT_TIB)tbi.TebBaseAddress;
|
||||||
|
ZwQueryInformationThread(hThread, ThreadMemoryPriority, &tbi, sizeof tbi,
|
||||||
|
0);
|
||||||
|
ZwWriteVirtualMemory(hProcess, tbi.TebBaseAddress, &tib->ExceptionList,
|
||||||
|
sizeof tib->ExceptionList, 0);
|
||||||
|
|
||||||
|
/* start (resume really) the child */
|
||||||
|
ZwResumeThread(hThread, 0);
|
||||||
|
|
||||||
|
/* clean up */
|
||||||
|
ZwClose(hThread);
|
||||||
|
ZwClose(hProcess);
|
||||||
|
|
||||||
|
/* exit with child's pid */
|
||||||
|
return (int)cid.UniqueProcess;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __FORK_H__ */
|
||||||
|
/*--------------------------------------------------------------------------*/
|
@ -14,17 +14,26 @@
|
|||||||
* using UDP is shown using the server-client model & socket programming
|
* using UDP is shown using the server-client model & socket programming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define bzero(b, len) \
|
||||||
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
||||||
|
#define close _close
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <winsock2.h> /// For the type in_addr_t and in_port_t
|
||||||
|
#else
|
||||||
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
||||||
#include <errno.h> /// To indicate what went wrong if an error occurs
|
|
||||||
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
||||||
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
||||||
#include <stdint.h> /// For specific bit size values of variables
|
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
||||||
|
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
#include <errno.h> /// To indicate what went wrong if an error occurs
|
||||||
|
#include <stdint.h> /// For specific bit size values of variables
|
||||||
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
||||||
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
||||||
#include <string.h> /// Various functions for manipulating arrays of characters
|
#include <string.h> /// Various functions for manipulating arrays of characters
|
||||||
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
|
||||||
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
|
||||||
#include <unistd.h> /// For miscellaneous symbolic constants and types, and miscellaneous functions
|
|
||||||
|
|
||||||
#define PORT 10000 /// Define port over which communication will take place
|
#define PORT 10000 /// Define port over which communication will take place
|
||||||
|
|
||||||
|
@ -14,17 +14,26 @@
|
|||||||
* using UDP is shown using the server-client model & socket programming
|
* using UDP is shown using the server-client model & socket programming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define bzero(b, len) \
|
||||||
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
||||||
|
#define close _close
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <winsock2.h> /// For the type in_addr_t and in_port_t
|
||||||
|
#else
|
||||||
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
||||||
#include <errno.h> /// To indicate what went wrong if an error occurs
|
|
||||||
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
||||||
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
||||||
#include <stdint.h> /// For specific bit size values of variables
|
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
||||||
|
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
#include <errno.h> /// To indicate what went wrong if an error occurs
|
||||||
|
#include <stdint.h> /// For specific bit size values of variables
|
||||||
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
||||||
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
||||||
#include <string.h> /// Various functions for manipulating arrays of characters
|
#include <string.h> /// Various functions for manipulating arrays of characters
|
||||||
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
|
||||||
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
|
||||||
#include <unistd.h> /// For miscellaneous symbolic constants and types, and miscellaneous functions
|
|
||||||
|
|
||||||
#define PORT 10000 /// Define port over which communication will take place
|
#define PORT 10000 /// Define port over which communication will take place
|
||||||
|
|
||||||
|
@ -17,16 +17,29 @@
|
|||||||
* can be represented using the TCP server-client model & socket programming
|
* can be represented using the TCP server-client model & socket programming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define bzero(b, len) \
|
||||||
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
||||||
|
#define pid_t int
|
||||||
|
#define close _close
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include "fork.h"
|
||||||
|
#define sleep(a) Sleep(a * 1000)
|
||||||
|
#else
|
||||||
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
||||||
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
||||||
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
||||||
#include <stdint.h> /// For specific bit size values of variables
|
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
||||||
|
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
#include <stdint.h> /// For specific bit size values of variables
|
||||||
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
||||||
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
||||||
#include <string.h> /// Various functions for manipulating arrays of characters
|
#include <string.h> /// Various functions for manipulating arrays of characters
|
||||||
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
|
||||||
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
|
||||||
#include <unistd.h> /// For miscellaneous symbolic constants and types, and miscellaneous functions
|
|
||||||
|
|
||||||
#define PORT 10000 /// Define port over which communication will take place
|
#define PORT 10000 /// Define port over which communication will take place
|
||||||
|
|
||||||
@ -141,6 +154,7 @@ int main()
|
|||||||
*/
|
*/
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
||||||
if (pid == 0) /// Value of 0 is for child process
|
if (pid == 0) /// Value of 0 is for child process
|
||||||
{
|
{
|
||||||
while (1)
|
while (1)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* @author [NVombat](https://github.com/NVombat)
|
* @author [NVombat](https://github.com/NVombat)
|
||||||
* @brief Server-side implementation of [TCP Full Duplex
|
* @brief Server-side implementation of [TCP Full Duplex
|
||||||
* Communication](http://www.tcpipguide.com/free/t_SimplexFullDuplexandHalfDuplexOperation.htm)
|
* Communication](http://www.tcpipguide.com/free/t_SimplexFullDuplexandHalfDuplexOperation.htm)
|
||||||
* @see tcp_full_duplex_server.c
|
* @see tcp_full_duplex_client.c
|
||||||
*
|
*
|
||||||
* @details
|
* @details
|
||||||
* The algorithm is based on the simple TCP client and server model. However,
|
* The algorithm is based on the simple TCP client and server model. However,
|
||||||
@ -17,16 +17,29 @@
|
|||||||
* can be represented using the TCP server-client model & socket programming
|
* can be represented using the TCP server-client model & socket programming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define bzero(b, len) \
|
||||||
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
||||||
|
#define pid_t int
|
||||||
|
#define close _close
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include "fork.h"
|
||||||
|
#define sleep(a) Sleep(a * 1000)
|
||||||
|
#else
|
||||||
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
#include <arpa/inet.h> /// For the type in_addr_t and in_port_t
|
||||||
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
||||||
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
||||||
#include <stdint.h> /// For specific bit size values of variables
|
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
||||||
|
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
#include <stdint.h> /// For specific bit size values of variables
|
||||||
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
||||||
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
||||||
#include <string.h> /// Various functions for manipulating arrays of characters
|
#include <string.h> /// Various functions for manipulating arrays of characters
|
||||||
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
|
||||||
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
|
||||||
#include <unistd.h> /// For miscellaneous symbolic constants and types, and miscellaneous functions
|
|
||||||
|
|
||||||
#define PORT 10000 /// Define port over which communication will take place
|
#define PORT 10000 /// Define port over which communication will take place
|
||||||
|
|
||||||
@ -162,7 +175,15 @@ int main()
|
|||||||
* place simultaneously this represents FULL DUPLEX COMMUNICATION
|
* place simultaneously this represents FULL DUPLEX COMMUNICATION
|
||||||
*/
|
*/
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifdef FORK_WINDOWS
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
pid = fork();
|
||||||
|
#endif
|
||||||
|
|
||||||
if (pid == 0) /// Value of 0 is for child process
|
if (pid == 0) /// Value of 0 is for child process
|
||||||
{
|
{
|
||||||
while (1)
|
while (1)
|
||||||
|
@ -15,15 +15,24 @@
|
|||||||
* can be represented using the TCP server-client model & socket programming
|
* can be represented using the TCP server-client model & socket programming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define bzero(b, len) \
|
||||||
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
||||||
|
#define close _close
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
||||||
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
||||||
#include <stdint.h> /// For specific bit size values of variables
|
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
// #include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
||||||
|
#include <stdint.h> /// For specific bit size values of variables
|
||||||
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
||||||
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
||||||
#include <string.h> /// Various functions for manipulating arrays of characters
|
#include <string.h> /// Various functions for manipulating arrays of characters
|
||||||
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
|
||||||
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
|
||||||
#include <unistd.h> /// For miscellaneous symbolic constants and types, and miscellaneous functions
|
|
||||||
|
|
||||||
#define PORT 8100 /// Define port over which communication will take place
|
#define PORT 8100 /// Define port over which communication will take place
|
||||||
|
|
||||||
|
@ -15,15 +15,24 @@
|
|||||||
* can be represented using the TCP server-client model & socket programming
|
* can be represented using the TCP server-client model & socket programming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define bzero(b, len) \
|
||||||
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
||||||
|
#define close _close
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <io.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
#include <netdb.h> /// For structures returned by the network database library - formatted internet addresses and port numbers
|
||||||
#include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
||||||
#include <stdint.h> /// For specific bit size values of variables
|
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
// #include <netinet/in.h> /// For in_addr and sockaddr_in structures
|
||||||
|
#include <stdint.h> /// For specific bit size values of variables
|
||||||
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
#include <stdio.h> /// Variable types, several macros, and various functions for performing input and output
|
||||||
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
#include <stdlib.h> /// Variable types, several macros, and various functions for performing general functions
|
||||||
#include <string.h> /// Various functions for manipulating arrays of characters
|
#include <string.h> /// Various functions for manipulating arrays of characters
|
||||||
#include <sys/socket.h> /// For macro definitions related to the creation of sockets
|
|
||||||
#include <sys/types.h> /// For definitions to allow for the porting of BSD programs
|
|
||||||
#include <unistd.h> /// For miscellaneous symbolic constants and types, and miscellaneous functions
|
|
||||||
|
|
||||||
#define PORT 8100 /// Define port over which communication will take place
|
#define PORT 8100 /// Define port over which communication will take place
|
||||||
|
|
||||||
|
@ -19,7 +19,11 @@
|
|||||||
#define MIN_PRINTF_H
|
#define MIN_PRINTF_H
|
||||||
|
|
||||||
#include <stdlib.h> /// for `malloc` and `free` functions
|
#include <stdlib.h> /// for `malloc` and `free` functions
|
||||||
#include <unistd.h> /// for `write` function
|
#ifdef _WIN32
|
||||||
|
#include <io.h> /// for `write` function
|
||||||
|
#else
|
||||||
|
#include <unistd.h> /// for `write` function
|
||||||
|
#endif
|
||||||
#include <stdarg.h> /// for `va_start` and `va_arg` functions
|
#include <stdarg.h> /// for `va_start` and `va_arg` functions
|
||||||
|
|
||||||
#define INT_MAX_LENGTH 10 /// used as standard length of string to store integers
|
#define INT_MAX_LENGTH 10 /// used as standard length of string to store integers
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* @brief [Matrix Chain Order](https://en.wikipedia.org/wiki/Matrix_chain_multiplication)
|
* @brief [Matrix Chain
|
||||||
|
* Order](https://en.wikipedia.org/wiki/Matrix_chain_multiplication)
|
||||||
* @details
|
* @details
|
||||||
* From Wikipedia: Matrix chain multiplication (or the matrix chain ordering problem)
|
* From Wikipedia: Matrix chain multiplication (or the matrix chain ordering
|
||||||
* is an optimization problem concerning the most efficient way to multiply a given sequence of matrices.
|
* problem) is an optimization problem concerning the most efficient way to
|
||||||
* The problem is not actually to perform the multiplications,
|
* multiply a given sequence of matrices. The problem is not actually to perform
|
||||||
* but merely to decide the sequence of the matrix multiplications involved.
|
* the multiplications, but merely to decide the sequence of the matrix
|
||||||
|
* multiplications involved.
|
||||||
* @author [CascadingCascade](https://github.com/CascadingCascade)
|
* @author [CascadingCascade](https://github.com/CascadingCascade)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <assert.h> /// for assert
|
#include <assert.h> /// for assert
|
||||||
#include <stdio.h> /// for IO operations
|
#include <limits.h> /// for INT_MAX macro
|
||||||
#include <limits.h> /// for INT_MAX macro
|
#include <stdio.h> /// for IO operations
|
||||||
#include <stdlib.h> /// for malloc() and free()
|
#include <stdlib.h> /// for malloc() and free()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Finds the optimal sequence using the classic O(n^3) algorithm.
|
* @brief Finds the optimal sequence using the classic O(n^3) algorithm.
|
||||||
@ -21,27 +23,49 @@
|
|||||||
* @param s location to store results
|
* @param s location to store results
|
||||||
* @returns number of operations
|
* @returns number of operations
|
||||||
*/
|
*/
|
||||||
int matrixChainOrder(int l,const int *p, int *s) {
|
int matrixChainOrder(int l, const int *p, int *s)
|
||||||
// mat stores the cost for a chain that starts at i and ends on j (inclusive on both ends)
|
{
|
||||||
int mat[l][l];
|
// mat stores the cost for a chain that starts at i and ends on j (inclusive
|
||||||
for (int i = 0; i < l; ++i) {
|
// on both ends)
|
||||||
|
int **mat = malloc(l * sizeof(int *));
|
||||||
|
for (int i = 0; i < l; ++i)
|
||||||
|
{
|
||||||
|
mat[i] = malloc(l * sizeof(int));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < l; ++i)
|
||||||
|
{
|
||||||
mat[i][i] = 0;
|
mat[i][i] = 0;
|
||||||
}
|
}
|
||||||
// cl denotes the difference between start / end indices, cl + 1 would be chain length.
|
// cl denotes the difference between start / end indices, cl + 1 would be
|
||||||
for (int cl = 1; cl < l; ++cl) {
|
// chain length.
|
||||||
for (int i = 0; i < l - cl; ++i) {
|
for (int cl = 1; cl < l; ++cl)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < l - cl; ++i)
|
||||||
|
{
|
||||||
int j = i + cl;
|
int j = i + cl;
|
||||||
mat[i][j] = INT_MAX;
|
mat[i][j] = INT_MAX;
|
||||||
for (int div = i; div < j; ++div) {
|
for (int div = i; div < j; ++div)
|
||||||
|
{
|
||||||
int q = mat[i][div] + mat[div + 1][j] + p[i] * p[div] * p[j];
|
int q = mat[i][div] + mat[div + 1][j] + p[i] * p[div] * p[j];
|
||||||
if (q < mat[i][j]) {
|
if (q < mat[i][j])
|
||||||
|
{
|
||||||
mat[i][j] = q;
|
mat[i][j] = q;
|
||||||
s[i * l + j] = div;
|
s[i * l + j] = div;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mat[0][l - 1];
|
int result = mat[0][l - 1];
|
||||||
|
|
||||||
|
// Free dynamically allocated memory
|
||||||
|
for (int i = 0; i < l; ++i)
|
||||||
|
{
|
||||||
|
free(mat[i]);
|
||||||
|
}
|
||||||
|
free(mat);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,14 +76,16 @@ int matrixChainOrder(int l,const int *p, int *s) {
|
|||||||
* @param j ending index
|
* @param j ending index
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
void printSolution(int l,int *s,int i,int j) {
|
void printSolution(int l, int *s, int i, int j)
|
||||||
if(i == j) {
|
{
|
||||||
printf("A%d",i);
|
if (i == j)
|
||||||
|
{
|
||||||
|
printf("A%d", i);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
putchar('(');
|
putchar('(');
|
||||||
printSolution(l,s,i,s[i * l + j]);
|
printSolution(l, s, i, s[i * l + j]);
|
||||||
printSolution(l,s,s[i * l + j] + 1,j);
|
printSolution(l, s, s[i * l + j] + 1, j);
|
||||||
putchar(')');
|
putchar(')');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,15 +93,16 @@ void printSolution(int l,int *s,int i,int j) {
|
|||||||
* @brief Self-test implementations
|
* @brief Self-test implementations
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
static void test() {
|
static void test()
|
||||||
int sizes[] = {35,15,5,10,20,25};
|
{
|
||||||
|
int sizes[] = {35, 15, 5, 10, 20, 25};
|
||||||
int len = 6;
|
int len = 6;
|
||||||
int *sol = malloc(len * len * sizeof(int));
|
int *sol = malloc(len * len * sizeof(int));
|
||||||
int r = matrixChainOrder(len,sizes,sol);
|
int r = matrixChainOrder(len, sizes, sol);
|
||||||
assert(r == 18625);
|
assert(r == 18625);
|
||||||
printf("Result : %d\n",r);
|
printf("Result : %d\n", r);
|
||||||
printf("Optimal ordering : ");
|
printf("Optimal ordering : ");
|
||||||
printSolution(len,sol,0,5);
|
printSolution(len, sol, 0, 5);
|
||||||
free(sol);
|
free(sol);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@ -85,7 +112,8 @@ static void test() {
|
|||||||
* @brief Main function
|
* @brief Main function
|
||||||
* @returns 0
|
* @returns 0
|
||||||
*/
|
*/
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
test(); // run self-test implementations
|
test(); // run self-test implementations
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ if(OpenGL_FOUND)
|
|||||||
FREEGLUT-PRJ
|
FREEGLUT-PRJ
|
||||||
URL https://github.com/FreeGLUTProject/freeglut/releases/download/v3.2.1/freeglut-3.2.1.tar.gz
|
URL https://github.com/FreeGLUTProject/freeglut/releases/download/v3.2.1/freeglut-3.2.1.tar.gz
|
||||||
URL_MD5 cd5c670c1086358598a6d4a9d166949d
|
URL_MD5 cd5c670c1086358598a6d4a9d166949d
|
||||||
CMAKE_GENERATOR ${CMAKE_GENERATOR} --config Release
|
CMAKE_GENERATOR ${CMAKE_GENERATOR}
|
||||||
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
|
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
|
||||||
CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM}
|
CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM}
|
||||||
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
|
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
|
||||||
|
@ -3,8 +3,9 @@
|
|||||||
* \brief [Exponential Search](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Search%20Algorithms/Exponential%20Search.md)
|
* \brief [Exponential Search](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Search%20Algorithms/Exponential%20Search.md)
|
||||||
* \author [Alessio Farinelli](https://github.com/faridevnz)
|
* \author [Alessio Farinelli](https://github.com/faridevnz)
|
||||||
*/
|
*/
|
||||||
#include <assert.h> /// for assert
|
#include <assert.h> /// for assert
|
||||||
#include <inttypes.h> /// for int64_t, uint16_t
|
#include <inttypes.h> /// for int64_t, uint16_t
|
||||||
|
#include <stdio.h> /// for printf
|
||||||
|
|
||||||
#define ELEMENT -10
|
#define ELEMENT -10
|
||||||
|
|
||||||
@ -81,7 +82,7 @@ int main()
|
|||||||
static void test()
|
static void test()
|
||||||
{
|
{
|
||||||
// empty array
|
// empty array
|
||||||
int64_t arr_empty[] = {};
|
int64_t arr_empty[] = { 0 };
|
||||||
assert(exponential_search(arr_empty, 0, 10) == -1);
|
assert(exponential_search(arr_empty, 0, 10) == -1);
|
||||||
// elent not found
|
// elent not found
|
||||||
int64_t arr_found[] = {1, 2, 3};
|
int64_t arr_found[] = {1, 2, 3};
|
||||||
@ -104,4 +105,6 @@ static void test()
|
|||||||
// find an element in an array of length n
|
// find an element in an array of length n
|
||||||
int64_t arr_middle[] = {-1, 2, 4, 6, 8};
|
int64_t arr_middle[] = {-1, 2, 4, 6, 8};
|
||||||
assert(exponential_search(arr_middle, 5, 6) == 3);
|
assert(exponential_search(arr_middle, 5, 6) == 3);
|
||||||
|
|
||||||
|
printf("All tests have successfully passed!\n");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user