By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

creating child process

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ch******@gmail-dot-com.no-spam.invalid (Clunixchit) writes:
[snip]
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>

[snip]

Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2

P: n/a
what do you mean?

Nov 14 '05 #3

P: n/a
Clunixchit <ch******@gmail-dot-com.no-spam.invalid> wrote:
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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

P: n/a
ch******@gmail-dot-com.no-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.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
This is one wacky game show.
Nov 14 '05 #5

P: n/a
> Anonymouswrote:
Clunixchit <ch******@gmail-dot-com.no-spam.invalid> wrote:
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 ___ Je***********@physik.fu-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?

Nov 14 '05 #6

P: n/a
ch******@gmail-dot-com.no-spam.invalid (Clunixchit) writes:
Anonymouswrote: Clunixchit <ch******@gmail-dot-com.no-spam.invalid> wrote:
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 ___ Je***********@physik.fu-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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.