10-07-2001
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 (i=0; i<NUMPROCESSES; i++)
if (fork() == 0)
child_code (i,total,&j);
for (i=0; i<NUMPROCESSES; i++)
return_pid = wait(0);
printf ("value of J <%d>\n", j);
}
10 More Discussions You Might Find Interesting
1. Programming
Hi
I have a question about the scope of variables for parent and a child
I had written my code here and the output of this but only in child
process the information is completely right even in main the informatin is wrong
well the child process will see the global variable te2 and can change... (5 Replies)
Discussion started by: netman
5 Replies
2. Programming
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
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. Shell Programming and Scripting
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
5. Programming
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
6. Shell Programming and Scripting
Hi All,
I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program.
I want 1st script to wait until the 'C' program completes.
I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies
7. Shell Programming and Scripting
Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know.
More detail.
ScriptA (bash), ScriptB (ksh), ScriptC (bash)
ScriptA, launches ScriptB
ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies
8. Programming
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
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
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
FORK(2) System Calls Manual FORK(2)
NAME
fork, rfork - manipulate process resources
SYNOPSIS
#include <u.h>
#include <libc.h>
int fork(void)
int rfork(int flags)
DESCRIPTION
Forking is the only way new processes are created. The flags argument to rfork selects which resources of the invoking process (parent)
are shared by the new process (child) or initialized to their default values. The resources include the file name space, the open file
descriptor table (which, when shared, permits processes to open and close files for other processes), the set of environment variables (see
env(3)), the note group (the set of processes that receive notes written to a member's notepg file; see proc(3)), and open files. Flags is
the logical OR of some subset of
RFPROC If set a new process is created; otherwise changes affect the current process.
RFNOWAIT
If set, the child process will be dissociated from the parent. Upon exit the child will leave no Waitmsg (see wait(2)) for the par-
ent to collect.
RFNAMEG
If set, the new process inherits a copy of the parent's name space; otherwise the new process shares the parent's name space. The
tag space for rendezvous(2) is considered part of the name space. Is mutually exclusive with RFCNAMEG.
RFCNAMEG
If set, the new process starts with a clean name space. A new name space must be built from a mount of an open file descriptor. Is
mutually exclusive with RFNAMEG.
RFENVG If set, the environment variables are copied; otherwise the two processes share environment variables. Is mutually exclusive with
RFCENVG.
RFCENVG
If set, the new process starts with an empty environment. Is mutually exclusive with RFENVG.
RFNOTEG
Each process is a member of a group of processes that all receive notes when a note is written to any of their notepg files (see
proc(3)). The group of a new process is by default the same as its parent, but if RFNOTEG is set (regardless of RFPROC), the
process becomes the first in a new group, isolated from previous processes.
RFFDG If set, the invoker's file descriptor table (see intro(2)) is copied; otherwise the two processes share a single table.
RFCFDG If set, the new process starts with a clean file descriptor table. Is mutually exclusive with RFFDG.
RFMEM If set, the kernel will mark segments of type data and bss as shared. The child will then inherit all the shared segments the parent
process owns. Other segment types will be unaffected. Subsequent forks by the parent will then propagate the shared data and bss
between children. The stack segment is always split. May be set only with RFPROC.
File descriptors in a shared file descriptor table are kept open until either they are explicitly closed or all processes sharing the table
exit.
If RFPROC is set, the value returned in the parent process is the process id of the child process; the value returned in the child is zero.
Without RFPROC, the return value is zero. Process ids range from 1 to the maximum integer (int) value. Rfork will sleep, if necessary,
until required process resources are available.
Fork is just a call of rfork(RFFDG|RFPROC).
SOURCE
/sys/src/libc/9syscall
/sys/src/libc/9sys/fork.c
SEE ALSO
intro(2), proc(3),
DIAGNOSTICS
These functions set errstr.
FORK(2)