LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions
User Name
Password
Linux - Distributions This forum is for Distribution specific questions.
Red Hat, Slackware, Debian, Novell, LFS, Mandriva, Ubuntu, Fedora - the list goes on and on... Note: An (*) indicates there is no official participation from that distribution here at LQ.

Notices


Reply
  Search this Thread
Old 10-25-2021, 09:13 AM   #1
cloudytechi147
LQ Newbie
 
Registered: Oct 2021
Location: India
Posts: 3

Rep: Reputation: Disabled
Is there similar commands to getprop/setprop in Linux or Mac?


I'm wondering if there are similar commands to getprop/setprop that exist in Android.

I want to enable to do changes for certain properties for a compiled app without compiling it again.

I know that on Android I can do so with ADB shell setprop but I didn't find something similar in Linux/Mac.

There is a way to do something like that with environment variable on the best Linux distribution (maybe the same thing on Mac) but is there something else?

I'll give an example of what I want to do: Let's say that I have a program that uses a default number of threads for parallelization and I want to change the number of threads after compilation. I can do so by adding an environment variable, setting it in my session then running my app.

The following code demonstrates this:

Code:
#include <sstream>
#include <cstdlib>

template <class T>
bool getEnv(const char* envVariable, T& value) {
    auto envValue = getenv(envVariable);
    if (!envValue) {
        return false;
    }
    if constexpr (std::is_same_v<T, int>) {
        auto tempValue = strtol(envValue, nullptr, 10);
        if (tempValue < std::numeric_limits<int>::min() ||
             tempValue > std::numeric_limits<int>::max()) {
            return false;
        }
        value = tempValue;
    } else if constexpr  (std::is_same_v<T, double>) {
        auto tempValue = strtod(envValue, nullptr);
        if (tempValue < std::numeric_limits<double>::min() || 
             tempValue > std::numeric_limits<double>::max()) {
            return false;
        }
        value = static_cast<double>(tempValue);
    } else {
        // make sure this won't compile
        static_assert(!std::is_same_v<T, T>);
    }
    return true;
}

int main() {
    int number_of_threads;
    int status = getEnv("NUMBER_OF_THREADS", number_of_threads);
    if (!status) {
        std::cerr << "Failed to get NUMBER_OF_THREADS\n";
        return 1;
    }
    std::cout << "NUMBER_OF_THREADS=" << number_of_threads << "\n";

    return 0;
}
On the terminal:

Code:
idancw:~/envTest/cmake-build-debug> export NUMBER_OF_THREADS=4
idancw:~/envTest/cmake-build-debug> ./test 
NUMBER_OF_THREADS=4
idancw:~/envTest/cmake-build-debug> export NUMBER_OF_THREADS=10
idancw:~/envTest/cmake-build-debug> ./test                     
NUMBER_OF_THREADS=10
Is there another way to do so without using the env variable, similar to Android?

I gauss that I can use the env approach on Mac too, but there is something else?

Thanks
 
Old 10-27-2021, 08:37 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,155

Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
short answer:
no
slightly longer answer:
if the application has a prefs file you may be able to tweak it from there, but it would depend on the app.
 
Old 10-28-2021, 08:36 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Yes, environment variables are not the solution here. The program gets a copy of all of them when the program starts, and getenv will not get updated values.

What you can do, is to setup a signal handler function. It's usually used for error handling and cleanup, but SIGUSR1 and SIGUSR2 can be used for anything.
Code:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signum){
  // Here we can e.g. read a config file, and update variables
  printf("\nInside handler function\n");
}

int main(){
  signal(SIGUSR1,sig_handler); // Register signal handler
  for(int i=1;;i++){    //Infinite loop
    printf("%d : Inside main function\n",i);
    sleep(1);  // Delay for 1 second
  }
  return 0;
}
You can then use commands like "killall -USR1 test" or kill -USR1 pid to tell the app it should read a config file or something.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Text to Speech/ speak to me similar to Mac OS for Linux Chels Linux - Newbie 3 10-02-2018 01:28 AM
[SOLVED] In terminal (bash), when you've run commands, and then you press the up arrow and all the commands are there, how to > to a text file. bk328115 Linux - Newbie 4 08-03-2018 10:57 PM
Is there a /proc/net/... file for IPv6 similar to /proc/net/arp for mac addresses? systemlordanubis Linux - Networking 1 03-02-2012 06:36 PM
Software availble to linux similar to mac's Garagesale? tems Linux - Software 2 02-17-2007 12:30 AM
Mac similar Window Manager MikeyMike Linux - General 12 09-24-2004 07:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions

All times are GMT -5. The time now is 05:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration