Skip to content

Commit

Permalink
added task1 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspirisha committed Mar 5, 2016
1 parent 1ecedfc commit 8aeea85
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
22 changes: 20 additions & 2 deletions tasks/linked_lists/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static void __init test_stack(void)
stack_entry_t *tos = NULL;
const char *tos_data = NULL;
const char* test_data[] = { "1", "2", "3", "4" };
unsigned long i = 0;
long i = 0;

pr_alert("Testing basic stack");

Expand All @@ -36,7 +36,25 @@ static void __init test_stack(void)

static void __init print_processes_backwards(void)
{
// TODO
struct task_struct *task_list;
LIST_HEAD(proc_stack);
long counter = 0;
const char *process_name = NULL;
stack_entry_t *stack_top = NULL;

for_each_process(task_list)
{
stack_push(&proc_stack, create_stack_entry((void*)(task_list->comm)));

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

Not sooo fast :)
You can't save pointers from global kernel tasks list to your local data structure.
You should copy name of each task on each iteration into your not global buffer.

like this:
50 for_each_process(tsk) {
51 char comm_buf = kmalloc(sizeof(tsk->comm), GFP_KERNEL);
52 if (!comm_buf) {
53 ret = -ENOMEM;
54 break;
55 }
56
57 tos_tsk = create_stack_entry((void
)get_task_comm(comm_buf, tsk));
58 if (!tos_tsk) {
59 kfree(comm_buf);
60 ret = -ENOMEM;
61 break;
62 }
63 stack_push(&process_stack, tos_tsk);
64 }

Also you should use mutual exclusive function get_task_comm for copying of the name.
Also you need to handle all the allocation errors kmalloc.
like it is written here: http://mit.spbau.ru/sewiki/index.php/Linux_kernel_2016#.D0.9E.D0.B1.D1.80.D0.B0.D0.B1.D0.BE.D1.82.D0.BA.D0.B0_.D0.BE.D1.88.D0.B8.D0.B1.D0.BE.D0.BA

counter++;
}

for (; counter > 0; counter--)
{
stack_top = stack_pop(&proc_stack);

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

You should call delete_stack_entry() on each stack entry created using create_stack_entry!
:(
And you should delete your local task command name buffers. If you had them...

process_name = STACK_ENTRY_DATA(stack_top, const char*);
printk(KERN_ALERT "%s\n", process_name);
}

}

static int __init ll_init(void)
Expand Down
27 changes: 16 additions & 11 deletions tasks/linked_lists/stack.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
#include <linux/slab.h>
#include "stack.h"

stack_entry_t* create_stack_entry(void *data)
{
// TODO
(void)data;
return NULL;
stack_entry_t *entry = kmalloc(sizeof(stack_entry_t), GFP_KERNEL);
if (!entry)
return 0;
entry->data = data;

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

INIT_LIST_HEAD(&entry->lh);

return entry;
}

void delete_stack_entry(stack_entry_t *entry)
{
// TODO
(void)entry;
kfree(entry);
}

void stack_push(struct list_head *stack, stack_entry_t *entry)
{
// TODO
(void)stack;
(void)entry;
list_add(&(entry->lh), stack);
}

stack_entry_t* stack_pop(struct list_head *stack)
{
// TODO
(void)stack;
return NULL;
struct list_head *ret_val = stack->next;

if (stack_empty(stack))
return 0;

list_del_init(ret_val);

return list_entry(ret_val, struct stack_entry, lh);
}

0 comments on commit 8aeea85

Please sign in to comment.