473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sched.h, offsetof( ) function

8
I need to find an offset of a pointer within a structure.

I'm using Linux, and I need to find the offset of a pointer member withing task_struct which is defined in sched.h as follows

Expand|Select|Wrap|Line Numbers
  1. struct task_struct {
  2.     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
  3.     struct thread_info *thread_info;
  4.     atomic_t usage;
  5.     unsigned long flags;    /* per process flags, defined below */
  6.     unsigned long ptrace;
  7.  
  8.     int lock_depth;        /* BKL lock depth */
  9.  
  10. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  11.     int oncpu;
  12. #endif
  13.     int prio, static_prio;
  14.     struct list_head run_list;
  15.     prio_array_t *array;
  16.  
  17. .... /*ect*/
  18.  
  19. ...
  20.  
  21. #define next_task(p)    list_entry((p)->tasks.next, struct task_struct, tasks)
  22. #define prev_task(p)    list_entry((p)->tasks.prev , struct task_struct, tasks)
  23.  
  24. #define for_each_process(p) \
  25.     for (p = &init_task ; (p = next_task(p)) != &init_task ; )
  26.  
The code snippet I am using is the following. I am using the 'offsetof( ) function to obtain the offset of the given pointer declared in the said structure.

Expand|Select|Wrap|Line Numbers
  1. #include <stddef.h>
  2. #include </usr/include/linux/sched.h>
  3.  
  4. int
  5. main
  6. ()
  7. {
  8.  
  9.     int A;
  10.     A = offsetof(struct task_struct, next_task);
  11.     printf ("%d \n", A);
  12. }
  13.  
I have tried this piece of code by declaring a small simple struct within the program, and it has worked.
However, ever since I've included the sched.h headerfile, I have been getting a neverending series of compliation errors from all the other h files included in sched.h
Am i making incorrect use of the headerfiles?

I am using gcc with a Bash shell.
Apr 18 '07 #1
4 2676
JosAH
11,448 Expert 8TB
Am i making incorrect use of the headerfiles?
Yes you are: you shouldn't include that file all by itself, i.e. it needs other
files that will include that file for you and define other necessary hulla-baloo
needed by the files included by that sched.h file. I'm not at my manuals now
so I can't help you any further with that now; one more thing: offsetof() is
innocent in this all and your use of it is correct ;-)

kind regards,

Jos
Apr 18 '07 #2
Andu
8
Yes, i was afraid of that....

I am at a loss at how to solve that, because i did try copying and pasting all the includes that sched.h has onto my program and i even went as far as including headerfiles that were included in the includes, and still i got a neverending list of errors.

Header files have always eluded me.....

But glad to know that the offsetof( ) is the innocent party :)
Apr 18 '07 #3
JosAH
11,448 Expert 8TB
Yes, i was afraid of that....

I am at a loss at how to solve that, because i did try copying and pasting all the includes that sched.h has onto my program and i even went as far as including headerfiles that were included in the includes, and still i got a neverending list of errors.

Header files have always eluded me.....

But glad to know that the offsetof( ) is the innocent party :)
I'm still not at my manuals, but here's a little trick/hack I use sometimes:
find some kernel sources (.c files) and grep for "task_struct" in them.
Those .c files certainly compile so they have included the correct .h files.

Just do as the Romans do and copy those #include directives to your own
.c file(s). See if they compile. If they do leave out an #include directive and
try again. You can figure out what #include directives to use in your own
sources then. I know it's a dirty hack/trick; but without manuals everything
is allowed ;-)

Basically those #include files are simple: they are simply included in your
own compilation unit. If you draw a graph off all that include stuff, it's just
a DAG (Directed Acyclic Graph) and the #ifndef FOO #define Foo #endif
thingies prevent cyclic include directives.

kind regards,

Jos
Apr 18 '07 #4
Andu
8
Although that sounded like a brilliant plan, I've tried it. It didn't work.. and I can't understand why that didn't work to be honest :S

I did try specifically as you said, I looked up quite a number of kernel source codes that referenced the task_struct, i copied their includes, and it still didn't work.. I was getting the exact same list of errors as before.

The funny thing is that when i cross referenced the different source code files that referenced task_struct, the only include that they had in common was the sched.h file...

This problem really seems to have it's heart set on tormenting me!

Do you know if there's somewhere I can look up exactly how to do this?

thanks a lot for your help :)
Apr 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Exits Funnel | last post by:
Consider this code which is a very trimmed down version of some I've inherited and am trying to port from windows to g++: //Begin test1.cpp class foo { int i; int j; }; class bar { bar (int...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
7
by: Fred Zwarts | last post by:
Consider the following definition: typedef struct { int a; int b; } s; Now I have a function void f (int i) { ... }
2
by: ycoci0 | last post by:
Hi all, Not exactly new to Python, just have not programmed a time dependent function using it before. I imagine that many of you may also program some JavaScript and may be familiar with...
8
by: Pawel | last post by:
Hallo group members. //p1.cpp #include <stdio.h> #include <linux/stddef.h> struct Person { int m_age; char* m_name; };
24
by: Francine.Neary | last post by:
Just out of personal curiosity :) What do people use offsetof() for? I mean, I can understand why you'd want to be able to take the address of a member of a struct, but you can do that with just...
2
by: Imre | last post by:
Hi I know that offsetof is basically a C leftover, and only works for POD types, not classes, so it is recommended that I use pointers to members instead. However, I have a problem where I don't...
5
by: mihirtr | last post by:
Hi, I have following structure. typedef struct{ char *abc; dummy_struct *tmp; char *xyz; }my_struct; I have pointer to tmp variable in this structure. Can I use offsetof()
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.