学习 Linux高级编程07_B
来源:互联网 发布:入室盗窃案件数据 编辑:程序博客网 时间:2024/06/05 16:55
Sighandle.c
#include<stdio.h>
#include<signal.h>
void handle(int s)
{
printf("hello start\n");
sleep(10);
printf("hello end\n");
}
main()
{
signal(SIGINT, handle);
while(1);
}
Sigaction.c
#include<stdio.h>
#include<signal.h>
#include<unist.h>
void handle(int s)
{
printf("OOOK!\n");
sleep(5);
printf("KKKOn");
}
main()
{
struct sigaction act = {0};
act.sa_handler = handle;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGINT);
act.sa_flags = 0;
sigaction(SIGUSER1,&act, 0);
while(1);
}
#include<stdio.h>
#include<signal.h>
#include<unist.h>
main()
{
union sigval val;
val.sival_int = 9999;
sigqueue(3382, SIGUSR1, val);
}
#include<stdio.h>
#include<signal.h>
#include<unist.h>
void handle(int s, siginfo_t *info, void *d)
{
printf("OOOK! %d\n" info->si_int);
sleep(5);
printf("KKKOn");
}
main()
{
struct sigaction act = {0};
//act.sa_handler = handle;
act.sa_sigaction = handle;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGINT);
act.sa_flags = SA_SIGINFO;
sigaction(SIGUSER1,&act, 0);
while(1);
}
procA.c
#include<stdio.h>
#include<fcntl.h>
#include<sys/mman.h>
main()
{
int *p;
int fd;
int i;
fd = open("tmp.txt", O_RDWR| O_CREATE, 0666);
ftruncate(fd, 4); //文件4个字节
p = mmap(0,
4,
PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
i = 0;
while(1)
{
sleep(1);
*p = i;
i ++;
}
close(fd);
}
procB.c
#include<stdio.h>
#include<fcntl.h>
#include<sys/mman.h>
main()
{
int *p;
int fd;
fd = open("tmp.txt", O_RDWR); //0666文件权限
ftruncate(fd, 4); //文件4个字节
p = mmap(0,
4,
PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
while(1)
{
sleep(1);
printf("%d\n", *p);
}
close(fd);
}
Mkfifo
fifoA.c
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
main()
{
int fd;//文件描述符
int i;
mkfifo("my.pipe",0666)//1建立管道文件名,权限
fd = open("my.pipe", O_RDWR);//2打开管道
i = 0;
while(1)
{
//3每隔1秒写数据
sleep(1);
write(fd, &i, 4);
i ++;
}
close(fd); //4关闭管道
unlink("my.pipe"); //5删除管道
}
fifoB.c
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
main()
{
int fd;//文件描述符
int i;
fd = open("my.pipe", O_RDWR);//2打开管道
while(1)
{
read(fd, &i, 4);
printf("%d\n", i);
}
close(fd); //4关闭管道
}
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<signal.h>
#incldue<stdlib.h> //exit()
int fd;
int i;
void end(int s)
{
close(fd);
unlink("my.pipe");
exit(-1);
}
main()
{
signal(SIGINT, end);
mkfifo("my.pipe",0666)//1建立管道文件名,权限
fd = open("my.pipe", O_RDWR);//2打开管道
i = 0;
while(1)
{
//3每隔1秒写数据
sleep(1);
write(fd, &i, 4);
i ++;
}
}
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<signal.h>
#include<stdlib.h>
int fd;//文件描述符
void end(int s)
{
close(fd); //4关闭管道
exit(-1);
}
main()
{
int i;
signal(SIGINT, end);
fd = open("my.pipe", O_RDWR);//2打开管道
while(1)
{
read(fd, &i, 4);
printf("%d\n", i);
}
}
Pipe1.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int fd[2];
int r;
r = pipe(fd);
printf("%d\n", getpid());
while(1);
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int fd[2];
int r;
char buf[20];
printf("%d\n", getpid());
r = pipe(fd);
write(fd[1], "hello", 5);
wirte(fd[1], "word", 5);
r = read(fd[0], buf, 20);
buf[r] = 0;
printf("%s\n", buf);
wirte(fd[1], "louis", 5);
r = read(fd[0], buf, 20);
buf[r] = 0;
printf("%s\n", buf);
while(1);
}
Demo5.c
#include<unistd.h>
#include<stdio.h>
main()
{
int fd[2];
pipe(fd);
if(fork())
{
//parent
close(fd[0]); //读关闭,只负责写
while(1)
{
write(fd[1], "hello", 5);
sleep(1);
}
}
else
{
//child
char buf[20];
int r;
close(fd[1]); //写关闭,只负责读
while(1)
{
r= read(fd[0], buf, 20);
buf[r] = 0;
printf("%s\n", buf);
}
}
}
Demo6.c
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
main()
{
int a, b;
int id=1;
while(1)
{
if(id == 1)
{
a = 2;
b = 5000;
}
if(id == 2)
{
a = 5001;
b = 10000;
}
if(fork())
{
//父进程
id ++; //记录创建几个
if(id > 2)
{
break;
}
continue;
}
else
{
//子进程
while(1)
{
sleep(1);
printf("pid= %d : a = %d, b = %d\n", getpid(), a, b);
}
exit(0);
}
}
while(1)
{
//打开文件,准备存储
printf("parent save\n");
sleep(1);
}
}
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<sched.h>
int isprimer(int ta)
{
int i = 2;
for(; i < ta; i ++)
{
if(ta % i == 0)
{
return 0;
}
}
return 1;
}
main()
{
int a, b;
int id=1;
while(1)
{
if(id == 1)
{
a = 2;
b = 5000;
}
if(id == 2)
{
a = 5001;
b = 10000;
}
if(fork())
{
//父进程
id ++; //记录创建几个
if(id > 2)
{
break;
}
continue;
}
else
{
//子进程
int i;
for(i = a; i <= b; i ++)
{
if(isprimer(i))
{
printf("%d: %d\n", getpid(), i);
}
usleep(100);
}
printf("%d exit!\n", getpid());
exit(0);
}
}
while(1)
{
//打开文件,准备存储
printf("parent save\n");
sched_yield(); //sched.h
}
}
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
int idx = 0;
void handle(int s)
{
int status;
if(s == SIGCHLD)
{
wait(&status);
printf("child exit!\n");
idx ++;
if(idx == 2)
{
exit(-1);
}
}
}
int isprimer(int ta)
{
int i = 2;
for(; i < ta; i ++)
{
if(ta % i == 0)
{
return 0;
}
}
return 1;
}
main()
{
int a, b;
int id=1;
int fd[2];
signal(SIGCHLD, handle);
pipe(fd);
while(1)
{
if(id == 1)
{
a = 2;
b = 30;
}
if(id == 2)
{
a = 31;
b = 60;
}
if(fork())
{
//父进程
id ++; //记录创建几个
if(id > 2)
{
break;
}
continue;
}
else
{
//子进程
int i;
close(fd[0]);
for(i = a; i <= b; i ++)
{
if(isprimer(i))
{
write(fd[1], &i, sizeof(int));
}
usleep(100);
}
printf("%d exit!\n", getpid());
exit(0);
}
}
while(1)
{
int re;
//打开文件,准备存储
close(fd[1]);
read(fd[0], &re, sizeof(int));
printf("%d\n", re);
}
}
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
int idx = 0;
int fddata = 0;
void handle(int s)
{
int status;
if(s == SIGCHLD)
{
wait(&status);
printf("child exit!\n");
idx ++;
if(idx == 2)
{
close(fddata);
printf("save over!!!\n");
exit(-1);
}
}
}
int isprimer(int ta)
{
int i = 2;
for(; i < ta; i ++)
{
if(ta % i == 0)
{
return 0;
}
}
return 1;
}
main()
{
int a, b;
int id=1;
int fd[2];
signal(SIGCHLD, handle);
pipe(fd);
while(1)
{
if(id == 1)
{
a = 2;
b = 30;
}
if(id == 2)
{
a = 31;
b = 60;
}
if(fork())
{
//父进程
id ++; //记录创建几个
if(id > 2)
{
break;
}
continue;
}
else
{
//子进程
int i;
close(fd[0]);
for(i = a; i <= b; i ++)
{
if(isprimer(i))
{
write(fd[1], &i, sizeof(int));
}
usleep(100);
}
printf("%d exit!\n", getpid());
exit(0);
}
}
int re;
char buf[20];
//打开文件,准备存储
close(fd[1]);
fddata = open("result.txt",
O_RDWR | O_CREAT, 0666);
while(1)
{
read(fd[0], &re, sizeof(int));
sprintf(buf, "%d\n", re);
write(fddata, buf, strlen(buf));
}
printf("parent exit\n");
}
- 学习 Linux高级编程07_B
- 学习 Linux高级编程05_B
- 学习 Linux高级编程06_B
- 学习 Linux高级编程08_B
- 学习 Linux高级编程09_B
- 学习 Linux高级编程07_A
- 学习 Linux高级编程01
- 学习 Linux高级编程02
- 学习 Linux高级编程03
- linux高级编程学习网址
- Linux高级编程学习资料
- 学习 Linux高级编程05_A
- 学习 Linux高级编程06_A
- 学习 Linux高级编程08_A
- 学习 Linux高级编程09_A
- 寒假学习 第四、五天 (linux 高级编程)
- c++ 学习笔记(高级linux编程) day1
- c++ 学习笔记(高级linux编程) day2
- 使用Java实现文件的压缩和解压缩
- WSDL格式说明
- 学习 Linux高级编程07_A
- Java的可移植性受到广泛使用
- R12 GL table
- 学习 Linux高级编程07_B
- 我是大龄我怕谁
- IT壹周刊:iOS7被指无新意 京东再陷假货危机
- MySQL server has gone away
- Permissions problems after running Qt Creator with priviliges on Linux(ubuntu)
- 学习 Linux高级编程08_A
- 教你修改eclipse创建android project时默认生成的包名
- Android应用程序的默认最大内存值
- java解析xml文件