📜  使用信号和共享内存在两个进程之间聊天应用程序

📅  最后修改于: 2021-05-30 07:17:16             🧑  作者: Mango

先决条件: C信号处理通过共享内存的IPC

UNIX系统中使用信号来通知进程发生了特定事件。根据事件的来源和原因,可以同步或异步接收信号。信号必须遵循以下模式–

1.信号是由特定事件的发生而产生的。

2.产生的信号被传送到特定的过程。

3.必须在接收过程中处理信号。

在此问题中,使用kill函数将消息从一个用户发送到另一用户。 kill函数需要两个输入-接收器进程的进程ID和信号类型。为此,我们使用共享内存来存储两个进程的进程ID。我们使用处理程序函数,该函数将打印从另一个进程接收到的消息。用户2将开始向用户1发送消息,然后他们将继续聊天。

User 1
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
  
#define FILLED 0
#define Ready 1
#define NotReady -1
  
struct memory {
    char buff[100];
    int status, pid1, pid2;
};
  
struct memory* shmptr;
  
// handler function to print message received from user2
  
void handler(int signum)
{
    // if signum is SIGUSR1, then user 1 is receiving a message from user2
  
    if (signum == SIGUSR1) {
        printf("Received User2: ");
        puts(shmptr->buff);
    }
}
  
int main()
{
    // process id of user1
  
    int pid = getpid();
  
    int shmid;
  
    // key value of shared memory
    int key = 12345;
  
    // shared memory create
    shmid = shmget(key, sizeof(struct memory), IPC_CREAT | 0666);
  
    // attaching the shared memory
  
    shmptr = (struct memory*)shmat(shmid, NULL, 0);
  
    // store the process id of user1 in shared memory
    shmptr->pid1 = pid;
    shmptr->status = NotReady;
  
    // calling the signal function using signal type SIGUSER1
    signal(SIGUSR1, handler);
  
    while (1) {
        while (shmptr->status != Ready)
            continue;
        sleep(1);
  
        // taking input from user1
  
        printf("User1: ");
        fgets(shmptr->buff, 100, stdin);
  
        shmptr->status = FILLED;
  
        // sending the message to user2 using kill function
  
        kill(shmptr->pid2, SIGUSR2);
    }
  
    shmdt((void*)shmptr);
    shmctl(shmid, IPC_RMID, NULL);
    return 0;
}


User 2
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
  
#define FILLED 0
#define Ready 1
#define NotReady -1
  
struct memory {
    char buff[100];
    int status, pid1, pid2;
};
  
struct memory* shmptr;
  
// handler function to print message received from user1
  
void handler(int signum)
{
    // if signum is SIGUSR2, then user 2 is receiving a message from user1
  
    if (signum == SIGUSR2) {
        printf("Received From User1: ");
        puts(shmptr->buff);
    }
}
  
// main function
  
int main()
{
    // process id of user2
    int pid = getpid();
  
    int shmid;
  
    // key value of shared memory
    int key = 12345;
  
    // shared memory create
  
    shmid = shmget(key, sizeof(struct memory), IPC_CREAT | 0666);
  
    // attaching the shared memory
  
    shmptr = (struct memory*)shmat(shmid, NULL, 0);
  
    // store the process id of user2 in shared memory
    shmptr->pid2 = pid;
  
    shmptr->status = NotReady;
  
    // calling the signal function using signal type SIGUSR2
    signal(SIGUSR2, handler);
  
    while (1) {
        sleep(1);
  
        // taking input from user2
  
        printf("User2: ");
        fgets(shmptr->buff, 100, stdin);
        shmptr->status = Ready;
  
        // sending the message to user1 using kill function
  
        kill(shmptr->pid1, SIGUSR1);
  
        while (shmptr->status == Ready)
            continue;
    }
  
    shmdt((void*)shmptr);
    return 0;
}


输出:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”