Can pointers be passed between processes?

Source: Internet
Author: User

The sigqueue function in Linux supports signal carrying parameters. The function prototype is as follows:

# Include <signal. h>

Int sigqueue (pid_t PID, int Sig, const Union sigval value );

The parameter value is a shared body and is defined as follows:

Union sigval {

Int sival_int;

Void * sival_ptr;

};

I noticed that one of the shared bodies is a pointer. I thought, can we pass a pointer between processes? Use a program to verify it.

// The example uses signals to transmit data, and this program sends data
// Option-D is followed by the data to be transmitted, option-S is followed by the signal to be sent, option-P is followed by the target process ID
# Include <signal. h>
# Include <stdio. h>
# Include <stdlib. h>

Int main (INT argc, char ** argv)
{
Union sigval value;
Int SIGNUM = sigterm; // sigterm is sent by default.
Pid_t PID;
Int I;

/* Initialize */
Value. sival_int = 0;

/* Check the number of parameters */
If (argc! = 3 & argc! = 5 & argc! = 7 ){
Printf ("./send_data_signo <-D data> <-s SIGNUM> [-p] [data] \ n ");
Exit (1 );
}

/* Parse the signal number, PID, and data to be transmitted from the command line parameters */
For (I = 1; I <argc; I ++ ){
If (! Strcmp (argv [I], "-d ")){
Value. sival_ptr = argv [I + 1]; // pointer to the string in the Parameter
Continue;
}
If (! Strcmp (argv [I], "-s ")){
SIGNUM = atoi (argv [I + 1]);
Continue;
}
If (! Strcmp (argv [I], "-P ")){
PID = atoi (argv [I + 1]);
Continue;
}
}

/* Use sigqueue to send the signal SIGNUM to the PID and carry the data value */
If (sigqueue (PID, SIGNUM, value) <0 ){
Perror ("sigqueue ");
Exit (1 );
}

Return 0;
}

 

// The example uses signals to transmit data, and this program receives the data
# Include <signal. h>
# Include <stdio. h>

/* Three-parameter signal processing program */
Void handler_sigint (INT signo, siginfo_t * siginfo, void * pvoid)
{
Printf ("Recv SIGINT, the data value is: % s \ n", (char *) siginfo-> si_ptr );
}

Int main ()
{
Struct sigaction Act;
 
/* Assign the act structure */
Act. sa_sigaction = handler_sigint;
Act. sa_flags = sa_siginfo; // specify the three-parameter signal processing function.
/* Install the signal processing function */
Sigaction (SIGINT, & act, null );
 
While (1)
;

Return 0;
}

 

After compilation, run the following:

In the first terminal:

#./Recv_data_signo

On the second terminal:

# Ps-

View the process number, and then

#./Send_data_signo-S 2-D hello-P 4832

The first terminal displays:

Segmentation fault

 

 

The so-calledSegmentation fault(Segment ErrorThe accessed memory exceeds the memory space of the program provided by the system.

It can be seen that, even if a pointer is successfully passed, it cannot be referenced in another process.

Let's try the same process.

 

# Include <signal. h>

# Include <stdio. h>

# Include <stdlib. h>

Void handler_sigint (INT signo, siginfo_t * siginfo, void * pvoid)

{

Printf ("Recv SIGINT, the data value is: % s \ n", (char *) (siginfo-> si_ptr ));

}

Int main (INT argc, char ** argv)

{
Struct sigaction Act;

Union sigval value;

Int SIGNUM = sigterm;

Pid_t PID;

Int I;

Act. sa_sigaction = handler_sigint;

Act. sa_flags = sa_siginfo;

 

Sigaction (SIGINT, & act, null );

 

Value. sival_ptr = NULL;

 

 

If (argc! = 3 & argc! = 5 ){

Printf ("./send_data_signo <-D data> <-s SIGNUM> \ n ");

Exit (1 );

}

 

 

For (I = 1; I <argc; I ++ ){

If (! Strcmp (argv [I], "-d ")){

Value. sival_ptr = argv [I + 1];

Continue;

}

If (! Strcmp (argv [I], "-s ")){

SIGNUM = atoi (argv [I + 1]);

Continue;

}

}

PID = getpid ();

 

If (sigqueue (PID, SIGNUM, value) <0 ){

Perror ("sigqueue ");

Exit (1 );

}

 

Return 0;

}

 

Compile and run:

#./Send_data_signo-S 2-D hello

Recv SIGINT, the data value is: Hello

 

Pointers can be transmitted normally in the same process.

 

References: Linux C programming practices Tong Yongqing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.