1.写出string的构造函数,析构函数
class string
{
public:
string(const char*str=NULL);
~string();
string(const string&another);
string&operator=(const string&rhs);
private:
char*m_data;
}
string::string(const char*str=NULL)
{
if(m_data==NULL)
{
m_data=new char[1];
*m_data='\0';
}
else
{
m_data=new char[strlen(str)+1];
strcpy(m_data,str);
}
}
string::string(const string&another)
{
if(this==&another)
return *this;
else
{
delete m_data;
m_data=new char[string(another.m_data)+1];
strcpy(m_data,another.m_data);
return *this;
}
}
string::~string()
{
if(m_data!=NULL)
{
delete m_data;
m_data=NULL;
}
}
2.设计两个线程通信
一个线程发送数据包,另一个线程读取数据包,下面是接收数据包的线程的工作函数
#include <pthread.h>
pthread_mutex_t mutex
pthread_cond_t cond
volatile unsigned int *buffer=NULL;
volatile unsigned int *id=NULL;
volatile unsigned in *size=NULL;
void *handle1(void*arg)
{
pthread_mutex_lock(&mutex);
while(*buffer==0)
{
pthread_cond_wait(&cond,&mutex);
}
BUFFER_getPacket(&id,&buffer,&size);
pthread_mutex_unlock(&mutex);
return void*1;
}
发送数据包线程的
#include <ptherad.h>
void*handler2(void*arg)
{
pthread_mutex_lock(&mutex);
if(*buffer==0)
{
Get_packet(id,&buffer,size);
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
}
3.谈一下linux系统调用的原理
1.简单的来说,是操作系统提供给用户空间的一组特殊接口。用户程序通过这组特殊的接口来获得操作系统提供内核的特殊服务,它把用户的请求传递到内核,待内核把请求处理完后,再把处理结果送给用户空间,(用户进程想要获得系统服务)
2.系统调用的实现利用了x86的软件中断,通过软件指令触发,跟嵌入式操作系统中的软中断有很大区别。
系统调用的参数通过eax,ebx,ecx等寄存器来存放这些参数
- 系统调用其实很简单,就是所以操作系统的API都是通过软件的中断动态的调用,通过调用int $0x80 触发软件中断,然后通过一些寄存器将参数传入,实现对操作系统API的调用。