Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fork - How to kill PIDs ? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I am new to C programming. I have two questions.

I am writing a program which is forked at some pont. Before the fork was included in the program, I was able to see the program running by issuing a "ps ax -l" in my linux system. Since I have included the fork in the program I am not able to list it running, only able to get the PID by using the "pidof ..." command. Why?

Also how can I kill the Forks after they finished, so only one PID is shown when "pidof programname" entered?


This is the Fork part:

switch (pid = fork()) {

case -1:

perror("Fork Failed");
break;

case 0:

/* program */

}

close(sock1);
_exit(0);
 
main()
{
int pid;
pid = fork();
if(pid == 0)
{
printf("child process id is %d\n",getpid());
exit(0); /* exit child */
}
else
{
printf("parent process id is%d\n",getpid());
exit(0); /* exit parent */
}
}

Not quite sure what you are asking but you can get your PIDs with getpid() and if you wish to terminate one of them you can use an exit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top