creating child process


 
Thread Tools Search this Thread
Top Forums Programming creating child process
# 1  
Old 11-03-2003
creating child process

i want to create 3 child processes from the same parent using folk. I know how to use folk but my child processes did not come from the same parent. Any suggestion what i did wrong ?
# 2  
Old 11-03-2003
You must be getting the parent and child mixed up.
# 3  
Old 11-03-2003
can u give me a sample code how u create 2 child process from the same parents ?
# 4  
Old 11-03-2003
Use our search function. There are several examples already in the forum.
# 5  
Old 05-24-2005
Let's say there is a process P; is there a way for it to know which process (the PID) has sent it a signal?
# 6  
Old 05-26-2005
CPU & Memory

hai im programming a simple program like you wanted, but its not working properly !!!

well first of all i want it to print A then B then A then B , five times
but i dont know y "for" is not working ?
with or without "for" it prints only "AB"

if this program works ill include it in my own script for my FC3 box!!
can you point whats wrong with my code?
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
	pid_t one, two;
	char A = 'A';
	char B = 'B';
	int i=0;

	int desc = open ("aba.txt", O_CREAT | O_RDWR , 0700 );
	if ( desc < 0 ) {
		perror ( "open");
		exit(-1);
	}
	
	lseek(desc,0,sizeof(char));
	
for (i=0;i<5;i++){
	printf("%d\n",i);
	one = fork();
	if ( one == 0 ) {
		write(desc,&A,sizeof(char));
		exit(0);
	}
	else {
		
		two = fork();
		if ( two == 0 ) {
			write(desc,&B,sizeof(char));
		}
		exit(0);
	}
	
	waitpid(one,NULL,0);
	waitpid(two,NULL,0);
}
	close(desc);
	system("cat aba.txt");
}

# 7  
Old 05-26-2005
am I lame ??
sounds like i didnt grab something !!!
ive changed my code now!!
whats wrong??
y for 2 loops, it prints 3 loops?
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
	pid_t one, two;
	char A = 'A';
	char B = 'B';
	int i=0;

	int desc = open ("aba.txt", O_CREAT | O_RDWR , 0700 );
	if ( desc < 0 ) {
		perror ("open");
		exit(-1);
	}
	
	lseek(desc,0,sizeof(char));
	
	one = fork();

	switch ( one ) {
		case  0 : for(i=0;i<2;i++){
				write(desc,&A,sizeof(char));	
				two = fork();
				if ( two == 0 ) {
					write(desc,&B,sizeof(char));
				}
				waitpid(one,NULL,0);
				waitpid(two,NULL,0);
			  }
			  exit(0);
			  
		case -1 : perror("fork");
			  exit(-1);
			  
		default : close(desc);
			  system("cat aba.txt");
			  exit(0);
	}

}

creating child process-snapshot7png
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Url check creating child process and generating false alerts

Hi All Below code is working as expected but creating too many child processes when the url is not up and every minute that process is sending false email alerts any help with the logic not to generate child process and not to send duplicate alerts app="https://url" appresult=$(wget... (2 Replies)
Discussion started by: srilinux09
2 Replies

2. UNIX for Advanced & Expert Users

Unix script seems to be momentarily creating child process for unknown reason

Hi, I have a unix script that basically has a while loop inside which it checks Oracle database for certain records. If it finds the records, it does some processing and then goes back to the while loop. If it doesnot find any matching records, then it sleeps for 30 seconds and then goes back to... (17 Replies)
Discussion started by: waavman
17 Replies

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

4. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

5. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

6. Shell Programming and Scripting

Creating a pipe using parent and child processes

Hello, I am trying to create a pipe that will direct stdout to in side of the pipe, and stdin to the out side of the pipe - I created two child processes to handle this. However, my pipe doesn't seem to be working correctly. Did I use execv() correctly? Command1 and command2 represent the two... (3 Replies)
Discussion started by: jre247
3 Replies

7. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

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. Shell Programming and Scripting

Child Process Name

Hi , I want to find out the child process name given its PID. I have used the ps command but it displays the parent process name against child PID. Is there any way to find out name of child program executing under any parent program? (1 Reply)
Discussion started by: sneha_heda
1 Replies

9. Shell Programming and Scripting

How to make the parent process to wait for the child process

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

10. UNIX for Dummies Questions & Answers

about child process

hello every one, i want to know more about creation of child process. UNDER WHAT CIRCUMSTANCES child process is created? WHAT ARE THE PREREQUISITES for a child process to be created? let us say we have a prog.c, prog.obj(compiled.c),.a\.out files. is any child PROCESS CREATED... (12 Replies)
Discussion started by: compbug
12 Replies
Login or Register to Ask a Question