/************** 进程A *************/
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
key_t key;
int shmid;
char *d;
void get_sig(int sig){
sprintf( d, "I am process_A,my process_id is %d", getpid());
}
int main(){
// 1.生成键值并做错误处理
key = ftok("/home/yj/Desktop/fork_test.c",'a');
if( -1 == key ){
fprintf( stderr, "ftok error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 2.打开目标共享内存,若不存在则创建
shmid = shmget( key, 128, IPC_CREAT|0644);
if( -1 == shmid ){
fprintf( stderr, "shmget error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 3.对物理内存地址做映射
d = (char *)shmat( shmid, NULL, 0);
if( NULL == d ){
fprintf( stderr, "shmat error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 4.注册信号处理函数
signal( SIGUSR1, get_sig);
while(1);
return 0;
}
/************** 进程B *************/
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
key_t key;
int shmid;
char *d;
void get_sig(int sig){
sprintf( d, "I am process_B,and my process_id is %d", getpid());
}
int main(){
// 1.生成键值并做错误处理
key = ftok("/home/yj/Desktop/fork_test.c",'a');
if( -1 == key ){
fprintf( stderr, "ftok error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 2.打开目标共享内存,若不存在则创建
shmid = shmget(key,128,IPC_CREAT|0644);
if( -1 == shmid ){
fprintf( stderr, "shmget error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 3.对物理内存地址做映射
d = (char *)shmat( shmid, NULL, 0);
if( NULL == d ){
fprintf( stderr, "shmat error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 4.注册信号处理函数
signal( SIGUSR2, get_sig);
while(1);
return 0;
}
/************** 进程C *************/
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
key_t key;
int shmid;
char *d;
int flag;
void get_sig(int sig){
sprintf( d, "I am process_B,and my process_id is %d", getpid());
}
int main(){
flag = 0;
// 1.生成键值并做错误处理
key = ftok("/home/yj/Desktop/fork_test.c",'a');
if( -1 == key ){
fprintf( stderr, "ftok error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 2.打开目标共享内存,若不存在则创建
shmid = shmget(key,128,IPC_CREAT|0644);
if( -1 == shmid ){
fprintf( stderr, "shmget error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 3.对物理内存地址做映射
d = (char *)shmat( shmid, NULL, 0);
if( NULL == d ){
fprintf( stderr, "shmat error,errno=%d,%s", errno, strerror(errno));
return 0;
}
// 4.注册信号处理函数
printf("**************************************\n");
printf("\t1:给进程A发送SIGUSR1信号\n");
printf("\t2:给进程B发送SIGUSR2信号\n");
while(1){
printf("please input:");
scanf("%d",&flag);
switch( flag ){
case 1:
system("killall -SIGUSR1 process_A");
sleep(3);
printf("%s\n",d);
bzero(d,100);
break;
case 2:
system("killall -SIGUSR2 process_B");
sleep(3);
printf("%s\n",d);
bzero(d,100);
break;
}
}
return 0;
}