fork() and child processes


 
Thread Tools Search this Thread
Top Forums Programming fork() and child processes
# 1  
Old 03-19-2008
fork() and child processes

Hello,

How many child processes are actually created when running this code ?

Code:
#include <signal.h>
#include <stdio.h>

int main () {
int i ;
setpgrp () ;
for (i = 0; i < 10; i++) {
if (fork () == 0) {
if ( i & 1 ) setpgrp () ;
printf ("Child id: %2d, group: %2d\n", getpid(), getpgrp()) ;
sleep (60) ;
exit (0) ;
}
}
kill (0, SIGINT) ;
return 0 ;
}


Last edited by Yogesh Sawant; 03-19-2008 at 09:50 AM.. Reason: added code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making processes using fork

Can anyone help me with this? Create a parent process that gets from the command line n arguments arg1, arg2, ... , argn. The parent will create n/3 son processes, each of them will create a file with the name argi by concatenate the files argi+1 and argi+2. How can i concatenate those... (1 Reply)
Discussion started by: bunicu01
1 Replies

2. Programming

Creating more processes with fork()

Hello people I need help How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work. Thanks (8 Replies)
Discussion started by: nekoj
8 Replies

3. Shell Programming and Scripting

fork processes

Hi, How to count how many processes opened by fork function in perl. Thanks (10 Replies)
Discussion started by: Anjan1
10 Replies

4. Programming

Generating Random Number in Child Process using Fork

Hello All, I am stuck up in a program where the rand functions ends up giving all the same integers. Tried sleep, but the numbers turned out to be same... Can anyone help me out how to fix this issue ? I have called the srand once in the program, but I feel like when I call fork the child process... (5 Replies)
Discussion started by: manisum
5 Replies

5. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

6. UNIX for Dummies Questions & Answers

Conserving processes: execve() vs fork()

Disclaimer: This is just a rainy day experiment. There is no expected "goal" other than to understand UNIX better. After reading about fork and exec, my understanding is that forking, as the UNIX shell does by design, consequentially may sacrafice some speed versus an approach that runs in... (1 Reply)
Discussion started by: uiop44
1 Replies

7. Programming

Problems with child comunicating with parent on fork()

Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks. The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing. 1)create a... (8 Replies)
Discussion started by: Zarnick
8 Replies

8. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

9. Programming

fork() with su (child with other user)

Hi all, i need to execute a program from within my c++ code. This is no problem. system(), fork(), execxy(). But now i want to able to execute the program as another user as the parent process. The whole thing is on solaris. I should be possible for both, users with no shell and no password... (1 Reply)
Discussion started by: heck
1 Replies

10. Programming

Reference Variables To A Child Process Created With Fork

Hi! IN THE FOLLOWING PROGRAM THE VALUE OF j REMAINS UNCHANGED . WHY ? IF I WANT A VARIABLE VALUE TO CHANGE LIKE THIS , IS THERE ANY WAY TO DO IT ? Or do we have to use shared memory variables? main() { int return_pid, i, total; int j=1; total = TOTALRECS+1; for... (2 Replies)
Discussion started by: AJAY BHATIA
2 Replies
Login or Register to Ask a Question
FORK(2) 						      BSD System Calls Manual							   FORK(2)

NAME
fork -- create a new process SYNOPSIS
#include <sys/types.h> #include <unistd.h> pid_t fork(void); DESCRIPTION
Fork() causes creation of a new process. The new process (child process) is an exact copy of the calling process (parent process) except for the following: o The child process has a unique process ID. o The child process has a different parent process ID (i.e., the process ID of the parent process). o The child process has its own copy of the parent's descriptors. These descriptors reference the same underlying objects, so that, for instance, file pointers in file objects are shared between the child and the parent, so that an lseek(2) on a descriptor in the child process can affect a subsequent read or write by the parent. This descriptor copying is also used by the shell to establish standard input and output for newly created processes as well as to set up pipes. o The child processes resource utilizations are set to 0; see setrlimit(2). RETURN VALUES
Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of -1 is returned to the parent process, no child process is created, and the global variable errno is set to indicate the error. ERRORS
Fork() will fail and no child process will be created if: [EAGAIN] The system-imposed limit on the total number of processes under execution would be exceeded. This limit is configuration- dependent. [EAGAIN] The system-imposed limit MAXUPRC (<sys/param.h>) on the total number of processes under execution by a single user would be exceeded. [ENOMEM] There is insufficient swap space for the new process. SEE ALSO
execve(2), wait(2) HISTORY
A fork() function call appeared in Version 6 AT&T UNIX. 4th Berkeley Distribution June 4, 1993 4th Berkeley Distribution