emaellie 0 Newbie Poster

Hi ya guys,
I have been learning about forks and processes, below is my code that i have created but am having difficulty on certain things.

1. Add code to print out and note the values of :

i. The values held within the array fd[ ] from the parent and child processes.
ii.The process id’s of the parent and child processes. ()

2. From within the child process, create another fork().
i. What is the overall structure of the processes afterwards?
ii. What happens to the original child process id?

#include <stdio.h>
#include <unistd.h>

int main()
{
int n;
int fd[2];
int pid;
const int MAXLINE = 4096;
char line[MAXLINE];

if (pipe(fd) < 0)
    printf(“Pipe Error”);
if ((pid = fork()) < 0)
{
    printf(“Fork Error”);
}
else if (pid > 0) // parent process
{
    close (fd[0]);
    write(fd[1], “Hello World\n”, 12);

    printf("Pid > 0: %d%d",pid,getpid(),getpid());
}
else // child process
{
    close (fd[1]);
    n = read(fd[0], line, MAXLINE);
    write (STDOUT_FILENO, line, n);
}
exit(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.