Sponsored Content
Full Discussion: Parent forking
Top Forums Programming Parent forking Post 302493432 by TWhitt24 on Wednesday 2nd of February 2011 05:45:45 PM
Old 02-02-2011
What I need to do is create 3 child processes, then output the parents PID, the first childs PID, the second childs PID, and then the third. Here is what I have been working with.
Code:
 #include <iostream>
  #include <stdio.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <sys/wait.h>
  using namespace std;
  int main(void)
  {
    pid_t pId;
    int status;
    int child1;
    int child2;
    int child3;
   
    if(pId = fork() > 0)
      {
        cout<<"This is the parent process, My PID is: "<<getpid()<<endl;
        pId = fork();
        wait(&status);
        
      }
    if(pId > 0)
      {
        pId = fork();
        wait(&status);
        exit(1);
      }
    else if(pId == 0)
  {
        child1 = getpid();
        cout<<"This is the first child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ls", "ls", "-1", 0);
        exit(1);
      }
    else if(pId == 0)
      {
        child2 = getpid();
        cout<<"This is the second child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ps", "-1f", 0);
       exit(1);
      }
    else if(pId == 0)
      {
        child3 = getpid();
        cout<<"This is the third child process, My PID is: "<<getpid()<<endl;
        exit(1);
      }
    exit(1);
  }
   child1 = getpid();
        cout<<"This is the first child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ls", "ls", "-1", 0);
        exit(1);
      }
    else if(pId == 0)
      {
        child2 = getpid();
        cout<<"This is the second child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ps", "-1f", 0);
        exit(1);
      }
    else if(pId == 0)
      {
        child3 = getpid();
        cout<<"This is the third child process, My PID is: "<<getpid()<<endl;
        exit(1);
      }
    exit(1);
  }

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 02-02-2011 at 07:07 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

Forking in a loop

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

Forking

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

forking and killing parent processes

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

forking a new process

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

forking process.

#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

full path of a file situated either in parent's dir. or parent's parent dir. so on...

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

Forking a new process without parent dependance

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

Forking and Pinging

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

forking a child process and kill its parent to show that child process has init() as its parent

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

need help in forking

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

NAME
setsid - creates a session and sets the process group ID SYNOPSIS
#include <unistd.h> pid_t setsid(void); DESCRIPTION
setsid() creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session, the process group leader of the new process group, and has no controlling tty. The process group ID and session ID of the calling process are set to the PID of the calling process. The calling process will be the only process in this new process group and in this new session. RETURN VALUE
On success, the (new) session ID of the calling process is returned. On error, (pid_t) -1 is returned, and errno is set to indicate the error. ERRORS
EPERM The process group ID of any process equals the PID of the calling process. Thus, in particular, setsid() fails if the calling process is already a process group leader. CONFORMING TO
SVr4, POSIX.1-2001. NOTES
A child created via fork(2) inherits its parent's session ID. The session ID is preserved across an execve(2). A process group leader is a process with process group ID equal to its PID. In order to be sure that setsid() will succeed, fork(2) and _exit(2), and have the child do setsid(). SEE ALSO
getsid(2), setpgid(2), setpgrp(2), tcgetsid(3), credentials(7) COLOPHON
This page is part of release 3.25 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 2008-12-03 SETSID(2)
All times are GMT -4. The time now is 03:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy