02-02-2011
Quote:
Originally Posted by
TWhitt24
I understand that. My question is how do I get the parent to fork and not the child.
Don't call fork() in the child if you don't want the child to fork. Your process gets told whether it's in the child by the return value of fork(), and can use that to decide whether to fork or not via if-statements like above. If you don't know how to use an if statement, refer to the above example, or post your own code so I can modify it with the appropriate logic.
10 More Discussions You Might Find Interesting
1. Programming
When I compile this C programme I get different outputs each time I run it
Please explain to me whats happening in the code if you can give me a detailed explanation. Because I am stuck with this.
#include <stdio.h>
main(){
int i = 0;
printf("I am the... (1 Reply)
Discussion started by: manjuWicky
1 Replies
2. UNIX for Advanced & Expert Users
When I compile this C programme I get different outputs each time I run it
Please explain to me whats happening in the code if you can give me a detailed explanation with the schedular functionality it will help a lot. Because I am stuck with this.
#include <stdio.h>
main(){... (3 Replies)
Discussion started by: manjuWicky
3 Replies
3. UNIX for Dummies Questions & Answers
Hi everybody,
I'm having some problems wiriting a program in UNIX using the "fork" and "kill" system calls.
I have to create a C program P0, which creates 9 other processes
P1, P2, ..., P9, where P0 is the father of P1, P1 the father of P2, and so on.
All the processes contain an infinite... (0 Replies)
Discussion started by: davewilliams20
0 Replies
4. Programming
Hi
I'm currently working with C on UNIX (HPUX) and need to be able to fork a seperate Java process from within a running C process.
I can run the following code from the command line via a script but am having difficulty getting it to work from within the code.
I am trying to use execl. Is... (4 Replies)
Discussion started by: themezzaman
4 Replies
5. Programming
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pID;
int i;
for (i = 0; i < 3; i++)
{
pID = fork ();
if (pID == 0)
{
printf ("Value of i --> %d... (2 Replies)
Discussion started by: kymthasneem
2 Replies
6. Shell Programming and Scripting
hi experts(novice people can stay away as it is no child's game),
i am developing a script which works like recycle bin of windows.
the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies
7. UNIX for Advanced & Expert Users
hi,
I want my program to fork a new process and then I want to kill the parent process. The parent program before dying will issue a SIGTERM to all its childs. Which eventually kills all Children.
I cant handle the SIGTERM at the child level.:(
What I was thinking of was the Parent... (3 Replies)
Discussion started by: tyler_durden
3 Replies
8. Shell Programming and Scripting
Keep in mind that I haven't done Perl scripting for a LONG time, so I'm quite rusty.
This is what I would like to do:
- using fork, create 3 or 4 processes to read 3 or 4 different text documents containing server names or IP addresses
- in each of those processes, Perl will ping each of those... (7 Replies)
Discussion started by: kooshi
7 Replies
9. Shell Programming and Scripting
Hi everyone
i am very new to linux , working on bash shell.
I am trying to solve the given problem
1. Create a process and then create children using fork
2. Check the Status of the application for successful running.
3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies
10. Programming
I have an input file with contents like:
5785690|68690|898809
7960789|89709|789789
7669900|87865|659708
7869098|65769|347658
so on..
I need to pass this file to 10 parallely running processes (forking)so that each line is processed by a process and no line is processed twice and write the... (1 Reply)
Discussion started by: rkrish
1 Replies
VFORK(2) BSD System Calls Manual VFORK(2)
NAME
vfork -- spawn new process in a virtual memory efficient way
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <unistd.h>
pid_t
vfork(void);
DESCRIPTION
The vfork system call creates a new process that does not have a new virtual address space, but rather shares address space with the parent,
thus avoiding potentially expensive copy-on-write operations normally associated with creating a new process. It is useful when the purpose
of fork(2) would have been to create a new system context for an execve(2). The vfork system call differs from fork(2) in that the child
borrows the parent's memory and thread of control until a call to execve(2) or an exit (either by a call to _exit(2) or abnormally). The
parent process is suspended while the child is using its resources.
The vfork system call returns 0 in the child's context and (later) the pid of the child in the parent's context.
The vfork system call can normally be used just like fork(2). It does not work, however, to return while running in the childs context from
the procedure that called vfork() since the eventual return from vfork() would then return to a no longer existent stack frame. Be careful,
also, to call _exit(2) rather than exit(3) if you can't execve(2), since exit(3) will flush and close standard I/O channels, and thereby mess
up the standard I/O data structures in the parent process. (Even with fork(2) it is wrong to call exit(3) since buffered data would then be
flushed twice.)
RETURN VALUES
Same as for fork(2).
ERRORS
Same as for fork(2).
SEE ALSO
execve(2), fork(2), sigaction(2), wait(2)
HISTORY
The vfork() function call appeared in 3.0BSD. In 4.4BSD, the semantics were changed to only suspend the parent. The original semantics were
reintroduced in NetBSD 1.4.
BUGS
Users should not depend on the memory sharing semantics of vfork() as other ways of speeding up the fork process may be developed in the
future.
To avoid a possible deadlock situation, processes that are children in the middle of a vfork() are never sent SIGTTOU or SIGTTIN signals;
rather, output or ioctl(2) calls are allowed and input attempts result in an end-of-file indication.
BSD
January 3, 1998 BSD