I have a client program that can be executed in a linux terminal. The client sends this message to the server, and immediately dies once it receives the ack from the server:
struct Msg {
char my_id[16];
};
The server just appends this my_id
to a log file.
The thing is, I want Msg::my_id
to be the same across the terminal/shell the client is executed from. How would I do this?
Say, I am a Linux user, and open two terminals: terminals X and Y.
I ran my client from X three times, and from Y twice. In that case, what should I add to the client in order for me to see three Xs and two Ys in the server side log file?
One thing I can think of is to take the ppid and send it. Would this always work? If not, what'd be better alternatives?
CodePudding user response:
@Author,
Proceed based on comment from Barmar.
I thought of sharing:
#include <unistd.h>
#include <string.h>
#if defined( CYGWIN_NT ) || defined( LINUX )
#include <iostream>
using namespace std;
#else
#include <iostream.h>
#endif
int main()
{
// SAMPLE PROGRAM.
struct Msg
{
char my_id[16];
};
Msg obj;
// I am not handling fork/... other related enhancements/updates at code.
sprintf( obj.my_id, "%llu", getppid() );
cout << obj.my_id << "\n";
sprintf( obj.my_id, "%s", ttyname(STDIN_FILENO) );
cout << obj.my_id << "\n";
return 0;
}
$ g -DCYGWIN_NT getmy_id.cpp -o ./a.out
$ ./a.out
$ ./a.out
47130
/dev/pty0
$ echo $$ current terminal pid
47130 current terminal pid
We can also achieve the same using:
user's last login:
How to get user's last login including the year in C