1、实验三 进程通信一. 实验学时与类型学时:2,课外学时:自定实验类型:设计性实验二. 实验目的了解Linux的软中断、管道、消息队列、共享存储区等进程间通信方式。三. 实验内容1. 软中断通信机制(1) 请编写一个程序:循环输出“how are you?”,在按下Ctrl+C后中断显示,输出“Byebye!”后退出程序。#include#includeint k=1;void int_func(int sig) /软中断处理函数 k=0; Int main() signal(SIGINT,int_func);/预置软中断信号处理函数 While(k=1) Printf(“how are you
2、?n”); Printf(“byebye!”);(2) 使用信号机制实现父子进程同步,父进程先输出A,然后子进程输出B。#include#includeint k=1;void func(int sig) k=0; main() int pid; pid=fork(); if(pid0) printf(“An”); kill(pid,12); else if(pid=0) signal(12,func); while(k=1) sleep(1); printf(“Bn”); 2. 管道机制(1) 父子进程通过管道传送一串字符。要求:子进程随机从键盘输入一串字符,通过管道发给父进程,父进程从管道
3、中将消息读出并显示出来。#include#includemain() int pid, fd2 ; char outpipe50, inpipe50; pipe(fd); pid=fork(); if (pid=0) Printf(“please input some message:n”); Fgets(inpipe,sizeof(inpipe),stdin);write(fd1,inpipe,50); else if (pid0); wait(0); Printf(“father get this message:n”); read(fd0,outpipe,50); printf(“%sn
4、”,outpipe); (2)父子进程通过管道互相发送字符串。要求:子进程向父进程通过管道发送”I am child.”,父进程回送”I am father.”,父子进程将各自收到的字符串显示在屏幕上。#inlcude#include#includemain() int pid, fd2 ; char str150, str250; pipe(fd); pid=fork();if (pid=0) strcpy(str1,”Im child”); write(fd1,str1,strlen(str1);Sleep(1); read(fd0,str2,50); printf(“Child rece
5、ived: %sn”,str2); else if (pid0) read(fd0,str1,50); printf(“Parent received:%sn”,str1); strcpy(str2,”Im father.”); write(fd1,str2,strlen(str2); 3. 消息队列机制(1) 父进程及其子进程通过一条消息队列互相传送数据。#include#include#include#includeint msgqid,qid;struct msg long mtype; char mtext256;pmsg;cancelqueue() msgctl(msgqid,IPC
6、_RMID,0); exit(0);main() int pid; Pid=fork(); If (pid0) msgqid=msgget(75, 0777);printf(“msg id: %dn”,msgqid);pmsg.mtype=1;*(int *)pmsg.mtext)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0);msgrcv(msgqid,&pmsg,256,getpid(),0);printf(“A:receive msg from %dn,*(int *)pmsg.mtext);Else if(pid=0) signal(2,can
7、celqueue);msgqid=msgget(75, 0777|IPC_CREAT);while(1) msgrcv(msgqid,&pmsg,256,1,0);qid=*(int *)pmsg.mtext;printf(“B:receive msg from %dn”,qid);pmsg.mtype=qid;*(int *)pmsg.mtext)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0); (2) 非父子进程之间实通过一条消息队列互相传递数据。A进程:#include#include#include#includestruct msg long
8、 mtype; char mtext256;pmsg;main() int msgqid,pid;msgqid=msgget(75, 0777);printf(“msg id: %dn”,msgqid);pmsg.mtype=1;*(int *)pmsg.mtext)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0);msgrcv(msgqid,&pmsg,256,getpid(),0);printf(“A:receive msg from %dn”,*(int *)pmsg.mtext);B进程:#include#include#include#incl
9、udeint msgqid,pid;struct msg long mtype; char mtext256; pmsg;cancelqueue() msgctl(msgqid,IPC_RMID,0); exit(0);main() signal(2,cancelqueue);msgqid=msgget(75, 0777|IPC_CREAT);while(1) msgrcv(msgqid,&pmsg,256,1,0);pid=*(int *)pmsg.mtext;printf(“B:receive msg from %dn”,pid);pmsg.mtype=pid;*(int *)pmsg.m
10、text)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0); 4. 共享内存机制(1) 编程实现基于共享内存的进程间通信。要求:进程A通过共享内存将自己的进程号传递给进程B。A:#include#includemain()int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int *)shmat(shmid, 0,0); *va=getpid(); printf(“get As pid:%dn”,*va);shmdt(va); B:#include#includemai
11、n() int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int*)shmat(shmid, 0,0); printf(“As pid is :%dn”,*va); shmdt(va); shmctl(shmid,IPC_RMID,0);(2) 若要通过共享内存实现进程A与进程B互送进程号,可怎样编程实现?A:#include#includemain()int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int *)shm
12、at(shmid, 0,0); *va=getpid();printf(“get As pid:%dn”,*va);Sleep(2);printf(“Bs pid is:%dn”,*va)shmdt(va) B:#include#includemain() int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int*)shmat(shmid, 0,0); printf(“As pid is :%sn”,*va); *va=getpid(); printf(“get Bs pid:%dn”,*va); shmd
13、t(va); shmctl(shmid,IPC_RMID,0);5. SOCKET进程通信(选做)编程实现基于SOCKET的进程间通信,通过网络实现进程之间数据通信。要求:分别编写服务器端和客户端方程序,运行于不同终端,二者可相互进行通信。Server:#include #include #include #include #include #include #include int main() int server_sockfd = -1; int client_sockfd = -1; int client_len = 0; struct sockaddr_in server_addr;
14、 struct sockaddr_in client_addr; /创建流套接字 server_sockfd = socket(AF_INET, SOCK_STREAM, 0); /设置服务器接收的连接地址和监听的端口 server_addr.sin_family = AF_INET;/指定网络套接字 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);/接受所有IP地址的连接 server_addr.sin_port = htons(9736);/绑定到9736端口 /绑定(命名)套接字 bind(server_sockfd,(struct soc
15、kaddr*)&server_addr, sizeof(server_addr); /创建套接字队列,监听套接字 listen(server_sockfd, 5); /忽略子进程停止或退出信号 signal(SIGCHLD, SIG_IGN); while(1) char ch = 0; client_len = sizeof(client_addr); printf(Server waitingn); /接受连接,创建新的套接字 client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_addr, &client_len)
16、; if(fork() = 0) /子进程中,读取客户端发过来的信息,处理信息,再发送给客户端 read(client_sockfd, &ch, 1); sleep(5); ch+; write(client_sockfd, &ch, 1); close(client_sockfd); exit(0); else /父进程中,关闭套接字 close(client_sockfd); Client:#include #include #include #include #include #include #include int main() int sockfd = -1; int len =
17、0; struct sockaddr_in address; int result; char ch = A; /创建流套接字 sockfd = socket(AF_INET, SOCK_STREAM, 0); /设置要连接的服务器的信息 address.sin_family = AF_INET;/使用网络套接字 address.sin_addr.s_addr = inet_addr(127.0.0.1);/服务器地址 address.sin_port = htons(9736);/服务器所监听的端口 len = sizeof(address); /连接到服务器 result = connec
18、t(sockfd, (struct sockaddr*)&address, len); if(result = -1) perror(ops:clientn); exit(1); /发送请求给服务器 write(sockfd, &ch, 1); /从服务器获取数据 read(sockfd, &ch, 1); printf(char form server = %cn, ch); close(sockfd); exit(0); 四. 思考与总结(1) 请对比下列程序与实验1(1)在按下Ctrl+C之后的运行现象。int k=1;int main(void) while(k=1) printf(“
19、how are you?n”); printf(“Byebye!n”);(2) 针对实验2(2),你能否通过信号量机制来实现?若能,请给出相应代码及运行结果分析。#include #include #include#include #include #include union semunint val;struct semid_ds *buf;unsigned short *array;arg;/生成信号量int sem_creat(key_t key)int semid;semid = semget(key,1,IPC_CREAT|0666);if (-1 = semid)printf(c
20、reate semaphore errorn);exit(-1);sem.val = 0;semctl(semid,0,SETVAL,arg);return semid;/删除信号量void del_sem(int semid)arg.val = 0;semctl(semid,0,IPC_RMID,arg);/pint p(int semid)struct sembuf sops=0,-1,IPC_NOWAIT;return (semop(semid,&sops,1);/vint v(int semid)struct sembuf sops=0,+1,IPC_NOWAIT;return (se
21、mop(semid,&sops,1);int main()key_t key;int pid,semid;char str150;struct semid_ds buf;key = ftok(/,0);semid = sem_creat(key);if(pid0) P(semid); Va=(char *)shmat(shmid,0,0); Printf(“father get this message:%sn”,va); Strcpy(va,”Im father!”); Printf(“father get this message:%sn”,str1); Shmat(va); V(semid);Else if(pid=0) P(semid); Va=(char *)shmat(shmid,0,0); Strcpy(va,”Im child!”); Sleep(2);Printf(“child get this message:%sn”,va); Shmat(va); V(semid);Shmctl(shmid,IPC_RMID,0);del_sem(semid);return 0;/gcc -o shm shm.c -g(3) 请结合本次实验,说明信号、管道、消息队列、共享内存这几种进程间通信机制的适用场合及其优缺点。