Computing.Net > Forums > Unix > C program

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 program

Reply to Message Icon

Name: lisa
Date: March 28, 2006 at 12:55:04 Pacific
OS: xp
CPU/Ram: pentium me
Product: toshiba
Comment:

i have a problem with SIGUSR1 and SIGUSR2
i coudnt understand it....

could you help me with this problem:

Write a C program (give it the name: family.c) which receives no arguments. The program should fork exactly two children. One child should sleep for 2 seconds and then send the signal SIGUSR1 to his parent. The other child should sleep for 5 seconds and then send the signal SIGUSR2 to his parent. The parent, while busy counting from 0 to 9 –sleeping one second after each count- will ignore the first signal but handle the other signal with a signal handler that just prints the following: “Son! Stop making noise!”

this my solution but without using the signal

int main (void)
{
int child_pid;
int child_pid1;
int i;



/* define the signal handler for the CHLD signal */

/* and the child process forking code... */
child_pid = fork();
switch (child_pid) {
case -1: /* fork() failed */
perror("fork");
exit(1);
case 0: /* inside child process */

sleep(2); /* sleep a little, so we'll have */
/* time to see what is going on */
exit(0);
default: /* inside parent process */
break;


child_pid1 = fork();
switch (child_pid1) {
case -1: /* fork() failed */
perror("fork");
exit(1);
case 0: /* inside child process */

sleep(5); /* sleep a little, so we'll have */
/* time to see what is going on */
exit(0);
default: /* inside parent process */
break;
}


/* parent process goes on, minding its own business... */
/* for example, some output... */
for (i=0; i<10; i++) {
printf("%d\n", i);
sleep(1); /* sleep for a second, so we'll have time to see the mix */
}

return 0;
}




Sponsored Link
Ads by Google

Response Number 1
Name: lchi2000g
Date: March 28, 2006 at 14:35:22 Pacific
Reply:

...
default: /* inside parent process */
break; <=== The problem is here. Remove this line.


child_pid1 = fork();
switch (child_pid1) {
case -1: /* fork() failed */
perror("fork");
exit(1);
case 0: /* inside child process */

...

Luke Chi


0
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 program

Call shell script from C++ program www.computing.net/answers/unix/call-shell-script-from-c-program/2276.html

learning C programming for UNIX www.computing.net/answers/unix/learning-c-programming-for-unix/5159.html

C program for RS232 www.computing.net/answers/unix/c-program-for-rs232-/127.html