Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:46+00:00 2026-05-26T20:05:46+00:00

I am working on a project where I have to implement a handler for

  • 0

I am working on a project where I have to implement a handler for several functions, all of which have a number associated with them. The number that they are associated with is how the different functions are called. Knowing this I want to be able to group them into an array using the number as the index to the functions. The problem is, arrays need to have the same type of elements inside of them right? so how can I place these functions into an array?

Here is how the functions are numbered:

enum 
  {
    SYS_HALT,                   /* Halt the operating system. */
    SYS_EXIT,                   /* Terminate this process. */
    SYS_EXEC,                   /* Start another process. */
    SYS_WAIT,                   /* Wait for a child process to die. */
    SYS_CREATE,                 /* Create a file. */
    SYS_REMOVE,                 /* Delete a file. */
    SYS_OPEN,                   /* Open a file. */
    SYS_FILESIZE,               /* Obtain a file's size. */
    SYS_READ,                   /* Read from a file. */
    SYS_WRITE,                  /* Write to a file. */
    SYS_SEEK,                   /* Change position in a file. */
    SYS_TELL,                   /* Report current position in a file. */
    SYS_CLOSE,                  /* Close a file. */
  };

And here is the list of function prototypes:

void halt (void) NO_RETURN;
void exit (int status) NO_RETURN;
pid_t exec (const char *file);
int wait (pid_t);
bool create (const char *file, unsigned initial_size);
bool remove (const char *file);
int open (const char *file);
int filesize (int fd);
int read (int fd, void *buffer, unsigned length);
int write (int fd, const void *buffer, unsigned length);
void seek (int fd, unsigned position);
unsigned tell (int fd);
void close (int fd);

Main question

Is there an easy way to put all of the functions into an array or other data structure in C?

Any help would be most appreciated, this is for a school project, so a small example or link would be very useful, but I’d like to create the solution myself.

Thanks

Edit: What I’d like to be able to do

I want to be able to define some array and store a pointer to the functions at the indexes of the enum ordinal numbers.

//I don't know how to handle the type here
<some_type> system_call_arr[128];

system_call_arr[SYS_HALT] = halt;
system_call_arr[SYS_EXIT] = exit;
system_call_arr[SYS_EXEC] = exec;

// and so on....

Edit 2

Since Ben said all of the arguments would fit into a 32 bit argument, couldn’t I define something like this:

typedef int (*sys_func) (uint32_t, uint32_t, uint32_t);
sys_func syscall_array[128];

syscall_array[SYS_HALT] = (sys_func)halt;
//etc....

Is something like this good practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:05:47+00:00Added an answer on May 26, 2026 at 8:05 pm

    The classic UNIX way to do this is to give all of those functions the same signature (say int sys_func(struct args *a) and then put those all into a function array. When you marshal arguments for the calls you just put them in as arg1, arg2, etc all in struct args and each syscall uses them differently. Or you can alias a special structure on top of it to give them meaningful names.

    In your edit you ask, “Is something like this good practice?” I gave my answer in the context of your question, which appears to be the implementation of a simple operating system kernel. Operating systems get to cheat in ways that normal applications never should. It can use assumptions about word size (e.g. that everything is 32-bit, as in ILP-32) and know things about the ABI because implementing these things is part of the operating system’s job. If your OS is portable to other platforms it will compile in different variants of things like syscall to handle these cases.

    Why did I suggest struct args rather than your method? Because in an operating system you will have a few layers between the user making the syscall and the actual code running from your function pointer table. If you pass these arguments explicitly through each layer you are going to copy them several times. If the first layer can simply provide a pointer that sort of “jumps” the arguments over the intermediate functions. If you’re dealing with an ABI where the calling convention is to push arguments on the stack, you can avoid copying the arguments altogether and just take an appropriate stack address as the args.

    You can find discussions of this and pretty much every other aspect of operating system design in The Design and Implementation of the 4.4 BSD Operating System by McKusick et. al. If you can find the older 4.3 version it’s actually a fairly slim volume and still entirely relevant to what you’re working on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on a project at the moment and we have to implement soft deletion
I have project that I'm working on that is going to require a webserver.
I have a fully working Setup project within Visual Studio 2008 that takes inputs
I'm working on a project where I have 2 web services that need the
I'm working on a project were we have number (5 at the moment) of
I am working a project that does not have a trunk / branches /
I am working on a college project, where I have to implement a simple
I'm working on a project that's trying to implement some editing features using a
I am working in a small educational project where we have to implement a
I am working on a project(I have to implement it in Perl but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.