1 利用文件IO读取文件数据存入链表,当触法ctrl+c时将链表数据存入文件。
main.c
#include"head.h"
FILE*fp_w;
linklist L;
void handler(int sig){out_file(fp_w,L);printf("文件写入完成\n");exit(0);
}int main(int argc, const char *argv[])
{__sighandler_t s =signal(SIGINT,handler);datatype buf;FILE*fp_r=fopen("../standard_io/cpy_f.c","r"); if(fp_r==NULL){perror("fopen");return -1;}fp_w=fopen("./1.c","w");if(fp_w==NULL){perror("fopen");return -1;}L=create_head();while(1){if(fscanf(fp_r,"%c",&buf)==-1){break;}insert(L,buf);}printf("文件读取完成\n");out_put(L);while(1);
END:return 0;
}
fun.c
#include"head.h"
linklist create_head(){linklist L=(linklist)malloc(sizeof(struct node));if(NULL==L){printf("申请空间失败\n");return NULL;}L->len=0;L->next=NULL;return L;
}linklist create_linknode(datatype key){linklist p=(linklist)malloc(sizeof(struct node));if(NULL==p){printf("申请空间失败\n");return NULL;}p->data=key;p->next=NULL;return p;
}
int insert(linklist L,datatype key){if(NULL==L){printf("传参失败\n");return -1;}linklist f=L;while(f->next!=NULL){f=f->next;}linklist p=create_linknode(key);if(p==NULL){printf("创建节点失败\n");return -1;}f->next=p;(L->len)++;return 0;
}
int out_put(linklist L){linklist p=L->next;for(int i=0;i<L->len;i++){printf("%c",p->data);p=p->next;}return 0;
}
int out_file(FILE*fp_w,linklist L){linklist p=L->next;for(int i=0;i<L->len;i++){if(fprintf(fp_w,"%c",p->data)<0){perror("fprintf");return -1;}p=p->next;}return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<pthread.h>
#include<sys/wait.h>
#include<signal.h>
typedef char datatype;
typedef struct node{union{int len;datatype data;};struct node* next;
}*linklist;linklist create_head();
linklist create_linknode(datatype key);
int insert(linklist L,datatype key);
int out_file(FILE*fp_w,linklist L);
int out_put(linklist L);
#endif
2写两个.c文件,要求用无名管道实现进程间通信,第一个进程输入三个边长,第二个线程输出面积
1.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<pthread.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
int main(int argc, const char *argv[])
{char flags=0;int pfd[2];if(pipe(pfd)<0){perror("pipe");return -1;}char std[2];std[0]=pfd[0];std[1]=pfd[1];int arg[3]={0};pid_t pid=fork();if(pid>0){
agi:printf("Please input data>>");scanf("%d %d %d",&arg[0],&arg[1],&arg[2]);if(arg[0]+arg[1]>arg[2]&&arg[0]+arg[2]>arg[1]&&arg[1]+arg[2]>arg[0]){printf("Input success\n");flags=1;if(write(pfd[1],&flags,1)<0){perror("write");return -1;}flags=0;printf("send success\n");write(pfd[1],&(arg[0]),4);printf("send a\n");write(pfd[1],&(arg[1]),4);printf("send b\n");write(pfd[1],&(arg[2]),4);printf("send c\n");int s=0;while(1){wait(&s);if(s!=0){goto end;}}}else{fprintf(stderr,"Intput error\n");goto agi;}}else if(pid==0){while(1){if(read(pfd[0],&flags,1)<0){perror("read");return -1;}if(flags==1){printf("exec success\n");execlp("./2","1",&std[0],&std[1],NULL);}}}
end: return 0;
}
2.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<pthread.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<math.h>
int main(int argc, const char *argv[])
{int a,b,c;int p_r=*(argv[1]);int p_w=*(argv[2]);if(read(p_r,&a,4)<0){perror("reaed");return -1;}if(read(p_r,&b,4)<0){perror("reaed");return -1;}if(read(p_r,&c,4)<0){perror("reaed");return -1;}int p=(a+b+c)/2;int S=sqrt(p*(p-a)*(p-b)*(p-c));printf("child S=%d\n",S);exit(S);
}
3 进程间并发交互 有名管道
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<unistd.h>
#include<errno.h>
#include<fcntl.h>
int main(int argc, const char *argv[])
{if(mkfifo("./fifo1",0664)<0){if(errno!=17){perror("mkfifo");return -1;}}printf("create fifo1 success\n ");int fd_w=open("./fifo1",O_WRONLY);printf("fd_w is open\n");int fd_r=open("./fifo2",O_RDONLY);printf("open file succsee fd_r=%d,fd_w=%d\n",fd_r,fd_w);char buf[128]="";pid_t pd=fork();while(1){if(pd>0){printf("please input>>");fgets(buf,sizeof(buf),stdin);buf[sizeof(buf)-1]=0;if(write(fd_w,buf,sizeof(buf))<0){perror("write");return -1;}if(strcmp(buf,"quit\n")==0)break;printf("input success\n");}else if(pd==0){bzero(buf,sizeof(buf));if(read(fd_r,buf,sizeof(buf))<0){perror("read");return -1;}if(strcmp(buf,"quit\n")==0)break;printf("B input:%s",buf);}}close(fd_w);close(fd_r);return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
#include<fcntl.h>
int main(int argc, const char *argv[])
{if(mkfifo("./fifo2",0664)<0){if(errno!=17){perror("mkfifo");return -1;}}printf("create fifo2 success\n");int fd_r=open("./fifo1",O_RDONLY);printf("fd_r if open\n");int fd_w=open("./fifo2",O_WRONLY);printf("open file succsee fd_r=%d,fd_w=%d\n",fd_r,fd_w);char buf[128]="";pid_t pd=fork();while(1){if(pd>0){bzero(buf,sizeof(buf));if(read(fd_r,buf,sizeof(buf))<0){perror("read");return -1;}if(strcmp(buf,"quit\n")==0)break;printf("A input:%s",buf);}else if(pd==0){printf("please input>>");fgets(buf,sizeof(buf),stdin);buf[sizeof(buf)-1]=0;if(write(fd_w,buf,sizeof(buf))<0){perror("write");return -1;}if(strcmp(buf,"quit\n")==0)break;printf("input success\n");}}close(fd_w);close(fd_r);return 0;
}