1.找出来我们之前写的链表的加载和保存的代码,实现,当按 ctrl + c的时候,保存链表
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
typedef struct Link
{
int data;
struct Link* next;
struct Link* prev;
}link_t;
link_t* head;
link_t* create_node()
{
link_t* head = malloc(sizeof(link_t));
head->next = head;
head->prev = head;
return head;
}
void insert_node(link_t* head,int data)
{
link_t* newnode = create_node();
newnode->data = data;
link_t* tail = head->prev;
tail->next = newnode;
newnode->next = head;
head->prev = newnode;
newnode->prev = tail;
}
link_t* find_node(link_t* head,int data)
{
link_t* p = head->next;
while(p != head)
{
if(p->data == data)
{
return p;
}
p = p->next;
}
return head;
}
void save_link(link_t* head,const char* filename)
{
FILE* fp = fopen(filename,"w");
if(fp==NULL){return;}
link_t* p = head->next;
while(p != head)
{
fprintf(fp,"%d\n",p->data);
p = p->next;
}
fclose(fp);
}
void load_link(link_t* head,const char* filename)
{
FILE* fp = fopen(filename,"r");
if(fp==NULL){return;}
while(1)
{
int data = 0;
int res = fscanf(fp,"%d\n",&data);
if(res!=1){break;}
//link_t* newnode = create_node();// 通过if判断表示读取到了数据,读取到数据之后才去创建节点
insert_node(head,data);
}
fclose(fp);
}
void remove_node(link_t* head,int data)
{
link_t* tar = find_node(head,data);
if(tar==head){return;}
tar->prev->next = tar->next;
tar->next->prev = tar->prev;
free(tar);
}
void show_link(link_t* head){
link_t* p = head->next;
while(p != head){
printf("%d\n",p->data);
p = p->next;
}
}
void handler(int signo)
{
if(signo==SIGINT)
{
save_link(head,"./zuo1.txt");
}
}
int main(int argc, const char *argv[])
{
int ch = 0;
head = create_node();
load_link(head,"./zuo1.txt");
signal(SIGINT,handler);
while(1)
{
printf("1:添加数据\n");
printf("2:查看数据\n");
printf("ctrl+c可以保存数据\n");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:{
int data = 0;
printf("请输入数据:");
scanf("%d",&data);
getchar();
insert_node(head,data);
break;
}
case 2:
show_link(head);
break;
default:
break;
}
}
return 0;
}
2.编写2个.c文件,生成2个可执行文件 1.c 输入正方形的长和宽 或者 三角形的三边长 2.c 输出长方形或者三角形的面积 要求数据通信使用无名管道实现
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
int main(int argc, const char *argv[])
{
int pipefd[2] = {0};
if(pipe(pipefd)==-1)
{
perror("pipe");
return 1;
}
int res = fork();
if(res > 0)
{
while(1)
{
double arr[3] = {0};
printf("请输入矩形2边长或者三角形的3边长:");
scanf("%lf %lf %lf",arr,arr+1,arr+2);
while(getchar()!='\n');
write(pipefd[1],arr,24);
sleep(1);
}
}
else if(res == 0)
{
char str_fd[16] = {0};
sprintf(str_fd,"%d",pipefd[0]);//将管道读端描述符转换成字符串类型
execl("./zuo2.2",str_fd,NULL);
printf("进程替换失败\n");
} return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <math.h>
int main(int argc, const char *argv[])
{
// 假设读取管道的描述符是 fd
int fd = atoi(argv[0]);
while(1)
{
double arr[3] = {0};
read(fd,arr,24);
if(arr[2] == 0)
{
// 说明计算的是矩形
double s = arr[0] * arr[1];
printf("矩形的面积为:%g\n",s);
}
else
{
double a = arr[0];
double b = arr[1];
double c = arr[2];
double p = (a + b + c) / 2;
double s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("三角形的面积为:%g\n",s);
}
}
return 0;
}
3.使用有名管道,实现2个终端之间的互相聊天功能 要求:能够做舔狗(能够并发)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
void* run(void* arg)
{
if(access("myfifo2",F_OK)==-1)
{
mkfifo("myfifo2",0666);
}
int rfd = open("./myfifo2",O_RDONLY);
int size = 0;
char buf[128] = {0};
while(1)
{
memset(buf,0,sizeof(buf));
int res = read(rfd,buf,128);
if(res == 0)
{
printf("管道破裂\n");
break;
}
printf("读取到的消息为:%s\n",buf);
}
}
int main(int argc, const char *argv[])
{
pthread_t id;
pthread_create(&id,NULL,run,NULL);
pthread_detach(id);
if(access("myfifo1",F_OK)==-1)
{
mkfifo("myfifo1",0666);
}
int wfd = open("./myfifo1",O_WRONLY | O_TRUNC);
int size = 0;
char buf[128] = {0};
while(1)
{
memset(buf,0,sizeof(buf));
printf("请输入:");
scanf("%128s",buf);
while(getchar()!=10);
size = strlen(buf);
write(wfd,buf,size);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
void* run(void* arg)
{
if(access("myfifo2",F_OK)==-1)
{
mkfifo("myfifo2",0666);
}
int wfd = open("./myfifo2",O_WRONLY | O_TRUNC);
int size = 0;
char buf[128] = {0};
while(1)
{
memset(buf,0,sizeof(buf));
printf("请输入:");
scanf("%128s",buf);
while(getchar()!=10);
size = strlen(buf);
write(wfd,buf,size);
}
}
int main(int argc, const char *argv[])
{
pthread_t id;
pthread_create(&id,NULL,run,NULL);
pthread_detach(id);
if(access("myfifo1",F_OK)==-1)
{
mkfifo("myfifo1",0666);
}
int rfd = open("./myfifo1",O_RDONLY);
int size = 0;
char buf[128] = {0};
while(1)
{
memset(buf,0,sizeof(buf));
int res = read(rfd,buf,128);
if(res == 0)
{
printf("管道破裂\n");
break;
}
printf("读取到的消息为:%s\n",buf);
}
return 0;
}