1

I have defined a stack, here is part of its definition in the header file:

typedef struct item {
    char *path;
    struct item *below;
} item_t;

struct forward_dir_stack {
    int _curr_size;
    item_t *top;
};

struct forward_dir_stack *create();
void push(struct forward_dir_stack *stack, char *path);
item_t *pop(struct forward_dir_stack *stack);
...

and I have two C programs foo.c and boo.c. Let's say that boo.c pushes something to the stack and foo.c pops something out of the stack.

I want to compile these two programs (and move them to /bin) and use them as two separated bash commands.

$ boo  # pushes something in stack (in the same terminal)
$ foo  # pops something from the stack (in the same terminal)

My goal is to get these two programs to share forward_dir_stack with each other in the same terminal they are running. (and of course, when new terminal is opened, another separate stack will be created and boo foo running in this terminal will work with this stack)

First idea that comes to my mind is to create a temporary file which will contain shell PID in the name so boo or foo will know where to push/pop. (also this means I have to re-implement the stack)

But isn't there more elegant/easy way how to share data structure between C programs in the same shell (and behave independently when running in another shell)? Somehow take advantage of the fact that foo and boo run in the same shell.

nikromen
  • 73
  • 3

0 Answers0