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 ?

You must be getting the parent and child mixed up.

can u give me a sample code how u create 2 child process from the same parents ?

Use our search function. There are several examples already in the forum.

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?

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?

#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");
}

am I lame ??
sounds like i didnt grab something !!!
ive changed my code now!!
whats wrong??
y for 2 loops, it prints 3 loops?

#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);
	}

}

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.

to be honest, im lost!!
i wish to have as output ABABABABAB.

You said that you want a total of three process from one parent using fork. Try this:

#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. :confused:
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.

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! :o

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

Though it does say that this is a bug...

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.