creating child process


 
Thread Tools Search this Thread
Top Forums Programming creating child process
# 8  
Old 05-26-2005
In the second example that you have posted, I think that you have made a couple of mistakes.

1. waitpid(one,NULL,0) should be done in case default. The parent of the first child will start executing the default case and the parent should be waiting for the child. In your example, the child is waiting for itself - and I do not know how that would really work.
2. waitpid(two,NULL,0) should be done in the else part of the if(two==0) loop. In the example, the parent is waiting for the child, but the child will also wait for itself, as the code after the if loop will execute in both cases.

After these changes, if you run your code once, the output in the file should be ABB.
# 9  
Old 05-26-2005
to be honest, im lost!!
i wish to have as output ABABABABAB.
# 10  
Old 05-26-2005
You said that you want a total of three process from one parent using fork. Try this:

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<3;i++) {
		one=fork();
		switch(one) {
			case 0:write(desc,&B,sizeof(char)); /*child proc*/
				_exit(0);

			case -1:perror("fork failed");
				exit(-1);
			
			default:write(desc,&A,sizeof(char));/*parent process*/
				waitpid(one,NULL,0);
		}						
	}
	close(desc);
}

This code has been tested on FreeBSD and output was ABABAB. However, for reasons not really known to me (not a FreeBSD regular), the file was truncated every time it was 'open'ed, so I always got only 6 chars in the file. Smilie
Apart from that, it works just like you want.
# 11  
Old 05-27-2005
Quote:
Originally Posted by blowtorch
However, for reasons not really known to me (not a FreeBSD regular), the file was truncated every time it was 'open'ed, so I always got only 6 chars in the file. Smilie
Apart from that, it works just like you want.
That lseek should be using symbolic constants, but it is probably equivalent to
lseek(desc,SEEK_SET,sizeof(char))

Why on Earth we are seeking to sizeof(char) I cannot say. But sizeof(char) is normally 1. So we move the current pointer from 0 to 1 and start writing. If you then write 6 characters, you will have a 7 character file and the first character will be null. Without that lseek, this would be equivalent to
echo "ababab\c" > file
(Or: echo -n "ababab" depending on your style of echo)
Repeatedly doing that doesn't result in a longer file, but you're not really truncating, you're overwriting. You could add "O_APPEND" to the open flags. That is like:
echo "ababab\c" >> file

Another approach would be to use lseek to move to the end of file prior to writing: lseek(desc, SEEK_END,0)

While I'm commenting on this, the results of code like this is not predictable. You are depending on the parent to reach the write first. There is no guarantee that this will be the case.
# 12  
Old 05-27-2005
Of course, Perderabo! Really stupid of me! I didn't even look at the part above the for loop (just copy-pasted it from code sample that was here). I really should have looked at the code before asking asking dumb questions! Smilie

About the order of execution, wouldn't vfork help here? Also, is vfork POSIX compliant? Cuz it is not implemented under Linux:

Quote:
BUGS
Under Linux, vfork is merely an alias for fork.
Though it does say that this is a bug...

Last edited by blowtorch; 05-27-2005 at 12:27 PM..
# 13  
Old 05-27-2005
vfork is a hack intended to speed up the "fork and then exec" sequence. Where it exists, that should be its only use. The side effect of the parent blocking until the child exits or execs did exist in BSD. And it may exist elsewhere. But if the parent is blocked until the child exits, forking would serve no purpose. The whole idea of multiple processes is that they can run together.

"The use of vfork() for any purpose except as a prelude to an immediate exec() or exit() is not supported." -- HP-UX vfork man page.
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