Rectangle 27 2

Listen for events from child from child to parent using an output

Parent-child component communication can be achieved in a couple of different ways.

For this strategy, use an @Input() decorator in the child component and input-binding syntax [] in your template to pass data in.

If you need to intercept these property in the child, use a setter function for @Input(), or use the lifecycle hook which responds to input properties changes, named ngOnChanges.

In this case, you child component will have to expose an EventEmitter decorated as @Ouput(); for example @Output() foo = new EventEmitter(). Then you will be able to listen for those events from the parent in the template using the event-binding syntax (foo).

To query a child or multiple children from the parent without children having to explicitly know about this behaviour, use ViewChild and ViewChildren decorators.

This is the most global case, and can be used for any kind of communication. Components do not have to be in parent-child relationship: they could be siblings, or grandchildren/grandparents. To use the same instance of a service in both component that need to communicate, you need to use Angular's dependency injection (DI) mechanism, via components' constructors.

You can read more about these types of component communication in the official guide Component interaction in Angular. You can find a lot of examples there, including on the links I've linked through the answer.

What is the best way to use nested components in Angular 4 and calling...

angular
Rectangle 27 15

pid_t pid = 0;
int pipefd[2];
FILE* output;
char line[256];
int status;

pipe(pipefd); //create a pipe
pid = fork(); //span a child process
if (pid == 0)
{
// Child. Let's redirect its standard output to our pipe and replace process with tail
 close(pipefd[0]);
 dup2(pipefd[1], STDOUT_FILENO);
 dup2(pipefd[1], STDERR_FILENO);
 execl("/usr/bin/tail", "/usr/bin/tail", "-f", "path/to/your/file", (char*) NULL);
}

//Only parent gets here. Listen to what the tail says
close(pipefd[1]);
output = fdopen(pipefd[0], "r");

while(fgets(line, sizeof(line), output)) //listen to what tail writes to its standard output
{
//if you need to kill the tail application, just kill it:
  if(something_goes_wrong)
    kill(pid, SIGKILL);
}

//or wait for the child process to terminate
waitpid(pid, &status, 0);

Do I need to close(pipefd[0]) after I am done reading at the end? or right before kill?

Incidentally, if you use GLib, there's a pre-baked implementation of this with tons of features, plus it handles all the errors codes and so on. See developer.gnome.org/glib/2.30/glib-Spawning-Processes.html (disclaimer: I implemented this, though many years ago)

@havoc or libHX's HXproc_* family of functions, which does not use functions with that awkardly-many parameters as glib.

sure, just mentioning the library I know.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

c - popen() alternative - Stack Overflow

c exec process popen
Rectangle 27 15

pid_t pid = 0;
int pipefd[2];
FILE* output;
char line[256];
int status;

pipe(pipefd); //create a pipe
pid = fork(); //span a child process
if (pid == 0)
{
// Child. Let's redirect its standard output to our pipe and replace process with tail
 close(pipefd[0]);
 dup2(pipefd[1], STDOUT_FILENO);
 dup2(pipefd[1], STDERR_FILENO);
 execl("/usr/bin/tail", "/usr/bin/tail", "-f", "path/to/your/file", (char*) NULL);
}

//Only parent gets here. Listen to what the tail says
close(pipefd[1]);
output = fdopen(pipefd[0], "r");

while(fgets(line, sizeof(line), output)) //listen to what tail writes to its standard output
{
//if you need to kill the tail application, just kill it:
  if(something_goes_wrong)
    kill(pid, SIGKILL);
}

//or wait for the child process to terminate
waitpid(pid, &status, 0);

Do I need to close(pipefd[0]) after I am done reading at the end? or right before kill?

Incidentally, if you use GLib, there's a pre-baked implementation of this with tons of features, plus it handles all the errors codes and so on. See developer.gnome.org/glib/2.30/glib-Spawning-Processes.html (disclaimer: I implemented this, though many years ago)

@havoc or libHX's HXproc_* family of functions, which does not use functions with that awkardly-many parameters as glib.

sure, just mentioning the library I know.

c - popen() alternative - Stack Overflow

c exec process popen