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 8789747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:29:58+00:00 2026-06-13T22:29:58+00:00

On Ubuntu – kernel 2.6.32.2 How to call already existing system call from user

  • 0

On Ubuntu – kernel 2.6.32.2

How to call already existing system call from user code directly without help of any library?
I read in books and on internet to solve this then written following code but still getting error. Please help

Want to find out the process id of current process

#include <stdio.h>
#include<linux/unistd.h> // for __NR_getpid
_syscall0(int, getpid)

int main() {
 printf("Current Process ID : %d\n",getpid());
 return 0;
}

Error While compilation :

root@Omkant:~/os# gcc -Wall getpid.c -o getpid
getpid.c:5:16: error: expected declaration specifiers or ‘...’ before ‘getpid’
getpid.c:5:1: warning: data definition has no type or storage class
getpid.c:5:1: warning: type defaults to ‘int’ in declaration of ‘_syscall0’
getpid.c: In function ‘main’:
getpid.c:8:2: warning: implicit declaration of function ‘getpid’

What’s the problem in the code ? please help…

  • 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-06-13T22:30:00+00:00Added an answer on June 13, 2026 at 10:30 pm

    The manpage for _syscall(2) states:

    Starting around kernel 2.6.18, the _syscall macros were removed from header files supplied to user space. Use syscall(2) instead. (Some architectures, notably ia64, never provided the _syscall macros; on those architectures, syscall(2) was always required.)

    Thus, your desired approach can’t work on more modern kernels. (You can clearly see that if you run the preprocessor on your code. It won’t resolve the _syscall0 macro) Try to use the syscall function instead:

    Here is an example for the usage, cited from syscall(2):

    #define _GNU_SOURCE
    #include <unistd.h>
    #include <sys/syscall.h>
    #include <sys/types.h>
    
    int
    main(int argc, char *argv[])
    {
        pid_t tid;
        tid = syscall(SYS_gettid);
    }
    

    As you asked for a direct way to call the Linux kernel without any userspace wrappers, I’ll show you examples for the 80386 and the amd64 architecture.

    First, you have to get the system call number from a table, such as this one. In case of getpid, the system call number is 39 for amd64 and 20 for 80386. Next, we create a function that calls the system for us. On the 80386 processor you use the interrupt 128 to call the system, on amd64 we use the special syscall instruction. The system call number goes into register eax, the output is also written to this register. In order to make the program easier, we write it in assembly. You can later use strace to verify it works correctly.

    This is the code for 80386. It should return the lowest byte of its pid as exit status.

            .global _start
    _start: mov $20,%eax       #system call number 20:
            int $128           #call the system
            mov %eax,%ebx      #move pid into register ebx
            mov $1,%eax        #system call number 1: exit, argument in ebx
            int $128           #exit
    

    Assemble with:

    as -m32 -o 80386.o 80386.s
    ld -m elf_i386 -o 80386 80386.o
    

    This is the same code for amd64:

            .global _start
    _start: mov $39,%eax    #system call 39: getpid
            syscall         #call the system
            mov %eax,%edi   #move pid into register edi
            mov $60,%eax    #system call 60: exit
            syscall         #call the system
    

    Assemble with:

    as -o amd64.o amd64.s
    ld -o amd64 amd64.o
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ubuntu 12.04 Sinatra 1.3.3 Why does passing an argument to a ruby system call
ubuntu 9.04, fedora 11, redhat... what are the differences from a web server/development standpoint?
On Ubuntu, I am trying to download a file (from a script) using wget.
My Ubuntu Subversion server is not directly accessible to the Internet, 192.168.1.2 My public
Ubuntu 11.10, Python 2.6. Background: I have an existing Python app that is using
My ubuntu setup, has various shell-scripts that help me get my work done. Of
On Ubuntu, I have install netbeans but it does not have any option to
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 I have a static library called sdpAPI.a I am trying
Running Ubuntu 11.10 + python2.7...built numpy from source and installed it, but when I
Ubuntu 10.04.3 LTS mongodb-1.2.2-1ubuntu1.1 django 1.3 mongoengine-0.5.2 pymongo-2.1.2 model: class User(Document): email = StringField(required=True)

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.