#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
//printf("suhao 202031050154");
int tempFd[2];//定义文件描述符数组
int tempRet=pipe(tempFd);//创建管道
if(tempRet == -1){
perror("pipe");
exit(1);
}
pid_t tempPid=fork();
if(tempPid > 0){//父进程—读
close(tempFd[1]);//关闭写端
char tempBuf[64]={0};
tempRet = read(tempFd[0], tempBuf, sizeof(tempBuf));//读数据
close(tempFd[0]);
write(STDOUT_FILENO, tempBuf, tempRet);//将读到的数据写到标准输出
wait(NULL);
} else if(tempPid == 0){//子进程—写
close(tempFd[0]);//关闭读端
char *tempStr="hello,suhao 202031050154\n";
write(tempFd[1], tempStr, strlen(tempStr)+1);//写数据
close(tempFd[1]);
}//of if
//printf("suhao 202031050154");
return 0;
}//of main
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
printf("suhao 202031050154\n");
int tempFd[2];
int tempRet = pipe(tempFd);
if(tempRet == -1){
perror("pipe err");
exit(1);
}//of if
int i;
pid_t tempPid, tempWpid;
for(i=0; i<2; i++){//2个子
if((tempPid = fork()) == 0){
break;
}//of if
}//of if
if(2 == i){//父进程,回收子进程
close(tempFd[0]); //关闭读
close(tempFd[1]); //关闭写
tempWpid = wait(NULL);
printf("wait child 1 success,pid=%d\n", tempWpid );
tempPid = wait(NULL);
printf("wait child 2 success,pid=%d\n", tempPid);
} else if(0 == i){//子进程1—写
close(tempFd[0]);
dup2(tempFd[1], STDOUT_FILENO);//定向到标准输出
execlp("ls", "ls", NULL);
} else if(1 == i){//子进程2—读
close(tempFd[1]);
dup2(tempFd[0], STDIN_FILENO);
execlp("wc", "wc", "-l", NULL);
}//of if
return 0;
}//of main
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
printf("suhao 202031050154\n");
FILE *tempRfp,*tempWfp;
char temBuf[100];
tempRfp = popen("ls","r"); //读取命令执行结果
tempWfp = popen("wc -l","w"); //将管道中的数据传递给进程
while(fgets(temBuf, sizeof(temBuf), tempRfp)!=NULL){
fputs(temBuf, tempWfp);
}//of while
pclose(tempRfp);
pclose(tempWfp);
return 0;
}//of main
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int paraArgc,char *paraArgv[]){
printf("suhao 202031050154\n");
if(paraArgc < 2){ //判断是否传入文件名
printf("./a.out fifoname\n");
exit(1);
}//of if
int tempRet = access(paraArgv[1], F_OK); //判断fifo文件是否存在
if(tempRet == -1){ //若fifo不存在就创建fifo
int tempFIFO = mkfifo(paraArgv[1], 0664);
if(tempFIFO == -1){ //判断文件是否创建成功
perror("mkfifo");
exit(1);
} else{
printf("fifo creat success!\n");
}//of if
}//of if
int tempFd = open(paraArgv[1], O_WRONLY); //读写方式打开
while(1){ //循环写入数据
char *tempStrp="hello,world!";
write(tempFd, tempStrp, strlen(tempStrp)+1);
sleep(1);
}//of while
close(tempFd);
return 0;
}//of main
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int paraArgc,char *argv[]){
printf("suhao 202031050154\n");
if(paraArgc < 2){ //判断是否传入文件名
printf("./a.out fifoname\n");
exit(1);
}//of if
int tempRet = access(argv[1], F_OK); //判断fifo文件是否存在
if(tempRet == -1){ //若fifo不存在就创建fifo
int tempFIFO = mkfifo(argv[1], 0664);
if(tempFIFO == -1){ //判断文件是否创建成功
perror("mkfifo");
exit(1);
} else{
printf("fifo creat success!\n");
}//of if
}//of if
int tempFd = open(argv[1], O_RDONLY); //只读方式打开
if(tempFd == -1){
perror("open");
exit(1);
}//of if
while(1){ //不断读取fifo中的数据并打印
char temBuf[1024]={0};
read(tempFd, temBuf, sizeof(temBuf));
printf("buf=%s\n", temBuf);
}//of while
close(tempFd); //关闭文件
return 0;
}//of main
msgrcv.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/msg.h>
#define MAX_TEXT 512
struct my_msg_st{
long int my_msg_type;
char anytext[MAX_TEXT];
};
int main(){
int tempIdx = 1;
int tempMsgid;
struct my_msg_st tempData;
long int tempMsgToRcv = 0;
//rcv msg
tempMsgid = msgget((key_t)1000, 0664 | IPC_CREAT);//获取消息队列
if (tempMsgid == -1){
perror("msgget err");
exit(-1);
}//of if
while (tempIdx < 5){
//接收消息
if (msgrcv(tempMsgid, (void*)&tempData, BUFSIZ, tempMsgToRcv, 0) == -1){
perror("msgrcv err");
exit(-1);
}//of if
//打印消息
printf("msg type:%ld\n", tempData.my_msg_type);
printf("msg content is:%s", tempData.anytext);
tempIdx ++;
}//of while
//删除消息队列
if (msgctl(tempMsgid, IPC_RMID, 0) == -1){
perror("msgctl err");
exit(-1);
}//of if
exit(0);
}//of main
//send
#include <stdio.h>
#include <stdlib.h>
#include <sys/msg.h>
#define MAX_TEXT 512
struct my_msg_st{
long int my_msg_type;
char anytext[MAX_TEXT];
};
int main(){
int tempIdx = 1;
int tempMsgid;
struct my_msg_st tempData;
long int tempMsgToRcv = 0;
//rcv msg
tempMsgid = msgget((key_t)1000, 0664 | IPC_CREAT);//获取消息队列
if (tempMsgid == -1){
perror("msgget err");
exit(-1);
}//of if
while (tempIdx < 5){
//接收消息
if (msgrcv(tempMsgid, (void*)&tempData, BUFSIZ, tempMsgToRcv, 0) == -1){
perror("msgrcv err");
exit(-1);
}//of if
//打印消息
printf("msg type:%ld\n", tempData.my_msg_type);
printf("msg content is:%s", tempData.anytext);
tempIdx ++;
}//of while
//删除消息队列
if (msgctl(tempMsgid, IPC_RMID, 0) == -1){
perror("msgctl err");
exit(-1);
}//of if
exit(0);
}//of main
//check
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define SEGSIZE 4096 //定义共享内存容量
typedef struct{ //读写数据结构体
char name[8];
int age;
} Stu;
int main() {
int tempShmId, i;
key_t tempKey;
char tempName[8];
Stu *tempSmap;
/*ftok函数的作用:系统建立IPC通讯(如消息队列、共享内存时)必须指定一个ID值。通常情况下,该id值通过ftok函数得到。*/
tempKey = ftok("/", 0); //获取关键字
if (tempKey == -1) {
perror("ftok error");
return -1;
}//of if
printf("key=%d\n", tempKey);
//创建共享内存
tempShmId= shmget(tempKey, SEGSIZE, IPC_CREAT | IPC_EXCL | 0664);
if (tempShmId == -1) {
perror("create shared memory error\n");
return -1;
}//of if
printf("shm_id=%d\n", tempShmId);
tempSmap = (Stu*)shmat(tempShmId, NULL, 0); //将进程与共享内存绑定
memset(tempName, 0x00, sizeof(tempName));
strcpy(tempName, "Jhon");
tempName[4] = '0';
for (i = 0; i < 3; i++){ //写数据
tempName[4] += 1;
strncpy((tempSmap+ i)->name, tempName, 5);
(tempSmap + i)->age = 20 + i;
}//of for i
if (shmdt(tempSmap) == -1){ //解除绑定
perror("detach error");
return -1;
}//of if
return 0;
}//of main
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct{
char name[8];
int age;
} Stu;
int main() {
int tempShmId, i;
key_t tempKey;
Stu *tempSmap;
struct shmid_ds tempBuf;
tempKey = ftok("/", 0); //获取关键字
if (tempKey == -1) {
perror("ftok error");
return -1;
}//of if
printf("key=%d\n", tempKey);
tempShmId = shmget(tempKey, 0, 0); //创建共享内存
if (tempShmId == -1) {
perror("shmget error");
return -1;
}//of if
printf("shm_id=%d\n", tempShmId);
tempSmap = (Stu*)shmat(tempShmId, NULL, 0); //将进程与共享内存绑定
for (i = 0; i < 3; i++){ //读数据
printf("name:%s\n", (*(tempSmap + i)).name);
printf("age :%d\n", (*(tempSmap + i)).age);
}//of for i
if (shmdt(tempSmap) == -1){ //解除绑定
perror("detach error");
return -1;
}//of if
shmctl(tempShmId, IPC_RMID, &tempBuf); //删除共享内存
return 0;
}//of main