1. linux中0,1,2代表标准输入,标准输出,标准错误信息输出
cout<<"input: "<<flush;
char buf[100] = {};
read(0, buf, 5);
write(1, buf, 5);
2. 精灵进程:a.fork后父进程结束;b.子进程中setsid();新建回话和组;c. umask(0022);屏蔽权限;d. close(所有文件描述符一般0~255);
#include <iostream>
using namespace std;
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void make_daemon();
void work();
int main()
{
make_daemon();
return 0;
}
void make_daemon()
{
if (fork() != 0)
exit(0);
setsid();
umask(0077);
for (int fd = 0; fd<256; ++fd)
close(fd);
work();
}
void work()
{
char ch = 'A';
for(;;)
{
int fd = open("tick.txt", O_WRONLY|O_CREAT|O_APPEND, 0777);
if (fd>= 0)
{
write(fd, &ch, 1);
close(fd);
}
ch++;
if(ch > 'Z')
ch = 'A';
sleep(3);
}
}
3. makefile写法:
a.out:a.o b.o c.o
g++ a.o b.o c.o #必须要用tab键
a.o:a.cc a.h
g++ -c a.cc
b.o:b.cc a.h
g++ -c b.cc
c.o:c.cc
g++ -c c.cc
# $^所有依赖文件
# $@目的文件
# $<第一个依赖文件
# abc = "g++ -c" $(abc)
4. man signal:获取所有信号相关
5. 登记信号处理函数:旧函数名 signal(信号, 函数名fun); void fun(int sig);
#include <iostream>
using namespace std;
#include <signal.h>
void fun(int sig)
{
signal(sig, fun);
if(sig == SIGINT)
cout<<"ctrl+c"<<endl;
else if(sig == SIGUSR1)
cout<<"sigusr1"<<endl;
else
cout<<"unknown"<<endl;
}
int main()
{
cout<<"===1==="<<endl;
signal(SIGINT, fun);
signal(SIGUSR1, fun);
signal(SIGUSR2, fun);
cout<<"===2==="<<endl;
for(int i = 0; i < 20; ++i)
{
sleep(10);
cerr<<'.';
}
return 0;
}
6. #kill -USR1 pid
kill命令发送信号
7. 在某些系统中,signal对某个信号一次有效,不稳定信号;有些系统中会取消正在进行中的阻塞,阻塞函数调用会以出错的方式返回
8. signal(信号, SIG_DEI/SIG_IGE);//default,igore,返回值SIG_ERR表示登记失败
9. kill(pid, 信号);
raise(信号);//给自己发信号
alarm(秒数);//给自己发SIGALRM.
10. pause();暂停直到处理一个信号为止
11. #mkfifo a.f
#ls
a.f|
#ll a.f
prw-r--r-- p为pipe管道的意思
12.int mkfifo("a.fifo", 0600);//管道要同时读写时才能打开
//写
#include <iostream>
using namespace std;
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
mkfifo("a.fifo", 0600); //返回int,负失败
int fd = open("a.fifo", O_WRONLY);
if(fd<0)
{
cerr<<"err"<<endl;
return -1;
}
cout<<"pipe ready!"<<endl;
for(;;)
{
cout<<"intput text:";
string str;
getline(cin, str);
write(fd, str.c_str(), str.size());
if (str == "bye")
break;
}
close(fd);
return 0;
}
//读
#include <iostream>
using namespace std;
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
int main()
{
mkfifo("a.fifo", 0600); //返回int,负失败
int fd = open("a.fifo", O_RDONLY);
if(fd<0)
{
cerr<<"err"<<endl;
return -1;
}
cout<<"pipe ready!"<<endl;
for(;;)
{
char buf[1000];
int n;
n = read(fd, buf, 1000);
buf[n] = 0;
cout<<"info:"<<buf<<endl;
if(strcmp(buf, "buf") == 0)
break;
}
close(fd);
return 0;
}
13. #ipcs 进程间通信状态
#ipcrm -q qid 删除消息队列
14. 消息队列:
//写
#include <iostream>
using namespace std;
#include <sys/ipc.h>
#include <sys/msg.h>
struct Msg
{
long no;//第一个必须long型
char name[20];
int age;
};
int main(int argc, char** argv)
{
if(argc == 1)
{
cout<<argv[0]<<" key"<<endl;
return 0;
}
int key = atoi(argv[1]);
int qid = msgget(key, IPC_CREAT|0600);
if(qid<0)
{
cout<<"cannot creat"<<endl;
return -1;
}
cout<<"ok"<<endl;
cout<<"key= 0x"<<hex<<key<<endl;
cout<<"qid= "<<dex<<qid<<endl;
Msg m;
cout<<"shuru:"<<end;
cin>> m.no>>m.name>>m.age;
msgsnd(qid, &m, sizeof(m), 0);
return 0;
}
//读
#include <iostream>
using namespace std;
#include <sys/ipc.h>
#include <sys/msg.h>
struct Msg
{
long no;//第一个必须long型
char name[20];
int age;
};
int main(int argc, char** argv)
{
if(argc == 1)
{
cout<<argv[0]<<" key"<<endl;
return 0;
}
int key = atoi(argv[1]);
int qid = msgget(key, IPC_CREAT|0600);
if(qid<0)
{
cout<<"cannot creat"<<endl;
return -1;
}
cout<<"ok"<<endl;
cout<<"key= 0x"<<hex<<key<<endl;
cout<<"qid= "<<dex<<qid<<endl;
Msg m;
cout<<"no:"<<end;
int no;
cin >> no; //输入通道号是用0表示不分通道,都收
msgrcv(qid, &m, sizeof(m), no, 0);
cout<<m.no<<end;
cout<<m.name<<endl;
cout<<m.age<<endl;
return 0;
}
//删除队列
#include <iostream>
using namespace std;
#include <sys/ipc.h>
#include <sys/msg.h>
struct Msg
{
long no;//第一个必须long型
char name[20];
int age;
};
int main(int argc, char** argv)
{
if(argc == 1)
{
cout<<argv[0]<<" key"<<endl;
return 0;
}
int key = atoi(argv[1]);
int qid = msgget(key, IPC_CREAT|0600);
if(qid<0)
{
cout<<"cannot creat"<<endl;
return -1;
}
cout<<"ok"<<endl;
cout<<"key= 0x"<<hex<<key<<endl;
cout<<"qid= "<<dex<<qid<<endl;
if(msgctl(qid, IPC_RMID, NULL)<0)
cout<<"erroer remove"<<endl;
else
cout<<"remove ok!"<<endl;
return 0;
}