Computing.Net > Forums > Unix > C under unix

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C under unix

Reply to Message Icon

Name: ivandd
Date: February 25, 2004 at 21:36:41 Pacific
OS: netBSD
CPU/Ram: p4
Comment:

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);
}



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: C under unix

Check for corrupted data under unix www.computing.net/answers/unix/check-for-corrupted-data-under-unix/5291.html

how to run the network program under uni www.computing.net/answers/unix/how-to-run-the-network-program-under-uni/3591.html

Installing UPS under unix based systems www.computing.net/answers/unix/installing-ups-under-unix-based-systems/391.html