在室友的压迫下被迫发帖!
一、信号量通信
代码:
#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;
}