Child processes with fork - c

I am trying to make a simple C program that will call the fork method three times and display identifiers of child processes (UID, GID, PID, PPID, PGID). And I am struggling with proper understanding what is really happening. There is my code in which I use separate methods for method fork() and for displaying identifiers, and also in for for parent process I am trying to use the waitpid method to wait for all child processes to die. I am really confused now because I cannot get it working in a way that clearly says it works in proper way. Can you give me any suggestions or show a better way for my problem?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void identifiers();
void forkMethod();
int main(void)
{
forkMethod();
return 0;
}
void forkMethod()
{
int k;
int status;
for (k=0;k<3;k++){
switch (fork()) {
case -1:
perror("fork error");
exit(EXIT_FAILURE);
break;
case 0:
identifiers();
break;
default:
//wait(&status);
waitpid(getpid(), &status, WNOHANG);
sleep(1);
break;
}
}
}
void identifiers()
{
pid_t pid = getpid();
pid_t ppid = getppid();
pid_t pgid = getpgid(pid);
pid_t uid = getuid();
pid_t gid = getgid();
printf("UID:%d GID:%d PID:%d PPID:%d PGID:%d\n", uid, gid, pid, ppid, pgid);
}
// ===========================================================
First thank you all for answers and pointing all of these mistakes I made,
I know the code is horrible and probably still have a few issues but now i think i have what i wanted to.
As #John Bollinger ask what is main functionality that is my list:
1. Write a function that displays identifiers UID, GUID, PID, PPID, PGID for specified process
2. call fork() function 3 times and display these identifiers for child processes
3. Use the sleep function to display processes in order from the oldest
4. display a processes tree based on results.
Thank you #juhist and #Jonathan Leffler for sharp and simple explanation. If there any significant issues in code please post
and know the final code is:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void identifiers(); // function that display identifiers
void forkMethod(); // function that calls fork() function 3 times
void tree(int); // function displaying processes tree
int main(void)
{
identifiers(); // displaying ID's for parent process
printf("Parent pid: %d\n", getpid());
printf("Child processes: \n");
forkMethod();
return 0;
}
void forkMethod()
{
int k;
int status;
int pid;
for (k=0;k<3;k++){
pid = fork();
switch (pid) {
case -1:
perror("fork error");
exit(EXIT_FAILURE);
break;
case 0:
identifiers();
exit(0);
default:
tree(getpid());
wait(&status);
sleep(1);
break;
}
}
}
void identifiers()
{
pid_t pid = getpid();
pid_t ppid = getppid();
pid_t pgid = getpgid(pid);
pid_t uid = getuid();
pid_t gid = getgid();
printf("\nUID:%d GID:%d PID:%d PPID:%d PGID:%d\n", uid, gid, pid, ppid, pgid);
}
void tree(int pid)
{
char pstree[] = "pstree -np ";
char cmd[12];
sprintf(cmd, "%s%d", pstree, pid);
system(cmd);
}

One of your problems is in this code:
case 0:
identifiers();
break;
Surely you meant to do this instead:
case 0:
identifiers();
exit(0);
Otherwise the child process will continue execution and you will get too many forks.
The other problem is that you're calling waitpid() with the parent pid, not the child pid. The calls does nothing useful as you're using the WNOHANG argument. Either use wait() or store the pid of the child as returned by fork somewhere and use that pid as the argument of waitpid().
Furthermore, you should consider checking the return value of waitpid(). In larger programs, if you have signal handlers it is possible that the call will be interrupted by signal and returns an error code with errno == EINTR. It is good practice to retry system calls interrupted by a signal and check for other possible error returns, too.

On this line:
waitpid(getpid(), &status, WNOHANG);
The getpid() will always get the parent pid, not the pid of the child.
Suggest changing switch(fork()) to
pid= fork(); switch(pid) { ... }
and the waitpid line to
waitpid(pid, &status, 0);

Related

Create a chain of n sub processes

In c++ create chain of n processes with n as input and the output of processes should be as parent1->child1(parent2)-->child2(parent3),by using recursive function im able to generate the output but unable to exit the loop i also need help in sending an input of n for which the loop should break.
below is my code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int foo(const char *whoami) {
printf("I am a %s. My pid is:%d my ppid is %d\n", whoami, getpid(), getppid() );
return 1;
}
int func() {
pid_t pid=fork();
if (pid==0) { /* only execute this if child */
foo("child");
pid_t pid=fork();
if (pid==0) { /* only execute this if child */
foo("child");
func();
exit(0);
}
}
exit(0);
}
wait(0); /* only the parent waits */
return 0;
}
int main(void){
foo("parent");
func();
return 0;
}
You can't exit the loop for a simple reason, and that is, you spawn child processes endless. Whenever you fork() a new process starts, then it forks again.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int n=5;
int foo(const char *whoami) {
printf("I am a %s. My pid is:%d my ppid is %d\n", whoami, getpid(), getppid() );
return 1;
}
int func(int n)
{
if (n == 0)
{
return 0;
}
int pid = fork();
if (pid == -1) {
exit(0);
}
if (pid==0) {
foo("child");
n = n-1;
func(n);
exit(0);
}
else {
wait(NULL);
}
return 0;
}
int main()
{
func(n);
return 0;
}
gcc -std=c99 prog.c -o prog
./prog
OUTPUT:
I am a child. My pid is: 1159 my ppid is 1158
I am a child. My pid is: 1160 my ppid is 1159
I am a child. My pid is: 1161 my ppid is 1160
I am a child. My pid is: 1162 my ppid is 1161
I am a child. My pid is: 1163 my ppid is 1162
From what you are saying i understand you are having the following problems:
1st. You are trying to send 'data' from one process to another
2nd. You are trying to find a way to stop your program from running.
Now for the first. If you want to do that and i understood it correctly, there are 2 ways to achieve that. One is the use of shared memory and the other is the use of pipelines. Shared memory is pretty obvious on what is doing. Pipes are taking the stdout of a process and redirecting it as a stdin in the next process.
Now you need a closure to your program. A child process is executed when it executes a command(exec) or when it is told so(with an IF statement for example and a return). You can create a statement of your liking, and when a child process meets your requirments then you can make it die(There is also a way to kill the parent process from the child process with the kill(pid, SIGKILL); command.
I didn't provide you with any code because it is unclear to me the exact nature of your problem.
Hope my assuming led you to something!

How to terminate a child process which is running another program by doing exec

I'm doing fork in my main program,and doing exec in the child process which will run another program. Now i want to terminate the child(i.e., the program invoked by exec) and return back to the main program(or parent program). how could i achieve this.. I tried with ctrl+c but its killing parent process and child also.please help me.
/*This is main.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
void sig_int(void);
void sig_term(void);
pid_t pid,ppid;
int main(char argc,char **argv){
int n;
char ch;
printf("***********Application to start or stop services**********\n");
do
{
printf("Enter 1 to start service no.1\n");
printf("Enter 2 to start service no.2\n");
printf("Enter 3 to start service no.3\n");
scanf("%d",&n);
if(fork() == 0)
{
switch(n)
{
case 1: printf("starting service no. 1..\n");
printf("checking whether the given service is already running...\n");
// system("./det.sh ./test")
pid = getpid();
printf("child process pid = %d\n",pid);
// signal(SIGINT,(void *)sig_int);
// signal(SIGTERM,(void *)sig_term);
//execl("/var/vR_main","vR_main",argv[1],argv[2],argv[3],argv[4],NULL);
execl("./test","test",0,0);//will run test.c
break;
case 2: printf("starting service no. 2..\n");
break;
case 3: printf("starting service no. 3..\n");
break;
}
}
else
{
int status;
wait(&status);
if (WIFEXITED(status))
printf("CHILD exited with %d\n", WEXITSTATUS(status));
if (WIFSIGNALED(status))
printf("signaled by %d\n", WTERMSIG(status));
if (WIFSTOPPED(status))
printf("stopped by %d\n", WSTOPSIG(status));
// sleep(2);
ppid = getpid();
printf("%d\n",ppid);
// wait();
printf("\nDo you want to continue...y/n:");
scanf(" %c",&ch);
}
}while(ch == 'y');
return 0;
}
void sig_int(void)
{
printf("caught signal\n");
kill(pid,SIGKILL);
// signal(SIGINT,SIG_DFL);
// exit(0);
}
void sig_term(void)
{
printf("killing the process\n");
signal(SIGINT,SIG_DFL);
// exit(0);
}
/*This is test.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
void sig_int(void);
void sig_term(void);
pid_t pid;
int main()
{
// int a=10,b=40,c=50,max;
pid = getpid();
printf("exec pid = %d\n",pid);
while (1)
{
signal(SIGINT,(void *)sig_int);
signal(SIGTERM,(void *)sig_term);
}
// max=a>b?a>c?a:c:b>c?b:c;
// printf("%d\n",max);
}
void sig_int(void)
{
printf("caught signal\n");
// signal(SIGINT,SIG_DFL);
kill(pid,SIGKILL);
// exit(0);
}
void sig_term(void)
{
printf("killing the process\n");
signal(SIGINT,SIG_DFL);
// exit(0);
}
Now I want to kill "test application" (invoked by exec),and return to the parent process or the "else block" to continue the program.
You need to do the following:
Do a kill(pid, SIGTERM) first - this gives the child process an opportunity to terminate gracefully
Wait a period of time (use sleep). The period of time depends on the time the child process takes to close down gracefully.
Use waitpid(pid, &status, WNOHANG) checking the return value. If the process has not aborted do step 4
Do a kill(pid, SIGKILL) then harvest the zombie by doing waitpid(pid, &status, 0).
These steps ensure that you give the child process to have a signal handler to close down and also ensures that you have no zombie processes.
Either in or outside your program, it is possible to use kill. By including <signal.h>, you can kill a process with a given PID (use the fork return value to do this).
#include <signal.h>
int pid;
switch (pid = fork())
{
case -1:
/* some stuff */
break;
case 0:
/* some stuff */
break;
default:
/* some stuff */
kill(pid, SIGTERM);
}
It is also possible to use kill command in the shell. To find the PID of your child process, you can run ps command.
man kill
The kill() function shall send a signal to a process or a group of processes specified by pid. The signal to be sent is specified by sig and is either one from the list given in <signal.h> or 0. If sig is 0 (the null signal), error checking is performed but no signal is actually sent. The null signal can be used to check the validity of pid.
POSIX defines the kill(2) system call for this:
kill(pid, SIGKILL);

Blocked process in c

I am working on some code to create a process that goes blocked and then ends, I have to be able to see the blocked state with ps.
I tried with this, but my C knowledge is not good. The code doesn't print anything.
Here it is:
#include <stdio.h>
#include <stdlib.h> //exit();
#include <unistd.h> //sleep();
int main(int argc, char *argv[]) {
createblocked();
}
int pid;
int i;
int estado;
void createblocked() {
pid = fork();
switch( pid ) {
case -1: // pid -1 error ocurred
perror("error\n");
break;
case 0: // pid 0 means its the child process
sleep(); // we put the child to sleep so the parent will be blocked.
printf("child sleeping...");
break;
default: // !=0 parent process
// wait function puts parent to wait for the child
// the child is sleeping so the parent will be blocked
wait( estado );
printf("parent waiting...\n");
printf("Child terminated.\n");
break;
}
exit(0);
}
It should be easy because its only a little program that goes blocked, but I am walking in circles I think. Any advice?
sleep() takes a parameter: the number of seconds to sleep. When you omit it, it tends to return immediately.
Also wait() takes an int *, not an int.
try this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
createblocked();
}
int pid;
int i;
int estado;
void createblocked() {
pid = fork();
switch(pid)
{
case -1: // pid -1 error ocurred
perror("error\n");
break;
case 0: // pid 0 means its the child process
printf("child sleeping...\n");
sleep(500); // we put the child to sleep so the parent will be blocked.
break;
default: // !=0 parent process
// wait function puts parent to wait for the child
// thechild is sleeping so the parent will be blocked
printf("parent waiting...\n");
wait(&estado);
printf("Child terminated.\n");
break;
}
exit(0);
}
note: I also moved the printf("parent waiting...\n") above the call to wait(), so you should see it before the parent blocks waiting on the child.
edit: Also, include <unistd.h>. While not strictly required in order for the program to work (on most systems), doing so will give you better compile-time error reporting for things like missing and/or incorrectly-typed function arguments.
man sleep
man wait
You should give the number of seconds as an argument in sleep().
For wait and sleep include <unistd.h>

Can a process have two PIDs?

I'm studying computer systems and I've made this very simple function which uses fork() to create a child process. fork() returns a pid_t that is 0 if it's a child process. But calling the getpid() function within this child process returns a different, nonzero pid. In the code I have below, is newPid only meaningful in the context of the program, and not to the operating system? Is it possibly only a relative value, measured against the pid of the parent?
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
void unixError(char* msg)
{
printf("%s: %s\n", msg, strerror(errno));
exit(0);
}
pid_t Fork()
{
pid_t pid;
if ((pid = fork()) < 0)
unixError("Fork error");
return pid;
}
int main(int argc, const char * argv[])
{
pid_t thisPid, parentPid, newPid;
int count = 0;
thisPid = getpid();
parentPid = getppid();
printf("thisPid = %d, parent pid = %d\n", thisPid, parentPid);
if ((newPid = Fork()) == 0) {
count++;
printf("I am the child. My pid is %d, my other pid is %d\n", getpid(), newPid);
exit(0);
}
printf("I am the parent. My pid is %d\n", thisPid);
return 0;
}
Output:
thisPid = 30050, parent pid = 30049
I am the parent. My pid is 30050
I am the child. My pid is 30052, my other pid is 0
Lastly, why is the child's pid 2 higher than the parent's, and not 1? The difference between the main function's pid and its parent is 1, but when we create a child it increments the pid by 2. Why is that?
From fork man page :
Return Value
On success, the PID of the child process is returned in the parent,
and 0 is returned in the child. On failure, -1 is returned in the
parent, no child process is created, and errno is set appropriately.
Fork does not returns the pid of the child, only in the parent. Therefore, the child process does not have two pids.
Try this
int main(int argc, const char * argv[])
{
pid_t thisPid, parentPid, newPid;
int count = 0;
thisPid = getpid();
parentPid = getppid();
printf("thisPid = %d, parent pid = %d\n", thisPid, parentPid);
if ((newPid = Fork()) == 0) {
count++;
printf("I am teh child. My pid is %d\n", getpid());
exit(0);
}
else
printf("I am the parent. My pid is %d, my child pid is %d\n", thisPid, newPid);
return 0;
}
Pids are one-per process. There will NEVER be more than 1 pid for a process - the internal data structures that handle the process in the OS only have a single PID field in them.
Beyond that, when you call fork() you are cloning the process that called fork, producing an exactly duplicate of it - all file handles, all memory, etc.. EXCEPT for its PID. That's why fork returns different values depending on if you're the child or parent process. This differing return values lets the program know if it's a child or a parent. The child gets 0, and can therefore know it's the child.
No, a pid is assigned to exactly one process at a time.
Process ids do not need to follow any rules when being assigned to processes. So if it looks as if a child pid is the increment of the parent's pid this is just luck.
By the pid of certain processes it is not possible to draw any conclusions regarding the processes relationship.
PIDs are not sequential on assignment (the actually follow no rules) and one process has only one PID at a time. Also there can never be two processes that share the same PID.

Creating a process that is not child of the process that created it

I want to create a process B from process A. However, I don't want B to be the child of A, which will be the case if I simply use fork. How can I achieve that? In other words I want process B to keep on executing even if process A is killed.
You can use the setsid() function.
Alternatively, as you have tagged your question "linux", maybe you want to use daemon() instead of fork() + setsid().
Why do you think that B would not keep executing after A is killed if B is a child of A? That's not true.
But if you still want B to not be a child of A then you can do this by fork()ing twice: once to create a child A½ and once to create B. B is a child of A½ and a grandchild of A. Then have A½ exit immediately. B will be inherited by init and will have no further relationship to A.
If you are concerned about signals like SIGHUP and SIGINT that are generated in response to events like a Control-C keypress that are broadcast to the entire foreground process group, see cnicutar's answer.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
switch(pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
printf(" CHILD: This is the child process!\n");
printf(" CHILD: My PID is %d\n", getpid());
printf(" CHILD: My parent's PID is %d\n", getppid());
/* you can exec another program here if you wish to */
printf(" CHILD: I'm outta here!\n");
break;
default:
printf("PARENT: This is the parent process!\n");
printf("PARENT: My PID is %d\n", getpid());
printf("PARENT: My child's PID is %d\n", pid);
printf("PARENT: I'm not going to wait for my child to exit\n");
signal(SIGCHLD, SIG_IGN);
printf("PARENT: I'm outta here!\n");
}
return 0;
}
If you don't send the signal(), then once after the parents completes the execution (and exits), the child process will be termed as a zombie process. For better understanding, execute the following program.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
/*
Execute this program and do 'ps ax | grep Z' to see that this
is put in a defunct state or zombie state
*/
int main()
{
pid_t child_pid;
child_pid = fork();
if (child_pid > 0) {
sleep(60);
} else {
return 0;
}
return 0;
}
The only way I see is having the child orphan (then adopted by init).
This may be achieved by terminating the father process before the child (but just take care of signal propagation).
Some nice samples may be found here

Resources