Update MentOs code to the latest development version.
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
# =============================================================================
|
||||
# Author: Enrico Fraccaroli
|
||||
# =============================================================================
|
||||
# Set the minimum required version of cmake.
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
# Initialize the project.
|
||||
project(tests C)
|
||||
|
||||
# =============================================================================
|
||||
# Set the default build type to Debug.
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
message(STATUS "Setting build type to 'Debug' as none was specified.")
|
||||
set(CMAKE_BUILD_TYPE
|
||||
"Debug"
|
||||
CACHE STRING "Choose the type of build." FORCE)
|
||||
endif()
|
||||
|
||||
# =============================================================================
|
||||
# Set the directory where the compiled binaries will be placed.
|
||||
set(MENTOS_TESTS_DIR ${CMAKE_SOURCE_DIR}/files/bin/tests)
|
||||
|
||||
# =============================================================================
|
||||
# Warning flags.
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpedantic")
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic-errors")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-variable")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unknown-pragmas")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-but-set-variable")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
|
||||
# Set the compiler options.
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -u_start")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdlib")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-builtin")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
|
||||
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -no-pie -Wl,-z,norelro")
|
||||
|
||||
# =============================================================================
|
||||
# Add the executables (manually).
|
||||
set(TESTS
|
||||
t_mem.c
|
||||
t_fork.c
|
||||
# Real-time programs
|
||||
t_periodic1.c
|
||||
t_periodic2.c
|
||||
t_periodic3.c
|
||||
# Exec
|
||||
t_exec.c
|
||||
t_exec_callee.c
|
||||
# Test environmental variables.
|
||||
t_setenv.c
|
||||
t_getenv.c
|
||||
# Test: signals.
|
||||
t_stopcont.c
|
||||
t_sigusr.c
|
||||
t_abort.c
|
||||
t_sigfpe.c
|
||||
t_siginfo.c
|
||||
t_sleep.c
|
||||
t_sigaction.c
|
||||
t_sigmask.c
|
||||
t_groups.c
|
||||
t_alarm.c
|
||||
t_kill.c
|
||||
t_itimer.c
|
||||
t_gid.c)
|
||||
|
||||
foreach(TEST ${TESTS})
|
||||
# Prepare the program name.
|
||||
string(REPLACE ".c" "" TEST_NAME ${TEST})
|
||||
# Set the name of the target.
|
||||
set(TARGET_NAME test_${TEST_NAME})
|
||||
|
||||
# Log the entry.
|
||||
message(VERBOSE "Test ${TEST_NAME} (source: ${TEST}, make target: ${TARGET_NAME})")
|
||||
|
||||
# Randomize .text section address so when debugging symbols don't clash The allowed range is from 256MB to 2.75GB Minimum allowed address: 0x10000000 Max allowed address:
|
||||
# 0xB0000000
|
||||
string(MD5 RAND_HASH ${TEST})
|
||||
string(SUBSTRING ${RAND_HASH} 1 3 TEXADDR_INFIX)
|
||||
string(
|
||||
RANDOM
|
||||
LENGTH 1
|
||||
ALPHABET 0123456789AB
|
||||
RANDOM_SEED ${RAND_HASH} TEXADDR_FIRST)
|
||||
|
||||
# Create the target.
|
||||
add_executable(${TARGET_NAME} ${TEST})
|
||||
|
||||
# Add the includes.
|
||||
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/libc/inc)
|
||||
|
||||
# Link the libc library.
|
||||
target_link_libraries(${TARGET_NAME} ${CMAKE_BINARY_DIR}/libc/libc.a)
|
||||
|
||||
# Add the dependency to libc.
|
||||
add_dependencies(${TARGET_NAME} libc)
|
||||
|
||||
# Add the linking properties.
|
||||
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "-Wl,-Ttext=0x${TEXADDR_FIRST}${TEXADDR_INFIX}0000,-e_start")
|
||||
|
||||
# Add the final target that strips the debugging symbols from the program.
|
||||
add_custom_target(
|
||||
${TEST_NAME}
|
||||
BYPRODUCTS ${MENTOS_TESTS_DIR}/${TEST_NAME}
|
||||
COMMAND mkdir -p ${MENTOS_TESTS_DIR}
|
||||
COMMAND strip --strip-debug ${PROJECT_BINARY_DIR}/${TARGET_NAME} -o ${MENTOS_TESTS_DIR}/${TEST_NAME}
|
||||
DEPENDS ${TARGET_NAME})
|
||||
|
||||
# Append the program name to the list of all the executables.
|
||||
list(APPEND ALL_TESTS ${TEST_NAME})
|
||||
endforeach()
|
||||
|
||||
# Add the overall target that builds all the programs.
|
||||
add_custom_target(all_tests ALL DEPENDS ${ALL_TESTS})
|
||||
@@ -0,0 +1,46 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_abort.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
|
||||
void sig_handler(int sig)
|
||||
{
|
||||
printf("handler(%d) : Starting handler.\n", sig);
|
||||
if (sig == SIGABRT) {
|
||||
|
||||
static int counter = 0;
|
||||
counter += 1;
|
||||
|
||||
printf("handler(%d) : Correct signal. ABRT (%d/3)\n", sig, counter);
|
||||
if (counter < 3)
|
||||
abort();
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
}
|
||||
printf("handler(%d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = sig_handler;
|
||||
|
||||
if (sigaction(SIGABRT, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGABRT, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
abort();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_alarm.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
void alarm_handler(int sig)
|
||||
{
|
||||
printf("handler(%d) : Starting handler.\n", sig);
|
||||
if (sig == SIGALRM) {
|
||||
printf("handler(%d) : Correct signal.\n", sig);
|
||||
|
||||
alarm(5);
|
||||
int rest = alarm(5);
|
||||
printf("handler(%d) : alarm(5) result: %d.\n", sig, rest);
|
||||
|
||||
rest = alarm(0);
|
||||
printf("handler(%d) : alarm(0) result: %d.\n", sig, rest);
|
||||
|
||||
exit(0);
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
}
|
||||
printf("handler(%d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = alarm_handler;
|
||||
if (sigaction(SIGALRM, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGALRM, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
alarm(5);
|
||||
while(1) { }
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_exec.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline void __print_usage(int argc, char *argv[])
|
||||
{
|
||||
if (argc > 0) {
|
||||
printf("%s: Usage: %s <exec_type>\n", argv[0], argv[0]);
|
||||
printf("exec_type: execl, execlp, execle, execlpe, execv, execvp, execve, execvpe\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int status;
|
||||
if (argc != 2) {
|
||||
__print_usage(argc, argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (setenv("ENV_VAR", "pwd0", 0) == -1) {
|
||||
printf("Failed to set env: `ENV_VAR`\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *file = "echo";
|
||||
char *path = "/bin/echo";
|
||||
char *exec_argv[] = { "echo", "ENV_VAR: ${ENV_VAR}", NULL };
|
||||
char *exec_envp[] = { "PATH=/bin", "ENV_VAR=pwd1", NULL };
|
||||
|
||||
if (fork() == 0) {
|
||||
if (strcmp(argv[1], "execl") == 0) {
|
||||
execl(path, "echo", "ENV_VAR: ${ENV_VAR}", NULL);
|
||||
} else if (strcmp(argv[1], "execlp") == 0) {
|
||||
execlp(file, "echo", "ENV_VAR: ${ENV_VAR}", NULL);
|
||||
} else if (strcmp(argv[1], "execle") == 0) {
|
||||
execle(path, "echo", "ENV_VAR: ${ENV_VAR}", exec_envp, NULL);
|
||||
} else if (strcmp(argv[1], "execlpe") == 0) {
|
||||
execlpe(file, "echo", "ENV_VAR: ${ENV_VAR}", exec_envp, NULL);
|
||||
} else if (strcmp(argv[1], "execv") == 0) {
|
||||
execv(path, exec_argv);
|
||||
} else if (strcmp(argv[1], "execvp") == 0) {
|
||||
execvp(file, exec_argv);
|
||||
} else if (strcmp(argv[1], "execve") == 0) {
|
||||
execve(path, exec_argv, exec_envp);
|
||||
} else if (strcmp(argv[1], "execvpe") == 0) {
|
||||
execvpe(file, exec_argv, exec_envp);
|
||||
} else {
|
||||
__print_usage(argc, argv);
|
||||
return 1;
|
||||
}
|
||||
printf("Exec failed.\n");
|
||||
return 1;
|
||||
}
|
||||
wait(&status);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_exec_callee.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *ENV_VAR = getenv("ENV_VAR");
|
||||
if (ENV_VAR == NULL) {
|
||||
printf("Failed to get env: `ENV_VAR`\n");
|
||||
return 1;
|
||||
}
|
||||
printf("ENV_VAR = %s\n", ENV_VAR);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_fork.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
return 1;
|
||||
}
|
||||
char *ptr;
|
||||
int N;
|
||||
N = strtol(argv[1], &ptr, 10);
|
||||
pid_t cpid, mypid;
|
||||
#if 0
|
||||
char buffer[256];
|
||||
|
||||
N = strtol(argv[1], &ptr, 10);
|
||||
|
||||
char *_argv[] = {
|
||||
"/bin/tests/t_fork",
|
||||
itoa(buffer, N - 1, 10),
|
||||
NULL
|
||||
};
|
||||
|
||||
printf("N = %d\n", N);
|
||||
if (N > 1) {
|
||||
|
||||
printf("Keep caling (N = %d)\n", N);
|
||||
|
||||
if ((cpid = fork()) == 0) {
|
||||
|
||||
printf("I'm the child (pid = %d, N = %d)!\n", getpid(), N);
|
||||
|
||||
execv(_argv[0], _argv);
|
||||
|
||||
}
|
||||
printf("Will wait for %d\n", N, cpid);
|
||||
while (wait(NULL) != -1) continue;
|
||||
}
|
||||
#else
|
||||
while (1) {
|
||||
mypid = getpid();
|
||||
if (N > 0) {
|
||||
if ((cpid = fork()) == 0) {
|
||||
N -= 1;
|
||||
continue;
|
||||
}
|
||||
printf("I'm %d and I will wait for %d (N = %d)!\n", mypid, cpid, N);
|
||||
while (wait(NULL) != -1) continue;
|
||||
printf("I'm %d and I waited for %d (N = %d)!\n", mypid, cpid, N);
|
||||
} else {
|
||||
printf("I'm %d and I will not wait!\n", mypid);
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_getenv.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *ENV_VAR = getenv("ENV_VAR");
|
||||
if (ENV_VAR == NULL) {
|
||||
printf("Failed to get env: `ENV_VAR`\n");
|
||||
return 1;
|
||||
}
|
||||
printf("ENV_VAR = %s\n", ENV_VAR);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_gid.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include "grp.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static void list_groups() {
|
||||
|
||||
group* iter;
|
||||
while ((iter = getgrent()) != NULL) {
|
||||
printf("Group\n\tname: %s\n\tpasswd: %s\n\tnames:\n",iter->gr_name, iter->gr_passwd);
|
||||
|
||||
size_t count = 0;
|
||||
while (iter->gr_mem[count] != NULL) {
|
||||
printf("\t\t%s\n",iter->gr_mem[count]);
|
||||
count += 1;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
printf("List of all groups:\n");
|
||||
|
||||
list_groups();
|
||||
setgrent();
|
||||
|
||||
printf("List all groups again:\n");
|
||||
|
||||
list_groups();
|
||||
endgrent();
|
||||
|
||||
group* root_group = getgrgid(0);
|
||||
if (strcmp(root_group->gr_name, "root") != 0) {
|
||||
printf("Error in getgrgid function.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
root_group = getgrnam("root");
|
||||
if (root_group->gr_gid != 0) {
|
||||
printf("Error in getgrnam function.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_groups.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pid_t gid = getgid();
|
||||
pid_t pid = getpid();
|
||||
pid_t sid = getsid(0);
|
||||
|
||||
printf("pid: %d, gid: %d, sid: %d\n\n", pid, gid, sid);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (fork() == 0) {
|
||||
pid_t gid_child = getgid();
|
||||
pid_t pid_child = getpid();
|
||||
|
||||
pid_t ppid_child = getppid();
|
||||
pid_t sid_child = getsid(ppid_child);
|
||||
|
||||
printf("%d) pid_child: %d, gid_child: %d, ppid_child: %d, sid_child: %d\n",
|
||||
i, pid_child, gid_child, ppid_child, sid_child);
|
||||
sleep(5);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
//int status;
|
||||
//while (wait(&status) > 0)
|
||||
// ;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_itimer.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
|
||||
void alarm_handler(int sig)
|
||||
{
|
||||
printf("handler(%d) : Starting handler.\n", sig);
|
||||
if (sig == SIGALRM) {
|
||||
|
||||
itimerval val = { 0 };
|
||||
getitimer(ITIMER_REAL, &val);
|
||||
printf("(sec: %d, usec: %d)\n", val.it_interval.tv_sec, val.it_interval.tv_usec);
|
||||
|
||||
static int counter = 0;
|
||||
counter += 1;
|
||||
|
||||
printf("handler(%d) : Correct signal x%d\n", sig, counter);
|
||||
if (counter == 4)
|
||||
{
|
||||
itimerval interval = { 0 }, prev = { 0 };
|
||||
interval.it_interval.tv_sec = 0;
|
||||
|
||||
setitimer(ITIMER_REAL, &interval, &prev);
|
||||
printf("prev: (sec: %d, usec: %d)", prev.it_interval.tv_sec, prev.it_interval.tv_usec);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
}
|
||||
printf("handler(%d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = alarm_handler;
|
||||
if (sigaction(SIGALRM, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGALRM, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
itimerval interval = { 0 };
|
||||
interval.it_interval.tv_sec = 4;
|
||||
setitimer(ITIMER_REAL, &interval, NULL);
|
||||
|
||||
while(1) { }
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_kill.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#define cpu_relax() asm volatile("pause\n" \
|
||||
: \
|
||||
: \
|
||||
: "memory")
|
||||
|
||||
static inline void fake_sleep(int times)
|
||||
{
|
||||
for (int j = 0; j < times; ++j)
|
||||
for (long i = 1; i < 1024000; ++i)
|
||||
cpu_relax();
|
||||
}
|
||||
|
||||
void child_sigusr1_handler(int sig)
|
||||
{
|
||||
printf("handler(sig: %d) : Starting handler (pid: %d).\n", sig, getpid());
|
||||
printf("handler(sig: %d) : Ending handler (pid: %d).\n", sig, getpid());
|
||||
}
|
||||
|
||||
void child_process()
|
||||
{
|
||||
while (1) {
|
||||
printf("I'm the child (%d): I'm playing around!\n", getpid());
|
||||
fake_sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("main : Creating child!\n");
|
||||
pid_t ppid;
|
||||
if ((ppid = fork()) == 0) {
|
||||
printf("I'm the child (%d)!\n", ppid);
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = child_sigusr1_handler;
|
||||
if (sigaction(SIGUSR1, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGUSR1, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
child_process();
|
||||
} else {
|
||||
printf("I'm the parent (%d)!\n", ppid);
|
||||
}
|
||||
fake_sleep(9);
|
||||
kill(ppid, SIGUSR1);
|
||||
fake_sleep(9);
|
||||
kill(ppid, SIGTERM);
|
||||
int status;
|
||||
wait(&status);
|
||||
printf("main : end (%d)\n", status);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_mem.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
return 1;
|
||||
}
|
||||
char *ptr;
|
||||
int N = strtol(argv[1], &ptr, 10), *V;
|
||||
for (int i = 0; i < N; ++i) {
|
||||
for (int j = 1; j < N; ++j) {
|
||||
V = (int *)malloc(j * sizeof(int));
|
||||
free(V);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_periodic1.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sched.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <strerror.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pid_t cpid = getpid();
|
||||
sched_param_t param;
|
||||
// Get current parameters.
|
||||
sched_getparam(cpid, ¶m);
|
||||
// Change parameters.
|
||||
param.period = 5000;
|
||||
param.deadline = 5000;
|
||||
param.is_periodic = true;
|
||||
// Set modified parameters.
|
||||
sched_setparam(cpid, ¶m);
|
||||
int counter = 0;
|
||||
if (fork() == 0) {
|
||||
char *_argv[] = { "/bin/periodic2", NULL };
|
||||
execv(_argv[0], _argv);
|
||||
printf("This is bad, I should not be here! EXEC NOT WORKING\n");
|
||||
}
|
||||
if (fork() == 0) {
|
||||
char *_argv[] = { "/bin/periodic3", NULL };
|
||||
execv(_argv[0], _argv);
|
||||
printf("This is bad, I should not be here! EXEC NOT WORKING\n");
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (++counter == 10) {
|
||||
counter = 0;
|
||||
}
|
||||
printf("[periodic1]counter %d\n",counter);
|
||||
if (waitperiod() == -1) {
|
||||
printf("[%s] Error in waitperiod: %s\n", argv[0], strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_periodic2.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sched.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <strerror.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pid_t cpid = getpid();
|
||||
sched_param_t param;
|
||||
// Get current parameters.
|
||||
sched_getparam(cpid, ¶m);
|
||||
// Change parameters.
|
||||
param.period = 4000;
|
||||
param.deadline = 4000;
|
||||
param.is_periodic = true;
|
||||
// Set modified parameters.
|
||||
sched_setparam(cpid, ¶m);
|
||||
int counter = 0;
|
||||
while (1) {
|
||||
if (++counter == 10)
|
||||
break;
|
||||
printf("[priodic2] counter: %d\n", counter);
|
||||
if (waitperiod() == -1) {
|
||||
printf("[%s] Error in waitperiod: %s\n", argv[0], strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_periodic3.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sched.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <strerror.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pid_t cpid = getpid();
|
||||
sched_param_t param;
|
||||
// Get current parameters.
|
||||
sched_getparam(cpid, ¶m);
|
||||
// Change parameters.
|
||||
param.period = 3000;
|
||||
param.deadline = 3000;
|
||||
param.is_periodic = true;
|
||||
// Set modified parameters.
|
||||
sched_setparam(cpid, ¶m);
|
||||
int counter = 0;
|
||||
while (1) {
|
||||
if (++counter == 10)
|
||||
break;
|
||||
printf("[periodic3] counter: %d\n", counter);
|
||||
if (waitperiod() == -1) {
|
||||
printf("[%s] Error in waitperiod: %s\n", argv[0], strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_setenv.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *_argv[] = { "/bin/tests/t_getenv", NULL };
|
||||
int status;
|
||||
|
||||
if (setenv("ENV_VAR", "pwd0", 0) == -1) {
|
||||
printf("Failed to set env: `PWD`\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fork() == 0) {
|
||||
execv(_argv[0], _argv);
|
||||
printf("This is bad, I should not be here! EXEC NOT WORKING\n");
|
||||
}
|
||||
|
||||
wait(&status);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_sigaction.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int *values = NULL;
|
||||
|
||||
void sigusr1_handler(int sig)
|
||||
{
|
||||
printf("handler(sig: %d) : Starting handler.\n", sig);
|
||||
printf("handler(sig: %d) : value : %d\n", sig, values);
|
||||
values = malloc(sizeof(int) * 4);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
values[i] = i;
|
||||
printf("values[%d] : `%d`\n", i, values[i]);
|
||||
}
|
||||
printf("handler(sig: %d) : value : %d\n", sig, values);
|
||||
printf("handler(sig: %d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = sigusr1_handler;
|
||||
if (sigaction(SIGUSR1, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGUSR1, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("main : Calling handler (%d).\n", SIGUSR1);
|
||||
printf("main : value : %d\n", values);
|
||||
int ret = kill(getpid(), SIGUSR1);
|
||||
printf("main : Returning from handler (%d): %d.\n", SIGUSR1, ret);
|
||||
printf("main : value : %d\n", values);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
printf("values[%d] : `%d`\n", i, values[i]);
|
||||
free(values);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_sigfpe.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
void sig_handler(int sig)
|
||||
{
|
||||
printf("handler(%d) : Starting handler.\n", sig);
|
||||
if (sig == SIGFPE) {
|
||||
printf("handler(%d) : Correct signal. FPE\n", sig);
|
||||
printf("handler(%d) : Exiting\n", sig);
|
||||
exit(1);
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
}
|
||||
printf("handler(%d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = sig_handler;
|
||||
|
||||
if (sigaction(SIGFPE, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGFPE, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Diving by zero (unrecoverable)...\n");
|
||||
|
||||
// Should trigger ALU error, fighting the compiler...
|
||||
int d = 1, e = 1;
|
||||
while (1) {
|
||||
d /= e;
|
||||
e -= 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_siginfo.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
|
||||
void sig_handler_info(int sig, siginfo_t *siginfo)
|
||||
{
|
||||
printf("handler(%d, %p) : Starting handler.\n", sig, siginfo);
|
||||
if (sig == SIGFPE) {
|
||||
printf("handler(%d, %p) : Correct signal.\n", sig, siginfo);
|
||||
printf("handler(%d, %p) : Code : %d\n", sig, siginfo, siginfo->si_code);
|
||||
printf("handler(%d, %p) : Exiting\n", sig, siginfo);
|
||||
exit(1);
|
||||
} else {
|
||||
printf("handler(%d, %p) : Wrong signal.\n", sig, siginfo);
|
||||
}
|
||||
printf("handler(%d, %p) : Ending handler.\n", sig, siginfo);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
|
||||
action.sa_handler = (sighandler_t)sig_handler_info;
|
||||
action.sa_flags = SA_SIGINFO;
|
||||
|
||||
if (sigaction(SIGFPE, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGFPE, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Diving by zero (unrecoverable)...\n");
|
||||
|
||||
// Should trigger ALU error, fighting the compiler...
|
||||
int d = 1, e = 1;
|
||||
while (1) {
|
||||
d /= e;
|
||||
e -= 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_sigmask.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
void sigusr1_handler(int sig)
|
||||
{
|
||||
printf("handler(sig: %d) : Starting handler.\n", sig);
|
||||
printf("handler(sig: %d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = sigusr1_handler;
|
||||
if (sigaction(SIGUSR1, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%d, %s).\n", SIGUSR1, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("main : Blocking signal (%d).\n", SIGUSR1);
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGUSR1);
|
||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||
|
||||
printf("main : Calling handler (%d).\n", SIGUSR1);
|
||||
ret = kill(getpid(), SIGUSR1);
|
||||
printf("main : Returning from handler (%d): %d.\n", SIGUSR1, ret);
|
||||
|
||||
printf("main : Unblocking signal (%d).\n", SIGUSR1);
|
||||
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
||||
|
||||
printf("main : Calling handler (%d).\n", SIGUSR1);
|
||||
ret = kill(getpid(), SIGUSR1);
|
||||
printf("main : Returning from handler (%d): %d.\n", SIGUSR1, ret);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_sigusr.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
|
||||
void sig_handler(int sig)
|
||||
{
|
||||
printf("handler(%d) : Starting handler.\n", sig);
|
||||
|
||||
static int counter = 0;
|
||||
if (sig == SIGUSR1 || sig == SIGUSR2) {
|
||||
printf("handler(%d) : Correct signal. SIGUSER\n", sig);
|
||||
counter += 1;
|
||||
|
||||
if (counter == 2)
|
||||
exit(1);
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
}
|
||||
printf("handler(%d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = sig_handler;
|
||||
|
||||
if (sigaction(SIGUSR1, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGUSR1, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (sigaction(SIGUSR2, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler (%s).\n", SIGUSR2, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
kill(getpid(), SIGUSR1);
|
||||
sleep(2);
|
||||
kill(getpid(), SIGUSR2);
|
||||
|
||||
while(1) { };
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_sleep.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Sleeping for 4 seconds... ");
|
||||
sleep(4);
|
||||
printf("COMPLETED.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/// MentOS, The Mentoring Operating system project
|
||||
/// @file t_stopcont.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <sys/unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "stdlib.h"
|
||||
#include <sys/wait.h>
|
||||
|
||||
int child_pid;
|
||||
|
||||
void wait_for_child(int signr)
|
||||
{
|
||||
printf("Signal received: %s\n", strsignal(signr));
|
||||
sleep(10);
|
||||
|
||||
printf("Sending continue sig to child\n");
|
||||
if (kill(child_pid, SIGCONT) == -1)
|
||||
printf("Error sending signal\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = wait_for_child;
|
||||
if (sigaction(SIGCHLD, &action, NULL) == -1) {
|
||||
printf("Failed to set signal handler. %d\n", SIGCHLD);
|
||||
return 1;
|
||||
}
|
||||
|
||||
child_pid = fork();
|
||||
if (child_pid != 0) {
|
||||
printf("Child PID: %d\n", child_pid);
|
||||
|
||||
sleep(2);
|
||||
printf("Sending stop sig to child\n");
|
||||
|
||||
#if 1
|
||||
if (kill(child_pid, SIGSTOP) == -1)
|
||||
printf("Errore invio stop\n");
|
||||
#endif
|
||||
|
||||
wait(NULL);
|
||||
} else {
|
||||
int c = 0;
|
||||
for (;;) {
|
||||
printf("c: %d\n", c++);
|
||||
sleep(3);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user