退出处理函数
class A
{
public:
A(){
cout<<"A()"<<endl;
}
~A(){
cout<<"~A()"<<endl;
}
};
void f(){
cout<<"f()"<<endl;
}
A gobj;
int main()
{
A obj;
atexit(f);
cout<<"main()"<<endl;
exit(0);
cout<<"after exit"<<endl;
}
模拟shell
#include <iostream>
using namespace std;
int main()
{
cout<<"welcom to use cozzw shell"<<endl;
for(;;)
{
cout<<"[cozzw shell]$";
string cmd;
getline(cin, cmd);
if(cmd == "bye")
break;
else
system(cmd.c_str());
}
}
fork
#include <iostream>
using namespace std;
int main()
{
cout<<"old pid: "<<getpid()<<endl;
cout<<"try to fork...."<<endl;
pid_t pid = fork();
if(pid < 0)
{
cout<<"fork error"<<endl;
return -1;
}
else
if(pid > 0)
{
for(int i=0; i<5; ++i)
{
cout<<"parent: "<<getpid()<<endl;
sleep(3)
}
}
else
{
for(int i=0; i<10; ++i)
{
cout<<"child: "<<getpid()<<endl;
cout<<"parent: "<<getppid()<<endl;
sleep(3);
}
}
cout<<"--------bye-------"<<endl;
}
回收子进程资源
#include <iostream>
#include <sys/wait.h>
using namespace std;
int main()
{
if(fork() == 0)
{
cout<<"child: "<<getpid()<<endl;
return 123;
}
else
{
cout<<"parent: "<<getpid()<<endl;
pid_t pid = wait(NULL);
cout<<"wait "<<pid<<endl;
return 0;
}
}
分离父子进程代码
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
for(int i=0; i<argc; ++i)
cout<<argv[i]<<endl;
}
g++ cmdline.cc -o cmdline
#include <iostream>
using namespace std;
int main()
{
char* arg[5] = {"cmdline", "aa", "bb", "cc"};
if(fork() == 0)
{
execvp("cmdline", arg);
cout<<"not found cmdline"<<endl;
return -1;
}
sleep(10);
}
#include <iostream>
using namespace std;
int main()
{
if(fork() == 0)
{
execlp("cmdline", "cmdline", "aa", "bb", "cc", NULL);
cout<<"not found cmdline"<<endl;
return -1;
}
sleep(10);
}
精灵进程
#include <iostream>
#include <fcntl.h>
using namespace std;
void work()
{
char ch = 'A';
for(;;)
{
int fd = open("tick.txt", O_WRONLY|O_CREAT|O_APPEND, 0777);
write(fd, &ch, 1);
close(fd);
++ch;
if(ch>'Z')
ch = 'A';
sleep(3);
}
}
int main()
{
if(fork() != 0)
exit(0);
setsid();
umask(0022);
for(int fd=0; fd<255; fd++)
{
close(fd);
}
work();
}
class A
{
public:
A(){
cout<<"A()"<<endl;
}
~A(){
cout<<"~A()"<<endl;
}
};
void f(){
cout<<"f()"<<endl;
}
A gobj;
int main()
{
A obj;
atexit(f);
cout<<"main()"<<endl;
exit(0);
cout<<"after exit"<<endl;
}
模拟shell
#include <iostream>
using namespace std;
int main()
{
cout<<"welcom to use cozzw shell"<<endl;
for(;;)
{
cout<<"[cozzw shell]$";
string cmd;
getline(cin, cmd);
if(cmd == "bye")
break;
else
system(cmd.c_str());
}
}
fork
#include <iostream>
using namespace std;
int main()
{
cout<<"old pid: "<<getpid()<<endl;
cout<<"try to fork...."<<endl;
pid_t pid = fork();
if(pid < 0)
{
cout<<"fork error"<<endl;
return -1;
}
else
if(pid > 0)
{
for(int i=0; i<5; ++i)
{
cout<<"parent: "<<getpid()<<endl;
sleep(3)
}
}
else
{
for(int i=0; i<10; ++i)
{
cout<<"child: "<<getpid()<<endl;
cout<<"parent: "<<getppid()<<endl;
sleep(3);
}
}
cout<<"--------bye-------"<<endl;
}
回收子进程资源
#include <iostream>
#include <sys/wait.h>
using namespace std;
int main()
{
if(fork() == 0)
{
cout<<"child: "<<getpid()<<endl;
return 123;
}
else
{
cout<<"parent: "<<getpid()<<endl;
pid_t pid = wait(NULL);
cout<<"wait "<<pid<<endl;
return 0;
}
}
分离父子进程代码
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
for(int i=0; i<argc; ++i)
cout<<argv[i]<<endl;
}
g++ cmdline.cc -o cmdline
#include <iostream>
using namespace std;
int main()
{
char* arg[5] = {"cmdline", "aa", "bb", "cc"};
if(fork() == 0)
{
execvp("cmdline", arg);
cout<<"not found cmdline"<<endl;
return -1;
}
sleep(10);
}
#include <iostream>
using namespace std;
int main()
{
if(fork() == 0)
{
execlp("cmdline", "cmdline", "aa", "bb", "cc", NULL);
cout<<"not found cmdline"<<endl;
return -1;
}
sleep(10);
}
精灵进程
#include <iostream>
#include <fcntl.h>
using namespace std;
void work()
{
char ch = 'A';
for(;;)
{
int fd = open("tick.txt", O_WRONLY|O_CREAT|O_APPEND, 0777);
write(fd, &ch, 1);
close(fd);
++ch;
if(ch>'Z')
ch = 'A';
sleep(3);
}
}
int main()
{
if(fork() != 0)
exit(0);
setsid();
umask(0022);
for(int fd=0; fd<255; fd++)
{
close(fd);
}
work();
}