简体   繁体   中英

c return value through parameters

So, I'm puzzled with this issue for over an hour.

Background:

I have an implementation of kernel threads in xv6 that I want to test.

The threads communicate return values via a field ret_val.

Each thread saves the return value in the other thread's ret_val (because it can technically be de-allocated after returning a value).

I have two parts in my code that do not work as I'd expect.

*Notes: the field proc->ret_val is of type void** the field proc->has_waiting is of type struct proc *

Part 1 (this is the part that stores the return value in the process's struct):

// store value in WAITING THREAD's ret_val.
    (*proc->has_waiting->ret_val) = (void*)ret_val;
    cprintf("(t_exit)process %d is taking the return value %s\n", proc->pid, (char *)ret_val);
    cprintf("(t_exit)process %d now has return value from %d -> %s\n", proc->has_waiting->pid, proc->pid, (char *)(*proc->has_waiting->ret_val));

This ^ part's job is to store a value in a process's ret_val (inside the "has_waiting" field, which is a pointer to a structure of a process).

This seems to work because the prints indicate the value is indeed saved.

Part 2 (this is the part that tries to read the process's struct ret_val field):

cprintf("(t_join) process %d is taking the return value %s\n", proc->pid, (char *)(*proc->ret_val));
            * ret_val = proc->ret_val; // it's t's duty to set proc's ret_val

This ^ part's job is to restore the value from within the struct's structure (ret_val field) before it's destroyed.

Part 2 does not work, the ret_val field is empty.

I've tried all sorts of casting manipulations but it seems I misunderstand a basic concept here.

I've verified the struct I'm addressing is the correct struct by printing out it's id (unique).

The value that I'm passing (in ret_val) is a static string (char*) I've defined in the main function that creates the processes (I wanted to make sure it's not destroyed or something).

I'd appreciate any help. If further information is needed let me know.

If I am reading this correctly, you "return" the value via this field:

proc->has_waiting->ret_val

but you reference it via this field:

proc->ret_val

You should be using the same reference in both places.

EDIT: How about simplifying this and making ret_val a char * ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM