Trying to understand how this works

[b][red]This message was edited by Smoggy at 2006-6-27 19:11:32[/red][/b][hr]
[b][red]This message was edited by Smoggy at 2006-6-27 16:34:30[/red][/b][hr]
Here is my program.
[code]int main(int argc, char *argv[])
{
pid_t pid;
int fd;
char buf1[] = "My name is Smoggy
I am in Unix programming
This is my program
";
char childBuf[] = "Fork has been called
This is the child process
The pid is equal to zero
";
char parentBuf[] = "This is the parent process
This is filew.c
This is a test program
";

if((fd = creat(argv[1], 0644)) < 0)
{
printf("Could not create %s
", argv[1]);
exit(1);
}
else
{
write(fd, buf1, sizeof(buf1));

if((pid = fork()) < 0)
printf("Fork error");
if(pid == 0)
write(fd, childBuf, sizeof(childBuf));
else
write(fd, parentBuf, sizeof(parentBuf));
}
close(fd);
exit(1);
}
[/code]

Here is my output:

[b]My name is Smoggy
I am in Unix programming
This is my program

This is the child process
The pid is equal to zero

This is filew.c
This is a test program[/b]

Why does my output look this way?
I don't want to know how to fix it, I already know how to fix it.
I'm just trying to understand how the child and the parent are doing this?




Comments

  • : [b][red]This message was edited by Smoggy at 2006-6-27 19:11:32[/red][/b][hr]
    : [b][red]This message was edited by Smoggy at 2006-6-27 16:34:30[/red][/b][hr]
    : Here is my program.
    : [code]int main(int argc, char *argv[])
    : {
    : pid_t pid;
    : int fd;
    : char buf1[] = "My name is Smoggy
    I am in Unix programming
    This is my program
    ";
    : char childBuf[] = "Fork has been called
    This is the child process
    The pid is equal to zero
    ";
    : char parentBuf[] = "This is the parent process
    This is filew.c
    This is a test program
    ";
    :
    : if((fd = creat(argv[1], 0644)) < 0)
    : {
    : printf("Could not create %s
    ", argv[1]);
    : exit(1);
    : }
    : else
    : {
    : write(fd, buf1, sizeof(buf1));
    :
    : if((pid = fork()) < 0)
    : printf("Fork error");
    : if(pid == 0)
    : write(fd, childBuf, sizeof(childBuf));
    : else
    : write(fd, parentBuf, sizeof(parentBuf));
    : }
    : close(fd);
    : exit(1);
    : }
    : [/code]
    :
    : Here is my output:
    :
    : [b]My name is Smoggy
    : I am in Unix programming
    : This is my program
    :
    : This is the child process
    : The pid is equal to zero
    :
    : This is filew.c
    : This is a test program[/b]
    :
    : Why does my output look this way?
    : I don't want to know how to fix it, I already know how to fix it.
    : I'm just trying to understand how the child and the parent are doing this?
    :
    :
    :
    :
    :

    hi I didn't understand ur problem.This output is correct as for my knowledge.just explain ur problem.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion