creating child process

C

Clunixchit

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));
}
exit(0);

case -1 : perror("fork");
exit(-1);

default : waitpid(one,NULL,0);
two = fork();
if ( two == 0 ) {
write(desc,&B,sizeof(char));
waitpid(two,NULL,0);
}
close(desc);
system("cat aba.txt");
exit(0);
}

}

in the end i wish to output ABABABABAB
 
J

Jens.Toerring

Clunixchit said:
what do you mean?

That what you try to do there is something Unix-specific and isn't
a C problem at all (which is the topic of this group). That's why
you got told to ask in comp.unix.programmer where fork() and
friends is an often discussed topic.

Regards, Jens
 
S

SM Ryan

(e-mail address removed)-spam.invalid (Clunixchit) wrote:
# am I lame ??
# sounds like i didnt grab something !!!
# ive changed my code now!!
# whats wrong??
# y for 2 loops, it prints 3 loops?

I never remember the rules for sharing file descriptors of disk files. You might
get a better response from comp.unix.programmer.

# case 0 : for(i=0;i<2;i++){
# write(desc,&A,sizeof(char));
# }

# default : waitpid(one,NULL,0);
# two = fork();
# if ( two == 0 ) {
# write(desc,&B,sizeof(char));
# waitpid(two,NULL,0);
# }

You're doing the wait for child two in child two. You're not doing a wait in the
parent.
 
C

Clunixchit

Anonymouswrote:
Clunixchit said:
what do you mean?
That what you try to do there is something Unix-specific and isn't
a C problem at all (which is the topic of this group). That's why
you got told to ask in comp.unix.programmer where fork() and
friends is an often discussed topic.

Regards, Jens
--
\ Jens Thoms Toerring ___ (e-mail address removed)-berlin.de
\__________________________
http://www.toerring.de[/quote:78737363b3]

where is comp.unix.programmer?
is it comp.unix.development?
the "notify me when a reply is posted" is not working?
 
K

Keith Thompson

Anonymouswrote:
Clunixchit said:
what do you mean?
That what you try to do there is something Unix-specific and isn't
a C problem at all (which is the topic of this group). That's why
you got told to ask in comp.unix.programmer where fork() and
friends is an often discussed topic.

Regards, Jens
--
\ Jens Thoms Toerring ___ (e-mail address removed)-berlin.de
\__________________________
http://www.toerring.de[/quote:78737363b3]

where is comp.unix.programmer?
is it comp.unix.development?
the "notify me when a reply is posted" is not working?

Please quote properly. The way your article was formatted makes it
appear that you wrote what Jens actually wrote, and that your
questions at the end ("where is comp.unix.programmer?") are part of
the signature. Quoted material is normally prefixed by "> ". Your
newsreader software should do this for you; if it doesn't, it's either
misconfigured or broken. The "[/quote:78737363b3]" stuff may be
specific to your system; please don't use it here.
where is comp.unix.programmer?

It's a newsgroup, like comp.lang.c. If your server doesn't carry it,
you'll need to talk to someone on your end.
is it comp.unix.development?

No, as far as I know there is no newsgroup named "comp.unix.development".
the "notify me when a reply is posted" is not working?

We can't help you with that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top