多线程与信号

消息处理 

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <signal.h>
#include <pthread.h>
struct msgbuf
{
	long mtype;  // 消息的标识(整数)
	char mtext[100];    // 消息的正文

};

int msg_id1;
void *t1(void *arg) {
struct msgbuf buf1;
while(1)
        {
                ssize_t msgrcv_ret1 = msgrcv(msg_id1, &buf1,sizeof(buf1), 2, 0);
                if(-1 == msgrcv_ret1) 
                {
                        printf("rcv msg failed\n");
                }
                if(strcmp(buf1.mtext,"bye\n")==0)
                {
                        break;
                }

                printf("MSG1:%s\n",buf1.mtext);
                        
        }               



}
int main()
{
	pthread_t tid1;

    	pthread_create(&tid1, NULL, t1, NULL);
	//1.创建KEY值
	key_t key = ftok("./", 0);
	if(-1 ==key)
	{
		printf("creat key failed");
		return -1;
	}
	
	//2.创建消息队列
	int msg_id = msgget(key, IPC_CREAT | 0777);
	if(-1 == msg_id)
	{
		printf("creat msg failed\n");
		return -1;
	}
	//创建KEY1
	        //1.创建KEY值
        key_t key1 = ftok("./", 1);
         if(-1 ==key)
        {
                printf("creat key failed");
                return -1;
        }

        //2.创建消息队列
         msg_id1 = msgget(key1, IPC_CREAT | 0777);
        if(-1 == msg_id1)
        {
                printf("creat msg failed\n");
                return -1;
        }
	
	//3.往消息队列里面发送消息
	//需要定义一个结构体变量buf
	struct msgbuf buf;

	
	//需要给结构体成员赋值
	buf.mtype = 1;
	
	while(1)
	{


		memset(buf.mtext,0,100);
		
		//消息从键盘获取
		fgets(buf.mtext,100,stdin);//以回车结尾

		
		int msgsnd_ret = msgsnd(msg_id,&buf,sizeof(buf), 0); //默认是阻塞的发送
		if(-1 == msgsnd_ret)
		{
			printf("send msg failed\n"); 
			return -1;
		}
		
		
		if(strcmp(buf.mtext, "bye\n") == 0)
		{
			break;
		}
	}
  msgctl(msg_id, IPC_RMID, NULL);
    return 0;	

	
}

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>


struct msgbuf
{
	long mtype;  // 消息的标识(整数)
	char mtext[100];    // 消息的正文

};
int msg_id1;
void *t1(void *arg) {
        //需要定义一个结构体变量buf
        struct msgbuf buf1;


        //需要给结构体成员赋值
        buf1.mtype = 2;

        while(1)
        {


                memset(buf1.mtext,0,100);

                //消息从键盘获取
                fgets(buf1.mtext,100,stdin);//以回车结尾


                int msgsnd_ret1 = msgsnd(msg_id1,&buf1,sizeof(buf1), 0); //默认是阻塞的发送
                if(-1 == msgsnd_ret1)
                {
                        printf("send msg failed\n");
                        return NULL;
                }


                if(strcmp(buf1.mtext, "bye\n") == 0)
                {
                        break;
                }
        }

}
int main()
{
 pthread_t tid1;

        pthread_create(&tid1, NULL, t1, NULL);
	
	//key值
	key_t key = ftok("./", 0);
	if(-1 ==key)
	{
		printf("creat key failed");
		return -1;
	}

	//创建消息队列
	int msg_id = msgget(key, IPC_CREAT | 0777);
	if(-1 == msg_id)
	{
		printf("creat msg failed\n");
		return -1;
	}
  //key值
        key_t key1 = ftok("./", 1);
        if(-1 ==key1)
        {
                printf("creat key failed");
                return -1;
        }

        //创建消息队列
        msg_id1 = msgget(key1, IPC_CREAT | 0777);
        if(-1 == msg_id1)
        {
                printf("creat msg failed\n");
                return -1;
        }
	
	//接受消息的消息
	struct msgbuf buf;
	while(1)
	{
		ssize_t msgrcv_ret = msgrcv(msg_id, &buf,sizeof(buf), 1, 0);
		if(-1 == msgrcv_ret)
		{
			printf("rcv msg failed\n");
		}
		if(strcmp(buf.mtext,"bye\n")==0)
		{
			break;
		}
		
		printf("MSG:%s\n",buf.mtext);
			
	}
	

}

信号处理 

#include <stdio.h>
#include <fcntl.h>              
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <signal.h>

sem_t *space, *data;
char *p;
int shm_id;

void cleanup(int sig) {
    // 释放信号量和共享内存
    sem_close(space);
    sem_unlink("/1");
    sem_close(data);
    sem_unlink("/2");
    shmdt(p);
    shmctl(shm_id, IPC_RMID, NULL);
    _exit(0);
}

int main() {
    key_t key = ftok("./", 2);
    shm_id = shmget(key, 2, IPC_CREAT | 0666);
    p = shmat(shm_id, NULL, 0);

    space = sem_open("/1", O_CREAT, 0666, 1);
    data = sem_open("/2", O_CREAT, 0666, 0);

    // 注册SIGINT信号处理函数
    signal(SIGINT, cleanup);

    char *msg = "0123456789";
    int i = 0;

    while (1) {
        sem_wait(space);
        memcpy(p, msg + i, 1);
        sem_post(data);
        i = (i + 1) % 10;
    }

    // 解除映射和清理资源
    cleanup(0); // 虽然在无限循环中不会到达这里,但确保资源得到释放
}

#include <stdio.h>
#include <fcntl.h>              
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <signal.h>

sem_t *space, *data;
char *p;
int shm_id;

void cleanup(int sig) {
    // 释放信号量和共享内存
    sem_close(space);
    sem_unlink("/1");
    sem_close(data);
    sem_unlink("/2");
    shmdt(p);
    _exit(0);
}

int main() {
    key_t key = ftok("./", 2);
    shm_id = shmget(key, 2, IPC_CREAT | 0666);
    p = shmat(shm_id, NULL, 0);

    space = sem_open("/1", O_CREAT);
    data = sem_open("/2", O_CREAT);

    // 注册SIGINT信号处理函数
    signal(SIGINT, cleanup);

    while (1) {
        sem_wait(data);
        fprintf(stderr, "%s", p);
        sem_post(space);
    }

    // 解除映射和清理资源
    cleanup(0); // 虽然在无限循环中不会到达这里,但确保资源得到释放
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值