Skip to content

Commit

Permalink
convert to Allman style for book project
Browse files Browse the repository at this point in the history
  • Loading branch information
George K. Thiruvathukal committed Jun 29, 2022
1 parent 4ffafc3 commit 6529b89
Show file tree
Hide file tree
Showing 109 changed files with 2,998 additions and 2,282 deletions.
37 changes: 20 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ project(OpSystemsCodeExamples)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

option(BUILD_PTH_EXAMPLE "Build GNU PTH example" ON)
option(BUILD_PTH_EXAMPLE "Build GNU PTH example" OFF)
option(BUILD_OPTIONAL_EXAMPLES "Build optional examples" OFF)

if(UNIX AND APPLE)
message("--> We only provide limited Apple support at this time. Please use a Linux system if at all possible..")
Expand Down Expand Up @@ -65,21 +66,23 @@ add_subdirectory(hello-lib)
add_subdirectory(charlist_t)


if(UNIX AND NOT APPLE)
if(BUILD_OPTIONAL_EXAMPLES)

if(BUILD_PTH_EXAMPLE)
message("Building GNU PTH example")
add_subdirectory(gnu_pth)
else()
message("Not building GNU PTH example")
endif()
if(UNIX AND NOT APPLE)
if(BUILD_PTH_EXAMPLE)
message("Building GNU PTH example")
add_subdirectory(gnu_pth)
else()
message("Not building GNU PTH example")
endif()

add_subdirectory(monitor)
add_subdirectory(mutex)
add_subdirectory(semaphore)
add_subdirectory(spin_lock)
add_subdirectory(std-hsearch)
add_subdirectory(std-tsearch)
add_subdirectory(stm)
else()
endif()
add_subdirectory(monitor)
add_subdirectory(mutex)
add_subdirectory(semaphore)
add_subdirectory(spin_lock)
add_subdirectory(std-hsearch)
add_subdirectory(std-tsearch)
add_subdirectory(stm)
else()
endif()
endif()
86 changes: 58 additions & 28 deletions better_gets/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,55 @@

int childProcesses[MAX_PID];

void addProcess(int childPid) {
void addProcess(int childPid)
{
printf("add(%d)\n", childPid);
for(int i = 0; i < MAX_PID; i++) {
if(childProcesses[i] == (-1)) {
for(int i = 0; i < MAX_PID; i++)
{
if(childProcesses[i] == (-1))
{
childProcesses[i] = childPid;
printf("added child pid = %d\n", childPid);
return;
}
}
}

void removeProcess(int childPid) {
void removeProcess(int childPid)
{
printf("remove(%d)\n", childPid);
for(int i = 0; i < MAX_PID; i++) {
if(childProcesses[i] == childPid) {
for(int i = 0; i < MAX_PID; i++)
{
if(childProcesses[i] == childPid)
{
childProcesses[i] = (-1);
printf("removed child pid = %d\n", childPid);
}
}
}

int countProcesses() {
int countProcesses()
{
int count = 0;
for(int i = 0; i < MAX_PID; i++) {
if(childProcesses[i] >= 0) {
for(int i = 0; i < MAX_PID; i++)
{
if(childProcesses[i] >= 0)
{
count += 1;
}
}
return count;
}

void clean_up_child_process(int signal_number) {
void clean_up_child_process(int signal_number)
{
printf("SIGCHLD recieved.\n");
int status;
pid_t pid;
while((pid = waitpid(-1, &status, WNOHANG)) > 0) {
if(pid > 0) {
while((pid = waitpid(-1, &status, WNOHANG)) > 0)
{
if(pid > 0)
{
printf("pid %d exited\n", pid);
removeProcess((int)pid);
}
Expand All @@ -56,36 +68,45 @@ void clean_up_child_process(int signal_number) {

char input[12];

int read_from_stdin() {
int read_from_stdin()
{
char buffer;
int error;
while((error = read(0, &buffer, 1)) == (-1)) {
if(error == 0) {
while((error = read(0, &buffer, 1)) == (-1))
{
if(error == 0)
{
return EOF;
}
}
return buffer;
}

void better_gets(char *buff, int len) {
void better_gets(char *buff, int len)
{
int i;
for(i = 0; i < len-1; i++) {
for(i = 0; i < len-1; i++)
{
int val = read_from_stdin();
if(val == EOF) {
if(val == EOF)
{
break;
}
buff[i] = val;
if(buff[i] == '\n') {
if(buff[i] == '\n')
{
break;
}
}
buff[i] = (char)0;
//printf("read: [%s]\n", buff);
}

void parent() {
void parent()
{
printf("parent continuing....\n");
for(int i = 0; i < 5; i++) {
for(int i = 0; i < 5; i++)
{
printf(">");
better_gets(&input[0], 12);
printf("you typed: %s\n", &input[0]);
Expand All @@ -94,15 +115,18 @@ void parent() {
printf("parent exiting\n");
}

void child() {
void child()
{
printf("child starting.\n");
sleep(5);
printf("child exiting.\n");
exit(EXIT_SUCCESS);
}

int main(int argc, char** argv) {
for(int i = 0; i < MAX_PID; i++) {
int main(int argc, char** argv)
{
for(int i = 0; i < MAX_PID; i++)
{
childProcesses[i] = (-1);
}

Expand All @@ -111,14 +135,20 @@ int main(int argc, char** argv) {
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);

for(int i = 0; i < 5; i++) {
for(int i = 0; i < 5; i++)
{
int childProcess = fork();
if(childProcess == 0) {
if(childProcess == 0)
{
child();
return 0;
} else if(childProcess > 0) {
}
else if(childProcess > 0)
{
addProcess(childProcess);
} else {
}
else
{
printf("fork failed\n");
}
}
Expand Down
12 changes: 8 additions & 4 deletions bounded-buffer/bboptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ int bb_options_get(bb_options_t* options, int argc, char **argv)
options->gen_count = GEN_COUNT;
options->bsize = BB_SIZE;

while (1) {
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
static struct option long_options[] =
{
{"suppliers", required_argument, 0, 's' },
{"consumers", required_argument, 0, 'c' },
{"sdelay", required_argument, 0, 'x' },
Expand All @@ -37,7 +39,8 @@ int bb_options_get(bb_options_t* options, int argc, char **argv)
if (c == -1)
break;

switch (c) {
switch (c)
{
case 'b':
options->bsize = atoi(optarg);
break;
Expand Down Expand Up @@ -93,7 +96,8 @@ int bb_options_get(bb_options_t* options, int argc, char **argv)

}

void bb_options_print(bb_options_t* options) {
void bb_options_print(bb_options_t* options)
{
printf("options { suppliers: %d, consumers: %d, sdelay: %d, cdelay: %d, gen count: %d, bsize: %d }\n",
options->no_suppliers, options->no_consumers, options->supplier_max_delay_ms, options->consumer_max_delay_ms, options->gen_count, options->bsize);

Expand Down
3 changes: 2 additions & 1 deletion bounded-buffer/bboptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#define BB_SIZE 100
#define GEN_COUNT 2000

typedef struct {
typedef struct
{
int no_suppliers;
int no_consumers;
int supplier_max_delay_ms;
Expand Down
58 changes: 35 additions & 23 deletions bounded-buffer/bbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include <stdlib.h>

void bounded_buffer_init(bounded_buffer_t* bb, int size) {
void bounded_buffer_init(bounded_buffer_t* bb, int size)
{
bb->entries = (entry_t**) malloc(size * sizeof(entry_t*));
for (int i=0; i < size; i++)
bb->entries[i] = NULL;
Expand All @@ -13,10 +14,12 @@ void bounded_buffer_init(bounded_buffer_t* bb, int size) {
pthread_cond_init (&bb->has_items, NULL);
}

void bounded_buffer_put(bounded_buffer_t* bb, entry_t* item) {
void bounded_buffer_put(bounded_buffer_t* bb, entry_t* item)
{
pthread_mutex_lock(&bb->lock);

while (bb->tail - bb->head >= bb->size) {
while (bb->tail - bb->head >= bb->size)
{
pthread_cond_wait(&bb->has_space, &bb->lock);
}

Expand All @@ -26,10 +29,12 @@ void bounded_buffer_put(bounded_buffer_t* bb, entry_t* item) {
pthread_mutex_unlock(&bb->lock);
}

entry_t* bounded_buffer_get(bounded_buffer_t* bb) {
entry_t* bounded_buffer_get(bounded_buffer_t* bb)
{
pthread_mutex_lock(&bb->lock);

while (bb->tail == bb->head) {
while (bb->tail == bb->head)
{
pthread_cond_wait(&bb->has_items, &bb->lock);
}

Expand All @@ -41,16 +46,19 @@ entry_t* bounded_buffer_get(bounded_buffer_t* bb) {
return entry;
}

int bounded_buffer_count(bounded_buffer_t* bb) {
int bounded_buffer_count(bounded_buffer_t* bb)
{
return bb->tail - bb->head;
}

void bounded_buffer_cleanup(bounded_buffer_t* bb) {
void bounded_buffer_cleanup(bounded_buffer_t* bb)
{
int unfreed_count = 0;
for (int i=0; i < bb->size; i++)
if (bb->entries[i] != NULL)
unfreed_count++;
if (unfreed_count > 0) {
if (unfreed_count > 0)
{
lwlog_info("Warning: %d entries in bounded buffer not freed\n", unfreed_count);
}
free(bb->entries);
Expand All @@ -59,19 +67,23 @@ void bounded_buffer_cleanup(bounded_buffer_t* bb) {
pthread_cond_destroy(&bb->has_items);
}

void bounded_buffer_print_info(bounded_buffer_t* bb) {
printf("buffer { size: %d, length: %d, head: %d, tail: %d, ", bb->size, bb->tail - bb->head, bb->head, bb->tail);
printf("entries : [");
int add_comma = 0;
for (int i=bb->head; i < bb->tail; i++) {
if (add_comma) {
printf(", %d", bb->entries[i % bb->size]->value);
}
else {
printf("%d", bb->entries[i % bb->size]->value);
}
add_comma = 1;
}
printf("]");
printf(" }\n");
void bounded_buffer_print_info(bounded_buffer_t* bb)
{
printf("buffer { size: %d, length: %d, head: %d, tail: %d, ", bb->size, bb->tail - bb->head, bb->head, bb->tail);
printf("entries : [");
int add_comma = 0;
for (int i=bb->head; i < bb->tail; i++)
{
if (add_comma)
{
printf(", %d", bb->entries[i % bb->size]->value);
}
else
{
printf("%d", bb->entries[i % bb->size]->value);
}
add_comma = 1;
}
printf("]");
printf(" }\n");
}
6 changes: 4 additions & 2 deletions bounded-buffer/bbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#define LOG_COLOR (1) // 0: off, 1: on, default: 1
#define LOG_LEVEL (7) // -1: off, 0~7: different log levels, default: 7

typedef struct {
typedef struct
{
int value;
} entry_t;

typedef struct {
typedef struct
{
int size;
entry_t** entries;
int head;
Expand Down
Loading

0 comments on commit 6529b89

Please sign in to comment.