Data flow control in inter-process communication in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have three modules(2 C executable+ 1 python script). I need three processes-one parent, two child processes. All of them won't stop until they see an End of file. They just keep running after execution.
Problem just arises here. Parent process needs the output of child process 1, and child process 2 needs the output of parent process. The order is: child process 1 outputs result and sleep for 5 seconds before next round of output-> parent process gets that output as input and output its own result -> child process 2 gets its parent's result and output final result to standard output. This is only one iteration. This loop will be repeated again and again until seeing an End of file.
I don't know how to make parent process wait for child process 1's each iterations output and forward it to child process 2. So as child process waits for parent process. I considered wait(), but that needs child process 1 to stop. But actually, none of those three process stops before End of file.
So, is there any good mechanism to implement this scenario?

You need to implement a pipe mechanism between the processes. Suggest you to go though the man pages of pipe(), dup() and also the link here for more info

Related

Creating child process in C linux [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Create child process by using fork() function.
The parent process runs change content of the process by execl() function which run cat f1.c command.
child process runs a traceroute www.google.com command.
Before asking questions here, please try it on your own and post what you have tried so far so we can guide you in the right direction. Also, it would be nice if you put more effort in asking better question. But to give you some guidance:
you can create child process by using fork. It returns an integer. If it is zero, that means you are in the child process. so you can do something like:
int pid;
if((pid=fork())==0){
// you are in child process
//use execl(constant char *path, constant char *commands); to run your commands
}
else {
//whatever you need to do in the parent process
}
You can find about execl() here :https://www.systutorials.com/docs/linux/man/3-execl/ It is basically a way to run a command. The first argument is a constant char pointer which points to the shell that you want to run the command in ("/bin/sh" etc.). The next arguments are the command it self ("cd", "mydir" etc.) terminated with null.
execl("/bin/sh","cd","mydir",NULL);

Fork System call in linux using c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
What if I'm writing this??
I got both of the printf statements executed!!
#include<stdio.h>
#include<stdlib.h>
int main(void){
if(fork())
printf("entering IF");
else
printf("entering ELSE");
return 0;
}
What exactly are you asking?
fork() duplicates the calling process, returning the child process' PID in the parent, and 0 in the child.
In C, if evaluates any non-zero value to true, meaning entering IF will be printed from the parent thread, and entering ELSE from the child thread.
It should be noted that order in which they are printed is nondeterministic, so you may see entering ELSE before entering IF.
See: man(2) fork
The fork system call return value as per manual pages is
Return Value
On success, the PID of the child process is returned in the parent, and 0 is
returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
So, the if statement is executed by the parent process and where as the else is executed by the child process.
The fork() creates a child process by duplicating the calling process. The process that invokes fork() is the parent process and the newly created process is the child process. So fork() splits a process in two, and returns 0 to the child process and the PID of the child process to the parent process, or -1 if the fork failed.
In the context of your program:
if(fork())
printf("entering IF");
else
printf("entering ELSE");
the parent process will print entering IF and child process will print entering ELSE.
The child process and the parent process run separately. So, you cannot determine which process gets schedules first but there are ways to synchronize them. For e.g. make parent process wait until child finishes, check this.
Additional:
Linux kernel (2.6.23 and later) provides a tunable paramter - sched_child_runs_first.
From this:
/proc/sys/kernel/sched_child_runs_first (since Linux 2.6.23)
If this file contains the value zero, then, after a fork(2),
the parent is first scheduled on the CPU. If the file con‐
tains a nonzero value, then the child is scheduled first on
the CPU. (Of course, on a multiprocessor system, the parent
and the child might both immediately be scheduled on a CPU.)

How to check if child process is finished in c by its pid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am building a very tiny shell in c,
I have the option on running programs in the background.
I keep a list of all my jobs meaning all the ones in the bg.
Now if i want to go and update this list, how can i check is a process is finished or if its still running.
ps
if i waited with waitpid for some process, will i still be able to check if the process is done? (i mean if i used waitpid it took the process of zombie state.
You should be able to call waitpid, passing it the process id and the WNOHANG option and call the WIFEXITED macro on the integer returned through status argument. See Just check status process in c.

unable to use fork() in recursion? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have got programming assignment to merge to arrays using binary insertion search(ie., in sorted order) using fork() system call in C in linux based OS.
I have done it WITHOUT using fork() and its working fine. Now I want to use fork() system call in it. I have read about fork() system call but, I am unable to use it in my program. Please help me out!
You could use fork() to have a child process execute each binary search and have the child return the insertion point to the parent through a pipe (or the child's exit status if the array will always be 255 elements or smaller).
That's the only thing I can see that makes any sense at all and you'd never do this in the real world -- only as an exercise to get a feel for fork().
Basically, you'd call fork() right before binSearch(). The child would execute the binSearch() and return the insertion point through a pipe (or its exit code). Meanwhile, the parent would wait for the insertion point result and then perform the insert.
A way to use fork() that might make a bit more sense would be to break the array to be sorted in half, with each half to be recursively sorted by a different child process that communicates their sorted half back through a pipe that only goes to their parent. The parent merges the two sorted halves together using merge sort and communicates the result up to its parent. The ultimate parent outputs the sorted array. That will exploit parallelism but in exchange for significant amounts of inter-process communication.

C program to create 3 child processes which read from 3 different files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have this question how to a write a C program that creates 3 child processes which read from 3 different files and write to the parent process using pipes.
Each child should wait a random amount of time (3 -10 seconds) between writing each 50 characters.
The father should read from pipes and
write everything he gets (from all 3 files) into one new file.
To expand on the answer by Rachit Jain, you could create an array with the filenames, and use the loop counter as an index into the array to know which file each child process should open. Works best when each child process should process the files the same, just do it in parallel.
As for the pipes, you really need three different pipes, one per child process, or the data from the children might become mixed. Use e.g. select or poll to check for input from the different pipes.
Read Advanced Linux Programming first.
You first need to create the 3 pipes with the pipe(2) syscall (repeated 3 times).
You then need to create the 3 child processes with the fork(2) syscall. Handle the 3 possible return values of fork: <0 on failure, ==0 in child, >0 in parent. Remember the pid_t in parent. In each child call dup2(2) to have the STDOUT_FILENO be the output of relevant pipe.
At last, in the parent, make a simplistic event loop, using the poll(2) multiplexing syscall to find out which pipe should be read(2)
Use sleep(3) and random(3) in the child, but don't forget to seed -using srand- the PRNG with something random (like its pid gotten by getpid(2) added to the current time(2)....; or use /dev/urandom see urandom(4))
As Jonathan Leffler commented, since 3*50 is less than PIPE_MAX, you might perhaps use a single pipe. I feel that using an event loop is more safe and more general (it will work if you replace 50 by 500000 which is greater than PIPE_MAX).
I don't think I want or have time to do more of your homework for you. I gave you enough hints above.
I believe that Federos Koros fedoroskoros#gmail.com is very wrong in trying to pay someone to do his homework, and I did sent him a private email about that. The question is now deleted.
fork () three times in a loop with a variable ,say i, and below put conditions for i=0 , i=1 and i=2. in each each condition read one of your file.
rest won't be tough task either now.

Resources