Thread: problem with struct?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    problem with struct?

    It seems that there is some problem with struct TCB
    but I cannot find out what's wrong with it.
    Please help!
    Thank you very much!

    The head file is the following:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Code:
    #ifndef UTHREAD_H 
    #define UTHREAD_H 
    
    #include <stdio.h> 
    #include <ucontext.h> 
    #include <semaphore.h> 
    #include <unistd.h> 
    #include <stdlib.h> 
    #include <errno.h> 
    #include <signal.h> 
    #include <sys/time.h> 
    
    /* you might find some of these definitions helpful */ 
    #define THR_QUANTUM_DEFAULT 5 /* default time quantum ms */ 
    #define THR_STACK_DEFAULT 65536 /* default stack bytes */ 
    #define THR_FAILURE -1 /* unable to create thread */ 
    #define THR_NOSUCHTHREAD -1 /* bad access to thread */ 
    #define MAX_JOIN_NUM 100 //maximum joinalbe number of thread 
    #define MILLION 1000000L    //convert sec to msec 
    
    #ifdef __CPLUS_PLUS__ 
    // C++:  Provide C linkages 
    namespace "std" { 
      extern "C" { 
    #endif 
    
        typedef int ThreadId_t; 
    
        /* thread states */ 
        typedef enum { 
          BLOCKED, /* blocked */ 
          RUNNING, /* currently dispatched */ 
          READY, /* in ready queue */ 
          COMPLETED, /* returned from thread */ 
          KILLED, /* killed by another thread 
    * (you don't need to implement this one) 
    */ 
        } ThreadState_t; 
    
    typedef enum { 
          THR_QUIET, /* no information */ 
          THR_SWITCH, /* report thread switch */ 
          THR_SUMMARY /* provide the same information as in thr_summary */ 
        } thr_diagnostic_t; 
      
        /* Thread control block 
        * Add to this structure as you see fit, but you may not 
        * change the name 
        */ 
        typedef struct { 
          ThreadId_t  id; /* unique identifier */ 
          ThreadState_t state; 
      ucontext_t context; //record context 
      void *result; //record final result 
      ThreadId_t j[MAX_JOIN_NUM]; //maxinum number of joinable thread for thr_join 
      int acn; //record the active number of thread 
      struct timeval exest; //record start of exe time 
      struct timeval exendt; //record end of exe time 
      struct timeval qust; //record start time in READY queue 
      long qtime; //record end time in READY queue 
      thr_diagnostic_t debug; //set debug 
      double acw; //average context switch time 
    58   TCB *next; 
        } TCB; 
    
    TCB *head; 
    
    
    64    void thr_init(); /* Initialize thread package */ 
        
    65    void thr_quantum(int N_ms);   /* Set time quantum to N ms */ 
    
    66    ThreadId_t thr_create(void * (*start_fn)(void *), void *arg); 
    
    67    void thr_exit(void *result); 
    
    68    int thr_join(ThreadId_t id, void **result); 
    
    69    void thr_diagnostics(thr_diagnostic_t Verbosity); 
    
    70    void thr_summary(FILE *FileHandle); 
    #ifdef __CPLUS_PLUS 
      } 
    } 
    #endif 
      
    #endif
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    thr_init.c is the following:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Code:
    #include "uthread.h" 
    
    void thr_init() 
    { 
    // initiate thread queue 
    head = (TCB *)malloc(sizeof(TCB)); 
    head->next = NULL; 
    head->id = 0; 
    head->state = RUNNING; 
    head->result = NULL; 
    head->acn = 1; 
    head->qtime = 0; 
    
    。。。。。。。。。。。。。。 
    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    when I compile, the following problems always comes out:
    "thr_init.c", line 1: warning: invalid white space character in directive (why it is invalid?)
    "uthread.h", line 58: syntax error before or at: TCB
    "uthread.h", line 64: member cannot be function: thr_init
    "uthread.h", line 65: member cannot be function: thr_quantum
    "uthread.h", line 66: member cannot be function: thr_create
    "uthread.h", line 67: member cannot be function: thr_exit
    "uthread.h", line 68: member cannot be function: thr_join
    "uthread.h", line 69: member cannot be function: thr_diagnostics
    "uthread.h", line 70: member cannot be function: thr_summary
    Last edited by Salem; 09-27-2008 at 11:37 PM. Reason: THINK what the code tags message is telling you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Do you really have something like "# ifndef" on line 1, where there's a space between?

    2. You cannot use a typedef name before it has been typedef'ed. So TCB isn't a valid name until you get to TCB; at the end of line 59. That does you no good in line 58. You'll have to give the struct a real name, like
    Code:
    typedef struct realname {
      /* all your stuff */
      struct realname *next;
    } TCB;
    The other errors are as if the compiler couldn't find the end of the typedef; fix these other errors first and see what happens.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    your recommendation works.
    Thank you very much!

    but there is still a problem with "warning: invalid white space character in directive"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Notice that that's line 1 of "thr_init.c", not this header file. (I didn't notice that the first time either.) So look at that line 1 and take out the illegal space.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    The following is my code

    Code:
    1.#include "uthread.h"
    
    void thr_init()
    {
    	// initiate thread queue
    	head = (TCB *)malloc(sizeof(TCB));
    	head->next = NULL;
    	head->id = 0; 
    	head->state = RUNNING;
    	head->result = NULL;
    	head->acn = 1;
    	head->qtime = 0;
    
    	//record start exe time for main thread
    	if (gettimeofday(&head->exest, NULL)) 
    	{
          fprintf(stderr, "cannot get start time of main thread\n");
          exit(1);
    	}
    
    	//record context
    	ucontext_t headcontext;
    	if (getcontext(&headcontext)!=0)
    	{
    		fprintf( stderr, "cannot get context for main thread" );
    		exit(1);
    	}
    	head->context = headcontext;
    
    	//start timer
    	time_t n = THR_QUANTUM_DEFAULT;
    	thr_quantum(n); 
    33.}
    the current problem:
    "thr_init.c", line 1: warning: invalid white space character in directive
    "thr_init.c", line 33: warning: newline not last character in file

    I cannot figure out why it always warns me these
    Last edited by Salem; 09-27-2008 at 11:35 PM. Reason: [code] at the start, and [/code] at the end - sheesh

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    "thr_init.c", line 33: warning: newline not last character in file
    That one should be easy for you to figure out.

    "thr_init.c", line 1: warning: invalid white space character in directive
    Did you write this code yourself? What OS and text editor did you use?
    Last edited by kpreston; 09-27-2008 at 07:37 PM.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Yes.
    I write this code by my self
    my OS is solaris unix
    my text editor is vi

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Somehow some weird character got in your file. Does your vi show you [unix] at the bottom when you edit this file? (It would be worse if it said [dos] or [mac], but I think you have to set filemode to automatically show yourself.) It's a silly suggestion, but:
    go to line 1 of your .c file
    type dd to delete the line
    type it in again very carefully
    save it and try again

    More than that, I don't know.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would help if you used [code][/code] tags for code, instead of trying to be creative.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM