By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,655 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,655 IT Pros & Developers. It's quick & easy.

switch + fork

P: 18
ok a have the next code:
Expand|Select|Wrap|Line Numbers
  1.       /* we use a fork to create a child process that sniffs the answers */
  2.       switch(pid = fork())
  3.     {
  4.     case -1: 
  5.       pe("Error fork");
  6.       exit(1);
  7.     case 0:
  8.       sniffer(filter,nbr_pkt, 0, dev, resolve);
  9.       exit(1);
  10.       break;
  11.     default:
  12.       /* we wait to let the sniffer starts */
  13.       sleep(1);
  14.       /* we send our packets */
  15.       while (nbr_pkt > 0) {
  16.         if (ack_flag == 1)    
  17.           send_tcp(srcaddr_r, htons(srcport), dstaddr_r, htons(dstport), th_seq, th_ack,  0, ttl, htons(ipid));
  18.         if (syn_flag == 1) {
  19.           th_ack = 0;
  20.           send_tcp(srcaddr_r, htons(srcport), dstaddr_r, htons(dstport), th_seq, th_ack,  1, ttl, htons(ipid));
  21.         }
  22.         --nbr_pkt;    
  23.       }
  24.       /* we start a timer :after 3 seconds, a signal SIGALRM is emitted */
  25.       alarm(3);
  26.       /* we wait for any signal (SIGALRM from hprobe or SIGCHLD from the sniffer) */
  27.       pause();
  28.     }
  29.       exit(0);
  30.     }
what i am interested to knew is what is happened when the exit(1) executes in the child process, why the sniffer isn't stoping and it keeps running, from what i knew the exit() function shall exit from the program, but in this case it is something else, and i don't understand how that is done.
thx. and if you have any questions regarding any another aspect of the program that will help you explain me this statement i will gladly answer
Jun 14 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 208
Could you please put your code in [code] brackets to make it more readable...Thanks
Jun 14 '07 #2

P: 18
ok a have the next code:
Expand|Select|Wrap|Line Numbers
  1.       /* we use a fork to create a child process that sniffs the answers */
  2.       switch(pid = fork())
  3.     {
  4.     case -1: 
  5.       pe("Error fork");
  6.       exit(1);
  7.     case 0:
  8.       sniffer(filter,nbr_pkt, 0, dev, resolve);
  9.       exit(1);
  10.       break;
  11.     default:
  12.       /* we wait to let the sniffer starts */
  13.       sleep(1);
  14.       /* we send our packets */
  15.       while (nbr_pkt > 0) {
  16.         if (ack_flag == 1)    
  17.           send_tcp(srcaddr_r, htons(srcport), dstaddr_r, htons(dstport), th_seq, th_ack,  0, ttl, htons(ipid));
  18.         if (syn_flag == 1) {
  19.           th_ack = 0;
  20.           send_tcp(srcaddr_r, htons(srcport), dstaddr_r, htons(dstport), th_seq, th_ack,  1, ttl, htons(ipid));
  21.         }
  22.         --nbr_pkt;    
  23.       }
  24.       /* we start a timer :after 3 seconds, a signal SIGALRM is emitted */
  25.       alarm(3);
  26.       /* we wait for any signal (SIGALRM from hprobe or SIGCHLD from the sniffer) */
  27.       pause();
  28.     }
  29.       exit(0);
  30.     }
what i am interested to knew is what is happened when the exit(1) executes in the child process, why the sniffer isn't stoping and it keeps running, from what i knew the exit() function shall exit from the program, but in this case it is something else, and i don't understand how that is done.
thx. and if you have any questions regarding any another aspect of the program that will help you explain me this statement i will gladly answer
I understand now for what is the exit(1) function call, it will generate an SIGCHLD signal that will be caught later in the program with the signal() function.
Now i what to know what is the break instruction roll, and it will ever be executed, if will look that the exit(1) function is just above it?
Jun 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.