views:

86

answers:

5

hi! everybody i have a c problem can any one help me . i wrote a code of process creation in c language , it uses pid & fork() call. the fork call is use for making child process now can any body tell me how to make parent process? i know that creating a child in process make the process parent automatically but i want to make a parent process right from the start so can any one tell me how to do this, Secondly i just create the process i don't know how to use it i cant assign any work(computation) to it.so can any one tell me how to use the process for work?

Third one i want to give a name to my process how can i do it & how i can control their execution? please if anyone can enlighten me than please help me to understand all this. i shall be thank full for this forever thanks in advance

A: 
  1. To create a parent process, take your code for creating a child and reverse the roles of parent and child. Presto change-o, the new process is the parent and the old process is the child.

  2. For communications between the processes, use a pipe(2), or several. Also, there is shared memory.

  3. To control execution, use kill(2) and wait(2). I'm not sure about assigning names, we might need to know what platform you're on.

Potatoswatter
+1  A: 

When you fork a process, one process becomes two processes. Each continues running at exactly the same place. The only difference is that the fork returns the PID of the child process to the parent process and returns the value 0 to the child process.

Without any help, the child process does not know its parent. If the two processes need to communicate with one another then they will need to use some sort of IPC mechanism.

A common form of IPC is pipe. If one opens pipes before forking, then both the child and the parent keep the open file descriptors. This will allow both processes to communicate with one another. The parent is now free to communicate its PID to the child process if so desired.

doron
The simplest way to communicate the parent pid to a child is to save the pid to a variable before forking. The child can also use the system call `getppid()`
JeremyP
Should have thought of that. Just me being thick
doron
A: 
Secondly i just create the process i don't know how to use it i cant assign any 
work(computation) to it

You need to use fork and exec in pair make it run the program you want to execute. Here is the wiki link for more information.

Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new program. fork() is the name of the system call that the parent process uses to "divide" itself ("fork") into two identical processes. After calling fork(), the created child process is actually an exact copy of the parent - which would probably be of limited use - so it replaces itself with another process using the system call exec().

Praveen S
+4  A: 

The fork call creates a new process that is identical to the existing process except for a few minor differences such as its pid, parent pid. The original process carries on from exactly the same place and this is the parent process. Which means your question is basically meaningless. You don't create the parent process, the original process becomes the parent process once it creates a child process.

It's a bit like asking "I created a child by getting pregnant and giving birth, but how do I create the parents?" You are a parent automatically.

Back to computers...

When you fork, the system call returns the pid of the child to the parent and 0 to the child, so you should have code something like:

int pid = fork();
if (pid == 0)
{
    // in child, do child processing - normally exec an executable
}
else if (pid > 0)
{
    // in parent, do some processing - often wait for child to complete
}
else
{
    // fork failed -  handle the error
}
JeremyP
A: 

About renaming (I assume you mean the name displayed by ps), to "rename" a process, just copy your new name into argv[0]

Hasturkun