Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
The C program that accepts a positive integer n as a parameter.
When executed, the process spawns child processes each executing a
copy of the same program, as following.The parameter of 2nd child can not work correctly.
Someone help out please. Or is there any better ideas(like recursive)?if n > 1, the process spawns two children with parameters n-1 and
n-2;if n <= 1, the process spawns no child processes.
Part of my work is as following:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <setjmp.h>sigjmp_buf position;
int main(int argc, char *argv[])
{int num;
pid_t pid, pid1;
char buffer[512];
static struct sigaction act;
void goback(void);
if (argc != 2)
{
printf("Usage: spanwner [posi_num]\n");
exit(1);
}
/* get value of pid from the parameter */
num = strtol(argv[1], NULL, 0);
if (num < 1)
{
printf("Usage: spanwner [posi_num]\n");
exit(1);
}
if(sigsetjmp(position,1) == 0)
{
act.sa_handler = goback;
sigaction(SIGINT, &act, NULL);
}
if(num > 1)
{
if ((pid = fork()) == 0 )
{
pid = getpid();
printf("\nParameter of process %d is %d\n", pid, num-1);
kill(pid, SIGINT);
if ((pid1 = fork()) == 0 )
{
pid1 = getpid();
printf("\nParameter of processss %d is %d\n", pid1, num-2);
kill(pid1, SIGINT);
}
}
else if (pid > 0)
{
wait((int *)0);
exit(0);
}
else
printf("forking fails!!");
}exit(0);
}
void goback(void)
{
siglongjmp(position,1);
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |