Sponsored Content
Full Discussion: fork() and child processes
Top Forums Shell Programming and Scripting fork() and child processes Post 302176823 by green_dot on Wednesday 19th of March 2008 08:42:11 AM
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:47 AM.. Reason: added code tags
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Programming

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", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SETPGID(2)						     Linux Programmer's Manual							SETPGID(2)

NAME
setpgid, getpgid, setpgrp, getpgrp - set/get process group SYNOPSIS
#include <unistd.h> int setpgid(pid_t pid, pid_t pgid); pid_t getpgid(pid_t pid); pid_t getpgrp(void); /* POSIX.1 version */ pid_t getpgrp(pid_t pid); /* BSD version */ int setpgrp(void); /* System V version */ int setpgrp(pid_t pid, pid_t pgid); /* BSD version */ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): getpgid(): _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L setpgrp() (POSIX.1): _SVID_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED setpgrp() (BSD), getpgrp() (BSD): _BSD_SOURCE && ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE || _XOPEN_SOURCE_EXTENDED || _GNU_SOURCE || _SVID_SOURCE) DESCRIPTION
All of these interfaces are available on Linux, and are used for getting and setting the process group ID (PGID) of a process. The pre- ferred, POSIX.1-specified ways of doing this are: getpgrp(void), for retrieving the calling process's PGID; and setpgid(), for setting a process's PGID. setpgid() sets the PGID of the process specified by pid to pgid. If pid is zero, then the process ID of the calling process is used. If pgid is zero, then the PGID of the process specified by pid is made the same as its process ID. If setpgid() is used to move a process from one process group to another (as is done by some shells when creating pipelines), both process groups must be part of the same session (see setsid(2) and credentials(7)). In this case, the pgid specifies an existing process group to be joined and the session ID of that group must match the session ID of the joining process. The POSIX.1 version of getpgrp(), which takes no arguments, returns the PGID of the calling process. getpgid() returns the PGID of the process specified by pid. If pid is zero, the process ID of the calling process is used. (Retrieving the PGID of a process other than the caller is rarely necessary, and the POSIX.1 getpgrp() is preferred for that task.) The System V-style setpgrp(), which takes no arguments, is equivalent to setpgid(0, 0). The BSD-specific setpgrp() call, which takes arguments pid and pgid, is equivalent to setpgid(pid, pgid). The BSD-specific getpgrp() call, which takes a single pid argument, is equivalent to getpgid(pid). RETURN VALUE
On success, setpgid() and setpgrp() return zero. On error, -1 is returned, and errno is set appropriately. The POSIX.1 getpgrp() always returns the PGID of the caller. getpgid(), and the BSD-specific getpgrp() return a process group on success. On error, -1 is returned, and errno is set appropriately. ERRORS
EACCES An attempt was made to change the process group ID of one of the children of the calling process and the child had already performed an execve(2) (setpgid(), setpgrp()). EINVAL pgid is less than 0 (setpgid(), setpgrp()). EPERM An attempt was made to move a process into a process group in a different session, or to change the process group ID of one of the children of the calling process and the child was in a different session, or to change the process group ID of a session leader (setpgid(), setpgrp()). ESRCH For getpgid(): pid does not match any process. For setpgid(): pid is not the calling process and not a child of the calling process. CONFORMING TO
setpgid() and the version of getpgrp() with no arguments conform to POSIX.1-2001. POSIX.1-2001 also specifies getpgid() and the version of setpgrp() that takes no arguments. (POSIX.1-2008 marks this setpgrp() specifica- tion as obsolete.) The version of getpgrp() with one argument and the version of setpgrp() that takes two arguments derive from 4.2BSD, and are not specified by POSIX.1. NOTES
A child created via fork(2) inherits its parent's process group ID. The PGID is preserved across an execve(2). Each process group is a member of a session and each process is a member of the session of which its process group is a member. A session can have a controlling terminal. At any time, one (and only one) of the process groups in the session can be the foreground process group for the terminal; the remaining process groups are in the background. If a signal is generated from the terminal (e.g., typ- ing the interrupt key to generate SIGINT), that signal is sent to the foreground process group. (See termios(3) for a description of the characters that generate signals.) Only the foreground process group may read(2) from the terminal; if a background process group tries to read(2) from the terminal, then the group is sent a SIGTSTP signal, which suspends it. The tcgetpgrp(3) and tcsetpgrp(3) functions are used to get/set the foreground process group of the controlling terminal. The setpgid() and getpgrp() calls are used by programs such as bash(1) to create process groups in order to implement shell job control. If a session has a controlling terminal, and the CLOCAL flag for that terminal is not set, and a terminal hangup occurs, then the session leader is sent a SIGHUP. If the session leader exits, then a SIGHUP signal will also be sent to each process in the foreground process group of the controlling terminal. If the exit of the process causes a process group to become orphaned, and if any member of the newly orphaned process group is stopped, then a SIGHUP signal followed by a SIGCONT signal will be sent to each process in the newly orphaned process group. An orphaned process group is one in which the parent of every member of process group is either itself also a member of the process group or is a member of a process group in a different session (see also credentials(7)). SEE ALSO
getuid(2), setsid(2), tcgetpgrp(3), tcsetpgrp(3), termios(3), credentials(7) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-09-26 SETPGID(2)
All times are GMT -4. The time now is 08:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy