LinuxQuestions.org
Visit Jeremy's Blog.
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 01-27-2009, 01:21 AM   #1
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Rep: Reputation: 15
Question Questions about profil.


1.Why doesn't it print the statics with the following code?
2.If it prints "buf[59] = 12", what does "12" mean?

Code:
      5 int buf[4096];
      6
      7 int main(void)
      8 {
      9     int scale, pos0, pos1, text;
     10     extern f(), g(), theend();
     11     pos0 = (int)main;
     12     pos1 = (int)theend;
     13     int i;
     14     for(i = 0;i < 4096;i++)
     15         buf[i] = 0;
     16
     17     printf("main: 0x%02X, f: 0x%02X, g: 0x%02X\n", pos0, (int)f, pos1);
     18     scale = 0xffff;
     19     text = pos1 - pos0;
     20     profil(buf, text, pos0, scale);
     21     for(i = 0;i < 100;)
     22     {
     23         f();
     24         g();
     25     }
     26     sleep(10);
     27     theend();
     28     printf("end\n");
     29
     30     return 0;
     31 }
     32 int f()
     33 {
     34     return 0;
     35 }
     36 int g()
     37 {
     38     return 0;
     39 }
     40 void theend()
     41 {
     42     int i;
     43     for(i = 0;i < 4096;i++)
     44         if(buf[i] != 0)
     45             printf("buf[%d] = %d\n", i, buf[i]);
     46 
     47 }
 
Old 01-27-2009, 03:44 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
If you want help, please specify the programming language that you are using.

Additionally, have you typed in this code?

What do you get?

I'm assuming that this is C code.

There are a number of issues that I got trying to compile this code (as was)
  1. theend is a function with type void, but you are setting a parameter to the value returned from this function.
  2. stdio is not included (in the part of the code that you provided)
  3. the functions should be located above the point at which they are called
  4. setting a parameter to the return from main() within main() will cause a core dump
 
Old 01-27-2009, 05:48 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Disillusionist spoke well. I'd like to approach this with slightly different words; between Disillusionist's words and mine, perhaps you can move forward even better.

First, I'm puzzled by Disillusionist's request that you specify the language. If it's not C, it's almost certain to be something close enough to C that we can use a C compiler to help find the problem. Additionally, most people who present a problem in a language that looks like C, but isn't C, will go out of their way to specify the language.

Second, Disillusionist is right on the mark by asking you to present clean code: code which will compile without errors. You're going to have to do so anyway to run your program; and your comments indicate that you actually got the program to run, even if it didn't do what you expect. Why not show us the clean code? It's only common courtesy, so we don't have to guess, or spend a lot of time correcting your code and hoping we corrected it the same way you did.

The only exception to that would be if you can't get the code to compile cleanly, and you can't figure out one of the syntax error messages. Then post the complete, exact code the way you compiled it, and post the exact error messages, as I'm about to do. If the program is huge, then whittle it down to a tiny complete program which won't compile and gives the syntax error message which is bothering you.

Rather than paraphrase the error messages, as Disillusionist did, I would either have said, "Hey, bucko, clean up your code!" or, if I were in a more civilized frame of mind than that, I would have raised my eyebrows innocently and said, "You got this to compile? Here are the error messages I got when I tried to compile it. Are you leaving out anything that we should know?" In that spirit, here's what I got upon compilation:
Code:
2.c: In function 'main':
2.c:13: warning: incompatible implicit declaration of built-in function 'printf'
2.c:26: error: expected ';' before 'return'
2.c: At top level:
2.c:37: error: conflicting types for 'theend'
2.c:6: error: previous declaration of 'theend' was here
2.c: In function 'theend':
2.c:41: warning: incompatible implicit declaration of built-in function 'printf'
When Disillusionist refers to "parameter" in his points 1 and 4, it would be more customalry to use the phrase "local variable", or just "variable".

Most of Disillusionist's other points will disappear when you present us with code that compiles, as you surely must have done to get it to run at all, even incorrectly.
 
Old 01-27-2009, 10:38 PM   #4
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Thanks.
The code I posted is a section of old C code in UNIX OS designing.I typed the code and compiled it.No warnings and errors.Here is the full version:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
                                                                                                                                                                               
unsigned int buf[4096];
                                                                                                                                                                               
int main(void)
{
    int scale, pos0, pos1, text;
    extern f(), g(), theend();
    pos0 = (int)main;
    pos1 = (int)theend;
    int i;
    for(i = 0;i < 4096;i++)
        buf[i] = 0;
    signal(SIGINT, (void *)theend);
    printf("main: 0x%02X, f: 0x%02X, g: 0x%02X\n", pos0, (int)f, pos1);
    scale = 0xffff;
    text = pos1 - pos0;
    profil(buf, text, pos0, scale);
    for(i = 0;i < 100;i++)
    {
        f();
        g();
    }
    sleep(3);
    theend();
    printf("end\n");
                                                                                                                                                                               
    return 0;
}
int f()
{
    return 0;
}
int g()
{
    return 0;
}
void theend()
{
    int i;
    for(i = 0;i < 4096;i++)
        if(buf[i] != 0)
            printf("buf[%d] = %d\n", i, buf[i]);
    exit(0);
}
Accordin to the specification of routine profil, the kernel checks the PC every 10 milliseceonds, and set the counter in the corresponding buffer(here is the buf[4096]), but when the program exits, buf[4096] only contains ZERO.
 
Old 01-27-2009, 10:47 PM   #5
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Oh, I forgot, thanks for the suggestions you gave me, they are good surely,I got them.

Thanks,Disillusionist and wje_lq!
 
Old 01-27-2009, 10:54 PM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
When I compile the code you posted in post 4 of this thread, I get this error output:
Code:
2.c:41: error: conflicting types for 'theend'
2.c:10: error: previous declaration of 'theend' was here
 
Old 01-28-2009, 02:25 AM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
cricketlong, I got the same errors as wje_lq and needed to change the function type of theend to get it to compile:
Code:
int theend()
{
    int i;
    for(i = 0;i < 4096;i++)
        if(buf[i] != 0)
            printf("buf[%d] = %d\n", i, buf[i]);
    return 0;
}
 
Old 01-28-2009, 03:17 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
cricketlong, the changes suggested by Disillusionist seem to fix the syntax error, but of course there was more than one way to get the syntax error to go away.

So my question to you is this: does Disillusionist's suggested change match what you actually did?
 
Old 01-28-2009, 11:05 PM   #9
cricketlong
Member
 
Registered: Dec 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Maybe we are using different compilers, I can compile the code I posted in post 4 in this thread with gcc-3.2.2 and then got it to run, my only problem is that I can not get the output I am expecting.
And one more, I do not about to discuss syntax here.I just want to find the answers to the questions I posted in post 1.

Thanks every one.
 
Old 01-29-2009, 01:07 AM   #10
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Quote:
And one more, I do not about to discuss syntax here.I just want to find the answers to the questions I posted in post 1.
Exactly. And so do we. But so we can avoid wasting our time as volunteers who are helping you at no charge, it is reasonable to find out exactly what source you used to get an error-free compile. It saves a lot of unnecessary chasing around, and it often helps us tell you, "It might seem that your problem is here, but it's really there."

And this case is a perfect example of that. You seem to have source code which compiles without error, although you redeclare with conflicting type the function named theend().

And when we came back with an objection that it didn't compile without error, you said, "It did for me under gcc 3.2.2."

That was not a dead-end part of the conversation. It's quite useful. I don't have gcc 3.2.2, but I have gcc 3.2.3 on my laptop (Slackware 9.1), and sure enough, it compiles without error, just as you said.

The reason it's important to niggle about this is that now I have a compiler I can use to reproduce your situation. I can't be bothered to pursue the problem elsewise, because I have a life (though my wife might beg to differ) and time is short.

Now:

There were several problems with your program. One of them is that scale wasn't set right. Read the man page for details; I did, and came up with a better value.

But the main one is that, as the man page clearly states, the buffer is changed only once every virtual 10 milliseconds; that is, only once every 10 milliseconds of compute time, not wall time. And since you're measuring where your program spends its compute time, this is probably what you want. But your program occupies hardly any compute time at all. The 3 second sleep doesn't change that, because you're not computing during that time. Why did you even put the 3 second sleep in there, anyway? In any case, you certainly want to rack up more compute time.

So I came up with this, and compiled it using gcc 3.2.3:
Code:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
                                                                                                                                                                               
unsigned int buf[4096];
                                                                                                                                                                               
int main(void)
{
    int scale, pos0, pos1, text;
    extern f(), g(), theend();
    pos0 = (int)main;
    pos1 = (int)theend;
    int i;
    for(i = 0;i < 4096;i++)
        buf[i] = 0;
    signal(SIGINT, (void *)theend);
    printf("main: 0x%02X, f: 0x%02X, g: 0x%02X\n", pos0, (int)f, pos1);
    /* scale = 0xffff; */
    text = pos1 - pos0;
    scale=sizeof(buf)*65536/(text);
    /* profil(buf, text, pos0, scale); */
    profil(buf, sizeof(buf), pos0, scale);
    while(clock()/CLOCKS_PER_SEC<1)
    {
      for(i = 0;i < 100;i++)
      {
          f();
          g();
      }
    }
    sleep(3);
    theend();
    printf("end\n");
                                                                                                                                                                               
    return 0;
}
int f()
{
    return 0;
}
int g()
{
    return 0;
}
void theend()
{
    int i;
    for(i = 0;i < 4096;i++)
        if(buf[i] != 0)
            printf("buf[%d] = %d\n", i, buf[i]);
    exit(0);
}
I kept the sleep(3), because it doesn't make any difference. The output I got was this:
Code:
main: 0x804843C, f: 0x8048554, g: 0x8048568
buf[2375] = 65536
buf[2976] = 9
buf[3085] = 589824
buf[3167] = 589824
buf[3222] = 9
buf[3822] = 983040
buf[3932] = 2
buf[3959] = 4
buf[3986] = 196608
Then I ran it again, and got this:
Code:
buf[2375] = 65536
buf[2976] = 5
buf[3085] = 655360
buf[3167] = 524288
buf[3222] = 11
buf[3822] = 786432
buf[3932] = 3
buf[3959] = 7
buf[3986] = 131072
buf[4068] = 65536
And a third time I got this:
Code:
buf[2703] = 1
buf[2976] = 2
buf[3085] = 720896
buf[3167] = 524288
buf[3222] = 15
buf[3822] = 1245184
buf[3932] = 5
buf[3959] = 6
It's going to be imprecise, of course. Your compute time is measured not only by how much time you spend in your program, but also by how much time the kernel spends computing on your behalf, and this can vary depending on many things I don't understand. I used the time command in front of the command to execute the program, and got this:
Code:
real    0m4.002s
user    0m0.830s
sys     0m0.170s
and then this:
Code:
real    0m4.003s
user    0m0.800s
sys     0m0.200s
and then this:
Code:
real    0m4.008s
user    0m0.810s
sys     0m0.190s
And yes, those multiples of 65536 in the program's output look sleazy, but that's probably explainable by artifacts of dealing with time precision. I don't really know.

I wasn't willing to do this until I knew that you and I were on the same page with what would and would not compile. My recommendation:
  1. Upgrade your compiler, so you and the people whose advice you seek are on the same page. Besides, it's really bad for the compiler to allow you to have the syntax you did. You may end up someday spending much time hunting down bugs which are caused by erroneous but permitted syntax. And I know that upgrading a compiler is a pain. So, if you want to avoid doing that:
  2. If you seek advice without upgrading your compiler, and advisors complain about syntax errors, just grit your teeth, fix the errors, and rerun your tests.
Hope this helps.

Last edited by wje_lq; 01-29-2009 at 03:11 AM. Reason: typos (typoes?)
 
  


Reply


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
basic questions on hostname and domain name + related postfix questions Moebius Linux - Newbie 7 09-04-2007 12:50 PM
LXer: Questions Answered, Questions Posed - What Happened? LXer Syndicated Linux News 0 05-14-2007 03:46 PM
swaret, questions, questions!! BashTin Slackware 11 04-26-2007 04:37 AM
Solaris - Questions! Questions! Questions! qs_tahmeed Solaris / OpenSolaris 2 07-16-2005 06:27 AM
window manager questions and/or theme questions t3gah Linux - Software 2 02-27-2005 05:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration