LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-23-2010, 08:53 PM   #1
TerryTham
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
Sleep ()


Dear Linuxquestion,

Problem statement:
i have a program using fork(), Parent/Child to activate a signal by kill() function. In order to execute the process, my child function required to give a sleep() function and quit after my signal() process is done. Here is the code:

Code:
/*main*/
        
pid_t   ret;                   
int     status;         
int     role = -1;          

ret = fork();
if (ret >0) /*parent content*/
{
  printf( " Parent Process: Arm Operating (pid %d)\n", getpid() );
  signal( SIGUSR1, RWCArmWash );
  role = 0;
  pause();
  ret = wait( &status );
}
  else if (ret == 0) {
  usleep (100);
  role = 1;
  kill( getppid(), SIGUSR1 );
  sleep (55);
}
Code:
/*signal function*/
void RWCArmWash ( int sig_num )
{
  int arm=0;
  int status=4;

  libInit();
  usleep(100);

  printf( "Parent (%d) got the SIGUSR1\n", getpid() );

  arm = ARMconnectToRobot();

  arm = ARMinitializeAndCalibrate ();

  arm = ARMreturnHomePosition ();

  arm = ARMdoWash (100, 10, true, 1);
  printf("\nArm Operating, Return Code = %d\n",arm);

  usleep(100);
  libClose();
}
The code can be debug and can be executed,
-----------------------------------------------------------------

My Question:

1. Due to the ARMdoWash () function required a period of time to perform, The Sleep() function is safe to use ? does it give any trouble ?

2. Because of the ARMdoWash () function can be various of time period (depend on the work cycle of the robot arm), to always adjust the sleep () time does not giving the best idea. Can it have a better way to replace this technique ?

-----------------------------------------------------------------

Thanks for the time, would appreciated for the help

Terry
 
Old 05-24-2010, 02:24 AM   #2
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
It is also required to handle zombie process:
http://<br /> http://murugesan.webn.../zombie/<br />

I am not sure on the implemented purpose of the following
Quote:
sleep (55);
from the child process.

That can be done as per the implemented source code, provided you are working on enhancement/modification/new proposal on existing code.

This can ALSO be modified/implemented in the following way:
Parent:
ignore SIGCHLD signal
Create child Process
signal( SIGUSR1, RWCArmWash );
wait for child process using wait system call
On receiving the signal SIGUSR1 perform the action in RWCArmWash
Once done with the action in "RWCArmWash"
parent can send signal to child process
Child Process:
perform any child action
send SIGUSR1 signal to parent process
go in a infinite while loop
(wait for the signal to receive it)


This proposal is taking the information that the parent process can create only one child process.


This will go further if the parent process creates many child process and doing its job in an infinite while loop (some what similar to ftp server handling the ftp clients).
 
Old 05-24-2010, 02:25 AM   #3
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
URL mentioned earlier:
http://murugesan.webnode.com/technical/c/zombie/
 
Old 05-24-2010, 08:35 PM   #4
TerryTham
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the reply.

i was trying to eliminate the sleep() function.
Therefore, im using variable to communicate through parent/child. Doing some pipe or some other IPC to passing variable. hopefully manage to do it.
 
Old 05-24-2010, 08:35 PM   #5
TerryTham
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the reply.

i was trying to eliminate the sleep() function.
Therefore, im using variable to communicate through parent/child. Doing some pipe or some other IPC to passing variable. hopefully manage to do it.
 
Old 05-25-2010, 09:41 PM   #6
eryn
Member
 
Registered: Jul 2009
Posts: 43

Rep: Reputation: 15
The problem is solved by modifying the ARMdoWash source code to test for position in a while loop.
 
  


Reply

Tags
sleep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sleep() munna_dude Programming 15 05-17-2007 04:25 AM
use of sleep AdmiralFreezbee Linux - Software 7 12-09-2005 02:32 AM
Searching for Server (BOOTP/DHCP) <sleep> <sleep> .. .. Eileen Linux - Networking 12 10-21-2005 01:14 AM
sleep TroelsSmit Linux - Newbie 10 05-22-2004 01:17 AM
sleep liguorir Linux - Software 1 08-10-2003 03:53 PM


All times are GMT -5. The time now is 09:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration