Linux下信号量、共享内存、消息队列通信(Linux第七次作业)

在室友的压迫下被迫发帖!

一、信号量通信

代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/sem.h>

typedef union semun {
	int val;
	struct semid_ds *buf;
	unsigned short *array;
} semun_def;

int sem_id;


int set_semvalue() {
	semun_def sem_union;
	sem_union.val = 1;
	if(semctl(sem_id,0,SETVAL,sem_union)==-1)
		return 0;
	return 1;
}

void sem_init() {
	//create a new signal
	sem_id = semget((key_t)1234,1,0666|IPC_CREAT);
	if(!set_semvalue()) {
		fprintf(stderr,"init failed\n");
		exit(EXIT_FAILURE);
	}
}

int sem_send(int sem_id) {
	struct sembuf sem_b;
	sem_b.sem_num = 0;
	sem_b.sem_op = 1;
	sem_b.sem_flg = SEM_UNDO;
	if(semop(sem_id,&sem_b,1)==-1) {
		fprintf(stderr,"semaphore_v failed\n");
		return 0;
	}
	return 1;
}

int sem_wait(int sem_id) {
	struct sembuf sem_b;
	sem_b.sem_num = 0;
	sem_b.sem_op = -1;
	sem_b.sem_flg = SEM_UNDO;
	if(semop(sem_id,&sem_b,1)==-1) {
		fprintf(stderr,"semaphore_p failed\n");
		return 0;
	}
	return 1;
}

void sem_del(int sem_id) {
	union semun sem_union;
	if(semctl(sem_id,0,IPC_RMID,sem_union)==-1)
		fprintf(stderr,"Failed to delete semaphore\n");
}


void process1(void) {
	while(1) {
		printf("\n\nprocess1:send a sem\n");
		sem_send(sem_id);
		sleep(2);
	}
}

void process2(void) {
	while(1) {
		printf("process2:wait a sem\n");
		if(sem_wait(sem_id) > 0)
			printf("rcv sem\n");
	}
}

int main(int argc, const char *argv[]) {
	pid_t pid,pid1;
	sem_init();

	pid = fork();
	if(pid == 0) {
		printf("process1 create ok\n");
		process1();
	} else if(pid < 0) {
		printf("process create failed\n");
		perror("fork");
	} else {

	}
	pid1 = fork();
	if(pid1 == 0) {
		printf("process2 create ok\n");
		process2();
	} else if(pid < 0) {
		printf("process2 create failed\n");
		perror("fork");
	} else {

	}

	return 0;
}

二、共享内存通信

代码:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#define BUFSZ 2048

int main(){
	int shmid;
	char *shmadd;
	
	if((shmid = shmget(IPC_PRIVATE,BUFSZ,0600))<0){
		perror("shmget");
		exit(-1); 
	}
	printf("created shared-memory:%d\n",shmid);
	system("ipcs -m");
	if((shmadd = (char *)shmat(shmid,0,0)) < (char *)0){
		perror("shmat");
		exit(-1);
	} 
	printf("attached shared-memory\n");
	system("ipcs -m");
	if((shmdt(shmadd)) < 0){
		perror("shmdt");
		exit(-1);
	}
	printf("detach shared-memory\n");
	system("ipcs -m");
	shmctl(shmid,IPC_RMID,NULL);
	system("ipcs -m");
	return 0;
}

三、消息队列通信

代码:

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/shm.h>
#include <error.h>
#define BUFSZ 2048

#define TYPE 100

struct msgbuf{
	long mtype;
	char mtext[BUFSZ];
}; 

int main(){
	int qid,len;
	key_t key;
	struct msgbuf msg;
	
	if((key = ftok(".",'a'))== -1){
		perror("ftok");
		exit(-1);
	}
	if((qid = msgget(key,IPC_CREAT|0666))== -1){
		perror("msgget");
		exit(-1);
	}
	
	printf("opened queue %d\n",qid);
	puts("Please enter the message to queue:");
	if((fgets(msg.mtext,BUFSZ,stdin))==NULL){
		puts("no message");
		exit(-1);
	}
	msg.mtype = TYPE;
	len = sizeof(msg)-sizeof(long);
	
	/*添加消息到消息队列中去*/
	if(msgsnd(qid,&msg,len,0)<0){
		perror("msgsnd");
		exit(-1);
	} 
	/*从消息队列中读取消息*/
	if(msgrcv(qid,&msg,len,0,0) < 0){
		perror("msgrcv");
		exit(-1);
	} 
	printf("message is:%s\n",msg.mtext);
	/*从系统中删除消息队列*/
	if(msgctl(qid,IPC_RMID,NULL) < 0){
		perror("msgctl");
		exit(-1);
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村头卖假发的小郑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值