Delay a process.

How to delay a process.

I need to to delay a process from 3sec.
At that 3sec other back ground processes also should stop.
(just sit 3sec for idle & then starts execution as normally)

I use sleep(3)-But it not stop the bg processes
I try to use loop but it not gurantee to wait 3sec.

What i want to do is,
A();
B();
//should wait for 3sec
C();
//should execute-A,execute-B,Strats to sleep,Stop sleep,execute-C

If i use like this,
A();
B();
sleep(3);
C();
//it execute-A,execute-B,Strats to sleep,execute-C in bg,Stop sleep

How to do?

introducing a sleep would effect the process,
either it is a foreground or background process

if you are so particular in guaranteeing 3 seconds
use sigALARM for 3 seconds

check for the following code;

# include<stdio.h>
# include<time.h>

void a();
void b();
void c();

int main()
{
  clock_t c1, c2, c3;
  c1=clock();

  (void)a();

  c2=clock();
  fprintf(stderr, "Diff returning from fun a %ld\n", c2 - c1);
  c1=clock();

  (void)b();

  c2=clock();
  fprintf(stderr, "Diff returning from fun b %ld\n", c2 - c1);
  c1=clock();
  sleep(3);

  (void)c();
  c2=clock();
  fprintf(stderr, "Diff returning from fun c %d\n", c2 - c1);
  return 0;
}

void a()
{
  int i;
  fprintf(stderr, "in a\n");
  for( i=0; i<500000; i++);
}
void b()
{
  int i;
  fprintf(stderr, "in b\n");
  for( i =0; i<200000; i++);
}
void c()
{
  int i;
  fprintf(stderr, "in c\n");
  for( i =0; i<600000; i++);
}

the above code uses clock function and hence only the real time used by the process would be affected the sleep(3) and not the cpu time.
clock function will account only for the CPU and system time and not the context switch to sleep state and hence sleep cannot track the 3 seconds

you need to use the following structure in /usr/include/sys/time.h

struct tms {
  clock_t  tms_utime;     /* user time */
  clock_t  tms_stime;     /* system time */
  clock_t  tms_cutime;    /* user time, children */
  clock_t  tms_cstime;    /* system time, children */
};

hope this helps!!!

What Matrix shows works just fine.

But I think this sounds like it is related to the ipc (interprocess communication) problem you posted earlier. You may want to send a signal to your child processes after three seconds, telling them to stop. The parent process can call waitpid to reap the children, then the parent can exit, too.