unable to use fork() in recursion? [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 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.

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);

correct kill syscall linux usage pattern [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 7 years ago.
Improve this question
man for int kill(pid_t pid, int sig); says:
If pid equals -1, then sig is sent to every process for which the
calling process has permission to send signals, except for process 1
(init), but see below.
Does this mean that if my program is run with root permissions and it accidentally (due to memory corruption or a hack) provides -1 as pid argument - this will cause a complete DoS for the entire system?
If so, is it recommended to always perform a double check for the pid argument value before calling this potentially disasterous call? (just sayin')
Does this mean that if my program is run with root permissions and it
accidentally (due to memory corruption or a hack) provides -1 as pid
argument - this will cause a complete DoS for the entire system?
Yes, such a scenario is possible. But the chances of it happening is very less. Because no program that run with root permissions would do such a thing. If a malicious user/binary has somehow got gained root privilege, then sending signals is just one of the problems.
If so, is it recommended to always perform a double check for the pid
argument value before calling this potentially disastrous call?
That's just super paranoid thinking. There are thousands of ways to do disastrous activities.You might as well worry about:
What if there's not malicious daemon, ran at system startup, does:
kill(-1, SIGKILL);
How do you know if library function you make wouldn't call reboot(2) and reboot your system?
etc.
Besides, PIDs are not just user provided values that need to be sanitized. PIDs are mostly values acquired within the program using system calls or library calls. So the chances of "accidentally" using -1 is zero. Basically, you someone/program has root privilege and decided to screw your system then there's not much you can do.

Data flow control in inter-process communication in 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 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

How to know whether a running application has exited and based on that do something in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do i know that a process [not called by c] has exited and based on that do something in C?
For eg. there is a running application , say notepad. I create an application to delete the text file created by it. I cannot do that while it is open . So how do I know when notepad exits and based on that take a decision in C.,.,
If you know the PID of the process, you can use the kill() function. Sending signal 0 to the process is guaranteed not to kill it, but merely to report on whether it is running.
If you don't know the PID of the process, there is often a pidfile on disk for the process concerned (e.g. /var/run/processname.pid) - look at the file that starts it for more information. If this PID file is not there, the process should not be running. If it is there, the file contains (as text) the PID of the process, which you can check using the method above.
If you do not know the PID of the process and it does not have a PID file, then you will have to walk /proc, or shell out to pgrep or similar. Avoid this if possible.

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