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

  • Home
  • SEARCH
  • 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 185847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:32:36+00:00 2026-05-11T15:32:36+00:00

I have an application where I need to write a new getpid function to

  • 0

I have an application where I need to write a new getpid function to replace the original one of the OS. The implementation would be similar to:

pid_t getpid(void) {     if (gi_PID != -1)     {         return gi_PID;     }     else     {         // OS level getpid() function     } } 

How can I call the original getpid() implementation of the OS through this function?

EDIT: I tried:

pid_t getpid(void) {     if (gi_PID != -1)     {         return gi_PID;     }     else     {         return _getpid();     } } 

as Jonathan has suggested. This gave me the following errors when compiling with g++:

In function pid_t getpid()': SerendibPlugin.cpp:882: error: _getpid’ undeclared (first use this function) SerendibPlugin.cpp:882: error: (Each undeclared identifier is reported only once for each function it appears in.)

EDIT 2: I’ve managed to get this to work by using a function pointer and setting it to the next second symbol with the id ‘getpid’, using dlsym(RTLD_NEXT, ‘getpid’).

Here’s my sample code:

vi xx.c 'xx.c' 23 lines, 425 characters  #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <iostream> #include <dlfcn.h>  using namespace std; pid_t(*___getpid)();  pid_t getpid(void) {     cout << 'My getpid' << endl;     cout << 'PID :' << (*___getpid)() << endl;     return (*___getpid)(); }  int main(void) {     ___getpid = (pid_t(*)())dlsym(RTLD_NEXT, 'getpid');     pid_t p1 = getpid();     printf('%d \n', (int)p1);     return(0); } 

g++ xx.c -o xout

My getpid PID :7802 7802  
  • 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. 2026-05-11T15:32:36+00:00Added an answer on May 11, 2026 at 3:32 pm

    On many systems, you will find that getpid() is a ‘weak symbol’ for _getpid(), which can be called in lieu of getpid().


    The first version of the answer mentioned __getpid(); the mention was removed swiftly since it was erroneous.

    This code works for me on Solaris 10 (SPARC) – with a C++ compiler:

    #include <sys/types.h> #include <unistd.h> #include <stdio.h>  extern 'C' pid_t _getpid();  pid_t getpid(void) {     return(-1); }  int main(void) {     pid_t p1 = getpid();     pid_t p2 = _getpid();     printf('%d vs %d\n', (int)p1, (int)p2);     return(0); } 

    This code works for me on Solaris 10 (SPARC) – with a C compiler:

    Black JL: cat xx.c #include <sys/types.h> #include <unistd.h> #include <stdio.h>  pid_t getpid(void) {     return(-1); }  int main(void) {     pid_t p1 = getpid();     pid_t p2 = _getpid();     printf('%d vs %d\n', (int)p1, (int)p2);     return(0); } Black JL: make xx && ./xx cc     xx.c   -o xx 'xx.c', line 13: warning: implicit function declaration: _getpid -1 vs 29808 Black JL: 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It turns out there is nothing wrong with the code… May 13, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer You have more than one choice. There are several contrib… May 13, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer <%# String.Format( "return askUser(\"{0}\",\"{1}\")", Eval("FirstName"), Eval("LastName")) %> May 13, 2026 at 12:10 am

Related Questions

I have created an application that writes some data to the root folder of
I have a project that I thought was going to be relatively easy, but
I'm writing a plug-in for an application where I have a custom class that
I need to have Where Linq functionality in my own code. Let me explain:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.