Re: [ILUG] Programming problem

From: Philip Reynolds (phil at domain redbrick.dcu.ie)
Date: Mon 24 Jul 2000 - 14:28:24 IST


Thomas Degris's [t_thomas at domain trados.ie] 27 lines of dribble included:
:>Hello, I don't know if it the right mailing list but I am trying to program
:>a little thing for Linux and I get two problems.
:>
:>The first is I would like use the function rand and I can't find out
:>(sorry, I am net very clever you know...) how I can initialize the series
:>of random number with the clock or something like that. I get each time the
:>same serie.

the function rand() is in stdlib.h, so a line
#include <stdlib.h>
At the start is what you want...

Basically to use it, you have to seed the randomizer first, generally done
using srand(time(NULL)) or srand(getpid())

To use time, you need to include time.h
To use getpid you need to include unistd.h

then calling rand() will produce random number type things.

:>My second problem is I would like run another program. So, I use :
:>pid=fork();
:>if (pid==1) { //maybe it's zero, I can't remember
:> execv(myprogram,argv);
:> exit(0);
:>}

fork() basically makes a child process which is an exact copy of the process
calling it (i.e. the program you are running), apart from some stuff mentioned
in the man pages and of course the PID of the child process =).
man fork should give you all the info.
I think execve might be what you want (again man pages explain better than I
do)
Phil.



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:06:58 GMT