Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help me !

4 views
Skip to first unread message

Wind Young

unread,
Dec 20, 2005, 10:37:32 AM12/20/05
to
Hi, everyone!
I wrote a program, but it couln't run. I am very thank you for your helping
me to check the code. Here is the code:
#include < unistd.h >
#include < signal.h >
#include < sys/param.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < stdio.h >
#include < time.h >
void init_daemon(void)
{
int pid;
int i;
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
setsid();
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
for(i=0;i< NOFILE;++i)
close(i);
chdir("/tmp");
umask(0);
return;
}
typedef struct CPU_USAGE
{
unsigned long cpu_user;
unsigned long cpu_sys;
unsigned long cpu_nice;
unsigned long cpu_idle;
}*CPU_USAGE;

CPU_USAGE get_cpu_usage( )
{
CPU_USAGE usage;
FILE *fp;
char tmp[10];

fp = fopen(CPU_FILE_PROC_STAT, "r");
if ( fp == NULL )
{
perror("fopen");
return (-1);
}
fscanf( fp, "%s %lu %lu %lu %lu", tmp, &(usage->cpu_user),
&(usage->cpu_sys), &(usage->cpu_nice), &(usage->cpu_idle) );
fclose( fp );
return usage;
}

double get_cpu_usage_rate( CPU_USAGE cur, CPU_USAGE old )
{
double user, sys, nice, idle, total;
double free_rate;
user = (double)(cur->cpu_user - old->cpu_user);
sys = (double)(cur->cpu_sys - old->cpu_sys);
nice = (double)(cur->cpu_nice - old->cpu_nice);
idle = (double)(cur->cpu_idle - old->cpu_idle);
total = user + sys + nice + idle;
free_rate = ( idle / total ) * 100;
return free_rate;
}


main()
{
FILE *fp;
time_t t;
CPU_USAGE current, old;
double cpurate;
init_daemon();
while(1)
{
old = get_cpu_usage_rate( CPU_USAGE cur, CPU_USAGE old );
sleep(60);
current=get_cpu_usage_rate( CPU_USAGE cur, CPU_USAGE old );
cpurate=Get_Cpu_Free_Rate(cur, old );
if((fp=fopen("test.log","a")) >=0)
{
t=time(0);
fprintf(fp,"Im here at %s\n",asctime(localtime(&t)) );
fprintf(fp,"\n%lu",cpurate );
fclose(fp);
}
}
}

Thank you again.

--
I work hard everyday so that all who love me will be proud of me .


Sjoerd

unread,
Dec 20, 2005, 1:22:14 PM12/20/05
to
Learn to use GDB.

Kasper Dupont

unread,
Dec 20, 2005, 1:31:06 PM12/20/05
to
Wind Young wrote:
>
> Hi, everyone!
> I wrote a program, but it couln't run. I am very thank you for your helping
> me to check the code. Here is the code:
> #include < unistd.h >
[...]
> }

This code doesn't even compile. I'd say it is
pretty obvious why. There are no spaces in the
names of the header files.

It wasn't really necesarry to post such a
large amount of code for that. Do yourself
(and everybody else) a favour by removing
anything unrelated to the problem before
posting your source code.

http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#source

--
Kasper Dupont
Note to self: Don't try to allocate
256000 pages with GFP_KERNEL on x86.

Roger Leigh

unread,
Dec 20, 2005, 2:18:06 PM12/20/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Wind Young" <eswn...@163.com> writes:

> I wrote a program, but it couln't run. I am very thank you for your
> helping me to check the code. Here is the code:

The formatting of your code could do with a lot of work. Did you
write it with MS Notepad? A proper editor will help you here: try vim
or emacs. This will help you write better quality code, because
mistakes will become apparent as you type.

More importantly, it fails to compile:

$ gcc -std=c99 -Wall -pedantic -o test test.c
test.c:1:22: error: unistd.h : No such file or directory
test.c:2:22: error: signal.h : No such file or directory
test.c:3:25: error: sys/param.h : No such file or directory
test.c:4:25: error: sys/types.h : No such file or directory
test.c:5:24: error: sys/stat.h : No such file or directory
test.c:6:21: error: stdio.h : No such file or directory
test.c:7:20: error: time.h : No such file or directory
test.c: In function ‘init_daemon’:
test.c:12: warning: implicit declaration of function ‘fork’
test.c:12: warning: suggest parentheses around assignment used as truth value
test.c:13: warning: implicit declaration of function ‘exit’
test.c:13: warning: incompatible implicit declaration of built-in function ‘exit’
test.c:15: warning: incompatible implicit declaration of built-in function ‘exit’
test.c:16: warning: implicit declaration of function ‘setsid’
test.c:17: warning: suggest parentheses around assignment used as truth value
test.c:18: warning: incompatible implicit declaration of built-in function ‘exit’
test.c:20: warning: incompatible implicit declaration of built-in function ‘exit’
test.c:21: error: ‘NOFILE’ undeclared (first use in this function)
test.c:21: error: (Each undeclared identifier is reported only once
test.c:21: error: for each function it appears in.)
test.c:22: warning: implicit declaration of function ‘close’
test.c:23: warning: implicit declaration of function ‘chdir’
test.c:24: warning: implicit declaration of function ‘umask’
test.c: In function ‘get_cpu_usage’:
test.c:38: error: ‘FILE’ undeclared (first use in this function)
test.c:38: error: ‘fp’ undeclared (first use in this function)
test.c:41: warning: implicit declaration of function ‘fopen’
test.c:41: error: ‘CPU_FILE_PROC_STAT’ undeclared (first use in this function)
test.c:42: error: ‘NULL’ undeclared (first use in this function)
test.c:44: warning: implicit declaration of function ‘perror’
test.c:45: warning: return makes pointer from integer without a cast
test.c:47: warning: implicit declaration of function ‘fscanf’
test.c:47: warning: incompatible implicit declaration of built-in function ‘fscanf’
test.c:49: warning: implicit declaration of function ‘fclose’
test.c: At top level:
test.c:70: warning: return type defaults to ‘int’
test.c: In function ‘main’:
test.c:71: error: ‘FILE’ undeclared (first use in this function)
test.c:71: error: ‘fp’ undeclared (first use in this function)
test.c:72: error: ‘time_t’ undeclared (first use in this function)
test.c:72: error: syntax error before ‘t’
test.c:78: error: syntax error before ‘CPU_USAGE’
test.c:79: warning: implicit declaration of function ‘sleep’
test.c:80: error: syntax error before ‘CPU_USAGE’
test.c:81: warning: implicit declaration of function ‘Get_Cpu_Free_Rate’
test.c:81: error: ‘cur’ undeclared (first use in this function)
test.c:84: error: ‘t’ undeclared (first use in this function)
test.c:84: warning: implicit declaration of function ‘time’
test.c:85: warning: implicit declaration of function ‘fprintf’
test.c:85: warning: incompatible implicit declaration of built-in function ‘fprintf’
test.c:85: warning: implicit declaration of function ‘asctime’
test.c:85: warning: implicit declaration of function ‘localtime’
test.c:85: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
test.c:86: warning: format ‘%lu’ expects type ‘long unsigned int’, but argument 3 has type ‘double’

The cause of these errors is not difficult to see; you have spaces in
the names of your headers:

> #include < unistd.h >

There is no file named ' unistd.h '. Try "#include <unistd.h>", and
include it /after/ the headers in sys/, because what gets prototyped
depends on which sys/ headers you included.

[...]


> void init_daemon(void)
> {
> int pid;
> int i;
> if(pid=fork())
> exit(0);
> else if(pid< 0)
> exit(1);
> setsid();
> if(pid=fork())
> exit(0);
> else if(pid< 0)
> exit(1);
> for(i=0;i< NOFILE;++i)
> close(i);
> chdir("/tmp");
> umask(0);
> return;
> }

This is unreadable. This is the same function, but indented with
emacs:

void
init_daemon(void)
{
int pid;
int i;
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
setsid();
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
for(i=0;i< NOFILE;++i)
close(i);
chdir("/tmp");
umask(0);
return;
}

Notice how the branches become immediately obvious. Comments and some
whitespace between lines would also increase readability. Also see
daemon(3).

> typedef struct CPU_USAGE

Why the ALL CAPS? All Caps is conventionally used for macros. Use
lower_case or CamelCase for typedefs, structs and enums. Whichever
coding style you choose, make sure to be consistent. Read the GNU
Coding Standards for a commonly-used standard (which I use), or the
Linux CodingStyle for an alternative form.

> CPU_USAGE get_cpu_usage( )

That should be

CPU_USAGE
get_cpu_usage (void)

>
> main()
> {

NO! The correct prototype for the main function is either:

int main (void);

or

int main (int argc, char *argv[]);

Nothing else is valid C.

The source reindented and with the headers fixed follows.

Note this still fails to compile:

test.c: In function ‘init_daemon’:
test.c:14: warning: suggest parentheses around assignment used as truth value
test.c:15: warning: implicit declaration of function ‘exit’

Missing <stdlib.h>

test.c:15: warning: incompatible implicit declaration of built-in function ‘exit’

Ditto.

test.c: In function ‘get_cpu_usage’:
test.c:45: error: ‘CPU_FILE_PROC_STAT’ undeclared (first use in this function)
test.c:49: warning: return makes pointer from integer without a cast

Erk!

test.c: At top level:
test.c:72: warning: return type defaults to ‘int’
test.c: In function ‘main’:

Bad prototype.

test.c:80: error: syntax error before ‘CPU_USAGE’
test.c:82: error: syntax error before ‘CPU_USAGE’
test.c:83: warning: implicit declaration of function ‘Get_Cpu_Free_Rate’
test.c:83: error: ‘cur’ undeclared (first use in this function)
test.c:84: warning: ordered comparison of pointer with integer zero
test.c:88: warning: format ‘%lu’ expects type ‘long unsigned int’, but argument 3 has type ‘double’

These all need fixing.


Hopefully that should get you started on fixing the remaining
problems.


Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFDqFjrVcFcaSW/uEgRAicrAKDIzDp0RpM5IhvSQXw8x9o699fWXQCgxKDk
EoSiFLAvPv3VPAPBf25LxTM=
=jUem
-----END PGP SIGNATURE-----

wind young

unread,
Dec 21, 2005, 5:23:28 AM12/21/05
to
Thank you very much for your help.
Last night after I post the code here, I rewrote the program and it could
run successfully.
I am very happy. The folloing is the code, and I'm very thankful for your
reading and telling me
some mistakes or some better solutions.

#include </usr/include/unistd.h>
#include </usr/include/signal.h>
#include </usr/include/sys/param.h>
#include </usr/include/sys/types.h>
#include </usr/include/sys/stat.h>
#include </usr/include/stdio.h>
#include </usr/include/time.h>

void init_daemon(void)
{
int pid;
int i;
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
setsid();
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
for(i=0;i< NOFILE;++i)
close(i);

umask(0);
chdir("/tmp");
return;
}

main()
{
FILE *fp,*fp1;
char tmp[10];
time_t t;
double currentuser,currentsys,currentnice,currentidle,
olduser,oldsys,oldnice, oldidle;
double user,sys,nice,idle,total;
double cpurate;
init_daemon();


while(1)
{
if((fp1=fopen("/proc/stat","r"))>=0)
fscanf( fp1, "%s %lf %lf %lf %lf", tmp, &olduser, &oldsys, &oldnice,
&oldidle);
fclose(fp1);
sleep(60);
if((fp1=fopen("/proc/stat","r"))>=0)
fscanf( fp1, "%s %lf %lf %lf %lf", tmp, &currentuser, &currentsys,
&currentnice, &currentidle);
fclose(fp1);
user=(currentuser-olduser);
sys=(currentsys-oldsys);
nice=(currentnice-oldnice);
idle=(currentidle-oldidle);
total=user+sys+nice+idle;
cpurate=(idle/total)*100;


if((fp=fopen("test.log","a")) >=0)
{
t=time(0);
fprintf(fp,"Im here at %s\n",asctime(localtime(&t)) );

fprintf(fp,"\n%lf",cpurate );
fprintf(fp,"\n%lf %lf %lf %lf\n",user,sys,nice,idle,total);
fprintf(fp,"\n%lf %lf %lf %lf\n",olduser,oldsys,oldnice,oldidle);
fprintf(fp,"\n%lf %lf %lf
%lf\n",currentuser,currentsys,currentnice,currentidle);
fclose(fp);

Kasper Dupont

unread,
Dec 21, 2005, 6:09:16 AM12/21/05
to

I get eight warnings when compiling this. Four of those
are obviously bugs. And two more really needs to be
fixed. And you still need to indent your code.

Roger Leigh

unread,
Dec 21, 2005, 6:01:14 AM12/21/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"wind young" <dk...@dfj.net> writes:

> Thank you very much for your help.

You're welcome. However, please don't top-post. You quoted my entire
reply for no good reason.


- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFDqTX6VcFcaSW/uEgRAqgwAJsFxIY08MIy6CXpqJLhIeJscXkiGQCfbREq
fwpMcCqkFyULuzhPdsu91a4=
=43/n
-----END PGP SIGNATURE-----

Roger Leigh

unread,
Dec 21, 2005, 6:27:06 AM12/21/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"wind young" <dk...@dfj.net> writes:

> #include </usr/include/unistd.h>

Why are you using the full path? What's wring with <unistd.h>?


- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFDqTwIVcFcaSW/uEgRAqDWAJ49wbICy4ZXakBxNNby7i9o+nzaGQCdHyGu
35eT9BlSzFvAB7h2aeCA2P0=
=MlXl
-----END PGP SIGNATURE-----

wind young

unread,
Dec 21, 2005, 9:30:51 AM12/21/05
to
"Roger Leigh" <${rleigh}@invalid.whinlatter.ukfsn.org.invalid>
??????:877j9yl...@hardknott.home.whinlatter.ukfsn.org...

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> "wind young" <dk...@dfj.net> writes:
>
>> #include </usr/include/unistd.h>
>
> Why are you using the full path? What's wring with <unistd.h>?
>
>

In fact , I use the header files just like #include<unistd.h>, but I get
tons of errors and warnings, such as "No such file unistd.h".
Then I have to using the full path, and there is no error and warning when I
compile it. At last I find it is the environmental variable that
doesn't be set. However, I don't know how to set it in the system. By the
way , I have no root's permission of the system.

Thank you again.


Roger Leigh

unread,
Dec 21, 2005, 12:06:03 PM12/21/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"wind young" <dk...@dfj.net> writes:

> "Roger Leigh" <${rleigh}@invalid.whinlatter.ukfsn.org.invalid>
> ??????:877j9yl...@hardknott.home.whinlatter.ukfsn.org...
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> "wind young" <dk...@dfj.net> writes:
>>
>>> #include </usr/include/unistd.h>
>>
>> Why are you using the full path? What's wring with <unistd.h>?
>
> In fact , I use the header files just like #include<unistd.h>, but I
> get tons of errors and warnings, such as "No such file unistd.h".

That was originally because you put spaces in the name. It's
standard, so if it doesn't work, you've done something wrong.

#include <unistd.h>

> Then I have to using the full path, and there is no error and
> warning when I compile it. At last I find it is the environmental
> variable that doesn't be set.

There is no environment variable. /usr/include is in the default
search path. Use -I to add extra dirs. "info gcc" for more detail.

> However, I don't know how to set it in the system. By the way , I
> have no root's permission of the system.

You don't need root.


- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFDqYt5VcFcaSW/uEgRAoqoAJ4j4bcVZKmDp7yMUQSpR0OIIi4zAwCgkr8Z
jvmz+p3v+k6PejGnvsfB3fM=
=Apad
-----END PGP SIGNATURE-----

wind young

unread,
Dec 22, 2005, 7:33:13 AM12/22/05
to

"Roger Leigh" <${rleigh}@invalid.whinlatter.ukfsn.org.invalid>
??????:87wthyj...@hardknott.home.whinlatter.ukfsn.org...

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> "wind young" <dk...@dfj.net> writes:
> That was originally because you put spaces in the name. It's
> standard, so if it doesn't work, you've done something wrong.
>
> #include <unistd.h>
>
>> Then I have to using the full path, and there is no error and
>> warning when I compile it. At last I find it is the environmental
>> variable that doesn't be set.
>
> There is no environment variable. /usr/include is in the default
> search path. Use -I to add extra dirs. "info gcc" for more detail.
>
>> However, I don't know how to set it in the system. By the way , I
>> have no root's permission of the system.
>
> You don't need root.
>
>
Thank you again .
I see.


0 new messages